17d523365SDimitry Andric //===- SplitModule.cpp - Split a module into partitions -------------------===//
27d523365SDimitry Andric //
37d523365SDimitry Andric // The LLVM Compiler Infrastructure
47d523365SDimitry Andric //
57d523365SDimitry Andric // This file is distributed under the University of Illinois Open Source
67d523365SDimitry Andric // License. See LICENSE.TXT for details.
77d523365SDimitry Andric //
87d523365SDimitry Andric //===----------------------------------------------------------------------===//
97d523365SDimitry Andric //
107d523365SDimitry Andric // This file defines the function llvm::SplitModule, which splits a module
117d523365SDimitry Andric // into multiple linkable partitions. It can be used to implement parallel code
127d523365SDimitry Andric // generation for link-time optimization.
137d523365SDimitry Andric //
147d523365SDimitry Andric //===----------------------------------------------------------------------===//
157d523365SDimitry Andric
167d523365SDimitry Andric #include "llvm/Transforms/Utils/SplitModule.h"
172cab237bSDimitry Andric #include "llvm/ADT/DenseMap.h"
183ca95b02SDimitry Andric #include "llvm/ADT/EquivalenceClasses.h"
192cab237bSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
202cab237bSDimitry Andric #include "llvm/ADT/SmallVector.h"
212cab237bSDimitry Andric #include "llvm/ADT/StringRef.h"
222cab237bSDimitry Andric #include "llvm/IR/Comdat.h"
232cab237bSDimitry Andric #include "llvm/IR/Constant.h"
243ca95b02SDimitry Andric #include "llvm/IR/Constants.h"
257d523365SDimitry Andric #include "llvm/IR/Function.h"
267d523365SDimitry Andric #include "llvm/IR/GlobalAlias.h"
277d523365SDimitry Andric #include "llvm/IR/GlobalObject.h"
282cab237bSDimitry Andric #include "llvm/IR/GlobalIndirectSymbol.h"
297d523365SDimitry Andric #include "llvm/IR/GlobalValue.h"
302cab237bSDimitry Andric #include "llvm/IR/GlobalVariable.h"
312cab237bSDimitry Andric #include "llvm/IR/Instruction.h"
327d523365SDimitry Andric #include "llvm/IR/Module.h"
332cab237bSDimitry Andric #include "llvm/IR/User.h"
342cab237bSDimitry Andric #include "llvm/IR/Value.h"
352cab237bSDimitry Andric #include "llvm/Support/Casting.h"
363ca95b02SDimitry Andric #include "llvm/Support/Debug.h"
372cab237bSDimitry Andric #include "llvm/Support/ErrorHandling.h"
387d523365SDimitry Andric #include "llvm/Support/MD5.h"
397d523365SDimitry Andric #include "llvm/Support/raw_ostream.h"
407d523365SDimitry Andric #include "llvm/Transforms/Utils/Cloning.h"
412cab237bSDimitry Andric #include "llvm/Transforms/Utils/ValueMapper.h"
422cab237bSDimitry Andric #include <algorithm>
432cab237bSDimitry Andric #include <cassert>
442cab237bSDimitry Andric #include <iterator>
452cab237bSDimitry Andric #include <memory>
463ca95b02SDimitry Andric #include <queue>
472cab237bSDimitry Andric #include <utility>
482cab237bSDimitry Andric #include <vector>
497d523365SDimitry Andric
507d523365SDimitry Andric using namespace llvm;
517d523365SDimitry Andric
522cab237bSDimitry Andric #define DEBUG_TYPE "split-module"
532cab237bSDimitry Andric
543ca95b02SDimitry Andric namespace {
552cab237bSDimitry Andric
562cab237bSDimitry Andric using ClusterMapType = EquivalenceClasses<const GlobalValue *>;
572cab237bSDimitry Andric using ComdatMembersType = DenseMap<const Comdat *, const GlobalValue *>;
582cab237bSDimitry Andric using ClusterIDMapType = DenseMap<const GlobalValue *, unsigned>;
592cab237bSDimitry Andric
602cab237bSDimitry Andric } // end anonymous namespace
613ca95b02SDimitry Andric
addNonConstUser(ClusterMapType & GVtoClusterMap,const GlobalValue * GV,const User * U)623ca95b02SDimitry Andric static void addNonConstUser(ClusterMapType &GVtoClusterMap,
633ca95b02SDimitry Andric const GlobalValue *GV, const User *U) {
643ca95b02SDimitry Andric assert((!isa<Constant>(U) || isa<GlobalValue>(U)) && "Bad user");
653ca95b02SDimitry Andric
663ca95b02SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(U)) {
673ca95b02SDimitry Andric const GlobalValue *F = I->getParent()->getParent();
683ca95b02SDimitry Andric GVtoClusterMap.unionSets(GV, F);
693ca95b02SDimitry Andric } else if (isa<GlobalIndirectSymbol>(U) || isa<Function>(U) ||
703ca95b02SDimitry Andric isa<GlobalVariable>(U)) {
713ca95b02SDimitry Andric GVtoClusterMap.unionSets(GV, cast<GlobalValue>(U));
723ca95b02SDimitry Andric } else {
733ca95b02SDimitry Andric llvm_unreachable("Underimplemented use case");
743ca95b02SDimitry Andric }
753ca95b02SDimitry Andric }
763ca95b02SDimitry Andric
773ca95b02SDimitry Andric // Adds all GlobalValue users of V to the same cluster as GV.
addAllGlobalValueUsers(ClusterMapType & GVtoClusterMap,const GlobalValue * GV,const Value * V)783ca95b02SDimitry Andric static void addAllGlobalValueUsers(ClusterMapType &GVtoClusterMap,
793ca95b02SDimitry Andric const GlobalValue *GV, const Value *V) {
803ca95b02SDimitry Andric for (auto *U : V->users()) {
813ca95b02SDimitry Andric SmallVector<const User *, 4> Worklist;
823ca95b02SDimitry Andric Worklist.push_back(U);
833ca95b02SDimitry Andric while (!Worklist.empty()) {
843ca95b02SDimitry Andric const User *UU = Worklist.pop_back_val();
853ca95b02SDimitry Andric // For each constant that is not a GV (a pure const) recurse.
863ca95b02SDimitry Andric if (isa<Constant>(UU) && !isa<GlobalValue>(UU)) {
873ca95b02SDimitry Andric Worklist.append(UU->user_begin(), UU->user_end());
883ca95b02SDimitry Andric continue;
893ca95b02SDimitry Andric }
903ca95b02SDimitry Andric addNonConstUser(GVtoClusterMap, GV, UU);
913ca95b02SDimitry Andric }
923ca95b02SDimitry Andric }
933ca95b02SDimitry Andric }
943ca95b02SDimitry Andric
953ca95b02SDimitry Andric // Find partitions for module in the way that no locals need to be
963ca95b02SDimitry Andric // globalized.
973ca95b02SDimitry Andric // Try to balance pack those partitions into N files since this roughly equals
983ca95b02SDimitry Andric // thread balancing for the backend codegen step.
findPartitions(Module * M,ClusterIDMapType & ClusterIDMap,unsigned N)993ca95b02SDimitry Andric static void findPartitions(Module *M, ClusterIDMapType &ClusterIDMap,
1003ca95b02SDimitry Andric unsigned N) {
1013ca95b02SDimitry Andric // At this point module should have the proper mix of globals and locals.
1023ca95b02SDimitry Andric // As we attempt to partition this module, we must not change any
1033ca95b02SDimitry Andric // locals to globals.
1044ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Partition module with (" << M->size()
1054ba319b5SDimitry Andric << ")functions\n");
1063ca95b02SDimitry Andric ClusterMapType GVtoClusterMap;
1073ca95b02SDimitry Andric ComdatMembersType ComdatMembers;
1083ca95b02SDimitry Andric
1093ca95b02SDimitry Andric auto recordGVSet = [&GVtoClusterMap, &ComdatMembers](GlobalValue &GV) {
1103ca95b02SDimitry Andric if (GV.isDeclaration())
1113ca95b02SDimitry Andric return;
1123ca95b02SDimitry Andric
1133ca95b02SDimitry Andric if (!GV.hasName())
1143ca95b02SDimitry Andric GV.setName("__llvmsplit_unnamed");
1153ca95b02SDimitry Andric
1163ca95b02SDimitry Andric // Comdat groups must not be partitioned. For comdat groups that contain
1173ca95b02SDimitry Andric // locals, record all their members here so we can keep them together.
1183ca95b02SDimitry Andric // Comdat groups that only contain external globals are already handled by
1193ca95b02SDimitry Andric // the MD5-based partitioning.
1203ca95b02SDimitry Andric if (const Comdat *C = GV.getComdat()) {
1213ca95b02SDimitry Andric auto &Member = ComdatMembers[C];
1223ca95b02SDimitry Andric if (Member)
1233ca95b02SDimitry Andric GVtoClusterMap.unionSets(Member, &GV);
1243ca95b02SDimitry Andric else
1253ca95b02SDimitry Andric Member = &GV;
1263ca95b02SDimitry Andric }
1273ca95b02SDimitry Andric
1283ca95b02SDimitry Andric // For aliases we should not separate them from their aliasees regardless
1293ca95b02SDimitry Andric // of linkage.
1303ca95b02SDimitry Andric if (auto *GIS = dyn_cast<GlobalIndirectSymbol>(&GV)) {
1313ca95b02SDimitry Andric if (const GlobalObject *Base = GIS->getBaseObject())
1323ca95b02SDimitry Andric GVtoClusterMap.unionSets(&GV, Base);
1333ca95b02SDimitry Andric }
1343ca95b02SDimitry Andric
1353ca95b02SDimitry Andric if (const Function *F = dyn_cast<Function>(&GV)) {
1363ca95b02SDimitry Andric for (const BasicBlock &BB : *F) {
1373ca95b02SDimitry Andric BlockAddress *BA = BlockAddress::lookup(&BB);
1383ca95b02SDimitry Andric if (!BA || !BA->isConstantUsed())
1393ca95b02SDimitry Andric continue;
1403ca95b02SDimitry Andric addAllGlobalValueUsers(GVtoClusterMap, F, BA);
1413ca95b02SDimitry Andric }
1423ca95b02SDimitry Andric }
1433ca95b02SDimitry Andric
1443ca95b02SDimitry Andric if (GV.hasLocalLinkage())
1453ca95b02SDimitry Andric addAllGlobalValueUsers(GVtoClusterMap, &GV, &GV);
1463ca95b02SDimitry Andric };
1473ca95b02SDimitry Andric
1482cab237bSDimitry Andric llvm::for_each(M->functions(), recordGVSet);
1492cab237bSDimitry Andric llvm::for_each(M->globals(), recordGVSet);
1502cab237bSDimitry Andric llvm::for_each(M->aliases(), recordGVSet);
1513ca95b02SDimitry Andric
1523ca95b02SDimitry Andric // Assigned all GVs to merged clusters while balancing number of objects in
1533ca95b02SDimitry Andric // each.
1543ca95b02SDimitry Andric auto CompareClusters = [](const std::pair<unsigned, unsigned> &a,
1553ca95b02SDimitry Andric const std::pair<unsigned, unsigned> &b) {
1563ca95b02SDimitry Andric if (a.second || b.second)
1573ca95b02SDimitry Andric return a.second > b.second;
1583ca95b02SDimitry Andric else
1593ca95b02SDimitry Andric return a.first > b.first;
1603ca95b02SDimitry Andric };
1613ca95b02SDimitry Andric
1623ca95b02SDimitry Andric std::priority_queue<std::pair<unsigned, unsigned>,
1633ca95b02SDimitry Andric std::vector<std::pair<unsigned, unsigned>>,
1643ca95b02SDimitry Andric decltype(CompareClusters)>
1653ca95b02SDimitry Andric BalancinQueue(CompareClusters);
1663ca95b02SDimitry Andric // Pre-populate priority queue with N slot blanks.
1673ca95b02SDimitry Andric for (unsigned i = 0; i < N; ++i)
1683ca95b02SDimitry Andric BalancinQueue.push(std::make_pair(i, 0));
1693ca95b02SDimitry Andric
1702cab237bSDimitry Andric using SortType = std::pair<unsigned, ClusterMapType::iterator>;
1712cab237bSDimitry Andric
1723ca95b02SDimitry Andric SmallVector<SortType, 64> Sets;
1733ca95b02SDimitry Andric SmallPtrSet<const GlobalValue *, 32> Visited;
1743ca95b02SDimitry Andric
1753ca95b02SDimitry Andric // To guarantee determinism, we have to sort SCC according to size.
1763ca95b02SDimitry Andric // When size is the same, use leader's name.
1773ca95b02SDimitry Andric for (ClusterMapType::iterator I = GVtoClusterMap.begin(),
1783ca95b02SDimitry Andric E = GVtoClusterMap.end(); I != E; ++I)
1793ca95b02SDimitry Andric if (I->isLeader())
1803ca95b02SDimitry Andric Sets.push_back(
1813ca95b02SDimitry Andric std::make_pair(std::distance(GVtoClusterMap.member_begin(I),
1823ca95b02SDimitry Andric GVtoClusterMap.member_end()), I));
1833ca95b02SDimitry Andric
184*b5893f02SDimitry Andric llvm::sort(Sets, [](const SortType &a, const SortType &b) {
1853ca95b02SDimitry Andric if (a.first == b.first)
186*b5893f02SDimitry Andric return a.second->getData()->getName() > b.second->getData()->getName();
1873ca95b02SDimitry Andric else
1883ca95b02SDimitry Andric return a.first > b.first;
1893ca95b02SDimitry Andric });
1903ca95b02SDimitry Andric
1913ca95b02SDimitry Andric for (auto &I : Sets) {
1923ca95b02SDimitry Andric unsigned CurrentClusterID = BalancinQueue.top().first;
1933ca95b02SDimitry Andric unsigned CurrentClusterSize = BalancinQueue.top().second;
1943ca95b02SDimitry Andric BalancinQueue.pop();
1953ca95b02SDimitry Andric
1964ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "Root[" << CurrentClusterID << "] cluster_size("
1974ba319b5SDimitry Andric << I.first << ") ----> " << I.second->getData()->getName()
1984ba319b5SDimitry Andric << "\n");
1993ca95b02SDimitry Andric
2003ca95b02SDimitry Andric for (ClusterMapType::member_iterator MI =
2013ca95b02SDimitry Andric GVtoClusterMap.findLeader(I.second);
2023ca95b02SDimitry Andric MI != GVtoClusterMap.member_end(); ++MI) {
2033ca95b02SDimitry Andric if (!Visited.insert(*MI).second)
2043ca95b02SDimitry Andric continue;
2054ba319b5SDimitry Andric LLVM_DEBUG(dbgs() << "----> " << (*MI)->getName()
2063ca95b02SDimitry Andric << ((*MI)->hasLocalLinkage() ? " l " : " e ") << "\n");
2073ca95b02SDimitry Andric Visited.insert(*MI);
2083ca95b02SDimitry Andric ClusterIDMap[*MI] = CurrentClusterID;
2093ca95b02SDimitry Andric CurrentClusterSize++;
2103ca95b02SDimitry Andric }
2113ca95b02SDimitry Andric // Add this set size to the number of entries in this cluster.
2123ca95b02SDimitry Andric BalancinQueue.push(std::make_pair(CurrentClusterID, CurrentClusterSize));
2133ca95b02SDimitry Andric }
2143ca95b02SDimitry Andric }
2153ca95b02SDimitry Andric
externalize(GlobalValue * GV)2167d523365SDimitry Andric static void externalize(GlobalValue *GV) {
2177d523365SDimitry Andric if (GV->hasLocalLinkage()) {
2187d523365SDimitry Andric GV->setLinkage(GlobalValue::ExternalLinkage);
2197d523365SDimitry Andric GV->setVisibility(GlobalValue::HiddenVisibility);
2207d523365SDimitry Andric }
2217d523365SDimitry Andric
2227d523365SDimitry Andric // Unnamed entities must be named consistently between modules. setName will
2237d523365SDimitry Andric // give a distinct name to each such entity.
2247d523365SDimitry Andric if (!GV->hasName())
2257d523365SDimitry Andric GV->setName("__llvmsplit_unnamed");
2267d523365SDimitry Andric }
2277d523365SDimitry Andric
2287d523365SDimitry Andric // Returns whether GV should be in partition (0-based) I of N.
isInPartition(const GlobalValue * GV,unsigned I,unsigned N)2297d523365SDimitry Andric static bool isInPartition(const GlobalValue *GV, unsigned I, unsigned N) {
2303ca95b02SDimitry Andric if (auto *GIS = dyn_cast<GlobalIndirectSymbol>(GV))
2313ca95b02SDimitry Andric if (const GlobalObject *Base = GIS->getBaseObject())
2327d523365SDimitry Andric GV = Base;
2337d523365SDimitry Andric
2347d523365SDimitry Andric StringRef Name;
2357d523365SDimitry Andric if (const Comdat *C = GV->getComdat())
2367d523365SDimitry Andric Name = C->getName();
2377d523365SDimitry Andric else
2387d523365SDimitry Andric Name = GV->getName();
2397d523365SDimitry Andric
2407d523365SDimitry Andric // Partition by MD5 hash. We only need a few bits for evenness as the number
2417d523365SDimitry Andric // of partitions will generally be in the 1-2 figure range; the low 16 bits
2427d523365SDimitry Andric // are enough.
2437d523365SDimitry Andric MD5 H;
2447d523365SDimitry Andric MD5::MD5Result R;
2457d523365SDimitry Andric H.update(Name);
2467d523365SDimitry Andric H.final(R);
2477d523365SDimitry Andric return (R[0] | (R[1] << 8)) % N == I;
2487d523365SDimitry Andric }
2497d523365SDimitry Andric
SplitModule(std::unique_ptr<Module> M,unsigned N,function_ref<void (std::unique_ptr<Module> MPart)> ModuleCallback,bool PreserveLocals)2507d523365SDimitry Andric void llvm::SplitModule(
2517d523365SDimitry Andric std::unique_ptr<Module> M, unsigned N,
2523ca95b02SDimitry Andric function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback,
2533ca95b02SDimitry Andric bool PreserveLocals) {
2543ca95b02SDimitry Andric if (!PreserveLocals) {
2557d523365SDimitry Andric for (Function &F : *M)
2567d523365SDimitry Andric externalize(&F);
2577d523365SDimitry Andric for (GlobalVariable &GV : M->globals())
2587d523365SDimitry Andric externalize(&GV);
2597d523365SDimitry Andric for (GlobalAlias &GA : M->aliases())
2607d523365SDimitry Andric externalize(&GA);
2613ca95b02SDimitry Andric for (GlobalIFunc &GIF : M->ifuncs())
2623ca95b02SDimitry Andric externalize(&GIF);
2633ca95b02SDimitry Andric }
2643ca95b02SDimitry Andric
2653ca95b02SDimitry Andric // This performs splitting without a need for externalization, which might not
2663ca95b02SDimitry Andric // always be possible.
2673ca95b02SDimitry Andric ClusterIDMapType ClusterIDMap;
2683ca95b02SDimitry Andric findPartitions(M.get(), ClusterIDMap, N);
2697d523365SDimitry Andric
2707d523365SDimitry Andric // FIXME: We should be able to reuse M as the last partition instead of
2717d523365SDimitry Andric // cloning it.
2723ca95b02SDimitry Andric for (unsigned I = 0; I < N; ++I) {
2737d523365SDimitry Andric ValueToValueMapTy VMap;
2747d523365SDimitry Andric std::unique_ptr<Module> MPart(
2754ba319b5SDimitry Andric CloneModule(*M, VMap, [&](const GlobalValue *GV) {
2763ca95b02SDimitry Andric if (ClusterIDMap.count(GV))
2773ca95b02SDimitry Andric return (ClusterIDMap[GV] == I);
2783ca95b02SDimitry Andric else
2797d523365SDimitry Andric return isInPartition(GV, I, N);
2807d523365SDimitry Andric }));
2817d523365SDimitry Andric if (I != 0)
2827d523365SDimitry Andric MPart->setModuleInlineAsm("");
2837d523365SDimitry Andric ModuleCallback(std::move(MPart));
2847d523365SDimitry Andric }
2857d523365SDimitry Andric }
286