Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Transforms/Utils/FunctionImportUtils.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
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 implements the FunctionImportGlobalProcessing class, used
10
// to perform the necessary global value handling for function importing.
11
//
12
//===----------------------------------------------------------------------===//
13
14
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
15
#include "llvm/IR/InstIterator.h"
16
using namespace llvm;
17
18
/// Checks if we should import SGV as a definition, otherwise import as a
19
/// declaration.
20
bool FunctionImportGlobalProcessing::doImportAsDefinition(
21
719
    const GlobalValue *SGV, SetVector<GlobalValue *> *GlobalsToImport) {
22
719
23
719
  // Only import the globals requested for importing.
24
719
  if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
25
427
    return false;
26
292
27
292
  assert(!isa<GlobalAlias>(SGV) &&
28
292
         "Unexpected global alias in the import list.");
29
292
30
292
  // Otherwise yes.
31
292
  return true;
32
292
}
33
34
bool FunctionImportGlobalProcessing::doImportAsDefinition(
35
719
    const GlobalValue *SGV) {
36
719
  if (!isPerformingImport())
37
0
    return false;
38
719
  return FunctionImportGlobalProcessing::doImportAsDefinition(SGV,
39
719
                                                              GlobalsToImport);
40
719
}
41
42
bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
43
192
    const GlobalValue *SGV) {
44
192
  assert(SGV->hasLocalLinkage());
45
192
  // Both the imported references and the original local variable must
46
192
  // be promoted.
47
192
  if (!isPerformingImport() && 
!isModuleExporting()78
)
48
5
    return false;
49
187
50
187
  if (isPerformingImport()) {
51
114
    assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
52
114
            !isNonRenamableLocal(*SGV)) &&
53
114
           "Attempting to promote non-renamable local");
54
114
    // We don't know for sure yet if we are importing this value (as either
55
114
    // a reference or a def), since we are simply walking all values in the
56
114
    // module. But by necessity if we end up importing it and it is local,
57
114
    // it must be promoted, so unconditionally promote all values in the
58
114
    // importing module.
59
114
    return true;
60
114
  }
61
73
62
73
  // When exporting, consult the index. We can have more than one local
63
73
  // with the same GUID, in the case of same-named locals in different but
64
73
  // same-named source files that were compiled in their respective directories
65
73
  // (so the source file name and resulting GUID is the same). Find the one
66
73
  // in this module.
67
73
  auto Summary = ImportIndex.findSummaryInModule(
68
73
      SGV->getGUID(), SGV->getParent()->getModuleIdentifier());
69
73
  assert(Summary && "Missing summary for global value when exporting");
70
73
  auto Linkage = Summary->linkage();
71
73
  if (!GlobalValue::isLocalLinkage(Linkage)) {
72
41
    assert(!isNonRenamableLocal(*SGV) &&
73
41
           "Attempting to promote non-renamable local");
74
41
    return true;
75
41
  }
76
32
77
32
  return false;
