LLVM 19.0.0git
SplitModule.cpp
Go to the documentation of this file.
1//===- SplitModule.cpp - Split a module into partitions -------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the function llvm::SplitModule, which splits a module
10// into multiple linkable partitions. It can be used to implement parallel code
11// generation for link-time optimization.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/IR/Comdat.h"
22#include "llvm/IR/Constant.h"
23#include "llvm/IR/Constants.h"
24#include "llvm/IR/Function.h"
25#include "llvm/IR/GlobalAlias.h"
27#include "llvm/IR/GlobalValue.h"
29#include "llvm/IR/Instruction.h"
30#include "llvm/IR/Module.h"
31#include "llvm/IR/User.h"
32#include "llvm/IR/Value.h"
34#include "llvm/Support/Debug.h"
36#include "llvm/Support/MD5.h"
40#include <algorithm>
41#include <cassert>
42#include <iterator>
43#include <memory>
44#include <queue>
45#include <utility>
46#include <vector>
47
48using namespace llvm;
49
50#define DEBUG_TYPE "split-module"
51
52namespace {
53
54using ClusterMapType = EquivalenceClasses<const GlobalValue *>;
55using ComdatMembersType = DenseMap<const Comdat *, const GlobalValue *>;
56using ClusterIDMapType = DenseMap<const GlobalValue *, unsigned>;
57
58} // end anonymous namespace
59
60static void addNonConstUser(ClusterMapType &GVtoClusterMap,
61 const GlobalValue *GV, const User *U) {
62 assert((!isa<Constant>(U) || isa<GlobalValue>(U)) && "Bad user");
63
64 if (const Instruction *I = dyn_cast<Instruction>(U)) {
65 const GlobalValue *F = I->getParent()->getParent();
66 GVtoClusterMap.unionSets(GV, F);
67 } else if (const GlobalValue *GVU = dyn_cast<GlobalValue>(U)) {
68 GVtoClusterMap.unionSets(GV, GVU);
69 } else {
70 llvm_unreachable("Underimplemented use case");
71 }
72}
73
74// Adds all GlobalValue users of V to the same cluster as GV.
75static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap,
76 const GlobalValue *GV, const Value *V) {
77 for (const auto *U : V->users()) {
79 Worklist.push_back(U);
80 while (!Worklist.empty()) {
81 const User *UU = Worklist.pop_back_val();
82 // For each constant that is not a GV (a pure const) recurse.
83 if (isa<Constant>(UU) && !isa<GlobalValue>(UU)) {
84 Worklist.append(UU->user_begin(), UU->user_end());
85 continue;
86 }
87 addNonConstUser(GVtoClusterMap, GV, UU);
88 }
89 }
90}
91
93 const GlobalObject *GO = GV->getAliaseeObject();
94 if (const auto *GI = dyn_cast_or_null<GlobalIFunc>(GO))
95 GO = GI->getResolverFunction();
96 return GO;
97}
98
99// Find partitions for module in the way that no locals need to be
100// globalized.
101// Try to balance pack those partitions into N files since this roughly equals
102// thread balancing for the backend codegen step.
103static void findPartitions(Module &M, ClusterIDMapType &ClusterIDMap,
104 unsigned N) {
105 // At this point module should have the proper mix of globals and locals.
106 // As we attempt to partition this module, we must not change any
107 // locals to globals.
108 LLVM_DEBUG(dbgs() << "Partition module with (" << M.size() << ")functions\n");
109 ClusterMapType GVtoClusterMap;
110 ComdatMembersType ComdatMembers;
111
112 auto recordGVSet = [&GVtoClusterMap, &ComdatMembers](GlobalValue &GV) {
113 if (GV.isDeclaration())
114 return;
115
116 if (!GV.hasName())
117 GV.setName("__llvmsplit_unnamed");
118
119 // Comdat groups must not be partitioned. For comdat groups that contain
120 // locals, record all their members here so we can keep them together.
121 // Comdat groups that only contain external globals are already handled by
122 // the MD5-based partitioning.
123 if (const Comdat *C = GV.getComdat()) {
124 auto &Member = ComdatMembers[C];
125 if (Member)
126 GVtoClusterMap.unionSets(Member, &GV);
127 else
128 Member = &GV;
129 }
130
131 // Aliases should not be separated from their aliasees and ifuncs should
132 // not be separated from their resolvers regardless of linkage.
133 if (const GlobalObject *Root = getGVPartitioningRoot(&GV))
134 if (&GV != Root)
135 GVtoClusterMap.unionSets(&GV, Root);
136
137 if (const Function *F = dyn_cast<Function>(&GV)) {
138 for (const BasicBlock &BB : *F) {
140 if (!BA || !BA->isConstantUsed())
141 continue;
142 addAllGlobalValueUsers(GVtoClusterMap, F, BA);
143 }
144 }
145
146 if (GV.hasLocalLinkage())
147 addAllGlobalValueUsers(GVtoClusterMap, &GV, &GV);
148 };
149
150 llvm::for_each(M.functions(), recordGVSet);
151 llvm::for_each(M.globals(), recordGVSet);
152 llvm::for_each(M.aliases(), recordGVSet);
153
154 // Assigned all GVs to merged clusters while balancing number of objects in
155 // each.
156 auto CompareClusters = [](const std::pair<unsigned, unsigned> &a,
157 const std::pair<unsigned, unsigned> &b) {
158 if (a.second || b.second)
159 return a.second > b.second;
160 else
161 return a.first > b.first;
162 };
163
164 std::priority_queue<std::pair<unsigned, unsigned>,
165 std::vector<std::pair<unsigned, unsigned>>,
166 decltype(CompareClusters)>
167 BalancinQueue(CompareClusters);
168 // Pre-populate priority queue with N slot blanks.
169 for (unsigned i = 0; i < N; ++i)
170 BalancinQueue.push(std::make_pair(i, 0));
171
172 using SortType = std::pair<unsigned, ClusterMapType::iterator>;
173
176
177 // To guarantee determinism, we have to sort SCC according to size.
178 // When size is the same, use leader's name.
179 for (ClusterMapType::iterator I = GVtoClusterMap.begin(),
180 E = GVtoClusterMap.end(); I != E; ++I)
181 if (I->isLeader())
182 Sets.push_back(
183 std::make_pair(std::distance(GVtoClusterMap.member_begin(I),
184 GVtoClusterMap.member_end()), I));
185
186 llvm::sort(Sets, [](const SortType &a, const SortType &b) {
187 if (a.first == b.first)
188 return a.second->getData()->getName() > b.second->getData()->getName();
189 else
190 return a.first > b.first;
191 });
192
193 for (auto &I : Sets) {
194 unsigned CurrentClusterID = BalancinQueue.top().first;
195 unsigned CurrentClusterSize = BalancinQueue.top().second;
196 BalancinQueue.pop();
197
198 LLVM_DEBUG(dbgs() << "Root[" << CurrentClusterID << "] cluster_size("
199 << I.first << ") ----> " << I.second->getData()->getName()
200 << "\n");
201
202 for (ClusterMapType::member_iterator MI =
203 GVtoClusterMap.findLeader(I.second);
204 MI != GVtoClusterMap.member_end(); ++MI) {
205 if (!Visited.insert(*MI).second)
206 continue;
207 LLVM_DEBUG(dbgs() << "----> " << (*MI)->getName()
208 << ((*MI)->hasLocalLinkage() ? " l " : " e ") << "\n");
209 Visited.insert(*MI);
210 ClusterIDMap[*MI] = CurrentClusterID;
211 CurrentClusterSize++;
212 }
213 // Add this set size to the number of entries in this cluster.
214 BalancinQueue.push(std::make_pair(CurrentClusterID, CurrentClusterSize));
215 }
216}
217
218static void externalize(GlobalValue *GV) {
219 if (GV->hasLocalLinkage()) {
222 }
223
224 // Unnamed entities must be named consistently between modules. setName will
225 // give a distinct name to each such entity.
226 if (!GV->hasName())
227 GV->setName("__llvmsplit_unnamed");
228}
229
230// Returns whether GV should be in partition (0-based) I of N.
231static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
232 if (const GlobalObject *Root = getGVPartitioningRoot(GV))
233 GV = Root;
234
236 if (const Comdat *C = GV->getComdat())
237 Name = C->getName();
238 else
239 Name = GV->getName();
240
241 // Partition by MD5 hash. We only need a few bits for evenness as the number
242 // of partitions will generally be in the 1-2 figure range; the low 16 bits
243 // are enough.
244 MD5 H;
246 H.update(Name);
247 H.final(R);
248 return (R[0] | (R[1] << 8)) % N == I;
249}
250
252 Module &M, unsigned N,
253 function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback,
254 bool PreserveLocals) {
255 if (!PreserveLocals) {
256 for (Function &F : M)
257 externalize(&F);
258 for (GlobalVariable &GV : M.globals())
259 externalize(&GV);
260 for (GlobalAlias &GA : M.aliases())
261 externalize(&GA);
262 for (GlobalIFunc &GIF : M.ifuncs())
263 externalize(&GIF);
264 }
265
266 // This performs splitting without a need for externalization, which might not
267 // always be possible.
268 ClusterIDMapType ClusterIDMap;
269 findPartitions(M, ClusterIDMap, N);
270
271 // FIXME: We should be able to reuse M as the last partition instead of
272 // cloning it. Note that the callers at the moment expect the module to
273 // be preserved, so will need some adjustments as well.
274 for (unsigned I = 0; I < N; ++I) {
276 std::unique_ptr<Module> MPart(
277 CloneModule(M, VMap, [&](const GlobalValue *GV) {
278 if (ClusterIDMap.count(GV))
279 return (ClusterIDMap[GV] == I);
280 else
281 return isInPartition(GV, I, N);
282 }));
283 if (I != 0)
284 MPart->setModuleInlineAsm("");
285 ModuleCallback(std::move(MPart));
286 }
287}
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
std::string Name
Generic implementation of equivalence classes through the use Tarjan's efficient union-find algorithm...
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
#define H(x, y, z)
Definition: MD5.cpp:57
Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N)
static void addNonConstUser(ClusterMapType &GVtoClusterMap, const GlobalValue *GV, const User *U)
Definition: SplitModule.cpp:60
static void findPartitions(Module &M, ClusterIDMapType &ClusterIDMap, unsigned N)
static void externalize(GlobalValue *GV)
static const GlobalObject * getGVPartitioningRoot(const GlobalValue *GV)
Definition: SplitModule.cpp:92
static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap, const GlobalValue *GV, const Value *V)
Definition: SplitModule.cpp:75
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
The address of a basic block.
Definition: Constants.h:889
static BlockAddress * lookup(const BasicBlock *BB)
Lookup an existing BlockAddress constant for the given BasicBlock.
Definition: Constants.cpp:1864
bool isConstantUsed() const
Return true if the constant has users other than constant expressions and other dangling things.
Definition: Constants.cpp:621
EquivalenceClasses - This represents a collection of equivalence classes and supports three efficient...
bool hasLocalLinkage() const
Definition: GlobalValue.h:527
const Comdat * getComdat() const
Definition: Globals.cpp:184
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:536
const GlobalObject * getAliaseeObject() const
Definition: Globals.cpp:368
@ HiddenVisibility
The GV is hidden.
Definition: GlobalValue.h:68
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:254
@ ExternalLinkage
Externally visible function.
Definition: GlobalValue.h:52
Definition: MD5.h:41
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
LLVM Value Representation.
Definition: Value.h:74
user_iterator user_begin()
Definition: Value.h:397
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
user_iterator user_end()
Definition: Value.h:405
bool hasName() const
Definition: Value.h:261
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
An efficient, type-erasing, non-owning reference to a callable.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1724
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1656
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
void SplitModule(Module &M, unsigned N, function_ref< void(std::unique_ptr< Module > MPart)> ModuleCallback, bool PreserveLocals=false)
Splits the module M into N linkable partitions.
std::unique_ptr< Module > CloneModule(const Module &M)
Return an exact copy of the specified module.
Definition: CloneModule.cpp:39
#define N