blob: 05523c56cc2aa0295090733c4add08f2d0f5808a [file] [log] [blame]
Bill Wendling91686d62014-01-16 06:29:36 +00001//===- llvm/unittest/Linker/LinkModulesTest.cpp - IRBuilder tests ---------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Bill Wendling91686d62014-01-16 06:29:36 +00006//
7//===----------------------------------------------------------------------===//
8
Chandler Carruth9a67b072017-06-06 11:06:56 +00009#include "llvm-c/Core.h"
10#include "llvm-c/Linker.h"
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +000011#include "llvm/ADT/STLExtras.h"
Rafael Espindola5cb9c822014-11-17 20:51:01 +000012#include "llvm/AsmParser/Parser.h"
Bill Wendling91686d62014-01-16 06:29:36 +000013#include "llvm/IR/BasicBlock.h"
14#include "llvm/IR/DataLayout.h"
15#include "llvm/IR/Function.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "llvm/IR/IRBuilder.h"
Bill Wendling91686d62014-01-16 06:29:36 +000017#include "llvm/IR/Module.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000018#include "llvm/Linker/Linker.h"
Rafael Espindola5cb9c822014-11-17 20:51:01 +000019#include "llvm/Support/SourceMgr.h"
Bill Wendling91686d62014-01-16 06:29:36 +000020#include "gtest/gtest.h"
21
22using namespace llvm;
23
24namespace {
25
26class LinkModuleTest : public testing::Test {
27protected:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000028 void SetUp() override {
Bill Wendling91686d62014-01-16 06:29:36 +000029 M.reset(new Module("MyModule", Ctx));
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000030 FunctionType *FTy = FunctionType::get(
31 Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/);
Bill Wendling91686d62014-01-16 06:29:36 +000032 F = Function::Create(FTy, Function::ExternalLinkage, "ba_func", M.get());
33 F->setCallingConv(CallingConv::C);
34
35 EntryBB = BasicBlock::Create(Ctx, "entry", F);
36 SwitchCase1BB = BasicBlock::Create(Ctx, "switch.case.1", F);
37 SwitchCase2BB = BasicBlock::Create(Ctx, "switch.case.2", F);
38 ExitBB = BasicBlock::Create(Ctx, "exit", F);
39
David Blaikie156d46e2015-03-24 23:34:31 +000040 AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
Bill Wendling91686d62014-01-16 06:29:36 +000041
42 GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
Craig Topper66f09ad2014-06-08 22:29:17 +000043 GlobalValue::InternalLinkage, nullptr,"switch.bas");
Bill Wendling91686d62014-01-16 06:29:36 +000044
45 // Global Initializer
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000046 std::vector<Constant *> Init;
Bill Wendling91686d62014-01-16 06:29:36 +000047 Constant *SwitchCase1BA = BlockAddress::get(SwitchCase1BB);
48 Init.push_back(SwitchCase1BA);
49
50 Constant *SwitchCase2BA = BlockAddress::get(SwitchCase2BB);
51 Init.push_back(SwitchCase2BA);
52
53 ConstantInt *One = ConstantInt::get(Type::getInt32Ty(Ctx), 1);
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000054 Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One,
55 Type::getInt8PtrTy(Ctx));
Bill Wendling91686d62014-01-16 06:29:36 +000056 Init.push_back(OnePtr);
57
58 GV->setInitializer(ConstantArray::get(AT, Init));
59 }
60
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000061 void TearDown() override { M.reset(); }
Bill Wendling91686d62014-01-16 06:29:36 +000062
NAKAMURA Takumib49b99b2014-04-29 15:52:27 +000063 LLVMContext Ctx;
Ahmed Charles56440fd2014-03-06 05:51:42 +000064 std::unique_ptr<Module> M;
Bill Wendling91686d62014-01-16 06:29:36 +000065 Function *F;
David Blaikie156d46e2015-03-24 23:34:31 +000066 ArrayType *AT;
Bill Wendling91686d62014-01-16 06:29:36 +000067 GlobalVariable *GV;
68 BasicBlock *EntryBB;
69 BasicBlock *SwitchCase1BB;
70 BasicBlock *SwitchCase2BB;
71 BasicBlock *ExitBB;
72};
73
Rafael Espindola9d2bfc42015-12-14 23:17:03 +000074static void expectNoDiags(const DiagnosticInfo &DI, void *C) {
75 EXPECT_TRUE(false);
76}
Rafael Espindolaf49a38f2015-12-04 22:08:53 +000077
Bill Wendling91686d62014-01-16 06:29:36 +000078TEST_F(LinkModuleTest, BlockAddress) {
Bill Wendling91686d62014-01-16 06:29:36 +000079 IRBuilder<> Builder(EntryBB);
80
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000081 std::vector<Value *> GEPIndices;
Bill Wendling91686d62014-01-16 06:29:36 +000082 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
Duncan P. N. Exon Smithc8925b12015-10-20 18:30:20 +000083 GEPIndices.push_back(&*F->arg_begin());
Bill Wendling91686d62014-01-16 06:29:36 +000084
David Blaikie156d46e2015-03-24 23:34:31 +000085 Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep");
James Y Knight14359ef2019-02-01 20:44:24 +000086 Value *Load = Builder.CreateLoad(AT->getElementType(), GEP, "switch.load");
Bill Wendling91686d62014-01-16 06:29:36 +000087
88 Builder.CreateRet(Load);
89
90 Builder.SetInsertPoint(SwitchCase1BB);
91 Builder.CreateBr(ExitBB);
92
93 Builder.SetInsertPoint(SwitchCase2BB);
94 Builder.CreateBr(ExitBB);
95
96 Builder.SetInsertPoint(ExitBB);
97 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
98
NAKAMURA Takumib49b99b2014-04-29 15:52:27 +000099 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
Reid Kleckner1efbe2e2017-09-15 21:12:13 +0000100 Ctx.setDiagnosticHandlerCallBack(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000101 Linker::linkModules(*LinkedModule, std::move(M));
Bill Wendling91686d62014-01-16 06:29:36 +0000102
103 // Check that the global "@switch.bas" is well-formed.
104 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
105 const Constant *Init = LinkedGV->getInitializer();
106
107 // @switch.bas = internal global [3 x i8*]
108 // [i8* blockaddress(@ba_func, %switch.case.1),
109 // i8* blockaddress(@ba_func, %switch.case.2),
110 // i8* inttoptr (i32 1 to i8*)]
111
112 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
113 EXPECT_EQ(AT, Init->getType());
114
115 Value *Elem = Init->getOperand(0);
116 ASSERT_TRUE(isa<BlockAddress>(Elem));
117 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
118 LinkedModule->getFunction("ba_func"));
119 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
120 LinkedModule->getFunction("ba_func"));
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000121
Bill Wendling91686d62014-01-16 06:29:36 +0000122 Elem = Init->getOperand(1);
123 ASSERT_TRUE(isa<BlockAddress>(Elem));
124 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
125 LinkedModule->getFunction("ba_func"));
126 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
127 LinkedModule->getFunction("ba_func"));
128
129 delete LinkedModule;
130}
131
Eli Benderskyff715e22015-06-12 23:26:42 +0000132static Module *getExternal(LLVMContext &Ctx, StringRef FuncName) {
133 // Create a module with an empty externally-linked function
134 Module *M = new Module("ExternalModule", Ctx);
135 FunctionType *FTy = FunctionType::get(
136 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
137
138 Function *F =
139 Function::Create(FTy, Function::ExternalLinkage, FuncName, M);
140 F->setCallingConv(CallingConv::C);
141
142 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
143 IRBuilder<> Builder(BB);
144 Builder.CreateRetVoid();
145 return M;
146}
147
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000148static Module *getInternal(LLVMContext &Ctx) {
Bill Wendling91686d62014-01-16 06:29:36 +0000149 Module *InternalM = new Module("InternalModule", Ctx);
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000150 FunctionType *FTy = FunctionType::get(
151 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
Bill Wendling91686d62014-01-16 06:29:36 +0000152
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000153 Function *F =
154 Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
Bill Wendling91686d62014-01-16 06:29:36 +0000155 F->setCallingConv(CallingConv::C);
156
157 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
158 IRBuilder<> Builder(BB);
159 Builder.CreateRetVoid();
160
161 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
162
163 GlobalVariable *GV =
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000164 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
Craig Topper66f09ad2014-06-08 22:29:17 +0000165 GlobalValue::InternalLinkage, nullptr, "g");
Bill Wendling91686d62014-01-16 06:29:36 +0000166
167 GV->setInitializer(ConstantStruct::get(STy, F));
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000168 return InternalM;
169}
Bill Wendling91686d62014-01-16 06:29:36 +0000170
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000171TEST_F(LinkModuleTest, EmptyModule) {
172 std::unique_ptr<Module> InternalM(getInternal(Ctx));
173 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
Reid Kleckner1efbe2e2017-09-15 21:12:13 +0000174 Ctx.setDiagnosticHandlerCallBack(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000175 Linker::linkModules(*EmptyM, std::move(InternalM));
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000176}
Bill Wendling91686d62014-01-16 06:29:36 +0000177
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000178TEST_F(LinkModuleTest, EmptyModule2) {
179 std::unique_ptr<Module> InternalM(getInternal(Ctx));
180 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
Reid Kleckner1efbe2e2017-09-15 21:12:13 +0000181 Ctx.setDiagnosticHandlerCallBack(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000182 Linker::linkModules(*InternalM, std::move(EmptyM));
Bill Wendling91686d62014-01-16 06:29:36 +0000183}
184
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000185TEST_F(LinkModuleTest, TypeMerge) {
186 LLVMContext C;
187 SMDiagnostic Err;
188
189 const char *M1Str = "%t = type {i32}\n"
190 "@t1 = weak global %t zeroinitializer\n";
191 std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C);
192
193 const char *M2Str = "%t = type {i32}\n"
194 "@t2 = weak global %t zeroinitializer\n";
195 std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
196
Reid Kleckner1efbe2e2017-09-15 21:12:13 +0000197 Ctx.setDiagnosticHandlerCallBack(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000198 Linker::linkModules(*M1, std::move(M2));
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000199
200 EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
201 M1->getNamedGlobal("t2")->getType());
202}
203
Rafael Espindola434e9562015-12-16 23:16:33 +0000204TEST_F(LinkModuleTest, NewCAPISuccess) {
205 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
206 std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar"));
207 LLVMBool Result =
208 LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release()));
209 EXPECT_EQ(0, Result);
210 // "bar" is present in destination module
211 EXPECT_NE(nullptr, DestM->getFunction("bar"));
212}
213
214static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
215 auto *Err = reinterpret_cast<std::string *>(C);
216 char *CErr = LLVMGetDiagInfoDescription(DI);
217 *Err = CErr;
218 LLVMDisposeMessage(CErr);
219}
220
221TEST_F(LinkModuleTest, NewCAPIFailure) {
222 // Symbol clash between two modules
223 LLVMContext Ctx;
224 std::string Err;
225 LLVMContextSetDiagnosticHandler(wrap(&Ctx), diagnosticHandler, &Err);
226
227 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
228 std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo"));
229 LLVMBool Result =
230 LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release()));
231 EXPECT_EQ(1, Result);
232 EXPECT_EQ("Linking globals named 'foo': symbol multiply defined!", Err);
233}
234
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000235TEST_F(LinkModuleTest, MoveDistinctMDs) {
236 LLVMContext C;
237 SMDiagnostic Err;
238
239 const char *SrcStr = "define void @foo() !attach !0 {\n"
240 "entry:\n"
241 " call void @llvm.md(metadata !1)\n"
242 " ret void, !attach !2\n"
243 "}\n"
244 "declare void @llvm.md(metadata)\n"
245 "!named = !{!3, !4}\n"
246 "!0 = distinct !{}\n"
247 "!1 = distinct !{}\n"
248 "!2 = distinct !{}\n"
249 "!3 = distinct !{}\n"
250 "!4 = !{!3}\n";
251
252 std::unique_ptr<Module> Src = parseAssemblyString(SrcStr, Err, C);
253 assert(Src);
254 ASSERT_TRUE(Src.get());
255
256 // Get the addresses of the Metadata before merging.
257 Function *F = &*Src->begin();
258 ASSERT_EQ("foo", F->getName());
259 BasicBlock *BB = &F->getEntryBlock();
260 auto *CI = cast<CallInst>(&BB->front());
261 auto *RI = cast<ReturnInst>(BB->getTerminator());
262 NamedMDNode *NMD = &*Src->named_metadata_begin();
263
264 MDNode *M0 = F->getMetadata("attach");
265 MDNode *M1 =
266 cast<MDNode>(cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
267 MDNode *M2 = RI->getMetadata("attach");
268 MDNode *M3 = NMD->getOperand(0);
269 MDNode *M4 = NMD->getOperand(1);
270
271 // Confirm a few things about the IR.
272 EXPECT_TRUE(M0->isDistinct());
273 EXPECT_TRUE(M1->isDistinct());
274 EXPECT_TRUE(M2->isDistinct());
275 EXPECT_TRUE(M3->isDistinct());
276 EXPECT_TRUE(M4->isUniqued());
277 EXPECT_EQ(M3, M4->getOperand(0));
278
279 // Link into destination module.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000280 auto Dst = std::make_unique<Module>("Linked", C);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000281 ASSERT_TRUE(Dst.get());
Reid Kleckner1efbe2e2017-09-15 21:12:13 +0000282 Ctx.setDiagnosticHandlerCallBack(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000283 Linker::linkModules(*Dst, std::move(Src));
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000284
285 // Check that distinct metadata was moved, not cloned. Even !4, the uniqued
286 // node, should effectively be moved, since its only operand hasn't changed.
287 F = &*Dst->begin();
288 BB = &F->getEntryBlock();
289 CI = cast<CallInst>(&BB->front());
290 RI = cast<ReturnInst>(BB->getTerminator());
291 NMD = &*Dst->named_metadata_begin();
292
293 EXPECT_EQ(M0, F->getMetadata("attach"));
294 EXPECT_EQ(M1, cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
295 EXPECT_EQ(M2, RI->getMetadata("attach"));
296 EXPECT_EQ(M3, NMD->getOperand(0));
297 EXPECT_EQ(M4, NMD->getOperand(1));
298
299 // Confirm a few things about the IR. This shouldn't have changed.
300 EXPECT_TRUE(M0->isDistinct());
301 EXPECT_TRUE(M1->isDistinct());
302 EXPECT_TRUE(M2->isDistinct());
303 EXPECT_TRUE(M3->isDistinct());
304 EXPECT_TRUE(M4->isUniqued());
305 EXPECT_EQ(M3, M4->getOperand(0));
306}
307
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000308TEST_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"
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000319 " call void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)\n"
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000320 " ret void\n"
321 "}\n"
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000322 "declare void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def*, i8, i32, i32, i1)\n";
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000323
324 const char *BarStr =
325 "%struct.rtx_def = type { i16 }\n"
326 "define void @bar(%struct.rtx_def* %a, i8 %b, i32 %c) {\n"
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000327 " call void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def* %a, i8 %b, i32 %c, i32 4, i1 true)\n"
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000328 " ret void\n"
329 "}\n"
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000330 "declare void @llvm.memset.p0s_struct.rtx_defs.i32(%struct.rtx_def*, i8, i32, i32, i1)\n";
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000331
332 std::unique_ptr<Module> 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.
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000337 ASSERT_TRUE(Foo->getFunction("llvm.memset.p0s_struct.rtx_defs.i32"));
338 ASSERT_FALSE(Foo->getFunction("llvm.memset.p0s_struct.rtx_defs.0.i32"));
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000339
340 std::unique_ptr<Module> 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.
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000345 ASSERT_FALSE(Bar->getFunction("llvm.memset.p0s_struct.rtx_defs.i32"));
346 ASSERT_TRUE(Bar->getFunction("llvm.memset.p0s_struct.rtx_def.0s.i32"));
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000347
348 // Link two modules together.
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000349 auto Dst = std::make_unique<Module>("Linked", C);
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000350 ASSERT_TRUE(Dst.get());
Reid Kleckner1efbe2e2017-09-15 21:12:13 +0000351 Ctx.setDiagnosticHandlerCallBack(expectNoDiags);
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000352 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.
Daniel Berlin3c1432f2017-02-15 23:16:20 +0000358 Function *F = Foo->getFunction("llvm.memset.p0s_struct.rtx_defs.i32");
Artur Pilipenko6c7a8ab2016-06-24 15:10:29 +0000359 ASSERT_EQ(F->getNumUses(), (unsigned)2);
360}
361
Bill Wendling91686d62014-01-16 06:29:36 +0000362} // end anonymous namespace