@@ -436,6 +436,8 @@ class ModuleLinker {
436
436
// / references.
437
437
bool DoneLinkingBodies;
438
438
439
+ bool HasError = false ;
440
+
439
441
public:
440
442
ModuleLinker (Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
441
443
DiagnosticHandlerFunction DiagnosticHandler, unsigned Flags,
@@ -483,6 +485,7 @@ class ModuleLinker {
483
485
// / Helper method for setting a message and returning an error code.
484
486
bool emitError (const Twine &Message) {
485
487
DiagnosticHandler (LinkDiagnosticInfo (DS_Error, Message));
488
+ HasError = true ;
486
489
return true ;
487
490
}
488
491
@@ -531,6 +534,7 @@ class ModuleLinker {
531
534
void upgradeMismatchedGlobalArray (StringRef Name);
532
535
void upgradeMismatchedGlobals ();
533
536
537
+ bool linkIfNeeded (GlobalValue &GV);
534
538
bool linkAppendingVarProto (GlobalVariable *DstGV,
535
539
const GlobalVariable *SrcGV);
536
540
@@ -904,16 +908,12 @@ Value *ModuleLinker::materializeDeclFor(Value *V) {
904
908
if (doneLinkingBodies ())
905
909
return nullptr ;
906
910
907
- GlobalValue *DGV = copyGlobalValueProto (TypeMap, SGV);
908
-
909
- if (Comdat *SC = SGV->getComdat ()) {
910
- if (auto *DGO = dyn_cast<GlobalObject>(DGV)) {
911
- Comdat *DC = DstM->getOrInsertComdat (SC->getName ());
912
- DGO->setComdat (DC);
913
- }
914
- }
915
-
916
- return DGV;
911
+ linkGlobalValueProto (SGV);
912
+ if (HasError)
913
+ return nullptr ;
914
+ Value *Ret = ValueMap[SGV];
915
+ assert (Ret);
916
+ return Ret;
917
917
}
918
918
919
919
void ValueMaterializerTy::materializeInitFor (GlobalValue *New,
@@ -922,15 +922,27 @@ void ValueMaterializerTy::materializeInitFor(GlobalValue *New,
922
922
}
923
923
924
924
void ModuleLinker::materializeInitFor (GlobalValue *New, GlobalValue *Old) {
925
+ if (auto *F = dyn_cast<Function>(New)) {
926
+ if (!F->isDeclaration ())
927
+ return ;
928
+ } else if (auto *V = dyn_cast<GlobalVariable>(New)) {
929
+ if (V->hasInitializer ())
930
+ return ;
931
+ } else {
932
+ auto *A = cast<GlobalAlias>(New);
933
+ if (A->getAliasee ())
934
+ return ;
935
+ }
936
+
937
+ if (Old->isDeclaration ())
938
+ return ;
939
+
925
940
if (isPerformingImport () && !doImportAsDefinition (Old))
926
941
return ;
927
942
928
- // Skip declarations that ValueMaterializer may have created in
929
- // case we link in only some of SrcM.
930
- if (shouldLinkOnlyNeeded () && Old->isDeclaration ())
943
+ if (!New->hasLocalLinkage () && DoNotLinkFromSource.count (Old))
931
944
return ;
932
945
933
- assert (!Old->isDeclaration () && " users should not pass down decls" );
934
946
linkGlobalValueBody (*Old);
935
947
}
936
948
@@ -1405,7 +1417,8 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
1405
1417
std::tie (SK, LinkFromSrc) = ComdatsChosen[SC];
1406
1418
C = DstM->getOrInsertComdat (SC->getName ());
1407
1419
C->setSelectionKind (SK);
1408
- ComdatMembers[SC].push_back (SGV);
1420
+ if (SGV->hasInternalLinkage ())
1421
+ LinkFromSrc = true ;
1409
1422
} else if (DGV) {
1410
1423
if (shouldLinkFromSource (LinkFromSrc, *DGV, *SGV))
1411
1424
return true ;
@@ -1425,31 +1438,12 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
1425
1438
if (DGV)
1426
1439
HasUnnamedAddr = HasUnnamedAddr && DGV->hasUnnamedAddr ();
1427
1440
1428
- if (!LinkFromSrc && !DGV)
1429
- return false ;
1430
-
1431
1441
GlobalValue *NewGV;
1432
- if (!LinkFromSrc) {
1442
+ if (!LinkFromSrc && DGV ) {
1433
1443
NewGV = DGV;
1434
1444
// When linking from source we setVisibility from copyGlobalValueProto.
1435
1445
setVisibility (NewGV, SGV, DGV);
1436
1446
} else {
1437
- // If the GV is to be lazily linked, don't create it just yet.
1438
- // The ValueMaterializerTy will deal with creating it if it's used.
1439
- if (!DGV && !shouldOverrideFromSrc () && SGV != ImportFunction &&
1440
- (SGV->hasLocalLinkage () || SGV->hasLinkOnceLinkage () ||
1441
- SGV->hasAvailableExternallyLinkage ())) {
1442
- DoNotLinkFromSource.insert (SGV);
1443
- return false ;
1444
- }
1445
-
1446
- // When we only want to link in unresolved dependencies, blacklist
1447
- // the symbol unless unless DestM has a matching declaration (DGV).
1448
- if (shouldLinkOnlyNeeded () && !(DGV && DGV->isDeclaration ())) {
1449
- DoNotLinkFromSource.insert (SGV);
1450
- return false ;
1451
- }
1452
-
1453
1447
NewGV = copyGlobalValueProto (TypeMap, SGV, DGV);
1454
1448
1455
1449
if (isPerformingImport () && !doImportAsDefinition (SGV))
@@ -1459,7 +1453,7 @@ bool ModuleLinker::linkGlobalValueProto(GlobalValue *SGV) {
1459
1453
NewGV->setUnnamedAddr (HasUnnamedAddr);
1460
1454
1461
1455
if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
1462
- if (C)
1456
+ if (C && LinkFromSrc )
1463
1457
NewGO->setComdat (C);
1464
1458
1465
1459
if (DGV && DGV->hasCommonLinkage () && SGV->hasCommonLinkage ())
@@ -1842,6 +1836,38 @@ static std::string mergeTriples(const Triple &SrcTriple, const Triple &DstTriple
1842
1836
return DstTriple.str ();
1843
1837
}
1844
1838
1839
+ bool ModuleLinker::linkIfNeeded (GlobalValue &GV) {
1840
+ GlobalValue *DGV = getLinkedToGlobal (&GV);
1841
+
1842
+ if (shouldLinkOnlyNeeded () && !(DGV && DGV->isDeclaration ()))
1843
+ return false ;
1844
+
1845
+ if (DGV && !GV.hasLocalLinkage ()) {
1846
+ GlobalValue::VisibilityTypes Visibility =
1847
+ getMinVisibility (DGV->getVisibility (), GV.getVisibility ());
1848
+ DGV->setVisibility (Visibility);
1849
+ GV.setVisibility (Visibility);
1850
+ }
1851
+
1852
+ if (const Comdat *SC = GV.getComdat ()) {
1853
+ bool LinkFromSrc;
1854
+ Comdat::SelectionKind SK;
1855
+ std::tie (SK, LinkFromSrc) = ComdatsChosen[SC];
1856
+ if (!LinkFromSrc) {
1857
+ DoNotLinkFromSource.insert (&GV);
1858
+ return false ;
1859
+ }
1860
+ }
1861
+
1862
+ if (!DGV && !shouldOverrideFromSrc () &&
1863
+ (GV.hasLocalLinkage () || GV.hasLinkOnceLinkage () ||
1864
+ GV.hasAvailableExternallyLinkage ())) {
1865
+ return false ;
1866
+ }
1867
+ MapValue (&GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1868
+ return HasError;
1869
+ }
1870
+
1845
1871
bool ModuleLinker::run () {
1846
1872
assert (DstM && " Null destination module" );
1847
1873
assert (SrcM && " Null source module" );
@@ -1901,24 +1927,30 @@ bool ModuleLinker::run() {
1901
1927
// Upgrade mismatched global arrays.
1902
1928
upgradeMismatchedGlobals ();
1903
1929
1930
+ for (GlobalVariable &GV : SrcM->globals ())
1931
+ if (const Comdat *SC = GV.getComdat ())
1932
+ ComdatMembers[SC].push_back (&GV);
1933
+
1934
+ for (Function &SF : *SrcM)
1935
+ if (const Comdat *SC = SF.getComdat ())
1936
+ ComdatMembers[SC].push_back (&SF);
1937
+
1938
+ for (GlobalAlias &GA : SrcM->aliases ())
1939
+ if (const Comdat *SC = GA.getComdat ())
1940
+ ComdatMembers[SC].push_back (&GA);
1941
+
1904
1942
// Insert all of the globals in src into the DstM module... without linking
1905
1943
// initializers (which could refer to functions not yet mapped over).
1906
1944
for (GlobalVariable &GV : SrcM->globals ())
1907
- if (linkGlobalValueProto (& GV))
1945
+ if (linkIfNeeded ( GV))
1908
1946
return true ;
1909
1947
1910
- // Link the functions together between the two modules, without doing function
1911
- // bodies... this just adds external function prototypes to the DstM
1912
- // function... We do this so that when we begin processing function bodies,
1913
- // all of the global values that may be referenced are available in our
1914
- // ValueMap.
1915
- for (Function &F :*SrcM)
1916
- if (linkGlobalValueProto (&F))
1948
+ for (Function &SF : *SrcM)
1949
+ if (linkIfNeeded (SF))
1917
1950
return true ;
1918
1951
1919
- // If there were any aliases, link them now.
1920
1952
for (GlobalAlias &GA : SrcM->aliases ())
1921
- if (linkGlobalValueProto (& GA))
1953
+ if (linkIfNeeded ( GA))
1922
1954
return true ;
1923
1955
1924
1956
for (AppendingVarInfo &AppendingVar : AppendingVars)
@@ -1933,37 +1965,6 @@ bool ModuleLinker::run() {
1933
1965
MapValue (GV, ValueMap, RF_MoveDistinctMDs, &TypeMap, &ValMaterializer);
1934
1966
}
1935
1967
1936
- // Link in the function bodies that are defined in the source module into
1937
- // DstM.
1938
- for (Function &SF : *SrcM) {
1939
- // Skip if no body (function is external).
1940
- if (SF.isDeclaration ())
1941
- continue ;
1942
-
1943
- // Skip if not linking from source.
1944
- if (DoNotLinkFromSource.count (&SF))
1945
- continue ;
1946
-
1947
- if (linkGlobalValueBody (SF))
1948
- return true ;
1949
- }
1950
-
1951
- // Resolve all uses of aliases with aliasees.
1952
- for (GlobalAlias &Src : SrcM->aliases ()) {
1953
- if (DoNotLinkFromSource.count (&Src))
1954
- continue ;
1955
- linkGlobalValueBody (Src);
1956
- }
1957
-
1958
- // Update the initializers in the DstM module now that all globals that may
1959
- // be referenced are in DstM.
1960
- for (GlobalVariable &Src : SrcM->globals ()) {
1961
- // Only process initialized GV's or ones not already in dest.
1962
- if (!Src.hasInitializer () || DoNotLinkFromSource.count (&Src))
1963
- continue ;
1964
- linkGlobalValueBody (Src);
1965
- }
1966
-
1967
1968
// Note that we are done linking global value bodies. This prevents
1968
1969
// metadata linking from creating new references.
1969
1970
DoneLinkingBodies = true ;
0 commit comments