llvm.org GIT mirror llvm / 140d9e6
Remangle intrinsics names when types are renamed This is a resubmittion of previously reverted rL273568. This is a fix for the problem mentioned in "LTO and intrinsics mangling" llvm-dev mail thread: http://lists.llvm.org/pipermail/llvm-dev/2016-April/098387.html Reviewers: mehdi_amini, reames Differential Revision: http://reviews.llvm.org/D19373 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273686 91177308-0d34-0410-b5e6-96231b3b80d8 Artur Pilipenko 4 years ago
8 changed file(s) with 169 addition(s) and 2 deletion(s). Raw diff Collapse all Expand all
1717
1818 #include "llvm/ADT/ArrayRef.h"
1919 #include "llvm/ADT/None.h"
20 #include "llvm/ADT/Optional.h"
2021 #include
2122
2223 namespace llvm {
148149 /// This method returns true on error.
149150 bool matchIntrinsicVarArg(bool isVarArg, ArrayRef &Infos);
150151
152 // Checks if the intrinsic name matches with its signature and if not
153 // returns the declaration with the same signature and remangled name.
154 llvm::Optional remangleIntrinsicFunction(Function *F);
155
151156 } // End Intrinsic namespace
152157
153158 } // End llvm namespace
1717 #include "llvm/AsmParser/SlotMapping.h"
1818 #include "llvm/IR/AutoUpgrade.h"
1919 #include "llvm/IR/CallingConv.h"
20 #include "llvm/IR/CallSite.h"
2021 #include "llvm/IR/Constants.h"
2122 #include "llvm/IR/DebugInfo.h"
2223 #include "llvm/IR/DebugInfoMetadata.h"
2324 #include "llvm/IR/DerivedTypes.h"
2425 #include "llvm/IR/InlineAsm.h"
2526 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/Intrinsics.h"
2628 #include "llvm/IR/LLVMContext.h"
2729 #include "llvm/IR/Module.h"
2830 #include "llvm/IR/Operator.h"
208210 // Look for intrinsic functions and CallInst that need to be upgraded
209211 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
210212 UpgradeCallsToIntrinsic(&*FI++); // must be post-increment, as we remove
213
214 // Some types could be renamed during loading if several modules are
215 // loaded in the same LLVMContext (LTO scenario). In this case we should
216 // remangle intrinsics names as well.
217 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) {
218 Function *F = &*FI++;
219 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F)) {
220 F->replaceAllUsesWith(Remangled.getValue());
221 F->eraseFromParent();
222 }
223 }
211224
212225 UpgradeDebugInfo(*M);
213226
1414 #include "llvm/Bitcode/LLVMBitCodes.h"
1515 #include "llvm/Bitcode/ReaderWriter.h"
1616 #include "llvm/IR/AutoUpgrade.h"
17 #include "llvm/IR/CallSite.h"
1718 #include "llvm/IR/Constants.h"
1819 #include "llvm/IR/DebugInfo.h"
1920 #include "llvm/IR/DebugInfoMetadata.h"
229230
230231 // When intrinsic functions are encountered which require upgrading they are
231232 // stored here with their replacement function.
232 typedef DenseMap UpgradedIntrinsicMap;
233 UpgradedIntrinsicMap UpgradedIntrinsics;
233 typedef DenseMap UpdatedIntrinsicMap;
234 UpdatedIntrinsicMap UpgradedIntrinsics;
235 // Intrinsics which were remangled because of types rename
236 UpdatedIntrinsicMap RemangledIntrinsics;
234237
235238 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
236239 DenseMap MDKindMap;
34243427 Function *NewFn;
34253428 if (UpgradeIntrinsicFunction(&F, NewFn))
34263429 UpgradedIntrinsics[&F] = NewFn;
3430 else if (auto Remangled = Intrinsic::remangleIntrinsicFunction(&F))
3431 // Some types could be renamed during loading if several modules are
3432 // loaded in the same LLVMContext (LTO scenario). In this case we should
3433 // remangle intrinsics names as well.
3434 RemangledIntrinsics[&F] = Remangled.getValue();
34273435 }
34283436
34293437 // Look for global variables which need to be renamed.
56005608 }
56015609 }
56025610
5611 // Update calls to the remangled intrinsics
5612 for (auto &I : RemangledIntrinsics)
5613 for (auto UI = I.first->materialized_user_begin(), UE = I.first->user_end();
5614 UI != UE;)
5615 // Don't expect any other users than call sites
5616 CallSite(*UI++).setCalledFunction(I.second);
5617
56035618 // Finish fn->subprogram upgrade for materialized functions.
56045619 if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
56055620 F->setSubprogram(SP);
56535668 I.first->eraseFromParent();
56545669 }
56555670 UpgradedIntrinsics.clear();
5671 // Do the same for remangled intrinsics
5672 for (auto &I : RemangledIntrinsics) {
5673 I.first->replaceAllUsesWith(I.second);
5674 I.first->eraseFromParent();
5675 }
5676 RemangledIntrinsics.clear();
56565677
56575678 UpgradeDebugInfo(*TheModule);
56585679
10791079 return true;
10801080 }
10811081
1082 Optional Intrinsic::remangleIntrinsicFunction(Function *F) {
1083 Intrinsic::ID ID = F->getIntrinsicID();
1084 if (!ID)
1085 return None;
1086
1087 FunctionType *FTy = F->getFunctionType();
1088 // Accumulate an array of overloaded types for the given intrinsic
1089 SmallVector ArgTys;
1090 {
1091 SmallVector Table;
1092 getIntrinsicInfoTableEntries(ID, Table);
1093 ArrayRef TableRef = Table;
1094
1095 // If we encounter any problems matching the signature with the descriptor
1096 // just give up remangling. It's up to verifier to report the discrepancy.
1097 if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
1098 return None;
1099 for (auto Ty : FTy->params())
1100 if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
1101 return None;
1102 if (Intrinsic::matchIntrinsicVarArg(FTy->isVarArg(), TableRef))
1103 return None;
1104 }
1105
1106 StringRef Name = F->getName();
1107 if (Name == Intrinsic::getName(ID, ArgTys))
1108 return None;
1109
1110 auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1111 NewDecl->setCallingConv(F->getCallingConv());
1112 assert(NewDecl->getFunctionType() == FTy && "Shouldn't change the signature");
1113 return NewDecl;
1114 }
1115
10821116 /// hasAddressTaken - returns true if there are any uses of this function
10831117 /// other than direct calls or invokes to it.
10841118 bool Function::hasAddressTaken(const User* *PutOffender) const {
1515 #include "llvm/IR/DebugInfo.h"
1616 #include "llvm/IR/DiagnosticPrinter.h"
1717 #include "llvm/IR/GVMaterializer.h"
18 #include "llvm/IR/Intrinsics.h"
1819 #include "llvm/IR/TypeFinder.h"
1920 #include "llvm/Support/Error.h"
2021 #include "llvm/Transforms/Utils/Cloning.h"
900901 if (ShouldLink || !ForAlias)
901902 forceRenaming(NewGV, SGV->getName());
902903 }
904
905 // Overloaded intrinsics have overloaded types names as part of their
906 // names. If we renamed overloaded types we should rename the intrinsic
907 // as well.
908 if (Function *F = dyn_cast(NewGV))
909 if (auto Remangled = Intrinsic::remangleIntrinsicFunction(F))
910 NewGV = Remangled.getValue();
911
903912 if (ShouldLink || ForAlias) {
904913 if (const Comdat *SC = SGV->getComdat()) {
905914 if (auto *GO = dyn_cast(NewGV)) {
0 %struct.rtx_def = type { i16, i16 }
1
2 define void @bar(%struct.rtx_def* %a, i8 %b, i32 %c) {
3 call void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)
4 ret void
5 }
6
7 declare void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def*, i8, i32, i32, i1)
0 ; RUN: llvm-as < %s > %t1
1 ; RUN: llvm-as < %p/Inputs/remangle_intrinsics.ll > %t2
2 ; RUN: llvm-lto %t1 %t2 | FileCheck %s
3
4 ; We have "struct.rtx_def" type in both modules being LTOed. Both modules use
5 ; an overloaded intrinsic which has this type in its signature/name. When
6 ; modules are loaded one of the types is renamed to "struct.rtx_def.0".
7 ; The intrinsic which uses this type should be remangled/renamed as well.
8 ; If we didn't do that verifier would complain.
9
10 ; CHECK: Wrote native object file
11
12 %struct.rtx_def = type { i16 }
13
14 define void @foo(%struct.rtx_def* %a, i8 %b, i32 %c) {
15 call void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)
16 ret void
17 }
18
19 declare void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def*, i8, i32, i32, i1)
20
21 ; Check that remangling code doesn't fail on an intrinsic with wrong signature
22 declare void @llvm.memset.i64(i8* nocapture, i8, i64, i32) nounwind
305305 EXPECT_EQ(M3, M4->getOperand(0));
306306 }
307307
308 TEST_F(LinkModuleTest, RemangleIntrinsics) {
309 LLVMContext C;
310 SMDiagnostic Err;
311
312 // We load two modules inside the same context C. In both modules there is a
313 // "struct.rtx_def" type. In the module loaded the second (Bar) this type will
314 // be renamed to "struct.rtx_def.0". Check that the intrinsics which have this
315 // type in the signature are properly remangled.
316 const char *FooStr =
317 "%struct.rtx_def = type { i16 }\n"
318 "define void @foo(%struct.rtx_def* %a, i8 %b, i32 %c) {\n"
319 " call void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)\n"
320 " ret void\n"
321 "}\n"
322 "declare void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def*, i8, i32, i32, i1)\n";
323
324 const char *BarStr =
325 "%struct.rtx_def = type { i16 }\n"
326 "define void @bar(%struct.rtx_def* %a, i8 %b, i32 %c) {\n"
327 " call void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)\n"
328 " ret void\n"
329 "}\n"
330 "declare void @llvm.memset.p0struct.rtx_def.i32(%struct.rtx_def*, i8, i32, i32, i1)\n";
331
332 std::unique_ptr Foo = parseAssemblyString(FooStr, Err, C);
333 assert(Foo);
334 ASSERT_TRUE(Foo.get());
335 // Foo is loaded first, so the type and the intrinsic have theis original
336 // names.
337 ASSERT_TRUE(Foo->getFunction("llvm.memset.p0struct.rtx_def.i32"));
338 ASSERT_FALSE(Foo->getFunction("llvm.memset.p0struct.rtx_def.0.i32"));
339
340 std::unique_ptr Bar = parseAssemblyString(BarStr, Err, C);
341 assert(Bar);
342 ASSERT_TRUE(Bar.get());
343 // Bar is loaded after Foo, so the type is renamed to struct.rtx_def.0. Check
344 // that the intrinsic is also renamed.
345 ASSERT_FALSE(Bar->getFunction("llvm.memset.p0struct.rtx_def.i32"));
346 ASSERT_TRUE(Bar->getFunction("llvm.memset.p0struct.rtx_def.0.i32"));
347
348 // Link two modules together.
349 auto Dst = llvm::make_unique("Linked", C);
350 ASSERT_TRUE(Dst.get());
351 Ctx.setDiagnosticHandler(expectNoDiags);
352 bool Failed = Linker::linkModules(*Foo, std::move(Bar));
353 ASSERT_FALSE(Failed);
354
355 // "struct.rtx_def" from Foo and "struct.rtx_def.0" from Bar are isomorphic
356 // types, so they must be uniquified by linker. Check that they use the same
357 // intrinsic definition.
358 Function *F = Foo->getFunction("llvm.memset.p0struct.rtx_def.i32");
359 ASSERT_EQ(F->getNumUses(), (unsigned)2);
360 }
361
308362 } // end anonymous namespace