blob: 322a44f8aafe5d03fd6d3d0fdd51a72465e783a4 [file] [log] [blame]
Bill Wendling91686d62014-01-16 06:29:36 +00001//===- 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
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +000010#include "llvm/ADT/STLExtras.h"
Rafael Espindola5cb9c822014-11-17 20:51:01 +000011#include "llvm/AsmParser/Parser.h"
Bill Wendling91686d62014-01-16 06:29:36 +000012#include "llvm/IR/BasicBlock.h"
13#include "llvm/IR/DataLayout.h"
14#include "llvm/IR/Function.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000015#include "llvm/IR/IRBuilder.h"
Bill Wendling91686d62014-01-16 06:29:36 +000016#include "llvm/IR/Module.h"
Chandler Carruthd9903882015-01-14 11:23:27 +000017#include "llvm/Linker/Linker.h"
Rafael Espindola5cb9c822014-11-17 20:51:01 +000018#include "llvm/Support/SourceMgr.h"
Eric Christophera6b96002015-12-18 01:46:52 +000019#include "llvm-c/Core.h"
Eli Benderskyff715e22015-06-12 23:26:42 +000020#include "llvm-c/Linker.h"
Bill Wendling91686d62014-01-16 06:29:36 +000021#include "gtest/gtest.h"
22
23using namespace llvm;
24
25namespace {
26
27class LinkModuleTest : public testing::Test {
28protected:
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000029 void SetUp() override {
Bill Wendling91686d62014-01-16 06:29:36 +000030 M.reset(new Module("MyModule", Ctx));
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000031 FunctionType *FTy = FunctionType::get(
32 Type::getInt8PtrTy(Ctx), Type::getInt32Ty(Ctx), false /*=isVarArg*/);
Bill Wendling91686d62014-01-16 06:29:36 +000033 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 Blaikie156d46e2015-03-24 23:34:31 +000041 AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
Bill Wendling91686d62014-01-16 06:29:36 +000042
43 GV = new GlobalVariable(*M.get(), AT, false /*=isConstant*/,
Craig Topper66f09ad2014-06-08 22:29:17 +000044 GlobalValue::InternalLinkage, nullptr,"switch.bas");
Bill Wendling91686d62014-01-16 06:29:36 +000045
46 // Global Initializer
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000047 std::vector<Constant *> Init;
Bill Wendling91686d62014-01-16 06:29:36 +000048 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 Takumi0f250ed2014-04-29 15:52:46 +000055 Constant *OnePtr = ConstantExpr::getCast(Instruction::IntToPtr, One,
56 Type::getInt8PtrTy(Ctx));
Bill Wendling91686d62014-01-16 06:29:36 +000057 Init.push_back(OnePtr);
58
59 GV->setInitializer(ConstantArray::get(AT, Init));
60 }
61
Alexander Kornienkof817c1c2015-04-11 02:11:45 +000062 void TearDown() override { M.reset(); }
Bill Wendling91686d62014-01-16 06:29:36 +000063
NAKAMURA Takumib49b99b2014-04-29 15:52:27 +000064 LLVMContext Ctx;
Ahmed Charles56440fd2014-03-06 05:51:42 +000065 std::unique_ptr<Module> M;
Bill Wendling91686d62014-01-16 06:29:36 +000066 Function *F;
David Blaikie156d46e2015-03-24 23:34:31 +000067 ArrayType *AT;
Bill Wendling91686d62014-01-16 06:29:36 +000068 GlobalVariable *GV;
69 BasicBlock *EntryBB;
70 BasicBlock *SwitchCase1BB;
71 BasicBlock *SwitchCase2BB;
72 BasicBlock *ExitBB;
73};
74
Rafael Espindola9d2bfc42015-12-14 23:17:03 +000075static void expectNoDiags(const DiagnosticInfo &DI, void *C) {
76 EXPECT_TRUE(false);
77}
Rafael Espindolaf49a38f2015-12-04 22:08:53 +000078
Bill Wendling91686d62014-01-16 06:29:36 +000079TEST_F(LinkModuleTest, BlockAddress) {
Bill Wendling91686d62014-01-16 06:29:36 +000080 IRBuilder<> Builder(EntryBB);
81
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000082 std::vector<Value *> GEPIndices;
Bill Wendling91686d62014-01-16 06:29:36 +000083 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
Duncan P. N. Exon Smithc8925b12015-10-20 18:30:20 +000084 GEPIndices.push_back(&*F->arg_begin());
Bill Wendling91686d62014-01-16 06:29:36 +000085
David Blaikie156d46e2015-03-24 23:34:31 +000086 Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep");
Bill Wendling91686d62014-01-16 06:29:36 +000087 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 Takumib49b99b2014-04-29 15:52:27 +0000100 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000101 Ctx.setDiagnosticHandler(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000102 Linker::linkModules(*LinkedModule, std::move(M));
Bill Wendling91686d62014-01-16 06:29:36 +0000103
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 Takumi0f250ed2014-04-29 15:52:46 +0000122
Bill Wendling91686d62014-01-16 06:29:36 +0000123 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 Benderskyff715e22015-06-12 23:26:42 +0000133static 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 Espindola9f8eff32014-10-28 00:24:16 +0000149static Module *getInternal(LLVMContext &Ctx) {
Bill Wendling91686d62014-01-16 06:29:36 +0000150 Module *InternalM = new Module("InternalModule", Ctx);
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000151 FunctionType *FTy = FunctionType::get(
152 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
Bill Wendling91686d62014-01-16 06:29:36 +0000153
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000154 Function *F =
155 Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
Bill Wendling91686d62014-01-16 06:29:36 +0000156 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 Takumi0f250ed2014-04-29 15:52:46 +0000165 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
Craig Topper66f09ad2014-06-08 22:29:17 +0000166 GlobalValue::InternalLinkage, nullptr, "g");
Bill Wendling91686d62014-01-16 06:29:36 +0000167
168 GV->setInitializer(ConstantStruct::get(STy, F));
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000169 return InternalM;
170}
Bill Wendling91686d62014-01-16 06:29:36 +0000171
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000172TEST_F(LinkModuleTest, EmptyModule) {
173 std::unique_ptr<Module> InternalM(getInternal(Ctx));
174 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000175 Ctx.setDiagnosticHandler(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000176 Linker::linkModules(*EmptyM, std::move(InternalM));
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000177}
Bill Wendling91686d62014-01-16 06:29:36 +0000178
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000179TEST_F(LinkModuleTest, EmptyModule2) {
180 std::unique_ptr<Module> InternalM(getInternal(Ctx));
181 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000182 Ctx.setDiagnosticHandler(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000183 Linker::linkModules(*InternalM, std::move(EmptyM));
Bill Wendling91686d62014-01-16 06:29:36 +0000184}
185
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000186TEST_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
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000198 Ctx.setDiagnosticHandler(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000199 Linker::linkModules(*M1, std::move(M2));
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000200
201 EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
202 M1->getNamedGlobal("t2")->getType());
203}
204
Eli Benderskyff715e22015-06-12 23:26:42 +0000205TEST_F(LinkModuleTest, CAPISuccess) {
206 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
207 std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar"));
208 char *errout = nullptr;
209 LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()),
210 LLVMLinkerDestroySource, &errout);
211 EXPECT_EQ(0, result);
212 EXPECT_EQ(nullptr, errout);
213 // "bar" is present in destination module
214 EXPECT_NE(nullptr, DestM->getFunction("bar"));
215}
216
217TEST_F(LinkModuleTest, CAPIFailure) {
218 // Symbol clash between two modules
219 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
220 std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo"));
221 char *errout = nullptr;
222 LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()),
223 LLVMLinkerDestroySource, &errout);
224 EXPECT_EQ(1, result);
225 EXPECT_STREQ("Linking globals named 'foo': symbol multiply defined!", errout);
Benjamin Kramerf1d570d2015-06-15 15:42:26 +0000226 LLVMDisposeMessage(errout);
Eli Benderskyff715e22015-06-12 23:26:42 +0000227}
228
Rafael Espindola434e9562015-12-16 23:16:33 +0000229TEST_F(LinkModuleTest, NewCAPISuccess) {
230 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
231 std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar"));
232 LLVMBool Result =
233 LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release()));
234 EXPECT_EQ(0, Result);
235 // "bar" is present in destination module
236 EXPECT_NE(nullptr, DestM->getFunction("bar"));
237}
238
239static void diagnosticHandler(LLVMDiagnosticInfoRef DI, void *C) {
240 auto *Err = reinterpret_cast<std::string *>(C);
241 char *CErr = LLVMGetDiagInfoDescription(DI);
242 *Err = CErr;
243 LLVMDisposeMessage(CErr);
244}
245
246TEST_F(LinkModuleTest, NewCAPIFailure) {
247 // Symbol clash between two modules
248 LLVMContext Ctx;
249 std::string Err;
250 LLVMContextSetDiagnosticHandler(wrap(&Ctx), diagnosticHandler, &Err);
251
252 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
253 std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo"));
254 LLVMBool Result =
255 LLVMLinkModules2(wrap(DestM.get()), wrap(SourceM.release()));
256 EXPECT_EQ(1, Result);
257 EXPECT_EQ("Linking globals named 'foo': symbol multiply defined!", Err);
258}
259
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000260TEST_F(LinkModuleTest, MoveDistinctMDs) {
261 LLVMContext C;
262 SMDiagnostic Err;
263
264 const char *SrcStr = "define void @foo() !attach !0 {\n"
265 "entry:\n"
266 " call void @llvm.md(metadata !1)\n"
267 " ret void, !attach !2\n"
268 "}\n"
269 "declare void @llvm.md(metadata)\n"
270 "!named = !{!3, !4}\n"
271 "!0 = distinct !{}\n"
272 "!1 = distinct !{}\n"
273 "!2 = distinct !{}\n"
274 "!3 = distinct !{}\n"
275 "!4 = !{!3}\n";
276
277 std::unique_ptr<Module> Src = parseAssemblyString(SrcStr, Err, C);
278 assert(Src);
279 ASSERT_TRUE(Src.get());
280
281 // Get the addresses of the Metadata before merging.
282 Function *F = &*Src->begin();
283 ASSERT_EQ("foo", F->getName());
284 BasicBlock *BB = &F->getEntryBlock();
285 auto *CI = cast<CallInst>(&BB->front());
286 auto *RI = cast<ReturnInst>(BB->getTerminator());
287 NamedMDNode *NMD = &*Src->named_metadata_begin();
288
289 MDNode *M0 = F->getMetadata("attach");
290 MDNode *M1 =
291 cast<MDNode>(cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
292 MDNode *M2 = RI->getMetadata("attach");
293 MDNode *M3 = NMD->getOperand(0);
294 MDNode *M4 = NMD->getOperand(1);
295
296 // Confirm a few things about the IR.
297 EXPECT_TRUE(M0->isDistinct());
298 EXPECT_TRUE(M1->isDistinct());
299 EXPECT_TRUE(M2->isDistinct());
300 EXPECT_TRUE(M3->isDistinct());
301 EXPECT_TRUE(M4->isUniqued());
302 EXPECT_EQ(M3, M4->getOperand(0));
303
304 // Link into destination module.
305 auto Dst = llvm::make_unique<Module>("Linked", C);
306 ASSERT_TRUE(Dst.get());
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000307 Ctx.setDiagnosticHandler(expectNoDiags);
Rafael Espindola434e9562015-12-16 23:16:33 +0000308 Linker::linkModules(*Dst, std::move(Src));
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000309
310 // Check that distinct metadata was moved, not cloned. Even !4, the uniqued
311 // node, should effectively be moved, since its only operand hasn't changed.
312 F = &*Dst->begin();
313 BB = &F->getEntryBlock();
314 CI = cast<CallInst>(&BB->front());
315 RI = cast<ReturnInst>(BB->getTerminator());
316 NMD = &*Dst->named_metadata_begin();
317
318 EXPECT_EQ(M0, F->getMetadata("attach"));
319 EXPECT_EQ(M1, cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
320 EXPECT_EQ(M2, RI->getMetadata("attach"));
321 EXPECT_EQ(M3, NMD->getOperand(0));
322 EXPECT_EQ(M4, NMD->getOperand(1));
323
324 // Confirm a few things about the IR. This shouldn't have changed.
325 EXPECT_TRUE(M0->isDistinct());
326 EXPECT_TRUE(M1->isDistinct());
327 EXPECT_TRUE(M2->isDistinct());
328 EXPECT_TRUE(M3->isDistinct());
329 EXPECT_TRUE(M4->isUniqued());
330 EXPECT_EQ(M3, M4->getOperand(0));
331}
332
Bill Wendling91686d62014-01-16 06:29:36 +0000333} // end anonymous namespace