Remove every uses of getGlobalContext() in LLVM (but the C API)
At the same time, fixes InstructionsTest::CastInst unittest: yes
you can leave the IR in an invalid state and exit when you don't
destroy the context (like the global one), no longer now.
This is the first part of http://reviews.llvm.org/D19094
From: Mehdi Amini <mehdi.amini@apple.com>
llvm-svn: 266379
diff --git a/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp b/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp
index 3ffa9cd..7cad841 100644
--- a/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp
+++ b/llvm/unittests/ExecutionEngine/ExecutionEngineTest.cpp
@@ -28,7 +28,7 @@
protected:
ExecutionEngineTest() {
- auto Owner = make_unique<Module>("<main>", getGlobalContext());
+ auto Owner = make_unique<Module>("<main>", Context);
M = Owner.get();
Engine.reset(EngineBuilder(std::move(Owner)).setErrorStr(&Error).create());
}
@@ -44,13 +44,13 @@
}
std::string Error;
+ LLVMContext Context;
Module *M; // Owned by ExecutionEngine.
std::unique_ptr<ExecutionEngine> Engine;
};
TEST_F(ExecutionEngineTest, ForwardGlobalMapping) {
- GlobalVariable *G1 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
+ GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
int32_t Mem1 = 3;
Engine->addGlobalMapping(G1, &Mem1);
EXPECT_EQ(&Mem1, Engine->getPointerToGlobalIfAvailable(G1));
@@ -63,8 +63,7 @@
Engine->updateGlobalMapping(G1, &Mem2);
EXPECT_EQ(&Mem2, Engine->getPointerToGlobalIfAvailable(G1));
- GlobalVariable *G2 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
+ GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
EXPECT_EQ(nullptr, Engine->getPointerToGlobalIfAvailable(G2))
<< "The NULL return shouldn't depend on having called"
<< " updateGlobalMapping(..., NULL)";
@@ -76,8 +75,7 @@
}
TEST_F(ExecutionEngineTest, ReverseGlobalMapping) {
- GlobalVariable *G1 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
+ GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
int32_t Mem1 = 3;
Engine->addGlobalMapping(G1, &Mem1);
@@ -87,8 +85,7 @@
EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
- GlobalVariable *G2 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
+ GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global2");
Engine->updateGlobalMapping(G2, &Mem1);
EXPECT_EQ(G2, Engine->getGlobalValueAtAddress(&Mem1));
EXPECT_EQ(G1, Engine->getGlobalValueAtAddress(&Mem2));
@@ -104,8 +101,7 @@
}
TEST_F(ExecutionEngineTest, ClearModuleMappings) {
- GlobalVariable *G1 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
+ GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
int32_t Mem1 = 3;
Engine->addGlobalMapping(G1, &Mem1);
@@ -115,8 +111,7 @@
EXPECT_EQ(nullptr, Engine->getGlobalValueAtAddress(&Mem1));
- GlobalVariable *G2 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global2");
+ GlobalVariable *G2 = NewExtGlobal(Type::getInt32Ty(Context), "Global2");
// After clearing the module mappings, we can assign a new GV to the
// same address.
Engine->addGlobalMapping(G2, &Mem1);
@@ -124,8 +119,7 @@
}
TEST_F(ExecutionEngineTest, DestructionRemovesGlobalMapping) {
- GlobalVariable *G1 =
- NewExtGlobal(Type::getInt32Ty(getGlobalContext()), "Global1");
+ GlobalVariable *G1 = NewExtGlobal(Type::getInt32Ty(Context), "Global1");
int32_t Mem1 = 3;
Engine->addGlobalMapping(G1, &Mem1);
// Make sure the reverse mapping is enabled.
diff --git a/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp b/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
index 38b60ea..ac84703 100644
--- a/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/IndirectionUtilsTest.cpp
@@ -17,7 +17,8 @@
namespace {
TEST(IndirectionUtilsTest, MakeStub) {
- ModuleBuilder MB(getGlobalContext(), "x86_64-apple-macosx10.10", "");
+ LLVMContext Context;
+ ModuleBuilder MB(Context, "x86_64-apple-macosx10.10", "");
Function *F = MB.createFunctionDecl<void(DummyStruct, DummyStruct)>("");
SmallVector<AttributeSet, 4> Attrs;
Attrs.push_back(
diff --git a/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp b/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
index a733bd5..89adb66 100644
--- a/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/ObjectLinkingLayerTest.cpp
@@ -25,6 +25,7 @@
class ObjectLinkingLayerExecutionTest : public testing::Test,
public OrcExecutionTest {
+
};
class SectionMemoryManagerWrapper : public SectionMemoryManager {
@@ -64,9 +65,10 @@
ObjectLinkingLayer<> ObjLayer;
- auto M = llvm::make_unique<Module>("", getGlobalContext());
+ LLVMContext Context;
+ auto M = llvm::make_unique<Module>("", Context);
M->setTargetTriple("x86_64-unknown-linux-gnu");
- Type *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+ Type *Int32Ty = IntegerType::get(Context, 32);
GlobalVariable *GV =
new GlobalVariable(*M, Int32Ty, false, GlobalValue::ExternalLinkage,
ConstantInt::get(Int32Ty, 42), "foo");
@@ -131,14 +133,13 @@
// instance (for Module 1) which is unsafe, as it will prevent relocation of
// Module 2.
- ModuleBuilder MB1(getGlobalContext(), "", "dummy");
+ ModuleBuilder MB1(Context, "", "dummy");
{
MB1.getModule()->setDataLayout(TM->createDataLayout());
Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("bar");
- BasicBlock *BarEntry = BasicBlock::Create(getGlobalContext(), "entry",
- BarImpl);
+ BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
IRBuilder<> Builder(BarEntry);
- IntegerType *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+ IntegerType *Int32Ty = IntegerType::get(Context, 32);
Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
Builder.CreateRet(FourtyTwo);
}
@@ -147,13 +148,12 @@
std::vector<object::ObjectFile*> Obj1Set;
Obj1Set.push_back(Obj1.getBinary());
- ModuleBuilder MB2(getGlobalContext(), "", "dummy");
+ ModuleBuilder MB2(Context, "", "dummy");
{
MB2.getModule()->setDataLayout(TM->createDataLayout());
Function *BarDecl = MB2.createFunctionDecl<int32_t(void)>("bar");
Function *FooImpl = MB2.createFunctionDecl<int32_t(void)>("foo");
- BasicBlock *FooEntry = BasicBlock::Create(getGlobalContext(), "entry",
- FooImpl);
+ BasicBlock *FooEntry = BasicBlock::Create(Context, "entry", FooImpl);
IRBuilder<> Builder(FooEntry);
Builder.CreateRet(Builder.CreateCall(BarDecl));
}
@@ -203,14 +203,13 @@
// RuntimeDyld::MemoryManager::needsToReserveAllocationSpace hook, which is
// called once per object before any sections are allocated.
- ModuleBuilder MB1(getGlobalContext(), "", "dummy");
+ ModuleBuilder MB1(Context, "", "dummy");
{
MB1.getModule()->setDataLayout(TM->createDataLayout());
Function *BarImpl = MB1.createFunctionDecl<int32_t(void)>("foo");
- BasicBlock *BarEntry = BasicBlock::Create(getGlobalContext(), "entry",
- BarImpl);
+ BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
IRBuilder<> Builder(BarEntry);
- IntegerType *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+ IntegerType *Int32Ty = IntegerType::get(Context, 32);
Value *FourtyTwo = ConstantInt::getSigned(Int32Ty, 42);
Builder.CreateRet(FourtyTwo);
}
@@ -219,14 +218,13 @@
std::vector<object::ObjectFile*> Obj1Set;
Obj1Set.push_back(Obj1.getBinary());
- ModuleBuilder MB2(getGlobalContext(), "", "dummy");
+ ModuleBuilder MB2(Context, "", "dummy");
{
MB2.getModule()->setDataLayout(TM->createDataLayout());
Function *BarImpl = MB2.createFunctionDecl<int32_t(void)>("bar");
- BasicBlock *BarEntry = BasicBlock::Create(getGlobalContext(), "entry",
- BarImpl);
+ BasicBlock *BarEntry = BasicBlock::Create(Context, "entry", BarImpl);
IRBuilder<> Builder(BarEntry);
- IntegerType *Int32Ty = IntegerType::get(getGlobalContext(), 32);
+ IntegerType *Int32Ty = IntegerType::get(Context, 32);
Value *Seven = ConstantInt::getSigned(Int32Ty, 7);
Builder.CreateRet(Seven);
}
diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
index 776d269..305325b 100644
--- a/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/OrcCAPITest.cpp
@@ -25,11 +25,11 @@
class OrcCAPIExecutionTest : public testing::Test, public OrcExecutionTest {
protected:
std::unique_ptr<Module> createTestModule(const Triple &TT) {
- ModuleBuilder MB(getGlobalContext(), TT.str(), "");
+ ModuleBuilder MB(Context, TT.str(), "");
Function *TestFunc = MB.createFunctionDecl<int()>("testFunc");
Function *Main = MB.createFunctionDecl<int(int, char*[])>("main");
- Main->getBasicBlockList().push_back(BasicBlock::Create(getGlobalContext()));
+ Main->getBasicBlockList().push_back(BasicBlock::Create(Context));
IRBuilder<> B(&Main->back());
Value* Result = B.CreateCall(TestFunc);
B.CreateRet(Result);
diff --git a/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h b/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h
index e8fab56..fe3da88 100644
--- a/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h
+++ b/llvm/unittests/ExecutionEngine/Orc/OrcTestCommon.h
@@ -54,6 +54,7 @@
};
protected:
+ LLVMContext Context;
std::unique_ptr<TargetMachine> TM;
private:
static bool NativeTargetInitialized;