1 //===- SplitModule.cpp - Split a module into partitions -------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #define DEBUG_TYPE "split-module"
17 
18 #include "llvm/Transforms/Utils/SplitModule.h"
19 #include "llvm/ADT/EquivalenceClasses.h"
20 #include "llvm/ADT/Hashing.h"
21 #include "llvm/ADT/MapVector.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/GlobalAlias.h"
26 #include "llvm/IR/GlobalObject.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/Support/CommandLine.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/MD5.h"
32 #include "llvm/Support/raw_ostream.h"
33 #include "llvm/Transforms/Utils/Cloning.h"
34 #include <queue>
35 
36 using namespace llvm;
37 
38 namespace {
39 typedef EquivalenceClasses<const GlobalValue *> ClusterMapType;
40 typedef DenseMap<const Comdat *, const GlobalValue *> ComdatMembersType;
41 typedef DenseMap<const GlobalValue *, unsigned> ClusterIDMapType;
42 }
43 
44 static void addNonConstUser(ClusterMapType &GVtoClusterMap,
45                             const GlobalValue *GV, const User *U) {
46   assert((!isa<Constant>(U) || isa<GlobalValue>(U)) && "Bad user");
47 
48   if (const Instruction *I = dyn_cast<Instruction>(U)) {
49     const GlobalValue *F = I->getParent()->getParent();
50     GVtoClusterMap.unionSets(GV, F);
51   } else if (isa<GlobalAlias>(U) || isa<Function>(U) ||
52              isa<GlobalVariable>(U)) {
53     GVtoClusterMap.unionSets(GV, cast<GlobalValue>(U));
54   } else {
55     llvm_unreachable("Underimplemented use case");
56   }
57 }
58 
59 // Adds all GlobalValue users of V to the same cluster as GV.
60 static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap,
61                                    const GlobalValue *GV, const Value *V) {
62   for (auto *U : V->users()) {
63     SmallVector<const User *, 4> Worklist;
64     Worklist.push_back(U);
65     while (!Worklist.empty()) {
66       const User *UU = Worklist.pop_back_val();
67       // For each constant that is not a GV (a pure const) recurse.
68       if (isa<Constant>(UU) && !isa<GlobalValue>(UU)) {
69         Worklist.append(UU->user_begin(), UU->user_end());
70         continue;
71       }
72       addNonConstUser(GVtoClusterMap, GV, UU);
73     }
74   }
75 }
76 
77 // Find partitions for module in the way that no locals need to be
78 // globalized.
79 // Try to balance pack those partitions into N files since this roughly equals
80 // thread balancing for the backend codegen step.
81 static void findPartitions(Module *M, ClusterIDMapType &ClusterIDMap,
82                            unsigned N) {
83   // At this point module should have the proper mix of globals and locals.
84   // As we attempt to partition this module, we must not change any
85   // locals to globals.
86   DEBUG(dbgs() << "Partition module with (" << M->size() << ")functions\n");
87   ClusterMapType GVtoClusterMap;
88   ComdatMembersType ComdatMembers;
89 
90   auto recordGVSet = [&GVtoClusterMap, &ComdatMembers](GlobalValue &GV) {
91     if (GV.isDeclaration())
92       return;
93 
94     if (!GV.hasName())
95       GV.setName("__llvmsplit_unnamed");
96 
97     // Comdat groups must not be partitioned. For comdat groups that contain
98     // locals, record all their members here so we can keep them together.
99     // Comdat groups that only contain external globals are already handled by
100     // the MD5-based partitioning.
101     if (const Comdat *C = GV.getComdat()) {
102       auto &Member = ComdatMembers[C];
103       if (Member)
104         GVtoClusterMap.unionSets(Member, &GV);
105       else
106         Member = &GV;
107     }
108 
109     // For aliases we should not separate them from their aliasees regardless
110     // of linkage.
111     if (GlobalAlias *GA = dyn_cast<GlobalAlias>(&GV)) {
112       if (const GlobalObject *Base = GA->getBaseObject())
113         GVtoClusterMap.unionSets(&GV, Base);
114     }
115 
116     if (const Function *F = dyn_cast<Function>(&GV)) {
117       for (const BasicBlock &BB : *F) {
118         BlockAddress *BA = BlockAddress::lookup(&BB);
119         if (!BA || !BA->isConstantUsed())
120           continue;
121         addAllGlobalValueUsers(GVtoClusterMap, F, BA);
122       }
123     }
124 
125     if (GV.hasLocalLinkage())
126       addAllGlobalValueUsers(GVtoClusterMap, &GV, &GV);
127   };
128 
129   std::for_each(M->begin(), M->end(), recordGVSet);
130   std::for_each(M->global_begin(), M->global_end(), recordGVSet);
131   std::for_each(M->alias_begin(), M->alias_end(), recordGVSet);
132 
133   // Assigned all GVs to merged clusters while balancing number of objects in
134   // each.
135   auto CompareClusters = [](const std::pair<unsigned, unsigned> &a,
136                             const std::pair<unsigned, unsigned> &b) {
137     if (a.second || b.second)
138       return a.second > b.second;
139     else
140       return a.first > b.first;
141   };
142 
143   std::priority_queue<std::pair<unsigned, unsigned>,
144                       std::vector<std::pair<unsigned, unsigned>>,
145                       decltype(CompareClusters)>
146       BalancinQueue(CompareClusters);
147   // Pre-populate priority queue with N slot blanks.
148   for (unsigned i = 0; i < N; ++i)
149     BalancinQueue.push(std::make_pair(i, 0));
150 
151   typedef std::pair<unsigned, ClusterMapType::iterator> SortType;
152   SmallVector<SortType, 64> Sets;
153   SmallPtrSet<const GlobalValue *, 32> Visited;
154 
155   // To guarantee determinism, we have to sort SCC according to size.
156   // When size is the same, use leader's name.
157   for (ClusterMapType::iterator I = GVtoClusterMap.begin(),
158                                 E = GVtoClusterMap.end(); I != E; ++I)
159     if (I->isLeader())
160       Sets.push_back(
161           std::make_pair(std::distance(GVtoClusterMap.member_begin(I),
162                                        GVtoClusterMap.member_end()), I));
163 
164   std::sort(Sets.begin(), Sets.end(), [](const SortType &a, const SortType &b) {
165     if (a.first == b.first)
166       return a.second->getData()->getName() > b.second->getData()->getName();
167     else
168       return a.first > b.first;
169   });
170 
171   for (auto &I : Sets) {
172     unsigned CurrentClusterID = BalancinQueue.top().first;
173     unsigned CurrentClusterSize = BalancinQueue.top().second;
174     BalancinQueue.pop();
175 
176     DEBUG(dbgs() << "Root[" << CurrentClusterID << "] cluster_size(" << I.first
177                  << ") ----> " << I.second->getData()->getName() << "\n");
178 
179     for (ClusterMapType::member_iterator MI =
180              GVtoClusterMap.findLeader(I.second);
181          MI != GVtoClusterMap.member_end(); ++MI) {
182       if (!Visited.insert(*MI).second)
183         continue;
184       DEBUG(dbgs() << "----> " << (*MI)->getName()
185                    << ((*MI)->hasLocalLinkage() ? " l " : " e ") << "\n");
186       Visited.insert(*MI);
187       ClusterIDMap[*MI] = CurrentClusterID;
188       CurrentClusterSize++;
189     }
190     // Add this set size to the number of entries in this cluster.
191     BalancinQueue.push(std::make_pair(CurrentClusterID, CurrentClusterSize));
192   }
193 }
194 
195 static void externalize(GlobalValue *GV) {
196   if (GV->hasLocalLinkage()) {
197     GV->setLinkage(GlobalValue::ExternalLinkage);
198     GV->setVisibility(GlobalValue::HiddenVisibility);
199   }
200 
201   // Unnamed entities must be named consistently between modules. setName will
202   // give a distinct name to each such entity.
203   if (!GV->hasName())
204     GV->setName("__llvmsplit_unnamed");
205 }
206 
207 // Returns whether GV should be in partition (0-based) I of N.
208 static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
209   if (auto GA = dyn_cast<GlobalAlias>(GV))
210     if (const GlobalObject *Base = GA->getBaseObject())
211       GV = Base;
212 
213   StringRef Name;
214   if (const Comdat *C = GV->getComdat())
215     Name = C->getName();
216   else
217     Name = GV->getName();
218 
219   // Partition by MD5 hash. We only need a few bits for evenness as the number
220   // of partitions will generally be in the 1-2 figure range; the low 16 bits
221   // are enough.
222   MD5 H;
223   MD5::MD5Result R;
224   H.update(Name);
225   H.final(R);
226   return (R[0] | (R[1] << 8)) % N == I;
227 }
228 
229 void llvm::SplitModule(
230     std::unique_ptr<Module> M, unsigned N,
231     std::function<void(std::unique_ptr<Module> MPart)> ModuleCallback,
232     bool PreserveLocals) {
233   if (!PreserveLocals) {
234     for (Function &F : *M)
235       externalize(&F);
236     for (GlobalVariable &GV : M->globals())
237       externalize(&GV);
238     for (GlobalAlias &GA : M->aliases())
239       externalize(&GA);
240   }
241 
242   // This performs splitting without a need for externalization, which might not
243   // always be possible.
244   ClusterIDMapType ClusterIDMap;
245   findPartitions(M.get(), ClusterIDMap, N);
246 
247   // FIXME: We should be able to reuse M as the last partition instead of
248   // cloning it.
249   for (unsigned I = 0; I < N; ++I) {
250     ValueToValueMapTy VMap;
251     std::unique_ptr<Module> MPart(
252         CloneModule(M.get(), VMap, [&](const GlobalValue *GV) {
253           if (ClusterIDMap.count(GV))
254             return (ClusterIDMap[GV] == I);
255           else
256             return isInPartition(GV, I, N);
257         }));
258     if (I != 0)
259       MPart->setModuleInlineAsm("");
260     ModuleCallback(std::move(MPart));
261   }
262 }
263