78
32
}
79
80
#ifndef NDEBUG
81
bool FunctionImportGlobalProcessing::isNonRenamableLocal(
82
    const GlobalValue &GV) const {
83
  if (!GV.hasLocalLinkage())
84
    return false;
85
  // This needs to stay in sync with the logic in buildModuleSummaryIndex.
86
  if (GV.hasSection())
87
    return true;
88
  if (Used.count(const_cast<GlobalValue *>(&GV)))
89
    return true;
90
  return false;
91
}
92
#endif
93
94
std::string FunctionImportGlobalProcessing::getName(const GlobalValue *SGV,
95
155
                                                    bool DoPromote) {
96
155
  // For locals that must be promoted to global scope, ensure that
97
155
  // the promoted name uniquely identifies the copy in the original module,
98
155
  // using the ID assigned during combined index creation. When importing,
99
155
  // we rename all locals (not just those that are promoted) in order to
100
155
  // avoid naming conflicts between locals imported from different modules.
101
155
  if (SGV->hasLocalLinkage() && (DoPromote || 
isPerformingImport()0
))
102
155
    return ModuleSummaryIndex::getGlobalNameForLocal(
103
155
        SGV->getName(),
104
155
        ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
105
0
  return SGV->getName();
106
0
}
107
108
GlobalValue::LinkageTypes
109
FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
110
2.12k
                                           bool DoPromote) {
111
2.12k
  // Any local variable that is referenced by an exported function needs
112
2.12k
  // to be promoted to global scope. Since we don't currently know which
113
2.12k
  // functions reference which local variables/functions, we must treat
114
2.12k
  // all as potentially exported if this module is exporting anything.
115
2.12k
  if (isModuleExporting()) {
116
1.30k
    if (SGV->hasLocalLinkage() && 
DoPromote73
)
117
41
      return GlobalValue::ExternalLinkage;
118
1.26k
    return SGV->getLinkage();
119
1.26k
  }
120
820
121
820
  // Otherwise, if we aren't importing, no linkage change is needed.
122
820
  if (!isPerformingImport())
123
21
    return SGV->getLinkage();
124
799
125
799
  switch (SGV->getLinkage()) {
126
799
  case GlobalValue::LinkOnceODRLinkage:
127
592
  case GlobalValue::ExternalLinkage:
128
592
    // External and linkonce definitions are converted to available_externally
129
592
    // definitions upon import, so that they are available for inlining
130
592
    // and/or optimization, but are turned into declarations later
131
592
    // during the EliminateAvailableExternally pass.
132
592
    if (doImportAsDefinition(SGV) && 
!isa<GlobalAlias>(SGV)249
)
133
249
      return GlobalValue::AvailableExternallyLinkage;
134
343
    // An imported external declaration stays external.
135
343
    return SGV->getLinkage();
136
343
137
343
  case GlobalValue::AvailableExternallyLinkage:
138
1
    // An imported available_externally definition converts
139
1
    // to external if imported as a declaration.
140
1
    if (!doImportAsDefinition(SGV))
141
0
      return GlobalValue::ExternalLinkage;
142
1
    // An imported available_externally declaration stays that way.
143
1
    return SGV->getLinkage();
144
1
145
56
  case GlobalValue::LinkOnceAnyLinkage:
146
56
  case GlobalValue::WeakAnyLinkage:
147
56
    // Can't import linkonce_any/weak_any definitions correctly, or we might
148
56
    // change the program semantics, since the linker will pick the first
149
56
    // linkonce_any/weak_any definition and importing would change the order
150
56
    // they are seen by the linker. The module linking caller needs to enforce
151
56
    // this.
152
56
    assert(!doImportAsDefinition(SGV));
153
56
    // If imported as a declaration, it becomes external_weak.
154
56
    return SGV->getLinkage();
155
56
156
56
  case GlobalValue::WeakODRLinkage:
157
13
    // For weak_odr linkage, there is a guarantee that all copies will be
158
13
    // equivalent, so the issue described above for weak_any does not exist,
159
13
    // and the definition can be imported. It can be treated similarly
160
13
    // to an imported externally visible global value.
161
13
    if (doImportAsDefinition(SGV) && 
!isa<GlobalAlias>(SGV)6
)
162
6
      return GlobalValue::AvailableExternallyLinkage;
163
7
    else
164
7
      return GlobalValue::ExternalLinkage;
165
0
166
6
  case GlobalValue::AppendingLinkage:
167
6
    // It would be incorrect to import an appending linkage variable,
168
6
    // since it would cause global constructors/destructors to be
169
6
    // executed multiple times. This should have already been handled
170
6
    // by linkIfNeeded, and we will assert in shouldLinkFromSource
171
6
    // if we try to import, so we simply return AppendingLinkage.
172
6
    return GlobalValue::AppendingLinkage;
173
0
174
114
  case GlobalValue::InternalLinkage:
175
114
  case GlobalValue::PrivateLinkage:
176
114
    // If we are promoting the local to global scope, it is handled
177
114
    // similarly to a normal externally visible global.
178
114
    if (DoPromote) {
179
114
      if (doImportAsDefinition(SGV) && 
!isa<GlobalAlias>(SGV)37
)
180
37
        return GlobalValue::AvailableExternallyLinkage;
181
77
      else
182
77
        return GlobalValue::ExternalLinkage;
183
0
    }
184
0
    // A non-promoted imported local definition stays local.
185
0
    // The ThinLTO pass will eventually force-import their definitions.
186
0
    return SGV->getLinkage();
187
0
188
0
  case GlobalValue::ExternalWeakLinkage:
189
0
    // External weak doesn't apply to definitions, must be a declaration.
190
0
    assert(!doImportAsDefinition(SGV));
191
0
    // Linkage stays external_weak.
192
0
    return SGV->getLinkage();
193
0
194
17
  case GlobalValue::CommonLinkage:
195
17
    // Linkage stays common on definitions.
196
17
    // The ThinLTO pass will eventually force-import their definitions.
197
17
    return SGV->getLinkage();
198
0
  }
199
0
200
0
  llvm_unreachable("unknown linkage type");
201
0
}
202
203
2.12k
void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
204
2.12k
205
2.12k
  ValueInfo VI;
206
2.12k
  if (GV.hasName()) {
207
2.09k
    VI = ImportIndex.getValueInfo(GV.getGUID());
208
2.09k
    // Set synthetic function entry counts.
209
2.09k
    if (VI && 
ImportIndex.hasSyntheticEntryCounts()2.04k
) {
210
20
      if (Function *
F19
= dyn_cast<Function>(&GV)) {
211
20
        if (!F->isDeclaration()) {
212
12
          for (auto &S : VI.getSummaryList()) {
213
12
            FunctionSummary *FS = dyn_cast<FunctionSummary>(S->getBaseObject());
214
12
            if (FS->modulePath() == M.getModuleIdentifier()) {
215
12
              F->setEntryCount(Function::ProfileCount(FS->entryCount(),
216
12
                                                      Function::PCT_Synthetic));
217
12
              break;
218
12
            }
219
12
          }
220
12
        }
221
20
      }
222
19
    }
223
2.09k
    // Check the summaries to see if the symbol gets resolved to a known local
224
2.09k
    // definition.
225
2.09k
    if (VI && 
VI.isDSOLocal()2.04k
) {
226
650
      GV.setDSOLocal(true);
227
650
      if (GV.hasDLLImportStorageClass())
228
2
        GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
229
650
    }
230
2.09k
  }
