llvm.org GIT mirror llvm / 7fd7eff
Lazily link GlobalVariables and GlobalAliases. We were already lazily linking functions, but all GlobalValues can be treated uniformly for this. The test updates are to ensure that a given GlobalValue is still linked in. This fixes pr21494. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223681 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 5 years ago
7 changed file(s) with 181 addition(s) and 127 deletion(s). Raw diff Collapse all Expand all
364364 class ValueMaterializerTy : public ValueMaterializer {
365365 TypeMapTy &TypeMap;
366366 Module *DstM;
367 std::vector<Function *> &LazilyLinkFunctions;
367 std::vector<GlobalValue *> &LazilyLinkGlobalValues;
368368
369369 public:
370370 ValueMaterializerTy(TypeMapTy &TypeMap, Module *DstM,
371 std::vector<Function *> &LazilyLinkFunctions)
371 std::vector<GlobalValue *> &LazilyLinkGlobalValues)
372372 : ValueMaterializer(), TypeMap(TypeMap), DstM(DstM),
373 LazilyLinkFunctions(LazilyLinkFunctions) {}
373 LazilyLinkGlobalValues(LazilyLinkGlobalValues) {}
374374
375375 Value *materializeValueFor(Value *V) override;
376376 };
412412 // Set of items not to link in from source.
413413 SmallPtrSet DoNotLinkFromSource;
414414
415 // Vector of functions to lazily link in.
416 std::vector LazilyLinkFunctions;
415 // Vector of GlobalValues to lazily link in.
416 std::vector LazilyLinkGlobalValues;
417417
418418 Linker::DiagnosticHandlerFunction DiagnosticHandler;
419419
421421 ModuleLinker(Module *dstM, Linker::IdentifiedStructTypeSet &Set, Module *srcM,
422422 Linker::DiagnosticHandlerFunction DiagnosticHandler)
423423 : DstM(dstM), SrcM(srcM), TypeMap(Set),
424 ValMaterializer(TypeMap, DstM, LazilyLinkFunctions),
424 ValMaterializer(TypeMap, DstM, LazilyLinkGlobalValues),
425425 DiagnosticHandler(DiagnosticHandler) {}
426426
427427 bool run();
483483 const GlobalVariable *SrcGV);
484484
485485 bool linkGlobalValueProto(GlobalValue *GV);
486 GlobalValue *linkGlobalVariableProto(const GlobalVariable *SGVar);
487 GlobalValue *linkFunctionProto(const Function *SF, GlobalValue *DGV);
488 GlobalValue *linkGlobalAliasProto(const GlobalAlias *SGA);
489
490486 bool linkModuleFlagsMetadata();
491487
492488 void linkAppendingVarInit(const AppendingVarInfo &AVI);
493 void linkGlobalInits();
494 bool linkFunctionBody(Function &Src);
495 void linkAliasBodies();
489
490 void linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src);
491 bool linkFunctionBody(Function &Dst, Function &Src);
492 void linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src);
493 bool linkGlobalValueBody(GlobalValue &Src);
494
496495 void linkNamedMDNodes();
497496 };
498497 }
538537 return false;
539538 }
540539
540 /// Loop through the global variables in the src module and merge them into the
541 /// dest module.
542 static GlobalVariable *copyGlobalVariableProto(TypeMapTy &TypeMap, Module &DstM,
543 const GlobalVariable *SGVar) {
544 // No linking to be performed or linking from the source: simply create an
545 // identical version of the symbol over in the dest module... the
546 // initializer will be filled in later by LinkGlobalInits.
547 GlobalVariable *NewDGV = new GlobalVariable(
548 DstM, TypeMap.get(SGVar->getType()->getElementType()),
549 SGVar->isConstant(), SGVar->getLinkage(), /*init*/ nullptr,
550 SGVar->getName(), /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
551 SGVar->getType()->getAddressSpace());
552
553 return NewDGV;
554 }
555
556 /// Link the function in the source module into the destination module if
557 /// needed, setting up mapping information.
558 static Function *copyFunctionProto(TypeMapTy &TypeMap, Module &DstM,
559 const Function *SF) {
560 // If there is no linkage to be performed or we are linking from the source,
561 // bring SF over.
562 return Function::Create(TypeMap.get(SF->getFunctionType()), SF->getLinkage(),
563 SF->getName(), &DstM);
564 }
565
566 /// Set up prototypes for any aliases that come over from the source module.
567 static GlobalAlias *copyGlobalAliasProto(TypeMapTy &TypeMap, Module &DstM,
568 const GlobalAlias *SGA) {
569 // If there is no linkage to be performed or we're linking from the source,
570 // bring over SGA.
571 auto *PTy = cast(TypeMap.get(SGA->getType()));
572 return GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
573 SGA->getLinkage(), SGA->getName(), &DstM);
574 }
575
576 static GlobalValue *copyGlobalValueProto(TypeMapTy &TypeMap, Module &DstM,
577 const GlobalValue *SGV) {
578 GlobalValue *NewGV;
579 if (auto *SGVar = dyn_cast(SGV))
580 NewGV = copyGlobalVariableProto(TypeMap, DstM, SGVar);
581 else if (auto *SF = dyn_cast(SGV))
582 NewGV = copyFunctionProto(TypeMap, DstM, SF);
583 else
584 NewGV = copyGlobalAliasProto(TypeMap, DstM, cast(SGV));
585 copyGVAttributes(NewGV, SGV);
586 return NewGV;
587 }
588
541589 Value *ValueMaterializerTy::materializeValueFor(Value *V) {
542 Function *SF = dyn_cast(V);
543 if (!SF)
590 auto *SGV = dyn_cast(V);
591 if (!SGV)
544592 return nullptr;
545593
546 Function *DF = Function::Create(TypeMap.get(SF->getFunctionType()),
547 SF->getLinkage(), SF->getName(), DstM);
548 copyGVAttributes(DF, SF);
549
550 if (Comdat *SC = SF->getComdat()) {
551 Comdat *DC = DstM->getOrInsertComdat(SC->getName());
552 DF->setComdat(DC);
553 }
554
555 LazilyLinkFunctions.push_back(SF);
556 return DF;
594 GlobalValue *DGV = copyGlobalValueProto(TypeMap, *DstM, SGV);
595
596 if (Comdat *SC = SGV->getComdat()) {
597 if (auto *DGO = dyn_cast(DGV)) {
598 Comdat *DC = DstM->getOrInsertComdat(SC->getName());
599 DGO->setComdat(DC);
600 }
601 }
602
603 LazilyLinkGlobalValues.push_back(SGV);
604 return DGV;
557605 }
558606
559607 bool ModuleLinker::getComdatLeader(Module *M, StringRef ComdatName,
10201068 if (!LinkFromSrc) {
10211069 NewGV = DGV;
10221070 } else {
1023 if (auto *SGVar = dyn_cast(SGV))
1024 NewGV = linkGlobalVariableProto(SGVar);
1025 else if (auto *SF = dyn_cast(SGV))
1026 NewGV = linkFunctionProto(SF, DGV);
1027 else
1028 NewGV = linkGlobalAliasProto(cast(SGV));
1029 }
1030
1031 if (!NewGV)
1032 return false;
1033
1034 if (NewGV != DGV)
1035 copyGVAttributes(NewGV, SGV);
1071 // If the GV is to be lazily linked, don't create it just yet.
1072 // The ValueMaterializerTy will deal with creating it if it's used.
1073 if (!DGV && (SGV->hasLocalLinkage() || SGV->hasLinkOnceLinkage() ||
1074 SGV->hasAvailableExternallyLinkage())) {
1075 DoNotLinkFromSource.insert(SGV);
1076 return false;
1077 }
1078
1079 NewGV = copyGlobalValueProto(TypeMap, *DstM, SGV);
1080 }
10361081
10371082 NewGV->setUnnamedAddr(HasUnnamedAddr);
10381083 NewGV->setVisibility(Visibility);
10651110 return false;
10661111 }
10671112
1068 /// Loop through the global variables in the src module and merge them into the
1069 /// dest module.
1070 GlobalValue *
1071 ModuleLinker::linkGlobalVariableProto(const GlobalVariable *SGVar) {
1072 // No linking to be performed or linking from the source: simply create an
1073 // identical version of the symbol over in the dest module... the
1074 // initializer will be filled in later by LinkGlobalInits.
1075 GlobalVariable *NewDGV = new GlobalVariable(
1076 *DstM, TypeMap.get(SGVar->getType()->getElementType()),
1077 SGVar->isConstant(), SGVar->getLinkage(), /*init*/ nullptr,
1078 SGVar->getName(), /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
1079 SGVar->getType()->getAddressSpace());
1080
1081 return NewDGV;
1082 }
1083
1084 /// Link the function in the source module into the destination module if
1085 /// needed, setting up mapping information.
1086 GlobalValue *ModuleLinker::linkFunctionProto(const Function *SF,
1087 GlobalValue *DGV) {
1088 // If the function is to be lazily linked, don't create it just yet.
1089 // The ValueMaterializerTy will deal with creating it if it's used.
1090 if (!DGV && (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
1091 SF->hasAvailableExternallyLinkage())) {
1092 DoNotLinkFromSource.insert(SF);
1093 return nullptr;
1094 }
1095
1096 // If there is no linkage to be performed or we are linking from the source,
1097 // bring SF over.
1098 return Function::Create(TypeMap.get(SF->getFunctionType()), SF->getLinkage(),
1099 SF->getName(), DstM);
1100 }
1101
1102 /// Set up prototypes for any aliases that come over from the source module.
1103 GlobalValue *ModuleLinker::linkGlobalAliasProto(const GlobalAlias *SGA) {
1104 // If there is no linkage to be performed or we're linking from the source,
1105 // bring over SGA.
1106 auto *PTy = cast(TypeMap.get(SGA->getType()));
1107 return GlobalAlias::create(PTy->getElementType(), PTy->getAddressSpace(),
1108 SGA->getLinkage(), SGA->getName(), DstM);
1109 }
1110
11111113 static void getArrayElements(const Constant *C,
11121114 SmallVectorImpl &Dest) {
11131115 unsigned NumElements = cast(C->getType())->getNumElements();
11501152
11511153 /// Update the initializers in the Dest module now that all globals that may be
11521154 /// referenced are in Dest.
1153 void ModuleLinker::linkGlobalInits() {
1154 // Loop over all of the globals in the src module, mapping them over as we go
1155 for (GlobalVariable &Src : SrcM->globals()) {
1156 // Only process initialized GV's or ones not already in dest.
1157 if (!Src.hasInitializer() || DoNotLinkFromSource.count(&Src))
1158 continue;
1159
1160 // Grab destination global variable.
1161 GlobalVariable *Dst = cast(ValueMap[&Src]);
1162 // Figure out what the initializer looks like in the dest module.
1163 Dst->setInitializer(MapValue(Src.getInitializer(), ValueMap, RF_None,
1164 &TypeMap, &ValMaterializer));
1165 }
1155 void ModuleLinker::linkGlobalInit(GlobalVariable &Dst, GlobalVariable &Src) {
1156 // Figure out what the initializer looks like in the dest module.
1157 Dst.setInitializer(MapValue(Src.getInitializer(), ValueMap, RF_None, &TypeMap,
1158 &ValMaterializer));
11661159 }
11671160
11681161 /// Copy the source function over into the dest function and fix up references
11691162 /// to values. At this point we know that Dest is an external function, and
11701163 /// that Src is not.
1171 bool ModuleLinker::linkFunctionBody(Function &Src) {
1172 Function *Dst = cast(ValueMap[&Src]);
1173 assert(Dst && Dst->isDeclaration() && !Src.isDeclaration());
1164 bool ModuleLinker::linkFunctionBody(Function &Dst, Function &Src) {
1165 assert(Dst.isDeclaration() && !Src.isDeclaration());
11741166
11751167 // Materialize if needed.
11761168 if (std::error_code EC = Src.materialize())
11781170
11791171 // Link in the prefix data.
11801172 if (Src.hasPrefixData())
1181 Dst->setPrefixData(MapValue(Src.getPrefixData(), ValueMap, RF_None,
1182 &TypeMap, &ValMaterializer));
1173 Dst.setPrefixData(MapValue(Src.getPrefixData(), ValueMap, RF_None, &TypeMap,
1174 &ValMaterializer));
11831175
11841176 // Link in the prologue data.
11851177 if (Src.hasPrologueData())
1186 Dst->setPrologueData(MapValue(Src.getPrologueData(), ValueMap, RF_None,
1178 Dst.setPrologueData(MapValue(Src.getPrologueData(), ValueMap, RF_None,
11871179 &TypeMap, &ValMaterializer));
11881180
1189
11901181 // Go through and convert function arguments over, remembering the mapping.
1191 Function::arg_iterator DI = Dst->arg_begin();
1182 Function::arg_iterator DI = Dst.arg_begin();
11921183 for (Argument &Arg : Src.args()) {
11931184 DI->setName(Arg.getName()); // Copy the name over.
11941185
11981189 }
11991190
12001191 // Splice the body of the source function into the dest function.
1201 Dst->getBasicBlockList().splice(Dst->end(), Src.getBasicBlockList());
1192 Dst.getBasicBlockList().splice(Dst.end(), Src.getBasicBlockList());
12021193
12031194 // At this point, all of the instructions and values of the function are now
12041195 // copied over. The only problem is that they are still referencing values in
12051196 // the Source function as operands. Loop through all of the operands of the
12061197 // functions and patch them up to point to the local versions.
1207 for (BasicBlock &BB : *Dst)
1198 for (BasicBlock &BB : Dst)
12081199 for (Instruction &I : BB)
12091200 RemapInstruction(&I, ValueMap, RF_IgnoreMissingEntries, &TypeMap,
12101201 &ValMaterializer);
12171208 return false;
12181209 }
12191210
1220 /// Insert all of the aliases in Src into the Dest module.
1221 void ModuleLinker::linkAliasBodies() {
1222 for (GlobalAlias &Src : SrcM->aliases()) {
1223 if (DoNotLinkFromSource.count(&Src))
1224 continue;
1225 if (Constant *Aliasee = Src.getAliasee()) {
1226 GlobalAlias *DA = cast(ValueMap[&Src]);
1227 Constant *Val =
1228 MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1229 DA->setAliasee(Val);
1230 }
1231 }
1211 void ModuleLinker::linkAliasBody(GlobalAlias &Dst, GlobalAlias &Src) {
1212 Constant *Aliasee = Src.getAliasee();
1213 Constant *Val =
1214 MapValue(Aliasee, ValueMap, RF_None, &TypeMap, &ValMaterializer);
1215 Dst.setAliasee(Val);
1216 }
1217
1218 bool ModuleLinker::linkGlobalValueBody(GlobalValue &Src) {
1219 Value *Dst = ValueMap[&Src];
1220 assert(Dst);
1221 if (auto *F = dyn_cast(&Src))
1222 return linkFunctionBody(cast(*Dst), *F);
1223 if (auto *GVar = dyn_cast(&Src)) {
1224 linkGlobalInit(cast(*Dst), *GVar);
1225 return false;
1226 }
1227 linkAliasBody(cast(*Dst), cast(Src));
1228 return false;
12321229 }
12331230
12341231 /// Insert all of the named MDNodes in Src into the Dest module.
15011498 if (DoNotLinkFromSource.count(&SF))
15021499 continue;
15031500
1504 if (linkFunctionBody(SF))
1501 if (linkGlobalValueBody(SF))
15051502 return true;
15061503 }
15071504
15081505 // Resolve all uses of aliases with aliasees.
1509 linkAliasBodies();
1506 for (GlobalAlias &Src : SrcM->aliases()) {
1507 if (DoNotLinkFromSource.count(&Src))
1508 continue;
1509 linkGlobalValueBody(Src);
1510 }
15101511
15111512 // Remap all of the named MDNodes in Src into the DstM module. We do this
15121513 // after linking GlobalValues so that MDNodes that reference GlobalValues
15191520
15201521 // Update the initializers in the DstM module now that all globals that may
15211522 // be referenced are in DstM.
1522 linkGlobalInits();
1523 for (GlobalVariable &Src : SrcM->globals()) {
1524 // Only process initialized GV's or ones not already in dest.
1525 if (!Src.hasInitializer() || DoNotLinkFromSource.count(&Src))
1526 continue;
1527 linkGlobalValueBody(Src);
1528 }
15231529
15241530 // Process vector of lazily linked in functions.
1525 while (!LazilyLinkFunctions.empty()) {
1526 Function *SF = LazilyLinkFunctions.back();
1527 LazilyLinkFunctions.pop_back();
1528
1529 if (linkFunctionBody(*SF))
1531 while (!LazilyLinkGlobalValues.empty()) {
1532 GlobalValue *SGV = LazilyLinkGlobalValues.back();
1533 LazilyLinkGlobalValues.pop_back();
1534
1535 if (linkGlobalValueBody(*SGV))
15301536 return true;
15311537 }
15321538
3434
3535 ; ZED1_AND_ZED2: d zed2
3636 @zed2 = linkonce_odr unnamed_addr global i32 42
37
38 define i32 @useZed2() {
39 %x = load i32* @zed2
40 ret i32 %x
41 }
2828 ; CHECK: r v1
2929 @v1 = linkonce_odr constant i32 32
3030
31 define i32 @useV1() {
32 %x = load i32* @v1
33 ret i32 %x
34 }
35
3136 ; CHECK: V v2
3237 @v2 = linkonce_odr global i32 32
38
39 define i32 @useV2() {
40 %x = load i32* @v2
41 ret i32 %x
42 }
3343
3444 declare void @f(void()*)
3545
1515
1616 ;; Intern in both testlink[12].ll
1717 @Intern1 = internal constant i32 52
18
19 @Use2Intern1 = global i32* @Intern1
1820
1921 ;; Intern in one but not in other
2022 @Intern2 = constant i32 12345
0 ; RUN: llvm-link -S %s -o - | FileCheck %s
11
2 ; CHECK: @foo = private externally_initialized global i8* null
2 ; CHECK-DAG: @foo = private externally_initialized global i8* null
33 @foo = private externally_initialized global i8* null
4 ; CHECK: @array = appending global [7 x i8] c"abcdefg", align 1
4
5 @useFoo = global i8** @foo
6
7 ; CHECK-DAG: @array = appending global [7 x i8] c"abcdefg", align 1
58 @array = appending global [7 x i8] c"abcdefg", align 1
69
0 ; RUN: llvm-link %s -S -o - | FileCheck %s
1
2 @g1 = private global i8 0
3 ; CHECK-NOT: @g1
4
5 @g2 = linkonce_odr global i8 0
6 ; CHECK-NOT: @g2
7
8 @a1 = private alias i8* @g1
9 ; CHECK-NOT: @a1
10
11 @a2 = linkonce_odr alias i8* @g2
12 ; CHECK-NOT: @a2
13
14 define private void @f1() {
15 ret void
16 }
17 ; CHECK-NOT: @f1
18
19 define linkonce_odr void @f2() {
20 ret void
21 }
22 ; CHECK-NOT: @f2
3838 ; CHECK-DAG: @Intern1 = internal constant i32 42
3939 @Intern1 = internal constant i32 42
4040
41 @UseIntern1 = global i32* @Intern1
42
4143 ; This should get renamed since there is a definition that is non-internal in
4244 ; the other module.
4345 ; CHECK-DAG: @Intern2{{[0-9]+}} = internal constant i32 792
4446 @Intern2 = internal constant i32 792
4547
48 @UseIntern2 = global i32* @Intern2
4649
4750 ; CHECK-DAG: @MyVarPtr = linkonce global { i32* } { i32* @MyVar }
4851 @MyVarPtr = linkonce global { i32* } { i32* @MyVar }
52
53 @UseMyVarPtr = global { i32* }* @MyVarPtr
4954
5055 ; CHECK-DAG: @MyVar = global i32 4
5156 @MyVar = external global i32