Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 1 | //===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder tests ---------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Chandler Carruth | 3c0d607 | 2017-06-06 11:06:56 +0000 | [diff] [blame] | 10 | #include "llvm-c/Core.h" |
| 11 | #include "llvm-c/Linker.h" |
Duncan P. N. Exon Smith | 268a755 | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/STLExtras.h" |
Rafael Espindola | c4fe4e9 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 13 | #include "llvm/AsmParser/Parser.h" |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 14 | #include "llvm/IR/BasicBlock.h" |
| 15 | #include "llvm/IR/DataLayout.h" |
| 16 | #include "llvm/IR/Function.h" |
Chandler Carruth | 1decd56 | 2014-03-04 10:07:28 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IRBuilder.h" |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Chandler Carruth | 1b27914 | 2015-01-14 11:23:27 +0000 | [diff] [blame] | 19 | #include "llvm/Linker/Linker.h" |
Rafael Espindola | c4fe4e9 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 20 | #include "llvm/Support/SourceMgr.h" |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 21 | #include "gtest/gtest.h" |
| 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class LinkModuleTest : public testing::Test { |
| 28 | protected: |
Alexander Kornienko | c16fc54 | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 29 | void SetUp() override { |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 30 | M.reset(new Module("MyModule", Ctx)); |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 31 | FunctionType *FTy = FunctionType::get( |
| 32 | Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 33 | F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get()); |
| 34 | F->setCallingConv(CallingConv::C); |
| 35 | |
| 36 | EntryBB = BasicBlock::Create(Ctx, "entry", F); |
| 37 | SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F); |
| 38 | SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F); |
| 39 | ExitBB = BasicBlock::Create(Ctx, "exit", F); |
| 40 | |
David Blaikie | 4e933df | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 41 | AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 42 | |
| 43 | GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 44 | GlobalValue::InternalLinkage, nullptr,"switch.bas"); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 45 | |
| 46 | // Global Initializer |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 47 | std::vector<Constant *> Init; |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 48 | Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB); |
| 49 | Init.push_back(SwitchCase1BA); |
| 50 | |
| 51 | Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB); |
| 52 | Init.push_back(SwitchCase2BA); |
| 53 | |
| 54 | ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1); |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 55 | Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One, |
| 56 | Type::getInt8PtrTy(Ctx)); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 57 | Init.push_back(OnePtr); |
| 58 | |
| 59 | GV->setInitializer(ConstantArray::get(AT, Init)); |
| 60 | } |
| 61 | |
Alexander Kornienko | c16fc54 | 2015-04-11 02:11:45 +0000 | [diff] [blame] | 62 | void TearDown() override { M.reset(); } |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 63 | |
NAKAMURA Takumi | 13085ed | 2014-04-29 15:52:27 +0000 | [diff] [blame] | 64 | LLVMContext Ctx; |
Ahmed Charles | f4ccd11 | 2014-03-06 05:51:42 +0000 | [diff] [blame] | 65 | std::unique_ptr<Module> M; |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 66 | Function *F; |
David Blaikie | 4e933df | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 67 | ArrayType *AT; |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 68 | GlobalVariable *GV; |
| 69 | BasicBlock *EntryBB; |
| 70 | BasicBlock *SwitchCase1BB; |
| 71 | BasicBlock *SwitchCase2BB; |
| 72 | BasicBlock *ExitBB; |
| 73 | }; |
| 74 | |
Rafael Espindola | 7a1fc2d | 2015-12-14 23:17:03 +0000 | [diff] [blame] | 75 | static void expectNoDiags(const DiagnosticInfo &DI, void *C) { |
| 76 | EXPECT_TRUE(false); |
| 77 | } |
Rafael Espindola | c55f4fb | 2015-12-04 22:08:53 +0000 | [diff] [blame] | 78 | |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 79 | TEST_F(LinkModuleTest, BlockAddress) { |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 80 | IRBuilder<> Builder(EntryBB); |
| 81 | |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 82 | std::vector<Value *> GEPIndices; |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 83 | GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0)); |
Duncan P. N. Exon Smith | 70f8c20 | 2015-10-20 18:30:20 +0000 | [diff] [blame] | 84 | GEPIndices.push_back(&*F->arg_begin()); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 85 | |
David Blaikie | 4e933df | 2015-03-24 23:34:31 +0000 | [diff] [blame] | 86 | Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep"); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 87 | Value *Load = Builder.CreateLoad(GEP, "switch.load"); |
| 88 | |
| 89 | Builder.CreateRet(Load); |
| 90 | |
| 91 | Builder.SetInsertPoint(SwitchCase1BB); |
| 92 | Builder.CreateBr(ExitBB); |
| 93 | |
| 94 | Builder.SetInsertPoint(SwitchCase2BB); |
| 95 | Builder.CreateBr(ExitBB); |
| 96 | |
| 97 | Builder.SetInsertPoint(ExitBB); |
| 98 | Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx))); |
| 99 | |
NAKAMURA Takumi | 13085ed | 2014-04-29 15:52:27 +0000 | [diff] [blame] | 100 | Module *LinkedModule = new Module("MyModuleLinked", Ctx); |
Reid Kleckner | 574c3a1 | 2017-09-15 21:12:13 +0000 | [diff] [blame] | 101 | Ctx.setDiagnosticHandlerCallBack(expectNoDiags); |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 102 | Linker::linkModules(*LinkedModule, std::move(M)); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 103 | |
| 104 | // Check that the global "@switch.bas" is well-formed. |
| 105 | const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas"); |
| 106 | const Constant *Init = LinkedGV->getInitializer(); |
| 107 | |
| 108 | // @switch.bas = internal global [3 x i8*] |
| 109 | // [i8* blockaddress(@ba_func, %switch.case.1), |
| 110 | // i8* blockaddress(@ba_func, %switch.case.2), |
| 111 | // i8* inttoptr (i32 1 to i8*)] |
| 112 | |
| 113 | ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3); |
| 114 | EXPECT_EQ(AT, Init->getType()); |
| 115 | |
| 116 | Value *Elem = Init->getOperand(0); |
| 117 | ASSERT_TRUE(isa<BlockAddress>(Elem)); |
| 118 | EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(), |
| 119 | LinkedModule->getFunction("ba_func")); |
| 120 | EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(), |
| 121 | LinkedModule->getFunction("ba_func")); |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 122 | |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 123 | Elem = Init->getOperand(1); |
| 124 | ASSERT_TRUE(isa<BlockAddress>(Elem)); |
| 125 | EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(), |
| 126 | LinkedModule->getFunction("ba_func")); |
| 127 | EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(), |
| 128 | LinkedModule->getFunction("ba_func")); |
| 129 | |
| 130 | delete LinkedModule; |
| 131 | } |
| 132 | |
Eli Bendersky | 21a987d | 2015-06-12 23:26:42 +0000 | [diff] [blame] | 133 | static Module *getExternal(LLVMContext &Ctx, StringRef FuncName) { |
| 134 | // Create a module with an empty externally-linked function |
| 135 | Module *M = new Module("ExternalModule", Ctx); |
| 136 | FunctionType *FTy = FunctionType::get( |
| 137 | Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/); |
| 138 | |
| 139 | Function *F = |
| 140 | Function::Create(FTy, Function::ExternalLinkage, FuncName, M); |
| 141 | F->setCallingConv(CallingConv::C); |
| 142 | |
| 143 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 144 | IRBuilder<> Builder(BB); |
| 145 | Builder.CreateRetVoid(); |
| 146 | return M; |
| 147 | } |
| 148 | |
Rafael Espindola | c35c39b | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 149 | static Module *getInternal(LLVMContext &Ctx) { |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 150 | Module *InternalM = new Module("InternalModule", Ctx); |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 151 | FunctionType *FTy = FunctionType::get( |
| 152 | Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 153 | |
Rafael Espindola | c35c39b | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 154 | Function *F = |
| 155 | Function::Create(FTy, Function::InternalLinkage, "bar", InternalM); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 156 | F->setCallingConv(CallingConv::C); |
| 157 | |
| 158 | BasicBlock *BB = BasicBlock::Create(Ctx, "", F); |
| 159 | IRBuilder<> Builder(BB); |
| 160 | Builder.CreateRetVoid(); |
| 161 | |
| 162 | StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0)); |
| 163 | |
| 164 | GlobalVariable *GV = |
NAKAMURA Takumi | ec96ef30 | 2014-04-29 15:52:46 +0000 | [diff] [blame] | 165 | new GlobalVariable(*InternalM, STy, false /*=isConstant*/, |
Craig Topper | b177041 | 2014-06-08 22:29:17 +0000 | [diff] [blame] | 166 | GlobalValue::InternalLinkage, nullptr, "g"); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 167 | |
| 168 | GV->setInitializer(ConstantStruct::get(STy, F)); |
Rafael Espindola | c35c39b | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 169 | return InternalM; |
| 170 | } |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 171 | |
Rafael Espindola | c35c39b | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 172 | TEST_F(LinkModuleTest, EmptyModule) { |
| 173 | std::unique_ptr<Module> InternalM(getInternal(Ctx)); |
| 174 | std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx)); |
Reid Kleckner | 574c3a1 | 2017-09-15 21:12:13 +0000 | [diff] [blame] | 175 | Ctx.setDiagnosticHandlerCallBack(expectNoDiags); |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 176 | Linker::linkModules(*EmptyM, std::move(InternalM)); |
Rafael Espindola | c35c39b | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 177 | } |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 178 | |
Rafael Espindola | c35c39b | 2014-10-28 00:24:16 +0000 | [diff] [blame] | 179 | TEST_F(LinkModuleTest, EmptyModule2) { |
| 180 | std::unique_ptr<Module> InternalM(getInternal(Ctx)); |
| 181 | std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx)); |
Reid Kleckner | 574c3a1 | 2017-09-15 21:12:13 +0000 | [diff] [blame] | 182 | Ctx.setDiagnosticHandlerCallBack(expectNoDiags); |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 183 | Linker::linkModules(*InternalM, std::move(EmptyM)); |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Rafael Espindola | c4fe4e9 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 186 | TEST_F(LinkModuleTest, TypeMerge) { |
| 187 | LLVMContext C; |
| 188 | SMDiagnostic Err; |
| 189 | |
| 190 | const char *M1Str = "%t = type {i32}\n" |
| 191 | "@t1 = weak global %t zeroinitializer\n"; |
| 192 | std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C); |
| 193 | |
| 194 | const char *M2Str = "%t = type {i32}\n" |
| 195 | "@t2 = weak global %t zeroinitializer\n"; |
| 196 | std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C); |
| 197 | |
Reid Kleckner | 574c3a1 | 2017-09-15 21:12:13 +0000 | [diff] [blame] | 198 | Ctx.setDiagnosticHandlerCallBack(expectNoDiags); |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 199 | Linker::linkModules(*M1, std::move(M2)); |
Rafael Espindola | c4fe4e9 | 2014-11-17 20:51:01 +0000 | [diff] [blame] | 200 | |
| 201 | EXPECT_EQ(M1->getNamedGlobal("t1")->getType(), |
| 202 | M1->getNamedGlobal("t2")->getType()); |
| 203 | } |
| 204 | |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 205 | TEST_F(LinkModuleTest, NewCAPISuccess) { |
| 206 | std::unique_ptr<Module> DestM(getExternal(Ctx, "foo")); |
| 207 | std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar")); |
| 208 | LLVMBool Result = |
| 209 | LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release())); |
| 210 | EXPECT_EQ(0, Result); |
| 211 | // "bar" is present in destination module |
| 212 | EXPECT_NE(nullptr, DestM->getFunction("bar")); |
| 213 | } |
| 214 | |
| 215 | static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) { |
| 216 | auto *Err = reinterpret_cast<std::string *>(C); |
| 217 | char *CErr = LLVMGetDiagInfoDescription(DI); |
| 218 | *Err = CErr; |
| 219 | LLVMDisposeMessage(CErr); |
| 220 | } |
| 221 | |
| 222 | TEST_F(LinkModuleTest, NewCAPIFailure) { |
| 223 | // Symbol clash between two modules |
| 224 | LLVMContext Ctx; |
| 225 | std::string Err; |
| 226 | LLVMContextSetDiagnosticHandler(wrap(&Ctx), diagnosticHandler, &Err); |
| 227 | |
| 228 | std::unique_ptr<Module> DestM(getExternal(Ctx, "foo")); |
| 229 | std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo")); |
| 230 | LLVMBool Result = |
| 231 | LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release())); |
| 232 | EXPECT_EQ(1, Result); |
| 233 | EXPECT_EQ("Linking globals named 'foo': symbol multiply defined!", Err); |
| 234 | } |
| 235 | |
Duncan P. N. Exon Smith | 268a755 | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 236 | TEST_F(LinkModuleTest, MoveDistinctMDs) { |
| 237 | LLVMContext C; |
| 238 | SMDiagnostic Err; |
| 239 | |
| 240 | const char *SrcStr = "define void @foo() !attach !0 {\n" |
| 241 | "entry:\n" |
| 242 | " call void @llvm.md(metadata !1)\n" |
| 243 | " ret void, !attach !2\n" |
| 244 | "}\n" |
| 245 | "declare void @llvm.md(metadata)\n" |
| 246 | "!named = !{!3, !4}\n" |
| 247 | "!0 = distinct !{}\n" |
| 248 | "!1 = distinct !{}\n" |
| 249 | "!2 = distinct !{}\n" |
| 250 | "!3 = distinct !{}\n" |
| 251 | "!4 = !{!3}\n"; |
| 252 | |
| 253 | std::unique_ptr<Module> Src = parseAssemblyString(SrcStr, Err, C); |
| 254 | assert(Src); |
| 255 | ASSERT_TRUE(Src.get()); |
| 256 | |
| 257 | // Get the addresses of the Metadata before merging. |
| 258 | Function *F = &*Src->begin(); |
| 259 | ASSERT_EQ("foo", F->getName()); |
| 260 | BasicBlock *BB = &F->getEntryBlock(); |
| 261 | auto *CI = cast<CallInst>(&BB->front()); |
| 262 | auto *RI = cast<ReturnInst>(BB->getTerminator()); |
| 263 | NamedMDNode *NMD = &*Src->named_metadata_begin(); |
| 264 | |
| 265 | MDNode *M0 = F->getMetadata("attach"); |
| 266 | MDNode *M1 = |
| 267 | cast<MDNode>(cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata()); |
| 268 | MDNode *M2 = RI->getMetadata("attach"); |
| 269 | MDNode *M3 = NMD->getOperand(0); |
| 270 | MDNode *M4 = NMD->getOperand(1); |
| 271 | |
| 272 | // Confirm a few things about the IR. |
| 273 | EXPECT_TRUE(M0->isDistinct()); |
| 274 | EXPECT_TRUE(M1->isDistinct()); |
| 275 | EXPECT_TRUE(M2->isDistinct()); |
| 276 | EXPECT_TRUE(M3->isDistinct()); |
| 277 | EXPECT_TRUE(M4->isUniqued()); |
| 278 | EXPECT_EQ(M3, M4->getOperand(0)); |
| 279 | |
| 280 | // Link into destination module. |
| 281 | auto Dst = llvm::make_unique<Module>("Linked", C); |
| 282 | ASSERT_TRUE(Dst.get()); |
Reid Kleckner | 574c3a1 | 2017-09-15 21:12:13 +0000 | [diff] [blame] | 283 | Ctx.setDiagnosticHandlerCallBack(expectNoDiags); |
Rafael Espindola | d912be9 | 2015-12-16 23:16:33 +0000 | [diff] [blame] | 284 | Linker::linkModules(*Dst, std::move(Src)); |
Duncan P. N. Exon Smith | 268a755 | 2015-08-03 17:09:38 +0000 | [diff] [blame] | 285 | |
| 286 | // Check that distinct metadata was moved, not cloned. Even !4, the uniqued |
| 287 | // node, should effectively be moved, since its only operand hasn't changed. |
| 288 | F = &*Dst->begin(); |
| 289 | BB = &F->getEntryBlock(); |
| 290 | CI = cast<CallInst>(&BB->front()); |
| 291 | RI = cast<ReturnInst>(BB->getTerminator()); |
| 292 | NMD = &*Dst->named_metadata_begin(); |
| 293 | |
| 294 | EXPECT_EQ(M0, F->getMetadata("attach")); |
| 295 | EXPECT_EQ(M1, cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata()); |
| 296 | EXPECT_EQ(M2, RI->getMetadata("attach")); |
| 297 | EXPECT_EQ(M3, NMD->getOperand(0)); |
| 298 | EXPECT_EQ(M4, NMD->getOperand(1)); |
| 299 | |
| 300 | // Confirm a few things about the IR. This shouldn't have changed. |
| 301 | EXPECT_TRUE(M0->isDistinct()); |
| 302 | EXPECT_TRUE(M1->isDistinct()); |
| 303 | EXPECT_TRUE(M2->isDistinct()); |
| 304 | EXPECT_TRUE(M3->isDistinct()); |
| 305 | EXPECT_TRUE(M4->isUniqued()); |
| 306 | EXPECT_EQ(M3, M4->getOperand(0)); |
| 307 | } |
| 308 | |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 309 | TEST_F(LinkModuleTest, RemangleIntrinsics) { |
| 310 | LLVMContext C; |
| 311 | SMDiagnostic Err; |
| 312 | |
| 313 | // We load two modules inside the same context C. In both modules there is a |
| 314 | // "struct.rtx_def" type. In the module loaded the second (Bar) this type will |
| 315 | // be renamed to "struct.rtx_def.0". Check that the intrinsics which have this |
| 316 | // type in the signature are properly remangled. |
| 317 | const char *FooStr = |
| 318 | "%struct.rtx_def = type { i16 }\n" |
| 319 | "define void @foo(%struct.rtx_def* %a, i8 %b, i32 %c) {\n" |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 320 | " call void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)\n" |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 321 | " ret void\n" |
| 322 | "}\n" |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 323 | "declare void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def*, i8, i32, i32, i1)\n"; |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 324 | |
| 325 | const char *BarStr = |
| 326 | "%struct.rtx_def = type { i16 }\n" |
| 327 | "define void @bar(%struct.rtx_def* %a, i8 %b, i32 %c) {\n" |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 328 | " call void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)\n" |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 329 | " ret void\n" |
| 330 | "}\n" |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 331 | "declare void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def*, i8, i32, i32, i1)\n"; |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 332 | |
| 333 | std::unique_ptr<Module> Foo = parseAssemblyString(FooStr, Err, C); |
| 334 | assert(Foo); |
| 335 | ASSERT_TRUE(Foo.get()); |
| 336 | // Foo is loaded first, so the type and the intrinsic have theis original |
| 337 | // names. |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 338 | ASSERT_TRUE(Foo->getFunction("llvm.memset.p0s_struct.rtx_defs.i32")); |
| 339 | ASSERT_FALSE(Foo->getFunction("llvm.memset.p0s_struct.rtx_defs.0.i32")); |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 340 | |
| 341 | std::unique_ptr<Module> Bar = parseAssemblyString(BarStr, Err, C); |
| 342 | assert(Bar); |
| 343 | ASSERT_TRUE(Bar.get()); |
| 344 | // Bar is loaded after Foo, so the type is renamed to struct.rtx_def.0. Check |
| 345 | // that the intrinsic is also renamed. |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 346 | ASSERT_FALSE(Bar->getFunction("llvm.memset.p0s_struct.rtx_defs.i32")); |
| 347 | ASSERT_TRUE(Bar->getFunction("llvm.memset.p0s_struct.rtx_def.0s.i32")); |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 348 | |
| 349 | // Link two modules together. |
| 350 | auto Dst = llvm::make_unique<Module>("Linked", C); |
| 351 | ASSERT_TRUE(Dst.get()); |
Reid Kleckner | 574c3a1 | 2017-09-15 21:12:13 +0000 | [diff] [blame] | 352 | Ctx.setDiagnosticHandlerCallBack(expectNoDiags); |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 353 | bool Failed = Linker::linkModules(*Foo, std::move(Bar)); |
| 354 | ASSERT_FALSE(Failed); |
| 355 | |
| 356 | // "struct.rtx_def" from Foo and "struct.rtx_def.0" from Bar are isomorphic |
| 357 | // types, so they must be uniquified by linker. Check that they use the same |
| 358 | // intrinsic definition. |
Daniel Berlin | d17bca9 | 2017-02-15 23:16:20 +0000 | [diff] [blame] | 359 | Function *F = Foo->getFunction("llvm.memset.p0s_struct.rtx_defs.i32"); |
Artur Pilipenko | 140d9e6 | 2016-06-24 15:10:29 +0000 | [diff] [blame] | 360 | ASSERT_EQ(F->getNumUses(), (unsigned)2); |
| 361 | } |
| 362 | |
Bill Wendling | a950ad4 | 2014-01-16 06:29:36 +0000 | [diff] [blame] | 363 | } // end anonymous namespace |