blob: 39a4e565c2e2c99038de06be7a94e8d3b2e77cd9 [file] [log] [blame]
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +00001//===- 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#include "llvm/Transforms/Utils/SplitModule.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000017#include "llvm/ADT/DenseMap.h"
Sergei Larind19d4d32016-01-18 21:07:13 +000018#include "llvm/ADT/EquivalenceClasses.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000019#include "llvm/ADT/SmallPtrSet.h"
20#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/Comdat.h"
23#include "llvm/IR/Constant.h"
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +000024#include "llvm/IR/Constants.h"
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000025#include "llvm/IR/Function.h"
26#include "llvm/IR/GlobalAlias.h"
27#include "llvm/IR/GlobalObject.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000028#include "llvm/IR/GlobalIndirectSymbol.h"
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000029#include "llvm/IR/GlobalValue.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000030#include "llvm/IR/GlobalVariable.h"
31#include "llvm/IR/Instruction.h"
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000032#include "llvm/IR/Module.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000033#include "llvm/IR/User.h"
34#include "llvm/IR/Value.h"
35#include "llvm/Support/Casting.h"
Sergei Larind19d4d32016-01-18 21:07:13 +000036#include "llvm/Support/Debug.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000037#include "llvm/Support/ErrorHandling.h"
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000038#include "llvm/Support/MD5.h"
39#include "llvm/Support/raw_ostream.h"
40#include "llvm/Transforms/Utils/Cloning.h"
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000041#include "llvm/Transforms/Utils/ValueMapper.h"
42#include <algorithm>
43#include <cassert>
44#include <iterator>
45#include <memory>
Sergei Larind19d4d32016-01-18 21:07:13 +000046#include <queue>
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000047#include <utility>
48#include <vector>
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +000049
50using namespace llvm;
51
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000052#define DEBUG_TYPE "split-module"
53
Sergei Larind19d4d32016-01-18 21:07:13 +000054namespace {
Eugene Zelenko5adb96c2017-10-26 00:55:39 +000055
56using ClusterMapType = EquivalenceClasses<const GlobalValue *>;
57using ComdatMembersType = DenseMap<const Comdat *, const GlobalValue *>;
58using ClusterIDMapType = DenseMap<const GlobalValue *, unsigned>;
59
60} // end anonymous namespace
Sergei Larind19d4d32016-01-18 21:07:13 +000061
62static void addNonConstUser(ClusterMapType &GVtoClusterMap,
63 const GlobalValue *GV, const User *U) {
64 assert((!isa<Constant>(U) || isa<GlobalValue>(U)) && "Bad user");
65
66 if (const Instruction *I = dyn_cast<Instruction>(U)) {
67 const GlobalValue *F = I->getParent()->getParent();
68 GVtoClusterMap.unionSets(GV, F);
David Majnemer95549492016-05-04 00:20:48 +000069 } else if (isa<GlobalIndirectSymbol>(U) || isa<Function>(U) ||
Sergei Larind19d4d32016-01-18 21:07:13 +000070 isa<GlobalVariable>(U)) {
71 GVtoClusterMap.unionSets(GV, cast<GlobalValue>(U));
72 } else {
73 llvm_unreachable("Underimplemented use case");
74 }
75}
76
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +000077// Adds all GlobalValue users of V to the same cluster as GV.
78static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap,
79 const GlobalValue *GV, const Value *V) {
80 for (auto *U : V->users()) {
81 SmallVector<const User *, 4> Worklist;
82 Worklist.push_back(U);
83 while (!Worklist.empty()) {
84 const User *UU = Worklist.pop_back_val();
85 // For each constant that is not a GV (a pure const) recurse.
86 if (isa<Constant>(UU) && !isa<GlobalValue>(UU)) {
87 Worklist.append(UU->user_begin(), UU->user_end());
88 continue;
89 }
90 addNonConstUser(GVtoClusterMap, GV, UU);
91 }
92 }
93}
94
Sergei Larind19d4d32016-01-18 21:07:13 +000095// Find partitions for module in the way that no locals need to be
96// globalized.
97// Try to balance pack those partitions into N files since this roughly equals
98// thread balancing for the backend codegen step.
99static void findPartitions(Module *M, ClusterIDMapType &ClusterIDMap,
100 unsigned N) {
101 // At this point module should have the proper mix of globals and locals.
102 // As we attempt to partition this module, we must not change any
103 // locals to globals.
Sergei Larind19d4d32016-01-18 21:07:13 +0000104 DEBUG(dbgs() << "Partition module with (" << M->size() << ")functions\n");
105 ClusterMapType GVtoClusterMap;
106 ComdatMembersType ComdatMembers;
107
108 auto recordGVSet = [&GVtoClusterMap, &ComdatMembers](GlobalValue &GV) {
109 if (GV.isDeclaration())
110 return;
111
112 if (!GV.hasName())
113 GV.setName("__llvmsplit_unnamed");
114
115 // Comdat groups must not be partitioned. For comdat groups that contain
116 // locals, record all their members here so we can keep them together.
117 // Comdat groups that only contain external globals are already handled by
118 // the MD5-based partitioning.
119 if (const Comdat *C = GV.getComdat()) {
120 auto &Member = ComdatMembers[C];
121 if (Member)
122 GVtoClusterMap.unionSets(Member, &GV);
123 else
124 Member = &GV;
125 }
126
Sergei Larin427f5702016-01-28 18:59:28 +0000127 // For aliases we should not separate them from their aliasees regardless
128 // of linkage.
David Majnemer95549492016-05-04 00:20:48 +0000129 if (auto *GIS = dyn_cast<GlobalIndirectSymbol>(&GV)) {
130 if (const GlobalObject *Base = GIS->getBaseObject())
Sergei Larin427f5702016-01-28 18:59:28 +0000131 GVtoClusterMap.unionSets(&GV, Base);
132 }
133
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000134 if (const Function *F = dyn_cast<Function>(&GV)) {
135 for (const BasicBlock &BB : *F) {
136 BlockAddress *BA = BlockAddress::lookup(&BB);
137 if (!BA || !BA->isConstantUsed())
Sergei Larind19d4d32016-01-18 21:07:13 +0000138 continue;
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000139 addAllGlobalValueUsers(GVtoClusterMap, F, BA);
Sergei Larind19d4d32016-01-18 21:07:13 +0000140 }
141 }
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000142
143 if (GV.hasLocalLinkage())
Dimitry Andrice4f5d012017-12-18 19:46:56 +0000144 addAllGlobalValueUsers(GVtoClusterMap, &GV, &GV);
145 };
146
147 llvm::for_each(M->functions(), recordGVSet);
148 llvm::for_each(M->globals(), recordGVSet);
149 llvm::for_each(M->aliases(), recordGVSet);
150
151 // Assigned all GVs to merged clusters while balancing number of objects in
152 // each.
Sergei Larind19d4d32016-01-18 21:07:13 +0000153 auto CompareClusters = [](const std::pair<unsigned, unsigned> &a,
154 const std::pair<unsigned, unsigned> &b) {
155 if (a.second || b.second)
156 return a.second > b.second;
157 else
158 return a.first > b.first;
159 };
160
161 std::priority_queue<std::pair<unsigned, unsigned>,
162 std::vector<std::pair<unsigned, unsigned>>,
163 decltype(CompareClusters)>
164 BalancinQueue(CompareClusters);
165 // Pre-populate priority queue with N slot blanks.
166 for (unsigned i = 0; i < N; ++i)
167 BalancinQueue.push(std::make_pair(i, 0));
168
Eugene Zelenko5adb96c2017-10-26 00:55:39 +0000169 using SortType = std::pair<unsigned, ClusterMapType::iterator>;
170
Sergei Larind19d4d32016-01-18 21:07:13 +0000171 SmallVector<SortType, 64> Sets;
Matthias Braunb30f2f512016-01-30 01:24:31 +0000172 SmallPtrSet<const GlobalValue *, 32> Visited;
Sergei Larind19d4d32016-01-18 21:07:13 +0000173
174 // To guarantee determinism, we have to sort SCC according to size.
175 // When size is the same, use leader's name.
176 for (ClusterMapType::iterator I = GVtoClusterMap.begin(),
177 E = GVtoClusterMap.end(); I != E; ++I)
178 if (I->isLeader())
179 Sets.push_back(
180 std::make_pair(std::distance(GVtoClusterMap.member_begin(I),
181 GVtoClusterMap.member_end()), I));
182
Mandeep Singh Grang636d94d2018-04-13 19:47:57 +0000183 llvm::sort(Sets.begin(), Sets.end(),
184 [](const SortType &a, const SortType &b) {
185 if (a.first == b.first)
186 return a.second->getData()->getName() >
187 b.second->getData()->getName();
188 else
189 return a.first > b.first;
190 });
Sergei Larind19d4d32016-01-18 21:07:13 +0000191
192 for (auto &I : Sets) {
193 unsigned CurrentClusterID = BalancinQueue.top().first;
194 unsigned CurrentClusterSize = BalancinQueue.top().second;
195 BalancinQueue.pop();
196
197 DEBUG(dbgs() << "Root[" << CurrentClusterID << "] cluster_size(" << I.first
198 << ") ----> " << I.second->getData()->getName() << "\n");
199
200 for (ClusterMapType::member_iterator MI =
201 GVtoClusterMap.findLeader(I.second);
202 MI != GVtoClusterMap.member_end(); ++MI) {
203 if (!Visited.insert(*MI).second)
204 continue;
205 DEBUG(dbgs() << "----> " << (*MI)->getName()
206 << ((*MI)->hasLocalLinkage() ? " l " : " e ") << "\n");
207 Visited.insert(*MI);
208 ClusterIDMap[*MI] = CurrentClusterID;
209 CurrentClusterSize++;
210 }
211 // Add this set size to the number of entries in this cluster.
212 BalancinQueue.push(std::make_pair(CurrentClusterID, CurrentClusterSize));
213 }
214}
215
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000216static void externalize(GlobalValue *GV) {
217 if (GV->hasLocalLinkage()) {
218 GV->setLinkage(GlobalValue::ExternalLinkage);
219 GV->setVisibility(GlobalValue::HiddenVisibility);
220 }
221
222 // Unnamed entities must be named consistently between modules. setName will
223 // give a distinct name to each such entity.
224 if (!GV->hasName())
225 GV->setName("__llvmsplit_unnamed");
226}
227
228// Returns whether GV should be in partition (0-based) I of N.
229static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
David Majnemer95549492016-05-04 00:20:48 +0000230 if (auto *GIS = dyn_cast<GlobalIndirectSymbol>(GV))
231 if (const GlobalObject *Base = GIS->getBaseObject())
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000232 GV = Base;
233
234 StringRef Name;
235 if (const Comdat *C = GV->getComdat())
236 Name = C->getName();
237 else
238 Name = GV->getName();
239
240 // Partition by MD5 hash. We only need a few bits for evenness as the number
241 // of partitions will generally be in the 1-2 figure range; the low 16 bits
242 // are enough.
243 MD5 H;
244 MD5::MD5Result R;
245 H.update(Name);
246 H.final(R);
247 return (R[0] | (R[1] << 8)) % N == I;
248}
249
250void llvm::SplitModule(
251 std::unique_ptr<Module> M, unsigned N,
Benjamin Kramerd3f4c052016-06-12 16:13:55 +0000252 function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback,
Sergei Larind19d4d32016-01-18 21:07:13 +0000253 bool PreserveLocals) {
254 if (!PreserveLocals) {
255 for (Function &F : *M)
256 externalize(&F);
257 for (GlobalVariable &GV : M->globals())
258 externalize(&GV);
259 for (GlobalAlias &GA : M->aliases())
260 externalize(&GA);
David Majnemer95549492016-05-04 00:20:48 +0000261 for (GlobalIFunc &GIF : M->ifuncs())
262 externalize(&GIF);
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000263 }
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000264
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000265 // This performs splitting without a need for externalization, which might not
266 // always be possible.
267 ClusterIDMapType ClusterIDMap;
268 findPartitions(M.get(), ClusterIDMap, N);
269
270 // FIXME: We should be able to reuse M as the last partition instead of
271 // cloning it.
272 for (unsigned I = 0; I < N; ++I) {
273 ValueToValueMapTy VMap;
274 std::unique_ptr<Module> MPart(
Rafael Espindola71867532018-02-14 19:50:40 +0000275 CloneModule(*M, VMap, [&](const GlobalValue *GV) {
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000276 if (ClusterIDMap.count(GV))
277 return (ClusterIDMap[GV] == I);
278 else
Sergei Larind19d4d32016-01-18 21:07:13 +0000279 return isInPartition(GV, I, N);
Evgeniy Stepanovf74f0912016-03-31 21:55:11 +0000280 }));
281 if (I != 0)
282 MPart->setModuleInlineAsm("");
283 ModuleCallback(std::move(MPart));
Peter Collingbourne1dc6a8d2015-08-21 02:48:20 +0000284 }
285}