llvm.org GIT mirror llvm / 39e89e8
Bring r254336 back: The difference is that now we don't error on out-of-comdat access to internal global values. We copy them instead. This seems to match the expectation of COFF linkers (see pr25686). Original message: 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@254418 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
8 changed file(s) with 153 addition(s) and 80 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 (!New->hasLocalLinkage() && DoNotLinkFromSource.count(Old))
930943 return;
931944
932 assert(!Old->isDeclaration() && "users should not pass down decls");
933945 linkGlobalValueBody(*Old);
934946 }
935947
14041416 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
14051417 C = DstM->getOrInsertComdat(SC->getName());
14061418 C->setSelectionKind(SK);
1407 ComdatMembers[SC].push_back(SGV);
1419 if (SGV->hasInternalLinkage())
1420 LinkFromSrc = true;
14081421 } else if (DGV) {
14091422 if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
14101423 return true;
14241437 if (DGV)
14251438 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
14261439
1427 if (!LinkFromSrc && !DGV)
1428 return false;
1429
14301440 GlobalValue *NewGV;
1431 if (!LinkFromSrc) {
1441 if (!LinkFromSrc && DGV) {
14321442 NewGV = DGV;
14331443 // When linking from source we setVisibility from copyGlobalValueProto.
14341444 setVisibility(NewGV, SGV, DGV);
14351445 } 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
14521446 NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);
14531447
14541448 if (isPerformingImport() && !doImportAsDefinition(SGV))
14581452 NewGV->setUnnamedAddr(HasUnnamedAddr);
14591453
14601454 if (auto *NewGO = dyn_cast(NewGV)) {
1461 if (C)
1455 if (C && LinkFromSrc)
14621456 NewGO->setComdat(C);
14631457
14641458 if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
18411835 return DstTriple.str();
18421836 }
18431837
1838 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
1839 GlobalValue *DGV = getLinkedToGlobal(&GV);
1840
1841 if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
1842 return false;
1843
1844 if (DGV && !GV.hasLocalLinkage()) {
1845 GlobalValue::VisibilityTypes Visibility =
1846 getMinVisibility(DGV->getVisibility(), GV.getVisibility());
1847 DGV->setVisibility(Visibility);
1848 GV.setVisibility(Visibility);
1849 }
1850
1851 if (const Comdat *SC = GV.getComdat()) {
1852 bool LinkFromSrc;
1853 Comdat::SelectionKind SK;
1854 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1855 if (!LinkFromSrc) {
1856 DoNotLinkFromSource.insert(&GV);
1857 return false;
1858 }
1859 }
1860
1861 if (!DGV && !shouldOverrideFromSrc() &&
1862 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
1863 GV.hasAvailableExternallyLinkage())) {
1864 return false;
1865 }
1866 MapValue(&GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1867 return HasError;
1868 }
1869
18441870 bool ModuleLinker::run() {
18451871 assert(DstM && "Null destination module");
18461872 assert(SrcM && "Null source module");
19001926 // Upgrade mismatched global arrays.
19011927 upgradeMismatchedGlobals();
19021928
1929 for (GlobalVariable &GV : SrcM->globals())
1930 if (const Comdat *SC = GV.getComdat())
1931 ComdatMembers[SC].push_back(&GV);
1932
1933 for (Function &SF : *SrcM)
1934 if (const Comdat *SC = SF.getComdat())
1935 ComdatMembers[SC].push_back(&SF);
1936
1937 for (GlobalAlias &GA : SrcM->aliases())
1938 if (const Comdat *SC = GA.getComdat())
1939 ComdatMembers[SC].push_back(&GA);
1940
19031941 // Insert all of the globals in src into the DstM module... without linking
19041942 // initializers (which could refer to functions not yet mapped over).
19051943 for (GlobalVariable &GV : SrcM->globals())
1906 if (linkGlobalValueProto(&GV))
1944 if (linkIfNeeded(GV))
19071945 return true;
19081946
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))
1947 for (Function &SF : *SrcM)
1948 if (linkIfNeeded(SF))
19161949 return true;
19171950
1918 // If there were any aliases, link them now.
19191951 for (GlobalAlias &GA : SrcM->aliases())
1920 if (linkGlobalValueProto(&GA))
1952 if (linkIfNeeded(GA))
19211953 return true;
19221954
19231955 for (AppendingVarInfo &AppendingVar : AppendingVars)
19301962 const GlobalValue *GV = SrcM->getNamedValue(C.getName());
19311963 if (GV)
19321964 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);
19641965 }
19651966
19661967 // 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 = internal global i8 1, comdat
2 define i8* @zed() {
3 call void @bax()
4 ret i8* @foo
5 }
6 define internal void @bax() 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: llvm-link -S %s %p/Inputs/comdat13.ll -o - | 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 ; The IR would be ilegal on ELF ("relocation refers to discarded section"),
5 ; but COFF linkers seem to just duplicate the comdat.
6
7 $foo = comdat any
8 @foo = internal global i8 0, comdat
9 define i8* @bar() {
10 ret i8* @foo
11 }
12
13 ; CHECK: $foo = comdat any
14
15 ; CHECK: @foo = internal global i8 0, comdat
16 ; CHECK: @foo.1 = internal global i8 1, comdat($foo)
17
18 ; CHECK: define i8* @bar() {
19 ; CHECK-NEXT: ret i8* @foo
20 ; CHECK-NEXT: }
21
22 ; CHECK: define i8* @zed() {
23 ; CHECK-NEXT: call void @bax()
24 ; CHECK-NEXT: ret i8* @foo.1
25 ; CHECK-NEXT: }
26
27 ; CHECK: define internal void @bax() comdat($foo) {
28 ; CHECK-NEXT: ret void
29 ; CHECK-NEXT: }
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 {