llvm.org GIT mirror llvm / a586fd2
Start deciding earlier what to link. A traditional linker is roughly split in symbol resolution and "copying stuff". The two tasks are badly mixed in lib/Linker. This starts splitting them apart. With this patch there are no direct call to linkGlobalValueBody or linkGlobalValueProto. Everything is linked via WapValue. This also includes a few fixes: * A GV goes undefined if the comdat is dropped (comdat11.ll). * We error if an internal GV goes undefined (comdat13.ll). * We don't link an unused comdat. The first two match the behavior of an ELF linker. The second one is equivalent to running globaldce on the input. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254336 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
8 changed file(s) with 139 addition(s) and 81 deletion(s). Raw diff Collapse all Expand all
435435 /// references.
436436 bool DoneLinkingBodies;
437437
438 bool HasError = false;
439
438440 public:
439441 ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
440442 DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
482484 /// Helper method for setting a message and returning an error code.
483485 bool emitError(const Twine &Message) {
484486 DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
487 HasError = true;
485488 return true;
486489 }
487490
530533 void upgradeMismatchedGlobalArray(StringRef Name);
531534 void upgradeMismatchedGlobals();
532535
536 bool linkIfNeeded(GlobalValue &GV);
533537 bool linkAppendingVarProto(GlobalVariable *DstGV,
534538 const GlobalVariable *SrcGV);
535539
903907 if (doneLinkingBodies())
904908 return nullptr;
905909
906 GlobalValue *DGV = copyGlobalValueProto(TypeMap, SGV);
907
908 if (Comdat *SC = SGV->getComdat()) {
909 if (auto *DGO = dyn_cast(DGV)) {
910 Comdat *DC = DstM->getOrInsertComdat(SC->getName());
911 DGO->setComdat(DC);
912 }
913 }
914
915 return DGV;
910 linkGlobalValueProto(SGV);
911 if (HasError)
912 return nullptr;
913 Value *Ret = ValueMap[SGV];
914 assert(Ret);
915 return Ret;
916916 }
917917
918918 void ValueMaterializerTy::materializeInitFor(GlobalValue *New,
921921 }
922922
923923 void ModuleLinker::materializeInitFor(GlobalValue *New, GlobalValue *Old) {
924 if (auto *F = dyn_cast(New)) {
925 if (!F->isDeclaration())
926 return;
927 } else if (auto *V = dyn_cast(New)) {
928 if (V->hasInitializer())
929 return;
930 } else {
931 auto *A = cast(New);
932 if (A->getAliasee())
933 return;
934 }
935
936 if (Old->isDeclaration())
937 return;
938
924939 if (isPerformingImport() && !doImportAsDefinition(Old))
925940 return;
926941
927 // Skip declarations that ValueMaterializer may have created in
928 // case we link in only some of SrcM.
929 if (shouldLinkOnlyNeeded() && Old->isDeclaration())
942 if (DoNotLinkFromSource.count(Old)) {
943 if (!New->hasExternalLinkage() && !New->hasExternalWeakLinkage() &&
944 !New->hasAppendingLinkage())
945 emitError("Declaration points to discarded value");
930946 return;
931
932 assert(!Old->isDeclaration() && "users should not pass down decls");
947 }
948
933949 linkGlobalValueBody(*Old);
934950 }
935951
14041420 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
14051421 C = DstM->getOrInsertComdat(SC->getName());
14061422 C->setSelectionKind(SK);
1407 ComdatMembers[SC].push_back(SGV);
14081423 } else if (DGV) {
14091424 if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
14101425 return true;
14241439 if (DGV)
14251440 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
14261441
1427 if (!LinkFromSrc && !DGV)
1428 return false;
1429
14301442 GlobalValue *NewGV;
1431 if (!LinkFromSrc) {
1443 if (!LinkFromSrc && DGV) {
14321444 NewGV = DGV;
14331445 // When linking from source we setVisibility from copyGlobalValueProto.
14341446 setVisibility(NewGV, SGV, DGV);
14351447 } else {
1436 // If the GV is to be lazily linked, don't create it just yet.
1437 // The ValueMaterializerTy will deal with creating it if it's used.
1438 if (!DGV && !shouldOverrideFromSrc() && SGV != ImportFunction &&
1439 (SGV->hasLocalLinkage() || SGV->hasLinkOnceLinkage() ||
1440 SGV->hasAvailableExternallyLinkage())) {
1441 DoNotLinkFromSource.insert(SGV);
1442 return false;
1443 }
1444
1445 // When we only want to link in unresolved dependencies, blacklist
1446 // the symbol unless unless DestM has a matching declaration (DGV).
1447 if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration())) {
1448 DoNotLinkFromSource.insert(SGV);
1449 return false;
1450 }
1451
14521448 NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);
14531449
14541450 if (isPerformingImport() && !doImportAsDefinition(SGV))
14581454 NewGV->setUnnamedAddr(HasUnnamedAddr);
14591455
14601456 if (auto *NewGO = dyn_cast(NewGV)) {
1461 if (C)
1457 if (C && LinkFromSrc)
14621458 NewGO->setComdat(C);
14631459
14641460 if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
18411837 return DstTriple.str();
18421838 }
18431839
1840 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
1841 GlobalValue *DGV = getLinkedToGlobal(&GV);
1842
1843 if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
1844 return false;
1845
1846 if (DGV && !GV.hasLocalLinkage()) {
1847 GlobalValue::VisibilityTypes Visibility =
1848 getMinVisibility(DGV->getVisibility(), GV.getVisibility());
1849 DGV->setVisibility(Visibility);
1850 GV.setVisibility(Visibility);
1851 }
1852
1853 if (const Comdat *SC = GV.getComdat()) {
1854 bool LinkFromSrc;
1855 Comdat::SelectionKind SK;
1856 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1857 if (!LinkFromSrc) {
1858 DoNotLinkFromSource.insert(&GV);
1859 return false;
1860 }
1861 }
1862
1863 if (!DGV && !shouldOverrideFromSrc() &&
1864 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
1865 GV.hasAvailableExternallyLinkage())) {
1866 return false;
1867 }
1868 MapValue(&GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1869 return HasError;
1870 }
1871
18441872 bool ModuleLinker::run() {
18451873 assert(DstM && "Null destination module");
18461874 assert(SrcM && "Null source module");
19001928 // Upgrade mismatched global arrays.
19011929 upgradeMismatchedGlobals();
19021930
1931 for (GlobalVariable &GV : SrcM->globals())
1932 if (const Comdat *SC = GV.getComdat())
1933 ComdatMembers[SC].push_back(&GV);
1934
1935 for (Function &SF : *SrcM)
1936 if (const Comdat *SC = SF.getComdat())
1937 ComdatMembers[SC].push_back(&SF);
1938
1939 for (GlobalAlias &GA : SrcM->aliases())
1940 if (const Comdat *SC = GA.getComdat())
1941 ComdatMembers[SC].push_back(&GA);
1942
19031943 // Insert all of the globals in src into the DstM module... without linking
19041944 // initializers (which could refer to functions not yet mapped over).
19051945 for (GlobalVariable &GV : SrcM->globals())
1906 if (linkGlobalValueProto(&GV))
1946 if (linkIfNeeded(GV))
19071947 return true;
19081948
1909 // Link the functions together between the two modules, without doing function
1910 // bodies... this just adds external function prototypes to the DstM
1911 // function... We do this so that when we begin processing function bodies,
1912 // all of the global values that may be referenced are available in our
1913 // ValueMap.
1914 for (Function &F :*SrcM)
1915 if (linkGlobalValueProto(&F))
1949 for (Function &SF : *SrcM)
1950 if (linkIfNeeded(SF))
19161951 return true;
19171952
1918 // If there were any aliases, link them now.
19191953 for (GlobalAlias &GA : SrcM->aliases())
1920 if (linkGlobalValueProto(&GA))
1954 if (linkIfNeeded(GA))
19211955 return true;
19221956
19231957 for (AppendingVarInfo &AppendingVar : AppendingVars)
19301964 const GlobalValue *GV = SrcM->getNamedValue(C.getName());
19311965 if (GV)
19321966 MapValue(GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1933 }
1934
1935 // Link in the function bodies that are defined in the source module into
1936 // DstM.
1937 for (Function &SF : *SrcM) {
1938 // Skip if no body (function is external).
1939 if (SF.isDeclaration())
1940 continue;
1941
1942 // Skip if not linking from source.
1943 if (DoNotLinkFromSource.count(&SF))
1944 continue;
1945
1946 if (linkGlobalValueBody(SF))
1947 return true;
1948 }
1949
1950 // Resolve all uses of aliases with aliasees.
1951 for (GlobalAlias &Src : SrcM->aliases()) {
1952 if (DoNotLinkFromSource.count(&Src))
1953 continue;
1954 linkGlobalValueBody(Src);
1955 }
1956
1957 // Update the initializers in the DstM module now that all globals that may
1958 // be referenced are in DstM.
1959 for (GlobalVariable &Src : SrcM->globals()) {
1960 // Only process initialized GV's or ones not already in dest.
1961 if (!Src.hasInitializer() || DoNotLinkFromSource.count(&Src))
1962 continue;
1963 linkGlobalValueBody(Src);
19641967 }
19651968
19661969 // Note that we are done linking global value bodies. This prevents
4040 if (Value *NewV =
4141 Materializer->materializeDeclFor(const_cast(V))) {
4242 VM[V] = NewV;
43 if (auto *GV = dyn_cast(V))
44 Materializer->materializeInitFor(cast(NewV),
45 const_cast(GV));
43 if (auto *NewGV = dyn_cast(NewV))
44 Materializer->materializeInitFor(
45 NewGV, const_cast(cast(V)));
4646 return NewV;
4747 }
4848 }
0 $foo = comdat any
1 @foo = global i8 1, comdat
2 define void @zed() {
3 call void @bar()
4 ret void
5 }
6 define void @bar() comdat($foo) {
7 ret void
8 }
0 $foo = comdat any
1 @foo = global i8 1, comdat
2 define void @zed() {
3 call void @bar()
4 ret void
5 }
6 define internal void @bar() comdat($foo) {
7 ret void
8 }
0 ; RUN: llvm-link -S %s %p/Inputs/comdat11.ll -o - | FileCheck %s
1
2 $foo = comdat any
3 @foo = global i8 0, comdat
4
5 ; CHECK: @foo = global i8 0, comdat
6
7 ; CHECK: define void @zed() {
8 ; CHECK: call void @bar()
9 ; CHECK: ret void
10 ; CHECK: }
11
12 ; CHECK: declare void @bar()
0 ; RUN: llvm-link %s -S -o - | FileCheck %s
1
2 $foo = comdat largest
3 define internal void @foo() comdat($foo) {
4 ret void
5 }
6
7 ; CHECK-NOT: foo
0 ; RUN: not llvm-link -S %s %p/Inputs/comdat13.ll -o %t.ll 2>&1 | FileCheck %s
1
2 ; In Inputs/comdat13.ll a function not in the $foo comdat (zed) references an
3 ; internal function in the comdat $foo.
4 ; We might want to have the verifier reject that, but for now we at least check
5 ; that the linker produces an error.
6 ; This is the IR equivalent of the "relocation refers to discarded section" in
7 ; an ELF linker.
8
9 ; CHECK: Declaration points to discarded value
10
11 $foo = comdat any
12 @foo = global i8 0, comdat
1313 define internal void @f2() comdat($f2) {
1414 ret void
1515 }
16 define void @f3() comdat($f2) {
17 ret void
18 }
1619
1720 ; CHECK-DAG: $f2 = comdat largest
1821 ; CHECK-DAG: define internal void @f2() comdat {