Coverage Report

Created: 2019-07-24 05:18

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