blob: ba8f4798172f25d893fc3cc1743a2f3b5a1e1cad [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"
Eli Benderskyff715e22015-06-12 23:26:42 +000019#include "llvm-c/Linker.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");
Bill Wendling91686d62014-01-16 06:29:36 +000086 Value *Load = Builder.CreateLoad(GEP, "switch.load");
87
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);
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000100 Ctx.setDiagnosticHandler(expectNoDiags);
101 Linker::linkModules(*LinkedModule, *M);
Bill Wendling91686d62014-01-16 06:29:36 +0000102
103 // Delete the original module.
104 M.reset();
105
106 // Check that the global "@switch.bas" is well-formed.
107 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
108 const Constant *Init = LinkedGV->getInitializer();
109
110 // @switch.bas = internal global [3 x i8*]
111 // [i8* blockaddress(@ba_func, %switch.case.1),
112 // i8* blockaddress(@ba_func, %switch.case.2),
113 // i8* inttoptr (i32 1 to i8*)]
114
115 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
116 EXPECT_EQ(AT, Init->getType());
117
118 Value *Elem = Init->getOperand(0);
119 ASSERT_TRUE(isa<BlockAddress>(Elem));
120 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
121 LinkedModule->getFunction("ba_func"));
122 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
123 LinkedModule->getFunction("ba_func"));
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000124
Bill Wendling91686d62014-01-16 06:29:36 +0000125 Elem = Init->getOperand(1);
126 ASSERT_TRUE(isa<BlockAddress>(Elem));
127 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
128 LinkedModule->getFunction("ba_func"));
129 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
130 LinkedModule->getFunction("ba_func"));
131
132 delete LinkedModule;
133}
134
Eli Benderskyff715e22015-06-12 23:26:42 +0000135static Module *getExternal(LLVMContext &Ctx, StringRef FuncName) {
136 // Create a module with an empty externally-linked function
137 Module *M = new Module("ExternalModule", Ctx);
138 FunctionType *FTy = FunctionType::get(
139 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
140
141 Function *F =
142 Function::Create(FTy, Function::ExternalLinkage, FuncName, M);
143 F->setCallingConv(CallingConv::C);
144
145 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
146 IRBuilder<> Builder(BB);
147 Builder.CreateRetVoid();
148 return M;
149}
150
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000151static Module *getInternal(LLVMContext &Ctx) {
Bill Wendling91686d62014-01-16 06:29:36 +0000152 Module *InternalM = new Module("InternalModule", Ctx);
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000153 FunctionType *FTy = FunctionType::get(
154 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
Bill Wendling91686d62014-01-16 06:29:36 +0000155
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000156 Function *F =
157 Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
Bill Wendling91686d62014-01-16 06:29:36 +0000158 F->setCallingConv(CallingConv::C);
159
160 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
161 IRBuilder<> Builder(BB);
162 Builder.CreateRetVoid();
163
164 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
165
166 GlobalVariable *GV =
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000167 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
Craig Topper66f09ad2014-06-08 22:29:17 +0000168 GlobalValue::InternalLinkage, nullptr, "g");
Bill Wendling91686d62014-01-16 06:29:36 +0000169
170 GV->setInitializer(ConstantStruct::get(STy, F));
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000171 return InternalM;
172}
Bill Wendling91686d62014-01-16 06:29:36 +0000173
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000174TEST_F(LinkModuleTest, EmptyModule) {
175 std::unique_ptr<Module> InternalM(getInternal(Ctx));
176 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000177 Ctx.setDiagnosticHandler(expectNoDiags);
178 Linker::linkModules(*EmptyM, *InternalM);
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000179}
Bill Wendling91686d62014-01-16 06:29:36 +0000180
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000181TEST_F(LinkModuleTest, EmptyModule2) {
182 std::unique_ptr<Module> InternalM(getInternal(Ctx));
183 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000184 Ctx.setDiagnosticHandler(expectNoDiags);
185 Linker::linkModules(*InternalM, *EmptyM);
Bill Wendling91686d62014-01-16 06:29:36 +0000186}
187
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000188TEST_F(LinkModuleTest, TypeMerge) {
189 LLVMContext C;
190 SMDiagnostic Err;
191
192 const char *M1Str = "%t = type {i32}\n"
193 "@t1 = weak global %t zeroinitializer\n";
194 std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C);
195
196 const char *M2Str = "%t = type {i32}\n"
197 "@t2 = weak global %t zeroinitializer\n";
198 std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
199
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000200 Ctx.setDiagnosticHandler(expectNoDiags);
201 Linker::linkModules(*M1, *M2);
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000202
203 EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
204 M1->getNamedGlobal("t2")->getType());
205}
206
Eli Benderskyff715e22015-06-12 23:26:42 +0000207TEST_F(LinkModuleTest, CAPISuccess) {
208 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
209 std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar"));
210 char *errout = nullptr;
211 LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()),
212 LLVMLinkerDestroySource, &errout);
213 EXPECT_EQ(0, result);
214 EXPECT_EQ(nullptr, errout);
215 // "bar" is present in destination module
216 EXPECT_NE(nullptr, DestM->getFunction("bar"));
217}
218
219TEST_F(LinkModuleTest, CAPIFailure) {
220 // Symbol clash between two modules
221 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
222 std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo"));
223 char *errout = nullptr;
224 LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()),
225 LLVMLinkerDestroySource, &errout);
226 EXPECT_EQ(1, result);
227 EXPECT_STREQ("Linking globals named 'foo': symbol multiply defined!", errout);
Benjamin Kramerf1d570d2015-06-15 15:42:26 +0000228 LLVMDisposeMessage(errout);
Eli Benderskyff715e22015-06-12 23:26:42 +0000229}
230
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000231TEST_F(LinkModuleTest, MoveDistinctMDs) {
232 LLVMContext C;
233 SMDiagnostic Err;
234
235 const char *SrcStr = "define void @foo() !attach !0 {\n"
236 "entry:\n"
237 " call void @llvm.md(metadata !1)\n"
238 " ret void, !attach !2\n"
239 "}\n"
240 "declare void @llvm.md(metadata)\n"
241 "!named = !{!3, !4}\n"
242 "!0 = distinct !{}\n"
243 "!1 = distinct !{}\n"
244 "!2 = distinct !{}\n"
245 "!3 = distinct !{}\n"
246 "!4 = !{!3}\n";
247
248 std::unique_ptr<Module> Src = parseAssemblyString(SrcStr, Err, C);
249 assert(Src);
250 ASSERT_TRUE(Src.get());
251
252 // Get the addresses of the Metadata before merging.
253 Function *F = &*Src->begin();
254 ASSERT_EQ("foo", F->getName());
255 BasicBlock *BB = &F->getEntryBlock();
256 auto *CI = cast<CallInst>(&BB->front());
257 auto *RI = cast<ReturnInst>(BB->getTerminator());
258 NamedMDNode *NMD = &*Src->named_metadata_begin();
259
260 MDNode *M0 = F->getMetadata("attach");
261 MDNode *M1 =
262 cast<MDNode>(cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
263 MDNode *M2 = RI->getMetadata("attach");
264 MDNode *M3 = NMD->getOperand(0);
265 MDNode *M4 = NMD->getOperand(1);
266
267 // Confirm a few things about the IR.
268 EXPECT_TRUE(M0->isDistinct());
269 EXPECT_TRUE(M1->isDistinct());
270 EXPECT_TRUE(M2->isDistinct());
271 EXPECT_TRUE(M3->isDistinct());
272 EXPECT_TRUE(M4->isUniqued());
273 EXPECT_EQ(M3, M4->getOperand(0));
274
275 // Link into destination module.
276 auto Dst = llvm::make_unique<Module>("Linked", C);
277 ASSERT_TRUE(Dst.get());
Rafael Espindola9d2bfc42015-12-14 23:17:03 +0000278 Ctx.setDiagnosticHandler(expectNoDiags);
279 Linker::linkModules(*Dst, *Src);
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000280
281 // Check that distinct metadata was moved, not cloned. Even !4, the uniqued
282 // node, should effectively be moved, since its only operand hasn't changed.
283 F = &*Dst->begin();
284 BB = &F->getEntryBlock();
285 CI = cast<CallInst>(&BB->front());
286 RI = cast<ReturnInst>(BB->getTerminator());
287 NMD = &*Dst->named_metadata_begin();
288
289 EXPECT_EQ(M0, F->getMetadata("attach"));
290 EXPECT_EQ(M1, cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
291 EXPECT_EQ(M2, RI->getMetadata("attach"));
292 EXPECT_EQ(M3, NMD->getOperand(0));
293 EXPECT_EQ(M4, NMD->getOperand(1));
294
295 // Confirm a few things about the IR. This shouldn't have changed.
296 EXPECT_TRUE(M0->isDistinct());
297 EXPECT_TRUE(M1->isDistinct());
298 EXPECT_TRUE(M2->isDistinct());
299 EXPECT_TRUE(M3->isDistinct());
300 EXPECT_TRUE(M4->isUniqued());
301 EXPECT_EQ(M3, M4->getOperand(0));
302}
303
Bill Wendling91686d62014-01-16 06:29:36 +0000304} // end anonymous namespace