llvm.org GIT mirror llvm / b195625
[ThinLTO] Revert r325320: Import global variables This caused some links to fail with ThinLTO due to missing symbols as well as causing some binaries to have failures at runtime. We're working with the author to get a test case, but want to get the tree green again. Further, it appears to introduce a data race. While the test usage of threads was disabled in r325361 & r325362, that isn't an acceptable fix. I've reverted both of these as well. This code needs to be thread safe. Test cases for this are already on the original commit thread. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@326638 91177308-0d34-0410-b5e6-96231b3b80d8 Chandler Carruth 1 year, 6 months ago
6 changed file(s) with 24 addition(s) and 128 deletion(s). Raw diff Collapse all Expand all
10501050 ValueMap.MD()[CU->getRawEnumTypes()].reset(nullptr);
10511051 ValueMap.MD()[CU->getRawMacros()].reset(nullptr);
10521052 ValueMap.MD()[CU->getRawRetainedTypes()].reset(nullptr);
1053 // We import global variables only temporarily in order for instcombine
1054 // and globalopt to perform constant folding and static constructor
1055 // evaluation. After that elim-avail-extern will covert imported globals
1056 // back to declarations, so we don't need debug info for them.
1053 // If we ever start importing global variable defs, we'll need to
1054 // add their DIGlobalVariable to the globals list on the imported
1055 // DICompileUnit. Confirm none are imported, and then we can
1056 // map the list of global variables to nullptr.
1057 assert(none_of(
1058 ValuesToLink,
1059 [](const GlobalValue *GV) { return isa(GV); }) &&
1060 "Unexpected importing of a GlobalVariable definition");
10571061 ValueMap.MD()[CU->getRawGlobalVariables()].reset(nullptr);
10581062
10591063 // Imported entities only need to be mapped in if they have local
6060 #define DEBUG_TYPE "function-import"
6161
6262 STATISTIC(NumImportedFunctions, "Number of functions imported");
63 STATISTIC(NumImportedGlobalVars, "Number of global variables imported");
6463 STATISTIC(NumImportedModules, "Number of modules imported from");
6564 STATISTIC(NumDeadSymbols, "Number of dead stripped symbols in index");
6665 STATISTIC(NumLiveSymbols, "Number of live symbols in index");
238237 return Index.getValueInfo(GUID);
239238 }
240239
241 static void computeImportForReferencedGlobals(
242 const FunctionSummary &Summary, const GVSummaryMapTy &DefinedGVSummaries,
243 FunctionImporter::ImportMapTy &ImportList,
244 StringMap *ExportLists) {
245 for (auto &VI : Summary.refs()) {
246 if (DefinedGVSummaries.count(VI.getGUID())) {
247 DEBUG(dbgs() << "Ref ignored! Target already in destination module.\n");
248 continue;
249 }
250
251 DEBUG(dbgs() << " ref -> " << VI.getGUID() << "\n");
252
253 for (auto &RefSummary : VI.getSummaryList())
254 if (RefSummary->getSummaryKind() == GlobalValueSummary::GlobalVarKind &&
255 // Don't try to import regular LTO summaries added to dummy module.
256 !RefSummary->modulePath().empty() &&
257 !GlobalValue::isInterposableLinkage(RefSummary->linkage()) &&
258 // For now we don't import global variables which have outgoing
259 // refs. Otherwise we have to promote referenced vars/functions.
260 RefSummary->refs().empty()) {
261 ImportList[RefSummary->modulePath()][VI.getGUID()] = 1;
262 if (ExportLists)
263 (*ExportLists)[RefSummary->modulePath()].insert(VI.getGUID());
264 }
265 }
266 }
267
268240 /// Compute the list of functions to import for a given caller. Mark these
269241 /// imported functions and the symbols they reference in their source module as
270242 /// exported from their source module.
274246 SmallVectorImpl &Worklist,
275247 FunctionImporter::ImportMapTy &ImportList,
276248 StringMap *ExportLists = nullptr) {
277 computeImportForReferencedGlobals(Summary, DefinedGVSummaries, ImportList,
278 ExportLists);
279249 for (auto &Edge : Summary.calls()) {
280250 ValueInfo VI = Edge.first;
281251 DEBUG(dbgs() << " edge -> " << VI.getGUID() << " Threshold:" << Threshold
418388 }
419389 }
420390
421 #ifndef NDEBUG
422 static bool isGlobalVarSummary(const ModuleSummaryIndex &Index,
423 GlobalValue::GUID G) {
424 if (const auto &VI = Index.getValueInfo(G)) {
425 auto SL = VI.getSummaryList();
426 if (!SL.empty())
427 return SL[0]->getSummaryKind() == GlobalValueSummary::GlobalVarKind;
428 }
429 return false;
430 }
431
432 static GlobalValue::GUID getGUID(GlobalValue::GUID G) { return G; }
433
434 static GlobalValue::GUID
435 getGUID(const std::pair &P) {
436 return P.first;
437 }
438
439 template
440 unsigned numGlobalVarSummaries(const ModuleSummaryIndex &Index, T &Cont) {
441 unsigned NumGVS = 0;
442 for (auto &V : Cont)
443 if (isGlobalVarSummary(Index, getGUID(V)))
444 ++NumGVS;
445 return NumGVS;
446 }
447 #endif
448
449391 /// Compute all the import and export for every module using the Index.
450392 void llvm::ComputeCrossModuleImport(
451393 const ModuleSummaryIndex &Index,
483425 for (auto &ModuleImports : ImportLists) {
484426 auto ModName = ModuleImports.first();
485427 auto &Exports = ExportLists[ModName];
486 unsigned NumGVS = numGlobalVarSummaries(Index, Exports);
487 DEBUG(dbgs() << "* Module " << ModName << " exports "
488 << Exports.size() - NumGVS << " functions and " << NumGVS
489 << " vars. Imports from "
490 << ModuleImports.second.size() << " modules.\n");
428 DEBUG(dbgs() << "* Module " << ModName << " exports " << Exports.size()
429 << " functions. Imports from " << ModuleImports.second.size()
430 << " modules.\n");
491431 for (auto &Src : ModuleImports.second) {
492432 auto SrcModName = Src.first();
493 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
494 DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
495 << " functions imported from " << SrcModName << "\n");
496 DEBUG(dbgs() << " - " << NumGVSPerMod << " global vars imported from "
433 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
497434 << SrcModName << "\n");
498435 }
499436 }
501438 }
502439
503440 #ifndef NDEBUG
504 static void dumpImportListForModule(const ModuleSummaryIndex &Index,
505 StringRef ModulePath,
441 static void dumpImportListForModule(StringRef ModulePath,
506442 FunctionImporter::ImportMapTy &ImportList) {
507443 DEBUG(dbgs() << "* Module " << ModulePath << " imports from "
508444 << ImportList.size() << " modules.\n");
509445 for (auto &Src : ImportList) {
510446 auto SrcModName = Src.first();
511 unsigned NumGVSPerMod = numGlobalVarSummaries(Index, Src.second);
512 DEBUG(dbgs() << " - " << Src.second.size() - NumGVSPerMod
513 << " functions imported from " << SrcModName << "\n");
514 DEBUG(dbgs() << " - " << NumGVSPerMod << " vars imported from "
447 DEBUG(dbgs() << " - " << Src.second.size() << " functions imported from "
515448 << SrcModName << "\n");
516449 }
517450 }
531464 ComputeImportForModule(FunctionSummaryMap, Index, ImportList);
532465
533466 #ifndef NDEBUG
534 dumpImportListForModule(Index, ModulePath, ImportList);
467 dumpImportListForModule(ModulePath, ImportList);
535468 #endif
536469 }
537470
558491 ImportList[Summary->modulePath()][GUID] = 1;
559492 }
560493 #ifndef NDEBUG
561 dumpImportListForModule(Index, ModulePath, ImportList);
494 dumpImportListForModule(ModulePath, ImportList);
562495 #endif
563496 }
564497
836769 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
837770 DEBUG(dbgs() << "Starting import for Module "
838771 << DestModule.getModuleIdentifier() << "\n");
839 unsigned ImportedCount = 0, ImportedGVCount = 0;
772 unsigned ImportedCount = 0;
840773
841774 IRMover Mover(DestModule);
842775 // Do the actual import of functions now, one Module at a time
896829 if (Import) {
897830 if (Error Err = GV.materialize())
898831 return std::move(Err);
899 ImportedGVCount += GlobalsToImport.insert(&GV);
832 GlobalsToImport.insert(&GV);
900833 }
901834 }
902835 for (GlobalAlias &GA : SrcModule->aliases()) {
953886 NumImportedModules++;
954887 }
955888
956 NumImportedFunctions += (ImportedCount - ImportedGVCount);
957 NumImportedGlobalVars += ImportedGVCount;
958
959 DEBUG(dbgs() << "Imported " << ImportedCount - ImportedGVCount
960 << " functions for Module " << DestModule.getModuleIdentifier()
961 << "\n");
962 DEBUG(dbgs() << "Imported " << ImportedGVCount
963 << " global variables for Module "
889 NumImportedFunctions += ImportedCount;
890
891 DEBUG(dbgs() << "Imported " << ImportedCount << " functions for Module "
964892 << DestModule.getModuleIdentifier() << "\n");
965893 return ImportedCount;
966894 }
+0
-4
test/ThinLTO/X86/Inputs/globals-import-cf-baz.ll less more
None target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1 target triple = "x86_64-pc-linux-gnu"
2
3 @baz = local_unnamed_addr constant i32 10, align 4
+0
-23
test/ThinLTO/X86/globals-import-const-fold.ll less more
None ; RUN: opt -module-summary %s -o %t1.bc
1 ; RUN: opt -module-summary %p/Inputs/globals-import-cf-baz.ll -o %t2.bc
2 ; RUN: llvm-lto -thinlto-action=thinlink %t1.bc %t2.bc -o %t3.index.bc
3
4 ; RUN: llvm-lto -thinlto-action=import %t1.bc %t2.bc -thinlto-index=%t3.index.bc
5 ; RUN: llvm-dis %t1.bc.thinlto.imported.bc -o - | FileCheck --check-prefix=IMPORT %s
6 ; RUN: llvm-lto -thinlto-action=optimize %t1.bc.thinlto.imported.bc -o %t1.bc.thinlto.opt.bc
7 ; RUN: llvm-dis %t1.bc.thinlto.opt.bc -o - | FileCheck --check-prefix=OPTIMIZE %s
8
9 ; IMPORT: @baz = available_externally local_unnamed_addr constant i32 10
10
11 ; OPTIMIZE: define i32 @main()
12 ; OPTIMIZE-NEXT: ret i32 10
13
14 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
15 target triple = "x86_64-pc-linux-gnu"
16
17 @baz = external local_unnamed_addr constant i32, align 4
18
19 define i32 @main() local_unnamed_addr {
20 %1 = load i32, i32* @baz, align 4
21 ret i32 %1
22 }
66 ; RUN: opt -function-import -stats -print-imports -enable-import-metadata -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
77 ; Try again with new pass manager
88 ; RUN: opt -passes='function-import' -stats -print-imports -enable-import-metadata -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
9 ; RUN: opt -passes='function-import' -debug-only=function-import -enable-import-metadata -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=DUMP
10 ; "-stats" and "-debug-only" require +Asserts.
9 ; "-stats" requires +Asserts.
1110 ; REQUIRES: asserts
1211
1312 ; Test import with smaller instruction limit
8079
8180 ; Ensure that all uses of local variable @P which has used in setfuncptr
8281 ; and callfuncptr are to the same promoted/renamed global.
83 ; CHECK-DAG: @P.llvm.{{.*}} = available_externally hidden global void ()* null
82 ; CHECK-DAG: @P.llvm.{{.*}} = external hidden global void ()*
8483 ; CHECK-DAG: %0 = load void ()*, void ()** @P.llvm.
8584 ; CHECK-DAG: store void ()* @staticfunc2.llvm.{{.*}}, void ()** @P.llvm.
8685
107106
108107 ; INSTLIMDEF-DAG: Import globalfunc2
109108 ; INSTLIMDEF-DAG: 13 function-import - Number of functions imported
110 ; INSTLIMDEF-DAG: 4 function-import - Number of global variables imported
111
112109 ; CHECK-DAG: !0 = !{!"{{.*}}/Inputs/funcimport.ll"}
113110
114111 ; The actual GUID values will depend on path to test.
144141 ; GUID-DAG: GUID {{.*}} is linkoncealias
145142 ; GUID-DAG: GUID {{.*}} is callfuncptr
146143 ; GUID-DAG: GUID {{.*}} is linkoncefunc
147
148 ; DUMP: Module [[M1:.*]] imports from 1 module
149 ; DUMP-NEXT: 13 functions imported from [[M2:.*]]
150 ; DUMP-NEXT: 4 vars imported from [[M2]]
151 ; DUMP: Imported 13 functions for Module [[M1]]
152 ; DUMP-NEXT: Imported 4 global variables for Module [[M1]]
55
66 ; Test to make sure importing and dead stripping works in the
77 ; case where the target is a local function that also indirectly calls itself.
8 ; RUN: llvm-lto2 run -thinlto-threads=1 -save-temps -o %t3 %t.bc %t2.bc -r %t.bc,fptr,plx -r %t.bc,main,plx -r %t2.bc,_Z6updatei,pl -r %t2.bc,fptr,l -print-imports 2>&1 | FileCheck %s --check-prefix=IMPORTS
8 ; RUN: llvm-lto2 run -save-temps -o %t3 %t.bc %t2.bc -r %t.bc,fptr,plx -r %t.bc,main,plx -r %t2.bc,_Z6updatei,pl -r %t2.bc,fptr,l -print-imports 2>&1 | FileCheck %s --check-prefix=IMPORTS
99 ; Make sure we import the promted indirectly called target
1010 ; IMPORTS: Import _ZL3foov.llvm.0
1111