1 //===- SplitModule.cpp - Split a module into partitions -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the function llvm::SplitModule, which splits a module
11 // into multiple linkable partitions. It can be used to implement parallel code
12 // generation for link-time optimization.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/SplitModule.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/IR/GlobalAlias.h"
20 #include "llvm/IR/GlobalObject.h"
21 #include "llvm/IR/GlobalValue.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/MD5.h"
24 #include "llvm/Support/raw_ostream.h"
25 #include "llvm/Transforms/Utils/Cloning.h"
29 static void externalize(GlobalValue *GV) {
30 if (GV->hasLocalLinkage()) {
31 GV->setLinkage(GlobalValue::ExternalLinkage);
32 GV->setVisibility(GlobalValue::HiddenVisibility);
35 // Unnamed entities must be named consistently between modules. setName will
36 // give a distinct name to each such entity.
38 GV->setName("__llvmsplit_unnamed");
41 // Returns whether GV should be in partition (0-based) I of N.
42 static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
43 if (auto GA = dyn_cast<GlobalAlias>(GV))
44 if (const GlobalObject *Base = GA->getBaseObject())
48 if (const Comdat *C = GV->getComdat())
53 // Partition by MD5 hash. We only need a few bits for evenness as the number
54 // of partitions will generally be in the 1-2 figure range; the low 16 bits
60 return (R[0] | (R[1] << 8)) % N == I;
63 void llvm::SplitModule(
64 std::unique_ptr<Module> M, unsigned N,
65 std::function<void(std::unique_ptr<Module> MPart)> ModuleCallback) {
66 for (Function &F : *M)
68 for (GlobalVariable &GV : M->globals())
70 for (GlobalAlias &GA : M->aliases())
73 // FIXME: We should be able to reuse M as the last partition instead of
75 for (unsigned I = 0; I != N; ++I) {
76 ValueToValueMapTy VMap;
77 std::unique_ptr<Module> MPart(
78 CloneModule(M.get(), VMap, [=](const GlobalValue *GV) {
79 return isInPartition(GV, I, N);
82 MPart->setModuleInlineAsm("");
83 ModuleCallback(std::move(MPart));