llvm.org GIT mirror llvm / 99faa52
Revert r273568 "Remangle intrinsics names when types are renamed" It broke 2008-07-15-Bswap.ll and 2009-09-01-PostRAProlog.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273574 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 4 years ago
8 changed file(s) with 2 addition(s) and 163 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"
2120 #include
2221
2322 namespace llvm {
143142 bool matchIntrinsicType(Type *Ty, ArrayRef &Infos,
144143 SmallVectorImpl &ArgTys);
145144
146 // Checks if the intrinsic name matches with its signature and if not
147 // returns the declaration with the same signature and remangled name.
148 llvm::Optional remangleIntrinsicFunction(Function *F);
149
150145 } // End Intrinsic namespace
151146
152147 } // 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"
2120 #include "llvm/IR/Constants.h"
2221 #include "llvm/IR/DebugInfo.h"
2322 #include "llvm/IR/DebugInfoMetadata.h"
2423 #include "llvm/IR/DerivedTypes.h"
2524 #include "llvm/IR/InlineAsm.h"
2625 #include "llvm/IR/Instructions.h"
27 #include "llvm/IR/Intrinsics.h"
2826 #include "llvm/IR/LLVMContext.h"
2927 #include "llvm/IR/Module.h"
3028 #include "llvm/IR/Operator.h"
210208 // Look for intrinsic functions and CallInst that need to be upgraded
211209 for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; )
212210 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 }
224211
225212 UpgradeDebugInfo(*M);
226213
1414 #include "llvm/Bitcode/LLVMBitCodes.h"
1515 #include "llvm/Bitcode/ReaderWriter.h"
1616 #include "llvm/IR/AutoUpgrade.h"
17 #include "llvm/IR/CallSite.h"
1817 #include "llvm/IR/Constants.h"
1918 #include "llvm/IR/DebugInfo.h"
2019 #include "llvm/IR/DebugInfoMetadata.h"
230229
231230 // When intrinsic functions are encountered which require upgrading they are
232231 // stored here with their replacement function.
233 typedef DenseMap UpdatedIntrinsicMap;
234 UpdatedIntrinsicMap UpgradedIntrinsics;
235 // Intrinsics which were remangled because of types rename
236 UpdatedIntrinsicMap RemangledIntrinsics;
232 typedef DenseMap UpgradedIntrinsicMap;
233 UpgradedIntrinsicMap UpgradedIntrinsics;
237234
238235 // Map the bitcode's custom MDKind ID to the Module's MDKind ID.
239236 DenseMap MDKindMap;
34273424 Function *NewFn;
34283425 if (UpgradeIntrinsicFunction(&F, NewFn))
34293426 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();
34353427 }
34363428
34373429 // Look for global variables which need to be renamed.
56085600 }
56095601 }
56105602
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
56185603 // Finish fn->subprogram upgrade for materialized functions.
56195604 if (DISubprogram *SP = FunctionsWithSPs.lookup(F))
56205605 F->setSubprogram(SP);
56685653 I.first->eraseFromParent();
56695654 }
56705655 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();
56775656
56785657 UpgradeDebugInfo(*TheModule);
56795658
10591059 llvm_unreachable("unhandled");
10601060 }
10611061
1062 Optional Intrinsic::remangleIntrinsicFunction(Function *F) {
1063 Intrinsic::ID ID = F->getIntrinsicID();
1064 if (!ID)
1065 return None;
1066
1067 // Accumulate an array of overloaded types for the given intrinsic
1068 SmallVector ArgTys;
1069 {
1070 SmallVector Table;
1071 getIntrinsicInfoTableEntries(ID, Table);
1072 ArrayRef TableRef = Table;
1073 FunctionType *FTy = F->getFunctionType();
1074
1075 // If we encounter any problems matching the signature with the descriptor
1076 // just give up remangling. It's up to verifier to report the discrepancy.
1077 if (Intrinsic::matchIntrinsicType(FTy->getReturnType(), TableRef, ArgTys))
1078 return None;
1079 for (auto Ty : FTy->params())
1080 if (Intrinsic::matchIntrinsicType(Ty, TableRef, ArgTys))
1081 return None;
1082 }
1083
1084 StringRef Name = F->getName();
1085 if (Name == Intrinsic::getName(ID, ArgTys))
1086 return None;
1087
1088 auto NewDecl = Intrinsic::getDeclaration(F->getParent(), ID, ArgTys);
1089 NewDecl->setCallingConv(F->getCallingConv());
1090 return NewDecl;
1091 }
1092
10931062 /// hasAddressTaken - returns true if there are any uses of this function
10941063 /// other than direct calls or invokes to it.
10951064 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"
1918 #include "llvm/IR/TypeFinder.h"
2019 #include "llvm/Support/Error.h"
2120 #include "llvm/Transforms/Utils/Cloning.h"
901900 if (ShouldLink || !ForAlias)
902901 forceRenaming(NewGV, SGV->getName());
903902 }
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
912903 if (ShouldLink || ForAlias) {
913904 if (const Comdat *SC = SGV->getComdat()) {
914905 if (auto *GO = dyn_cast(NewGV)) {
+0
-8
test/LTO/X86/Inputs/remangle_intrinsics.ll less more
None %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
-20
test/LTO/X86/remangle_intrinsics.ll less more
None ; 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)
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
362308 } // end anonymous namespace