llvm.org GIT mirror llvm / 7c4c88c
This reverts commit r254336 and r254344. They broke a bot and I am debugging why. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254347 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 4 years ago
6 changed file(s) with 80 addition(s) and 112 deletion(s). Raw diff Collapse all Expand all
435435 /// references.
436436 bool DoneLinkingBodies;
437437
438 bool HasError = false;
439
440438 public:
441439 ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
442440 DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
484482 /// Helper method for setting a message and returning an error code.
485483 bool emitError(const Twine &Message) {
486484 DiagnosticHandler(LinkDiagnosticInfo(DS_Error, Message));
487 HasError = true;
488485 return true;
489486 }
490487
533530 void upgradeMismatchedGlobalArray(StringRef Name);
534531 void upgradeMismatchedGlobals();
535532
536 bool linkIfNeeded(GlobalValue &GV);
537533 bool linkAppendingVarProto(GlobalVariable *DstGV,
538534 const GlobalVariable *SrcGV);
539535
907903 if (doneLinkingBodies())
908904 return nullptr;
909905
910 linkGlobalValueProto(SGV);
911 if (HasError)
912 return nullptr;
913 Value *Ret = ValueMap[SGV];
914 assert(Ret);
915 return Ret;
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;
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
939924 if (isPerformingImport() && !doImportAsDefinition(Old))
940925 return;
941926
942 if (DoNotLinkFromSource.count(Old))
927 // Skip declarations that ValueMaterializer may have created in
928 // case we link in only some of SrcM.
929 if (shouldLinkOnlyNeeded() && Old->isDeclaration())
943930 return;
944931
932 assert(!Old->isDeclaration() && "users should not pass down decls");
945933 linkGlobalValueBody(*Old);
946934 }
947935
14161404 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
14171405 C = DstM->getOrInsertComdat(SC->getName());
14181406 C->setSelectionKind(SK);
1407 ComdatMembers[SC].push_back(SGV);
14191408 } else if (DGV) {
14201409 if (shouldLinkFromSource(LinkFromSrc, *DGV, *SGV))
14211410 return true;
14351424 if (DGV)
14361425 HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr();
14371426
1427 if (!LinkFromSrc && !DGV)
1428 return false;
1429
14381430 GlobalValue *NewGV;
1439 if (!LinkFromSrc && DGV) {
1431 if (!LinkFromSrc) {
14401432 NewGV = DGV;
14411433 // When linking from source we setVisibility from copyGlobalValueProto.
14421434 setVisibility(NewGV, SGV, DGV);
14431435 } 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
14441452 NewGV = copyGlobalValueProto(TypeMap, SGV, DGV);
14451453
14461454 if (isPerformingImport() && !doImportAsDefinition(SGV))
14501458 NewGV->setUnnamedAddr(HasUnnamedAddr);
14511459
14521460 if (auto *NewGO = dyn_cast(NewGV)) {
1453 if (C && LinkFromSrc)
1461 if (C)
14541462 NewGO->setComdat(C);
14551463
14561464 if (DGV && DGV->hasCommonLinkage() && SGV->hasCommonLinkage())
18331841 return DstTriple.str();
18341842 }
18351843
1836 bool ModuleLinker::linkIfNeeded(GlobalValue &GV) {
1837 GlobalValue *DGV = getLinkedToGlobal(&GV);
1838
1839 if (shouldLinkOnlyNeeded() && !(DGV && DGV->isDeclaration()))
1840 return false;
1841
1842 if (DGV && !GV.hasLocalLinkage()) {
1843 GlobalValue::VisibilityTypes Visibility =
1844 getMinVisibility(DGV->getVisibility(), GV.getVisibility());
1845 DGV->setVisibility(Visibility);
1846 GV.setVisibility(Visibility);
1847 }
1848
1849 if (const Comdat *SC = GV.getComdat()) {
1850 bool LinkFromSrc;
1851 Comdat::SelectionKind SK;
1852 std::tie(SK, LinkFromSrc) = ComdatsChosen[SC];
1853 if (!LinkFromSrc) {
1854 DoNotLinkFromSource.insert(&GV);
1855 return false;
1856 }
1857 }
1858
1859 if (!DGV && !shouldOverrideFromSrc() &&
1860 (GV.hasLocalLinkage() || GV.hasLinkOnceLinkage() ||
1861 GV.hasAvailableExternallyLinkage())) {
1862 return false;
1863 }
1864 MapValue(&GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1865 return HasError;
1866 }
1867
18681844 bool ModuleLinker::run() {
18691845 assert(DstM && "Null destination module");
18701846 assert(SrcM && "Null source module");
19241900 // Upgrade mismatched global arrays.
19251901 upgradeMismatchedGlobals();
19261902
1927 for (GlobalVariable &GV : SrcM->globals())
1928 if (const Comdat *SC = GV.getComdat())
1929 ComdatMembers[SC].push_back(&GV);
1930
1931 for (Function &SF : *SrcM)
1932 if (const Comdat *SC = SF.getComdat())
1933 ComdatMembers[SC].push_back(&SF);
1934
1935 for (GlobalAlias &GA : SrcM->aliases())
1936 if (const Comdat *SC = GA.getComdat())
1937 ComdatMembers[SC].push_back(&GA);
1938
19391903 // Insert all of the globals in src into the DstM module... without linking
19401904 // initializers (which could refer to functions not yet mapped over).
19411905 for (GlobalVariable &GV : SrcM->globals())
1942 if (linkIfNeeded(GV))
1906 if (linkGlobalValueProto(&GV))
19431907 return true;
19441908
1945 for (Function &SF : *SrcM)
1946 if (linkIfNeeded(SF))
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))
19471916 return true;
19481917
1918 // If there were any aliases, link them now.
19491919 for (GlobalAlias &GA : SrcM->aliases())
1950 if (linkIfNeeded(GA))
1920 if (linkGlobalValueProto(&GA))
19511921 return true;
19521922
19531923 for (AppendingVarInfo &AppendingVar : AppendingVars)
19601930 const GlobalValue *GV = SrcM->getNamedValue(C.getName());
19611931 if (GV)
19621932 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);
19631964 }
19641965
19651966 // 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 *NewGV = dyn_cast(NewV))
44 Materializer->materializeInitFor(
45 NewGV, const_cast(cast(V)));
43 if (auto *GV = dyn_cast(V))
44 Materializer->materializeInitFor(cast(NewV),
45 const_cast(GV));
4646 return NewV;
4747 }
4848 }
+0
-9
test/Linker/Inputs/comdat11.ll less more
None $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
-13
test/Linker/comdat11.ll less more
None ; 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
-8
test/Linker/comdat12.ll less more
None ; 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
1313 define internal void @f2() comdat($f2) {
1414 ret void
1515 }
16 define void @f3() comdat($f2) {
17 ret void
18 }
1916
2017 ; CHECK-DAG: $f2 = comdat largest
2118 ; CHECK-DAG: define internal void @f2() comdat {