231
2.12k
232
2.12k
  // Mark read/write-only variables which can be imported with specific
233
2.12k
  // attribute. We can't internalize them now because IRMover will fail
234
2.12k
  // to link variable definitions to their external declarations during
235
2.12k
  // ThinLTO import. We'll internalize read-only variables later, after
236
2.12k
  // import is finished. See internalizeGVsAfterImport.
237
2.12k
  //
238
2.12k
  // If global value dead stripping is not enabled in summary then
239
2.12k
  // propagateConstants hasn't been run. We can't internalize GV
240
2.12k
  // in such case.
241
2.12k
  if (!GV.isDeclaration() && 
VI1.55k
&&
ImportIndex.withGlobalValueDeadStripping()1.52k
) {
242
776
    const auto &SL = VI.getSummaryList();
243
776
    auto *GVS = SL.empty() ? 
nullptr2
:
dyn_cast<GlobalVarSummary>(SL[0].get())774
;
244
776
    // At this stage "maybe" is "definitely"
245
776
    if (GVS && 
(148
GVS->maybeReadOnly()148
||
GVS->maybeWriteOnly()107
))
246
51
      cast<GlobalVariable>(&GV)->addAttribute("thinlto-internalize");
247
776
  }
248
2.12k
249
2.12k
  bool DoPromote = false;
250
2.12k
  if (GV.hasLocalLinkage() &&
251
2.12k
      
(192
(DoPromote = shouldPromoteLocalToGlobal(&GV))192
||
isPerformingImport()37
)) {
252
155
    // Save the original name string before we rename GV below.
253
155
    auto Name = GV.getName().str();
254
155
    // Once we change the name or linkage it is difficult to determine
255
155
    // again whether we should promote since shouldPromoteLocalToGlobal needs
256
155
    // to locate the summary (based on GUID from name and linkage). Therefore,
257
155
    // use DoPromote result saved above.
258
155
    GV.setName(getName(&GV, DoPromote));
259
155
    GV.setLinkage(getLinkage(&GV, DoPromote));
260
155
    if (!GV.hasLocalLinkage())
261
155
      GV.setVisibility(GlobalValue::HiddenVisibility);
262
155
263
155
    // If we are renaming a COMDAT leader, ensure that we record the COMDAT
264
155
    // for later renaming as well. This is required for COFF.
265
155
    if (const auto *C = GV.getComdat())
266
5
      if (C->getName() == Name)
267
2
        RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
268
155
  } else
269
1.96k
    GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
270
2.12k
271
2.12k
  // Remove functions imported as available externally defs from comdats,
272
2.12k
  // as this is a declaration for the linker, and will be dropped eventually.
273
2.12k
  // It is illegal for comdats to contain declarations.
274
2.12k
  auto *GO = dyn_cast<GlobalObject>(&GV);
275
2.12k
  if (GO && 
GO->isDeclarationForLinker()1.90k
&&
GO->hasComdat()866
) {
276
4
    // The IRMover should not have placed any imported declarations in
277
4
    // a comdat, so the only declaration that should be in a comdat
278
4
    // at this point would be a definition imported as available_externally.
279
4
    assert(GO->hasAvailableExternallyLinkage() &&
280
4
           "Expected comdat on definition (possibly available external)");
281
4
    GO->setComdat(nullptr);
282
4
  }
283
2.12k
}
284
285
568
void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
286
568
  for (GlobalVariable &GV : M.globals())
287
328
    processGlobalForThinLTO(GV);
288
568
  for (Function &SF : M)
289
1.59k
    processGlobalForThinLTO(SF);
290
568
  for (GlobalAlias &GA : M.aliases())
291
207
    processGlobalForThinLTO(GA);
292
568
293
568
  // Replace any COMDATS that required renaming (because the COMDAT leader was
294
568
  // promoted and renamed).
295
568
  if (!RenamedComdats.empty())
296
2
    for (auto &GO : M.global_objects())
297
4
      if (auto *C = GO.getComdat()) {
298
2
        auto Replacement = RenamedComdats.find(C);
299
2
        if (Replacement != RenamedComdats.end())
300
2
          GO.setComdat(Replacement->second);
301
2
      }
302
568
}
303
304
569
bool FunctionImportGlobalProcessing::run() {
305
569
  processGlobalsForThinLTO();
306
569
  return false;
307
569
}
308
309
bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
310
566
                                  SetVector<GlobalValue *> *GlobalsToImport) {
311
566
  FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport);
312
566
  return ThinLTOProcessing.run();
313
566
}