llvm.org GIT mirror llvm / 0e60374
ThinLTO/ModuleLinker: add a flag to not always pull-in linkonce when performing importing Summary: The function importer already decided what symbols need to be pulled in. Also these magically added ones will not be in the export list for the source module, which can confuse the internalizer for instance. Reviewers: tejohnson, rafael Subscribers: joker.eph, llvm-commits Differential Revision: http://reviews.llvm.org/D19096 From: Mehdi Amini <mehdi.amini@apple.com> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266948 91177308-0d34-0410-b5e6-96231b3b80d8 Mehdi Amini 3 years ago
8 changed file(s) with 96 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
2828 None = 0,
2929 OverrideFromSrc = (1 << 0),
3030 LinkOnlyNeeded = (1 << 1),
31 InternalizeLinkedSymbols = (1 << 2)
31 InternalizeLinkedSymbols = (1 << 2),
32 /// Don't force link referenced linkonce definitions, import declaration.
33 DontForceLinkLinkonceODR = (1 << 3)
34
3235 };
3336
3437 Linker(Module &M);
4747 : Index(Index), ModuleLoader(ModuleLoader) {}
4848
4949 /// Import functions in Module \p M based on the supplied import list.
50 bool importFunctions(Module &M, const ImportMapTy &ImportList);
50 /// \p ForceImportReferencedDiscardableSymbols will set the ModuleLinker in
51 /// a mode where referenced discarable symbols in the source modules will be
52 /// imported as well even if they are not present in the ImportList.
53 bool importFunctions(Module &M, const ImportMapTy &ImportList,
54 bool ForceImportReferencedDiscardableSymbols = false);
5155
5256 private:
5357 /// The summaries index used to trigger importing.
4444 /// to Add.
4545 void addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add);
4646
47 bool shouldLinkReferencedLinkOnce() {
48 return !(Flags & Linker::DontForceLinkLinkonceODR);
49 }
4750 bool shouldOverrideFromSrc() { return Flags & Linker::OverrideFromSrc; }
4851 bool shouldLinkOnlyNeeded() { return Flags & Linker::LinkOnlyNeeded; }
4952 bool shouldInternalizeLinkedSymbols() {
412415 }
413416
414417 void ModuleLinker::addLazyFor(GlobalValue &GV, IRMover::ValueAdder Add) {
418 if (!shouldLinkReferencedLinkOnce())
419 // For ThinLTO we don't import more than what was required.
420 // The client has to guarantee that the linkonce will be availabe at link
421 // time (by promoting it to weak for instance).
422 return;
423
415424 // Add these to the internalize list
416425 if (!GV.hasLinkOnceLinkage())
417426 return;
4747
4848 static cl::opt PrintImports("print-imports", cl::init(false), cl::Hidden,
4949 cl::desc("Print imported functions"));
50
51 // Temporary allows the function import pass to disable always linking
52 // referenced discardable symbols.
53 static cl::opt
54 DontForceImportReferencedDiscardableSymbols("disable-force-link-odr",
55 cl::init(false), cl::Hidden);
5056
5157 // Load lazily a module from \p FileName in \p Context.
5258 static std::unique_ptr loadFile(const std::string &FileName,
326332 // index.
327333 //
328334 bool FunctionImporter::importFunctions(
329 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList) {
335 Module &DestModule, const FunctionImporter::ImportMapTy &ImportList,
336 bool ForceImportReferencedDiscardableSymbols) {
330337 DEBUG(dbgs() << "Starting import for Module "
331338 << DestModule.getModuleIdentifier() << "\n");
332339 unsigned ImportedCount = 0;
419426 << " from " << SrcModule->getSourceFileName() << "\n";
420427 }
421428
422 if (TheLinker.linkInModule(std::move(SrcModule), Linker::Flags::None,
423 &GlobalsToImport))
429 // Instruct the linker that the client will take care of linkonce resolution
430 unsigned Flags = Linker::Flags::None;
431 if (!ForceImportReferencedDiscardableSymbols)
432 Flags |= Linker::Flags::DontForceLinkLinkonceODR;
433
434 if (TheLinker.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
424435 report_fatal_error("Function Import: link error");
425436
426437 ImportedCount += GlobalsToImport.size();
522533 return loadFile(Identifier, M.getContext());
523534 };
524535 FunctionImporter Importer(*Index, ModuleLoader);
525 return Importer.importFunctions(M, ImportList);
536 return Importer.importFunctions(
537 M, ImportList, !DontForceImportReferencedDiscardableSymbols);
526538 }
527539 };
528540 } // anonymous namespace
22 ; RUN: llvm-lto -thinlto -o %t3 %t1.bc %t2.bc
33 ; RUN: llvm-link -import=bar:%t2.bc %t1.bc -summary-index=%t3.thinlto.bc -S | FileCheck %s
44
5 ; CHECK: define linkonce_odr hidden void @foo() {
5 ; CHECK: define available_externally hidden void @foo() {
66 define available_externally hidden void @foo() {
77 ret void
88 }
9898 ret void
9999 }
100100
101 define void @referencelargelinkonce() #0 {
102 entry:
103 call void @linkonceodr()
104 ret void
105 }
101106
107 ; A large enough linkonce_odr function that should never be imported
108 define linkonce_odr void @linkonceodr() #0 {
109 entry:
110 call void @globalfunc2()
111 call void @globalfunc2()
112 call void @globalfunc2()
113 call void @globalfunc2()
114 call void @globalfunc2()
115 call void @globalfunc2()
116 call void @globalfunc2()
117 call void @globalfunc2()
118 call void @globalfunc2()
119 call void @globalfunc2()
120 call void @globalfunc2()
121 call void @globalfunc2()
122 call void @globalfunc2()
123 call void @globalfunc2()
124 call void @globalfunc2()
125 call void @globalfunc2()
126 call void @globalfunc2()
127 call void @globalfunc2()
128 call void @globalfunc2()
129 call void @globalfunc2()
130 call void @globalfunc2()
131 call void @globalfunc2()
132 call void @globalfunc2()
133 call void @globalfunc2()
134 call void @globalfunc2()
135 call void @globalfunc2()
136 call void @globalfunc2()
137 call void @globalfunc2()
138 call void @globalfunc2()
139 call void @globalfunc2()
140 ret void
141 }
142
143
33 ; RUN: llvm-lto -thinlto -print-summary-global-ids -o %t3 %t.bc %t2.bc 2>&1 | FileCheck %s --check-prefix=GUID
44
55 ; Do the import now
6 ; RUN: opt -function-import -stats -print-imports -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
6 ; RUN: opt -disable-force-link-odr -function-import -stats -print-imports -summary-file %t3.thinlto.bc %t.bc -S 2>&1 | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIMDEF
77 ; "-stats" requires +Asserts.
88 ; REQUIRES: asserts
99
1010 ; Test import with smaller instruction limit
11 ; RUN: opt -function-import -summary-file %t3.thinlto.bc %t.bc -import-instr-limit=5 -S | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIM5
11 ; RUN: opt -disable-force-link-odr -function-import -summary-file %t3.thinlto.bc %t.bc -import-instr-limit=5 -S | FileCheck %s --check-prefix=CHECK --check-prefix=INSTLIM5
1212 ; INSTLIM5-NOT: @staticfunc.llvm.
13
14 ; Test import with smaller instruction limit and without the -disable-force-link-odr
15 ; RUN: opt -function-import -summary-file %t3.thinlto.bc %t.bc -import-instr-limit=5 -S | FileCheck %s --check-prefix=INSTLIM5ODR
16 ; INSTLIM5ODR: define linkonce_odr void @linkonceodr()
17
1318
1419 define i32 @main() #0 {
1520 entry:
2227 call void (...) @setfuncptr()
2328 call void (...) @callfuncptr()
2429 call void (...) @weakfunc()
30 call void (...) @referencelargelinkonce()
2531 ret i32 0
2632 }
2733
7783 ; CHECK-DAG: %0 = load void ()*, void ()** @P.llvm.
7884 ; CHECK-DAG: store void ()* @staticfunc2.llvm.{{.*}}, void ()** @P.llvm.
7985
86 ; Ensure that @referencelargelinkonce definition is pulled in, but later we
87 ; also check that the linkonceodr function is not.
88 ; CHECK-DAG: define available_externally void @referencelargelinkonce()
89 ; INSTLIM5-DAG: declare void @linkonceodr()
90 declare void @referencelargelinkonce(...)
91
8092 ; Won't import weak func
8193 ; CHECK-DAG: declare void @weakfunc(...)
8294 declare void @weakfunc(...) #1
8698 ; INSTLIM5-DAG: declare hidden void @funcwithpersonality.llvm.{{.*}}()
8799
88100 ; INSTLIMDEF-DAG: Import globalfunc2
89 ; INSTLIMDEF-DAG: 11 function-import - Number of functions imported
101 ; INSTLIMDEF-DAG: 13 function-import - Number of functions imported
90102
91103 ; The actual GUID values will depend on path to test.
92104 ; GUID-DAG: GUID {{.*}} is weakalias
275275 if (renameModuleForThinLTO(*SrcModule, *Index, &GlobalsToImport))
276276 return true;
277277
278 if (L.linkInModule(std::move(SrcModule), Linker::Flags::None,
279 &GlobalsToImport))
278 // Instruct the linker to not automatically import linkonce defintion.
279 unsigned Flags = Linker::Flags::DontForceLinkLinkonceODR;
280
281 if (L.linkInModule(std::move(SrcModule), Flags, &GlobalsToImport))
280282 return false;
281283 }
282284