llvm.org GIT mirror llvm / 3253066
[LTO] Drop non-prevailing definitions only if linkage is not local or appending Summary: This fixes PR 37422 In ELF, non-weak symbols can also be non-prevailing. In this particular PR, the __llvm_profile_* symbols are non-prevailing but weren't getting dropped - causing multiply-defined errors with lld. Also add a test, strong_non_prevailing.ll, to ensure that multiple copies of a strong symbol are dropped. To fix the test regressions exposed by this fix, - do not mark prevailing copies for symbols with 'appending' linkage. There's no one prevailing copy for such symbols. - fix the prevailing version in dead-strip-fulllto.ll - explicitly pass exported symbols to llvm-lto in fumcimport.ll and funcimport_var.ll Reviewers: tejohnson, pcc Subscribers: mehdi_amini, inglorion, eraman, steven_wu, dexonsmith, dang, srhines, llvm-commits Differential Revision: https://reviews.llvm.org/D54125 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@346436 91177308-0d34-0410-b5e6-96231b3b80d8 Pirama Arumuga Nainar 10 months ago
11 changed file(s) with 70 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
3939 class Target;
4040 class raw_pwrite_stream;
4141
42 /// Resolve Weak and LinkOnce values in the \p Index. Linkage changes recorded
43 /// in the index and the ThinLTO backends must apply the changes to the Module
44 /// via thinLTOResolveWeakForLinkerModule.
42 /// Resolve linkage for prevailing symbols in the \p Index. Linkage changes
43 /// recorded in the index and the ThinLTO backends must apply the changes to
44 /// the module via thinLTOResolvePrevailingInModule.
4545 ///
4646 /// This is done for correctness (if value exported, ensure we always
4747 /// emit a copy), and compile-time optimization (allow drop of duplicates).
48 void thinLTOResolveWeakForLinkerInIndex(
48 void thinLTOResolvePrevailingInIndex(
4949 ModuleSummaryIndex &Index,
5050 function_ref
5151 isPrevailing,
200200 StringRef ModulePath, StringRef OutputFilename,
201201 const std::map &ModuleToSummariesForIndex);
202202
203 /// Resolve WeakForLinker values in \p TheModule based on the information
203 /// Resolve prevailing symbol linkages in \p TheModule based on the information
204204 /// recorded in the summaries during global summary-based analysis.
205 void thinLTOResolveWeakForLinkerModule(Module &TheModule,
206 const GVSummaryMapTy &DefinedGlobals);
205 void thinLTOResolvePrevailingInModule(Module &TheModule,
206 const GVSummaryMapTy &DefinedGlobals);
207207
208208 /// Internalize \p TheModule based on the information recorded in the summaries
209209 /// during global summary-based analysis.
281281 Key = toHex(Hasher.result());
282282 }
283283
284 static void thinLTOResolveWeakForLinkerGUID(
284 static void thinLTOResolvePrevailingGUID(
285285 GlobalValueSummaryList &GVSummaryList, GlobalValue::GUID GUID,
286286 DenseSet &GlobalInvolvedWithAlias,
287287 function_ref
290290 recordNewLinkage) {
291291 for (auto &S : GVSummaryList) {
292292 GlobalValue::LinkageTypes OriginalLinkage = S->linkage();
293 if (!GlobalValue::isWeakForLinker(OriginalLinkage))
293 // Ignore local and appending linkage values since the linker
294 // doesn't resolve them.
295 if (GlobalValue::isLocalLinkage(OriginalLinkage) ||
296 GlobalValue::isAppendingLinkage(S->linkage()))
294297 continue;
295298 // We need to emit only one of these. The prevailing module will keep it,
296299 // but turned into a weak, while the others will drop it when possible.
314317 }
315318 }
316319
317 // Resolve Weak and LinkOnce values in the \p Index.
320 /// Resolve linkage for prevailing symbols in the \p Index.
318321 //
319322 // We'd like to drop these functions if they are no longer referenced in the
320323 // current module. However there is a chance that another module is still
321324 // referencing them because of the import. We make sure we always emit at least
322325 // one copy.
323 void llvm::thinLTOResolveWeakForLinkerInIndex(
326 void llvm::thinLTOResolvePrevailingInIndex(
324327 ModuleSummaryIndex &Index,
325328 function_ref
326329 isPrevailing,
336339 GlobalInvolvedWithAlias.insert(&AS->getAliasee());
337340
338341 for (auto &I : Index)
339 thinLTOResolveWeakForLinkerGUID(I.second.SummaryList, I.first,
340 GlobalInvolvedWithAlias, isPrevailing,
341 recordNewLinkage);
342 thinLTOResolvePrevailingGUID(I.second.SummaryList, I.first,
343 GlobalInvolvedWithAlias, isPrevailing,
344 recordNewLinkage);
342345 }
343346
344347 static void thinLTOInternalizeAndPromoteGUID(
349352 if (GlobalValue::isLocalLinkage(S->linkage()))
350353 S->setLinkage(GlobalValue::ExternalLinkage);
351354 } else if (EnableLTOInternalization &&
352 !GlobalValue::isLocalLinkage(S->linkage()))
355 // Ignore local and appending linkage values since the linker
356 // doesn't resolve them.
357 !GlobalValue::isLocalLinkage(S->linkage()) &&
358 !GlobalValue::isAppendingLinkage(S->linkage()))
353359 S->setLinkage(GlobalValue::InternalLinkage);
354360 }
355361 }
12041210 GlobalValue::LinkageTypes NewLinkage) {
12051211 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
12061212 };
1207 thinLTOResolveWeakForLinkerInIndex(ThinLTO.CombinedIndex, isPrevailing,
1208 recordNewLinkage);
1213 thinLTOResolvePrevailingInIndex(ThinLTO.CombinedIndex, isPrevailing,
1214 recordNewLinkage);
12091215
12101216 std::unique_ptr BackendProc =
12111217 ThinLTO.Backend(Conf, ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
489489
490490 dropDeadSymbols(Mod, DefinedGlobals, CombinedIndex);
491491
492 thinLTOResolveWeakForLinkerModule(Mod, DefinedGlobals);
492 thinLTOResolvePrevailingInModule(Mod, DefinedGlobals);
493493
494494 if (Conf.PostPromoteModuleHook && !Conf.PostPromoteModuleHook(Task, Mod))
495495 return finalizeOptimizationRemarks(std::move(DiagnosticOutputFile));
456456 if (!SingleModule) {
457457 promoteModule(TheModule, Index);
458458
459 // Apply summary-based LinkOnce/Weak resolution decisions.
460 thinLTOResolveWeakForLinkerModule(TheModule, DefinedGlobals);
459 // Apply summary-based prevailing-symbol resolution decisions.
460 thinLTOResolvePrevailingInModule(TheModule, DefinedGlobals);
461461
462462 // Save temps: after promotion.
463463 saveTempBitcode(TheModule, SaveTempsDir, count, ".1.promoted.bc");
499499 return codegenModule(TheModule, TM);
500500 }
501501
502 /// Resolve LinkOnce/Weak symbols. Record resolutions in the \p ResolvedODR map
502 /// Resolve prevailing symbols. Record resolutions in the \p ResolvedODR map
503503 /// for caching, and in the \p Index for application during the ThinLTO
504504 /// backends. This is needed for correctness for exported symbols (ensure
505505 /// at least one copy kept) and a compile-time optimization (to drop duplicate
506506 /// copies when possible).
507 static void resolveWeakForLinkerInIndex(
507 static void resolvePrevailingInIndex(
508508 ModuleSummaryIndex &Index,
509509 StringMap>
510510 &ResolvedODR) {
526526 ResolvedODR[ModuleIdentifier][GUID] = NewLinkage;
527527 };
528528
529 thinLTOResolveWeakForLinkerInIndex(Index, isPrevailing, recordNewLinkage);
529 thinLTOResolvePrevailingInIndex(Index, isPrevailing, recordNewLinkage);
530530 }
531531
532532 // Initialize the TargetMachine builder for a given Triple
674674 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
675675 ExportLists);
676676
677 // Resolve LinkOnce/Weak symbols.
677 // Resolve prevailing symbols
678678 StringMap> ResolvedODR;
679 resolveWeakForLinkerInIndex(Index, ResolvedODR);
680
681 thinLTOResolveWeakForLinkerModule(
679 resolvePrevailingInIndex(Index, ResolvedODR);
680
681 thinLTOResolvePrevailingInModule(
682682 TheModule, ModuleToDefinedGVSummaries[ModuleIdentifier]);
683683
684684 // Promote the exported values in the index, so that they are promoted
941941 // on the index, and nuke this map.
942942 StringMap> ResolvedODR;
943943
944 // Resolve LinkOnce/Weak symbols, this has to be computed early because it
944 // Resolve prevailing symbols, this has to be computed early because it
945945 // impacts the caching.
946 resolveWeakForLinkerInIndex(*Index, ResolvedODR);
946 resolvePrevailingInIndex(*Index, ResolvedODR);
947947
948948 // Use global summary-based analysis to identify symbols that can be
949949 // internalized (because they aren't exported or preserved as per callback).
896896 return true;
897897 }
898898
899 /// Fixup WeakForLinker linkages in \p TheModule based on summary analysis.
900 void llvm::thinLTOResolveWeakForLinkerModule(
899 /// Fixup prevailing symbol linkages in \p TheModule based on summary analysis.
900 void llvm::thinLTOResolvePrevailingInModule(
901901 Module &TheModule, const GVSummaryMapTy &DefinedGlobals) {
902902 auto updateLinkage = [&](GlobalValue &GV) {
903903 // See if the global summary analysis computed a new resolved linkage.
914914 // as we need access to the resolution vectors for each input file in
915915 // order to find which symbols have been redefined.
916916 // We may consider reorganizing this code and moving the linkage recording
917 // somewhere else, e.g. in thinLTOResolveWeakForLinkerInIndex.
917 // somewhere else, e.g. in thinLTOResolvePrevailingInIndex.
918918 if (NewLinkage == GlobalValue::WeakAnyLinkage) {
919919 GV.setLinkage(NewLinkage);
920920 return;
921921 }
922922
923 if (!GlobalValue::isWeakForLinker(GV.getLinkage()))
923 if (GlobalValue::isLocalLinkage(GV.getLinkage()) ||
924 // In case it was dead and already converted to declaration.
925 GV.isDeclaration())
924926 return;
925927 // Check for a non-prevailing def that has interposable linkage
926928 // (e.g. non-odr weak or linkonce). In that case we can't simply
931933 GlobalValue::isInterposableLinkage(GV.getLinkage())) {
932934 if (!convertToDeclaration(GV))
933935 // FIXME: Change this to collect replaced GVs and later erase
934 // them from the parent module once thinLTOResolveWeakForLinkerGUID is
936 // them from the parent module once thinLTOResolvePrevailingGUID is
935937 // changed to enable this for aliases.
936938 llvm_unreachable("Expected GV to be converted");
937939 } else {
0 ; RUN: opt -module-summary -o %t %s
11 ; RUN: opt -module-summary -o %t2 %S/Inputs/dead-strip-fulllto.ll
22
3 ; RUN: llvm-lto2 run %t -r %t,main,px -r %t,live1,p -r %t,live2,p -r %t,dead2,p \
4 ; RUN: %t2 -r %t2,live1, -r %t2,live2, -r %t2,dead1,p -r %t2,dead2, -r %t2,odr, \
3 ; RUN: llvm-lto2 run %t -r %t,main,px -r %t,live1, -r %t,live2,p -r %t,dead2,p \
4 ; RUN: %t2 -r %t2,live1,p -r %t2,live2, -r %t2,dead1,p -r %t2,dead2, -r %t2,odr, \
55 ; RUN: -save-temps -o %t3
66 ; RUN: llvm-nm %t3.0 | FileCheck --check-prefix=FULL %s
77 ; RUN: llvm-nm %t3.1 | FileCheck --check-prefix=THIN %s
88
9 ; RUN: llvm-lto2 run %t -r %t,main,px -r %t,live1,p -r %t,live2,p -r %t,dead2,p \
10 ; RUN: %t2 -r %t2,live1, -r %t2,live2, -r %t2,dead1,p -r %t2,dead2, -r %t2,odr, \
9 ; RUN: llvm-lto2 run %t -r %t,main,px -r %t,live1, -r %t,live2,p -r %t,dead2,p \
10 ; RUN: %t2 -r %t2,live1,p -r %t2,live2, -r %t2,dead1,p -r %t2,dead2, -r %t2,odr, \
1111 ; RUN: -save-temps -o %t3 -O0
1212 ; RUN: llvm-nm %t3.0 | FileCheck --check-prefix=FULL %s
1313 ; RUN: llvm-nm %t3.1 | FileCheck --check-prefix=THIN %s
0 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1 target triple = "x86_64-unknown-linux-gnu"
2
3 $__llvm_profile_filename = comdat any
4
5 @__llvm_profile_filename = constant [19 x i8] c"default_%m.profraw\00", comdat
3939 ; CODEGEN: T _main
4040
4141 ; Verify that all run together
42 ; RUN: llvm-lto -thinlto-action=run %t2.bc %t.bc
42 ; RUN: llvm-lto -thinlto-action=run %t2.bc %t.bc -exported-symbol=_main
4343 ; RUN: llvm-nm -o - < %t.bc.thinlto.o | FileCheck %s --check-prefix=ALL
4444 ; RUN: llvm-nm -o - < %t2.bc.thinlto.o | FileCheck %s --check-prefix=ALL2
4545 ; ALL: T _callfuncptr
0 ; RUN: opt -module-summary %s -o %t.bc
1 ; RUN: opt -module-summary %p/Inputs/strong_non_prevailing.ll -o %t2.bc
2
3 ; RUN: llvm-lto -thinlto-action=run %t.bc %t2.bc -exported-symbol=__llvm_profile_filename
4 ; RUN: llvm-nm -o - < %t.bc.thinlto.o | FileCheck %s --check-prefix=EXPORTED
5 ; RUN: llvm-nm -o - < %t2.bc.thinlto.o 2>&1 | FileCheck %s --check-prefix=NOT_EXPORTED
6
7 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
8 target triple = "x86_64-unknown-linux-gnu"
9
10 $__llvm_profile_filename = comdat any
11
12 @__llvm_profile_filename = constant [19 x i8] c"default_%m.profraw\00", comdat
13
14 ; EXPORTED: N __llvm_profile_filename
15 ; NOT_EXPORTED-NOT: N __llvm_profile_filename
33 ; RUN: opt -module-summary %p/Inputs/funcimport_var2.ll -o %t2.bc
44 ; RUN: llvm-lto -thinlto -thinlto-action=thinlink -o %t3 %t.bc %t2.bc
55 ; RUN: llvm-lto -thinlto -thinlto-action=import -thinlto-index=%t3 %t.bc %t2.bc
6 ; RUN: llvm-lto -thinlto -thinlto-action=run %t.bc %t2.bc
6 ; RUN: llvm-lto -thinlto -thinlto-action=run %t.bc %t2.bc -exported-symbol=_Z4LinkPKcS0_
77 ; RUN: llvm-nm %t.bc.thinlto.o | FileCheck %s
88 ; RUN: llvm-lto2 run %t.bc %t2.bc -o %t.out \
99 ; RUN: -r %t.bc,_Z4LinkPKcS0_,plx \