| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 1 | //===- SplitModule.cpp - Split a module into partitions -------------------===// |
| 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 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 | |
| 15 | #include "llvm/Transforms/Utils/SplitModule.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/DenseMap.h" |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/EquivalenceClasses.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallPtrSet.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "llvm/ADT/StringRef.h" |
| 21 | #include "llvm/IR/Comdat.h" |
| 22 | #include "llvm/IR/Constant.h" |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 23 | #include "llvm/IR/Constants.h" |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 24 | #include "llvm/IR/Function.h" |
| 25 | #include "llvm/IR/GlobalAlias.h" |
| 26 | #include "llvm/IR/GlobalObject.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 27 | #include "llvm/IR/GlobalIndirectSymbol.h" |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 28 | #include "llvm/IR/GlobalValue.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 29 | #include "llvm/IR/GlobalVariable.h" |
| 30 | #include "llvm/IR/Instruction.h" |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Module.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 32 | #include "llvm/IR/User.h" |
| 33 | #include "llvm/IR/Value.h" |
| 34 | #include "llvm/Support/Casting.h" |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MD5.h" |
| 38 | #include "llvm/Support/raw_ostream.h" |
| 39 | #include "llvm/Transforms/Utils/Cloning.h" |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 40 | #include "llvm/Transforms/Utils/ValueMapper.h" |
| 41 | #include <algorithm> |
| 42 | #include <cassert> |
| 43 | #include <iterator> |
| 44 | #include <memory> |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 45 | #include <queue> |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 46 | #include <utility> |
| 47 | #include <vector> |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 48 | |
| 49 | using namespace llvm; |
| 50 | |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 51 | #define DEBUG_TYPE "split-module" |
| 52 | |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 53 | namespace { |
| Eugene Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 54 | |
| 55 | using ClusterMapType = EquivalenceClasses<const GlobalValue *>; |
| 56 | using ComdatMembersType = DenseMap<const Comdat *, const GlobalValue *>; |
| 57 | using ClusterIDMapType = DenseMap<const GlobalValue *, unsigned>; |
| 58 | |
| 59 | } // end anonymous namespace |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 60 | |
| 61 | static void addNonConstUser(ClusterMapType &GVtoClusterMap, |
| 62 | const GlobalValue *GV, const User *U) { |
| 63 | assert((!isa<Constant>(U) || isa<GlobalValue>(U)) && "Bad user"); |
| 64 | |
| 65 | if (const Instruction *I = dyn_cast<Instruction>(U)) { |
| 66 | const GlobalValue *F = I->getParent()->getParent(); |
| 67 | GVtoClusterMap.unionSets(GV, F); |
| David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 68 | } else if (isa<GlobalIndirectSymbol>(U) || isa<Function>(U) || |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 69 | isa<GlobalVariable>(U)) { |
| 70 | GVtoClusterMap.unionSets(GV, cast<GlobalValue>(U)); |
| 71 | } else { |
| 72 | llvm_unreachable("Underimplemented use case"); |
| 73 | } |
| 74 | } |
| 75 | |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 76 | // Adds all GlobalValue users of V to the same cluster as GV. |
| 77 | static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap, |
| 78 | const GlobalValue *GV, const Value *V) { |
| 79 | for (auto *U : V->users()) { |
| 80 | SmallVector<const User *, 4> Worklist; |
| 81 | Worklist.push_back(U); |
| 82 | while (!Worklist.empty()) { |
| 83 | const User *UU = Worklist.pop_back_val(); |
| 84 | // For each constant that is not a GV (a pure const) recurse. |
| 85 | if (isa<Constant>(UU) && !isa<GlobalValue>(UU)) { |
| 86 | Worklist.append(UU->user_begin(), UU->user_end()); |
| 87 | continue; |
| 88 | } |
| 89 | addNonConstUser(GVtoClusterMap, GV, UU); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 94 | // Find partitions for module in the way that no locals need to be |
| 95 | // globalized. |
| 96 | // Try to balance pack those partitions into N files since this roughly equals |
| 97 | // thread balancing for the backend codegen step. |
| 98 | static void findPartitions(Module *M, ClusterIDMapType &ClusterIDMap, |
| 99 | unsigned N) { |
| 100 | // At this point module should have the proper mix of globals and locals. |
| 101 | // As we attempt to partition this module, we must not change any |
| 102 | // locals to globals. |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 103 | LLVM_DEBUG(dbgs() << "Partition module with (" << M->size() |
| 104 | << ")functions\n"); |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 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 Larin | 427f570 | 2016-01-28 18:59:28 +0000 | [diff] [blame] | 127 | // For aliases we should not separate them from their aliasees regardless |
| 128 | // of linkage. |
| David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 129 | if (auto *GIS = dyn_cast<GlobalIndirectSymbol>(&GV)) { |
| 130 | if (const GlobalObject *Base = GIS->getBaseObject()) |
| Sergei Larin | 427f570 | 2016-01-28 18:59:28 +0000 | [diff] [blame] | 131 | GVtoClusterMap.unionSets(&GV, Base); |
| 132 | } |
| 133 | |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 134 | 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 Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 138 | continue; |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 139 | addAllGlobalValueUsers(GVtoClusterMap, F, BA); |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 142 | |
| 143 | if (GV.hasLocalLinkage()) |
| Dimitry Andric | e4f5d01 | 2017-12-18 19:46:56 +0000 | [diff] [blame] | 144 | 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 Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 153 | 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 Zelenko | 5adb96c | 2017-10-26 00:55:39 +0000 | [diff] [blame] | 169 | using SortType = std::pair<unsigned, ClusterMapType::iterator>; |
| 170 | |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 171 | SmallVector<SortType, 64> Sets; |
| Matthias Braun | b30f2f51 | 2016-01-30 01:24:31 +0000 | [diff] [blame] | 172 | SmallPtrSet<const GlobalValue *, 32> Visited; |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 173 | |
| 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 | |
| Fangrui Song | 0cac726 | 2018-09-27 02:13:45 +0000 | [diff] [blame] | 183 | llvm::sort(Sets, [](const SortType &a, const SortType &b) { |
| 184 | if (a.first == b.first) |
| 185 | return a.second->getData()->getName() > b.second->getData()->getName(); |
| 186 | else |
| 187 | return a.first > b.first; |
| 188 | }); |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 189 | |
| 190 | for (auto &I : Sets) { |
| 191 | unsigned CurrentClusterID = BalancinQueue.top().first; |
| 192 | unsigned CurrentClusterSize = BalancinQueue.top().second; |
| 193 | BalancinQueue.pop(); |
| 194 | |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 195 | LLVM_DEBUG(dbgs() << "Root[" << CurrentClusterID << "] cluster_size(" |
| 196 | << I.first << ") ----> " << I.second->getData()->getName() |
| 197 | << "\n"); |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 198 | |
| 199 | for (ClusterMapType::member_iterator MI = |
| 200 | GVtoClusterMap.findLeader(I.second); |
| 201 | MI != GVtoClusterMap.member_end(); ++MI) { |
| 202 | if (!Visited.insert(*MI).second) |
| 203 | continue; |
| Nicola Zaghen | d34e60c | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 204 | LLVM_DEBUG(dbgs() << "----> " << (*MI)->getName() |
| 205 | << ((*MI)->hasLocalLinkage() ? " l " : " e ") << "\n"); |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 206 | Visited.insert(*MI); |
| 207 | ClusterIDMap[*MI] = CurrentClusterID; |
| 208 | CurrentClusterSize++; |
| 209 | } |
| 210 | // Add this set size to the number of entries in this cluster. |
| 211 | BalancinQueue.push(std::make_pair(CurrentClusterID, CurrentClusterSize)); |
| 212 | } |
| 213 | } |
| 214 | |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 215 | static void externalize(GlobalValue *GV) { |
| 216 | if (GV->hasLocalLinkage()) { |
| 217 | GV->setLinkage(GlobalValue::ExternalLinkage); |
| 218 | GV->setVisibility(GlobalValue::HiddenVisibility); |
| 219 | } |
| 220 | |
| 221 | // Unnamed entities must be named consistently between modules. setName will |
| 222 | // give a distinct name to each such entity. |
| 223 | if (!GV->hasName()) |
| 224 | GV->setName("__llvmsplit_unnamed"); |
| 225 | } |
| 226 | |
| 227 | // Returns whether GV should be in partition (0-based) I of N. |
| 228 | static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) { |
| David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 229 | if (auto *GIS = dyn_cast<GlobalIndirectSymbol>(GV)) |
| 230 | if (const GlobalObject *Base = GIS->getBaseObject()) |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 231 | GV = Base; |
| 232 | |
| 233 | StringRef Name; |
| 234 | if (const Comdat *C = GV->getComdat()) |
| 235 | Name = C->getName(); |
| 236 | else |
| 237 | Name = GV->getName(); |
| 238 | |
| 239 | // Partition by MD5 hash. We only need a few bits for evenness as the number |
| 240 | // of partitions will generally be in the 1-2 figure range; the low 16 bits |
| 241 | // are enough. |
| 242 | MD5 H; |
| 243 | MD5::MD5Result R; |
| 244 | H.update(Name); |
| 245 | H.final(R); |
| 246 | return (R[0] | (R[1] << 8)) % N == I; |
| 247 | } |
| 248 | |
| 249 | void llvm::SplitModule( |
| 250 | std::unique_ptr<Module> M, unsigned N, |
| Benjamin Kramer | d3f4c05 | 2016-06-12 16:13:55 +0000 | [diff] [blame] | 251 | function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback, |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 252 | bool PreserveLocals) { |
| 253 | if (!PreserveLocals) { |
| 254 | for (Function &F : *M) |
| 255 | externalize(&F); |
| 256 | for (GlobalVariable &GV : M->globals()) |
| 257 | externalize(&GV); |
| 258 | for (GlobalAlias &GA : M->aliases()) |
| 259 | externalize(&GA); |
| David Majnemer | 9554949 | 2016-05-04 00:20:48 +0000 | [diff] [blame] | 260 | for (GlobalIFunc &GIF : M->ifuncs()) |
| 261 | externalize(&GIF); |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 262 | } |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 263 | |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 264 | // This performs splitting without a need for externalization, which might not |
| 265 | // always be possible. |
| 266 | ClusterIDMapType ClusterIDMap; |
| 267 | findPartitions(M.get(), ClusterIDMap, N); |
| 268 | |
| 269 | // FIXME: We should be able to reuse M as the last partition instead of |
| 270 | // cloning it. |
| 271 | for (unsigned I = 0; I < N; ++I) { |
| 272 | ValueToValueMapTy VMap; |
| 273 | std::unique_ptr<Module> MPart( |
| Rafael Espindola | 7186753 | 2018-02-14 19:50:40 +0000 | [diff] [blame] | 274 | CloneModule(*M, VMap, [&](const GlobalValue *GV) { |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 275 | if (ClusterIDMap.count(GV)) |
| 276 | return (ClusterIDMap[GV] == I); |
| 277 | else |
| Sergei Larin | d19d4d3 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 278 | return isInPartition(GV, I, N); |
| Evgeniy Stepanov | f74f091 | 2016-03-31 21:55:11 +0000 | [diff] [blame] | 279 | })); |
| 280 | if (I != 0) |
| 281 | MPart->setModuleInlineAsm(""); |
| 282 | ModuleCallback(std::move(MPart)); |
| Peter Collingbourne | 1dc6a8d | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 283 | } |
| 284 | } |