blob: 904ba58ce48e608fc528798c28d9bca1d2cb18ae [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
74TEST_F(LinkModuleTest, BlockAddress) {
Bill Wendling91686d62014-01-16 06:29:36 +000075 IRBuilder<> Builder(EntryBB);
76
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +000077 std::vector<Value *> GEPIndices;
Bill Wendling91686d62014-01-16 06:29:36 +000078 GEPIndices.push_back(ConstantInt::get(Type::getInt32Ty(Ctx), 0));
79 GEPIndices.push_back(F->arg_begin());
80
David Blaikie156d46e2015-03-24 23:34:31 +000081 Value *GEP = Builder.CreateGEP(AT, GV, GEPIndices, "switch.gep");
Bill Wendling91686d62014-01-16 06:29:36 +000082 Value *Load = Builder.CreateLoad(GEP, "switch.load");
83
84 Builder.CreateRet(Load);
85
86 Builder.SetInsertPoint(SwitchCase1BB);
87 Builder.CreateBr(ExitBB);
88
89 Builder.SetInsertPoint(SwitchCase2BB);
90 Builder.CreateBr(ExitBB);
91
92 Builder.SetInsertPoint(ExitBB);
93 Builder.CreateRet(ConstantPointerNull::get(Type::getInt8PtrTy(Ctx)));
94
NAKAMURA Takumib49b99b2014-04-29 15:52:27 +000095 Module *LinkedModule = new Module("MyModuleLinked", Ctx);
Rafael Espindola9f8eff32014-10-28 00:24:16 +000096 Linker::LinkModules(LinkedModule, M.get());
Bill Wendling91686d62014-01-16 06:29:36 +000097
98 // Delete the original module.
99 M.reset();
100
101 // Check that the global "@switch.bas" is well-formed.
102 const GlobalVariable *LinkedGV = LinkedModule->getNamedGlobal("switch.bas");
103 const Constant *Init = LinkedGV->getInitializer();
104
105 // @switch.bas = internal global [3 x i8*]
106 // [i8* blockaddress(@ba_func, %switch.case.1),
107 // i8* blockaddress(@ba_func, %switch.case.2),
108 // i8* inttoptr (i32 1 to i8*)]
109
110 ArrayType *AT = ArrayType::get(Type::getInt8PtrTy(Ctx), 3);
111 EXPECT_EQ(AT, Init->getType());
112
113 Value *Elem = Init->getOperand(0);
114 ASSERT_TRUE(isa<BlockAddress>(Elem));
115 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
116 LinkedModule->getFunction("ba_func"));
117 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
118 LinkedModule->getFunction("ba_func"));
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000119
Bill Wendling91686d62014-01-16 06:29:36 +0000120 Elem = Init->getOperand(1);
121 ASSERT_TRUE(isa<BlockAddress>(Elem));
122 EXPECT_EQ(cast<BlockAddress>(Elem)->getFunction(),
123 LinkedModule->getFunction("ba_func"));
124 EXPECT_EQ(cast<BlockAddress>(Elem)->getBasicBlock()->getParent(),
125 LinkedModule->getFunction("ba_func"));
126
127 delete LinkedModule;
128}
129
Eli Benderskyff715e22015-06-12 23:26:42 +0000130static Module *getExternal(LLVMContext &Ctx, StringRef FuncName) {
131 // Create a module with an empty externally-linked function
132 Module *M = new Module("ExternalModule", Ctx);
133 FunctionType *FTy = FunctionType::get(
134 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
135
136 Function *F =
137 Function::Create(FTy, Function::ExternalLinkage, FuncName, M);
138 F->setCallingConv(CallingConv::C);
139
140 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
141 IRBuilder<> Builder(BB);
142 Builder.CreateRetVoid();
143 return M;
144}
145
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000146static Module *getInternal(LLVMContext &Ctx) {
Bill Wendling91686d62014-01-16 06:29:36 +0000147 Module *InternalM = new Module("InternalModule", Ctx);
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000148 FunctionType *FTy = FunctionType::get(
149 Type::getVoidTy(Ctx), Type::getInt8PtrTy(Ctx), false /*=isVarArgs*/);
Bill Wendling91686d62014-01-16 06:29:36 +0000150
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000151 Function *F =
152 Function::Create(FTy, Function::InternalLinkage, "bar", InternalM);
Bill Wendling91686d62014-01-16 06:29:36 +0000153 F->setCallingConv(CallingConv::C);
154
155 BasicBlock *BB = BasicBlock::Create(Ctx, "", F);
156 IRBuilder<> Builder(BB);
157 Builder.CreateRetVoid();
158
159 StructType *STy = StructType::create(Ctx, PointerType::get(FTy, 0));
160
161 GlobalVariable *GV =
NAKAMURA Takumi0f250ed2014-04-29 15:52:46 +0000162 new GlobalVariable(*InternalM, STy, false /*=isConstant*/,
Craig Topper66f09ad2014-06-08 22:29:17 +0000163 GlobalValue::InternalLinkage, nullptr, "g");
Bill Wendling91686d62014-01-16 06:29:36 +0000164
165 GV->setInitializer(ConstantStruct::get(STy, F));
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000166 return InternalM;
167}
Bill Wendling91686d62014-01-16 06:29:36 +0000168
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000169TEST_F(LinkModuleTest, EmptyModule) {
170 std::unique_ptr<Module> InternalM(getInternal(Ctx));
171 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
172 Linker::LinkModules(EmptyM.get(), InternalM.get());
173}
Bill Wendling91686d62014-01-16 06:29:36 +0000174
Rafael Espindola9f8eff32014-10-28 00:24:16 +0000175TEST_F(LinkModuleTest, EmptyModule2) {
176 std::unique_ptr<Module> InternalM(getInternal(Ctx));
177 std::unique_ptr<Module> EmptyM(new Module("EmptyModule1", Ctx));
178 Linker::LinkModules(InternalM.get(), EmptyM.get());
Bill Wendling91686d62014-01-16 06:29:36 +0000179}
180
Rafael Espindola5cb9c822014-11-17 20:51:01 +0000181TEST_F(LinkModuleTest, TypeMerge) {
182 LLVMContext C;
183 SMDiagnostic Err;
184
185 const char *M1Str = "%t = type {i32}\n"
186 "@t1 = weak global %t zeroinitializer\n";
187 std::unique_ptr<Module> M1 = parseAssemblyString(M1Str, Err, C);
188
189 const char *M2Str = "%t = type {i32}\n"
190 "@t2 = weak global %t zeroinitializer\n";
191 std::unique_ptr<Module> M2 = parseAssemblyString(M2Str, Err, C);
192
193 Linker::LinkModules(M1.get(), M2.get(), [](const llvm::DiagnosticInfo &){});
194
195 EXPECT_EQ(M1->getNamedGlobal("t1")->getType(),
196 M1->getNamedGlobal("t2")->getType());
197}
198
Eli Benderskyff715e22015-06-12 23:26:42 +0000199TEST_F(LinkModuleTest, CAPISuccess) {
200 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
201 std::unique_ptr<Module> SourceM(getExternal(Ctx, "bar"));
202 char *errout = nullptr;
203 LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()),
204 LLVMLinkerDestroySource, &errout);
205 EXPECT_EQ(0, result);
206 EXPECT_EQ(nullptr, errout);
207 // "bar" is present in destination module
208 EXPECT_NE(nullptr, DestM->getFunction("bar"));
209}
210
211TEST_F(LinkModuleTest, CAPIFailure) {
212 // Symbol clash between two modules
213 std::unique_ptr<Module> DestM(getExternal(Ctx, "foo"));
214 std::unique_ptr<Module> SourceM(getExternal(Ctx, "foo"));
215 char *errout = nullptr;
216 LLVMBool result = LLVMLinkModules(wrap(DestM.get()), wrap(SourceM.get()),
217 LLVMLinkerDestroySource, &errout);
218 EXPECT_EQ(1, result);
219 EXPECT_STREQ("Linking globals named 'foo': symbol multiply defined!", errout);
Benjamin Kramerf1d570d2015-06-15 15:42:26 +0000220 LLVMDisposeMessage(errout);
Eli Benderskyff715e22015-06-12 23:26:42 +0000221}
222
Duncan P. N. Exon Smith4fb46cb2015-08-03 17:09:38 +0000223TEST_F(LinkModuleTest, MoveDistinctMDs) {
224 LLVMContext C;
225 SMDiagnostic Err;
226
227 const char *SrcStr = "define void @foo() !attach !0 {\n"
228 "entry:\n"
229 " call void @llvm.md(metadata !1)\n"
230 " ret void, !attach !2\n"
231 "}\n"
232 "declare void @llvm.md(metadata)\n"
233 "!named = !{!3, !4}\n"
234 "!0 = distinct !{}\n"
235 "!1 = distinct !{}\n"
236 "!2 = distinct !{}\n"
237 "!3 = distinct !{}\n"
238 "!4 = !{!3}\n";
239
240 std::unique_ptr<Module> Src = parseAssemblyString(SrcStr, Err, C);
241 assert(Src);
242 ASSERT_TRUE(Src.get());
243
244 // Get the addresses of the Metadata before merging.
245 Function *F = &*Src->begin();
246 ASSERT_EQ("foo", F->getName());
247 BasicBlock *BB = &F->getEntryBlock();
248 auto *CI = cast<CallInst>(&BB->front());
249 auto *RI = cast<ReturnInst>(BB->getTerminator());
250 NamedMDNode *NMD = &*Src->named_metadata_begin();
251
252 MDNode *M0 = F->getMetadata("attach");
253 MDNode *M1 =
254 cast<MDNode>(cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
255 MDNode *M2 = RI->getMetadata("attach");
256 MDNode *M3 = NMD->getOperand(0);
257 MDNode *M4 = NMD->getOperand(1);
258
259 // Confirm a few things about the IR.
260 EXPECT_TRUE(M0->isDistinct());
261 EXPECT_TRUE(M1->isDistinct());
262 EXPECT_TRUE(M2->isDistinct());
263 EXPECT_TRUE(M3->isDistinct());
264 EXPECT_TRUE(M4->isUniqued());
265 EXPECT_EQ(M3, M4->getOperand(0));
266
267 // Link into destination module.
268 auto Dst = llvm::make_unique<Module>("Linked", C);
269 ASSERT_TRUE(Dst.get());
270 Linker::LinkModules(Dst.get(), Src.get(),
271 [](const llvm::DiagnosticInfo &) {});
272
273 // Check that distinct metadata was moved, not cloned. Even !4, the uniqued
274 // node, should effectively be moved, since its only operand hasn't changed.
275 F = &*Dst->begin();
276 BB = &F->getEntryBlock();
277 CI = cast<CallInst>(&BB->front());
278 RI = cast<ReturnInst>(BB->getTerminator());
279 NMD = &*Dst->named_metadata_begin();
280
281 EXPECT_EQ(M0, F->getMetadata("attach"));
282 EXPECT_EQ(M1, cast<MetadataAsValue>(CI->getArgOperand(0))->getMetadata());
283 EXPECT_EQ(M2, RI->getMetadata("attach"));
284 EXPECT_EQ(M3, NMD->getOperand(0));
285 EXPECT_EQ(M4, NMD->getOperand(1));
286
287 // Confirm a few things about the IR. This shouldn't have changed.
288 EXPECT_TRUE(M0->isDistinct());
289 EXPECT_TRUE(M1->isDistinct());
290 EXPECT_TRUE(M2->isDistinct());
291 EXPECT_TRUE(M3->isDistinct());
292 EXPECT_TRUE(M4->isUniqued());
293 EXPECT_EQ(M3, M4->getOperand(0));
294}
295
Bill Wendling91686d62014-01-16 06:29:36 +0000296} // end anonymous namespace