Skip to content

Commit

Permalink
Remove every uses of getGlobalContext() in LLVM (but the C API)
Browse files Browse the repository at this point in the history
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 <[email protected]>

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266379 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
joker-eph committed Apr 14, 2016
1 parent 6d955a1 commit 8be7707
Show file tree
Hide file tree
Showing 80 changed files with 1,108 additions and 1,079 deletions.
2 changes: 1 addition & 1 deletion bindings/ocaml/llvm/llvm.mli
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ val create_context : unit -> llcontext
[llvm::LLVMContext::~LLVMContext]. *)
val dispose_context : llcontext -> unit

(** See the function [llvm::getGlobalContext]. *)
(** See the function [LLVMGetGlobalContext]. *)
val global_context : unit -> llcontext

(** [mdkind_id context name] returns the MDKind ID that corresponds to the
Expand Down
5 changes: 0 additions & 5 deletions docs/ProgrammersManual.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2419,11 +2419,6 @@ determine what context they belong to by looking at their own ``Type``. If you
are adding new entities to LLVM IR, please try to maintain this interface
design.

For clients that do *not* require the benefits of isolation, LLVM provides a
convenience API ``getGlobalContext()``. This returns a global, lazily
initialized ``LLVMContext`` that may be used in situations where isolation is
not a concern.

.. _jitthreading:

Threads and the JIT
Expand Down
2 changes: 2 additions & 0 deletions docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Non-comprehensive list of changes in this release
(other than GlobalValue). This is intended to be used in release builds by
clients that are interested in saving CPU/memory as much as possible.

* There is no longer a "global context" available in LLVM, except for the C API.

* .. note about autoconf build having been removed.

* .. note about C API functions LLVMParseBitcode,
Expand Down
12 changes: 6 additions & 6 deletions docs/tutorial/LangImpl3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ parser, which will be used to report errors found during code generation
.. code-block:: c++

static std::unique_ptr<Module> *TheModule;
static IRBuilder<> Builder(getGlobalContext());
static IRBuilder<> Builder(LLVMContext);
static std::map<std::string, Value*> NamedValues;

Value *LogErrorV(const char *Str) {
Expand Down Expand Up @@ -116,7 +116,7 @@ First we'll do numeric literals:
.. code-block:: c++

Value *NumberExprAST::codegen() {
return ConstantFP::get(getGlobalContext(), APFloat(Val));
return ConstantFP::get(LLVMContext, APFloat(Val));
}
In the LLVM IR, numeric constants are represented with the
Expand Down Expand Up @@ -165,7 +165,7 @@ variables <LangImpl7.html#user-defined-local-variables>`_.
case '<':
L = Builder.CreateFCmpULT(L, R, "cmptmp");
// Convert bool 0/1 to double 0.0 or 1.0
return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
"booltmp");
default:
return LogErrorV("invalid binary operator");
Expand Down Expand Up @@ -264,9 +264,9 @@ with:
Function *PrototypeAST::codegen() {
// Make the function type: double(double,double) etc.
std::vector<Type*> Doubles(Args.size(),
Type::getDoubleTy(getGlobalContext()));
Type::getDoubleTy(LLVMContext));
FunctionType *FT =
FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
FunctionType::get(Type::getDoubleTy(LLVMContext), Doubles, false);
Function *F =
Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
Expand Down Expand Up @@ -340,7 +340,7 @@ assert that the function is empty (i.e. has no body yet) before we start.
.. code-block:: c++

// Create a new basic block to start insertion into.
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
Builder.SetInsertPoint(BB);
// Record the function arguments in the NamedValues map.
Expand Down
3 changes: 2 additions & 1 deletion docs/tutorial/LangImpl4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ for us:

void InitializeModuleAndPassManager(void) {
// Open a new module.
TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
Context LLVMContext;
TheModule = llvm::make_unique<Module>("my cool jit", LLVMContext);
TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());

// Create a new pass manager attached to it.
Expand Down
22 changes: 11 additions & 11 deletions docs/tutorial/LangImpl5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ for ``IfExprAST``:
// Convert condition to a bool by comparing equal to 0.0.
CondV = Builder.CreateFCmpONE(
CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
CondV, ConstantFP::get(LLVMContext, APFloat(0.0)), "ifcond");

This code is straightforward and similar to what we saw before. We emit
the expression for the condition, then compare that value to zero to get
Expand All @@ -305,9 +305,9 @@ a truth value as a 1-bit (bool) value.
// Create blocks for the then and else cases. Insert the 'then' block at the
// end of the function.
BasicBlock *ThenBB =
BasicBlock::Create(getGlobalContext(), "then", TheFunction);
BasicBlock *ElseBB = BasicBlock::Create(getGlobalContext(), "else");
BasicBlock *MergeBB = BasicBlock::Create(getGlobalContext(), "ifcont");
BasicBlock::Create(LLVMContext, "then", TheFunction);
BasicBlock *ElseBB = BasicBlock::Create(LLVMContext, "else");
BasicBlock *MergeBB = BasicBlock::Create(LLVMContext, "ifcont");
Builder.CreateCondBr(CondV, ThenBB, ElseBB);

Expand Down Expand Up @@ -400,7 +400,7 @@ code:
TheFunction->getBasicBlockList().push_back(MergeBB);
Builder.SetInsertPoint(MergeBB);
PHINode *PN =
Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp");
PN->addIncoming(ThenV, ThenBB);
PN->addIncoming(ElseV, ElseBB);
Expand Down Expand Up @@ -625,7 +625,7 @@ expression).
Function *TheFunction = Builder.GetInsertBlock()->getParent();
BasicBlock *PreheaderBB = Builder.GetInsertBlock();
BasicBlock *LoopBB =
BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
BasicBlock::Create(LLVMContext, "loop", TheFunction);
// Insert an explicit fall through from the current block to the LoopBB.
Builder.CreateBr(LoopBB);
Expand All @@ -642,7 +642,7 @@ the two blocks.
Builder.SetInsertPoint(LoopBB);

// Start the PHI node with an entry for Start.
PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(LLVMContext),
2, VarName.c_str());
Variable->addIncoming(StartVal, PreheaderBB);
Expand Down Expand Up @@ -693,7 +693,7 @@ table.
return nullptr;
} else {
// If not specified, use 1.0.
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
StepVal = ConstantFP::get(LLVMContext, APFloat(1.0));
}
Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
Expand All @@ -712,7 +712,7 @@ iteration of the loop.
// Convert condition to a bool by comparing equal to 0.0.
EndCond = Builder.CreateFCmpONE(
EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
EndCond, ConstantFP::get(LLVMContext, APFloat(0.0)), "loopcond");

Finally, we evaluate the exit value of the loop, to determine whether
the loop should exit. This mirrors the condition evaluation for the
Expand All @@ -723,7 +723,7 @@ if/then/else statement.
// Create the "after loop" block and insert it.
BasicBlock *LoopEndBB = Builder.GetInsertBlock();
BasicBlock *AfterBB =
BasicBlock::Create(getGlobalContext(), "afterloop", TheFunction);
BasicBlock::Create(LLVMContext, "afterloop", TheFunction);
// Insert the conditional branch into the end of LoopEndBB.
Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
Expand Down Expand Up @@ -751,7 +751,7 @@ insertion position to it.
NamedValues.erase(VarName);

// for expr always returns 0.0.
return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
return Constant::getNullValue(Type::getDoubleTy(LLVMContext));
}

The final code handles various cleanups: now that we have the "NextVar"
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/LangImpl6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ default case for our existing binary operator node:
case '<':
L = Builder.CreateFCmpULT(L, R, "cmptmp");
// Convert bool 0/1 to double 0.0 or 1.0
return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
"booltmp");
default:
break;
Expand Down Expand Up @@ -288,7 +288,7 @@ The final piece of code we are missing, is a bit of top-level magic:
BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();

// Create a new basic block to start insertion into.
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
Builder.SetInsertPoint(BB);
if (Value *RetVal = Body->codegen()) {
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/LangImpl7.rst
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ the function:
const std::string &VarName) {
IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
TheFunction->getEntryBlock().begin());
return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
return TmpB.CreateAlloca(Type::getDoubleTy(LLVMContext), 0,
VarName.c_str());
}
Expand Down Expand Up @@ -812,7 +812,7 @@ previous value that we replace in OldBindings.
if (!InitVal)
return nullptr;
} else { // If not specified, use 0.0.
InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
InitVal = ConstantFP::get(LLVMContext, APFloat(0.0));
}
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
Expand Down
2 changes: 1 addition & 1 deletion examples/BrainF/BrainFDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void addMainFunction(Module *mod) {
int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");

LLVMContext &Context = getGlobalContext();
LLVMContext Context;

if (InputFilename == "") {
errs() << "Error: You must specify the filename of the program to "
Expand Down
6 changes: 3 additions & 3 deletions examples/ExceptionDemo/ExceptionDemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1951,12 +1951,12 @@ int main(int argc, char *argv[]) {

llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::LLVMContext &context = llvm::getGlobalContext();
llvm::IRBuilder<> theBuilder(context);
llvm::LLVMContext Context;
llvm::IRBuilder<> theBuilder(Context);

// Make the module, which holds all the code.
std::unique_ptr<llvm::Module> Owner =
llvm::make_unique<llvm::Module>("my cool jit", context);
llvm::make_unique<llvm::Module>("my cool jit", Context);
llvm::Module *module = Owner.get();

std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());
Expand Down
17 changes: 8 additions & 9 deletions examples/Kaleidoscope/Chapter3/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ static std::unique_ptr<PrototypeAST> ParseExtern() {
//===----------------------------------------------------------------------===//

static std::unique_ptr<Module> TheModule;
static IRBuilder<> Builder(getGlobalContext());
static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);
static std::map<std::string, Value *> NamedValues;

Value *LogErrorV(const char *Str) {
Expand All @@ -390,7 +391,7 @@ Value *LogErrorV(const char *Str) {
}

Value *NumberExprAST::codegen() {
return ConstantFP::get(getGlobalContext(), APFloat(Val));
return ConstantFP::get(TheContext, APFloat(Val));
}

Value *VariableExprAST::codegen() {
Expand All @@ -417,8 +418,7 @@ Value *BinaryExprAST::codegen() {
case '<':
L = Builder.CreateFCmpULT(L, R, "cmptmp");
// Convert bool 0/1 to double 0.0 or 1.0
return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
"booltmp");
return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
default:
return LogErrorV("invalid binary operator");
}
Expand Down Expand Up @@ -446,10 +446,9 @@ Value *CallExprAST::codegen() {

Function *PrototypeAST::codegen() {
// Make the function type: double(double,double) etc.
std::vector<Type *> Doubles(Args.size(),
Type::getDoubleTy(getGlobalContext()));
std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext));
FunctionType *FT =
FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);

Function *F =
Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
Expand All @@ -473,7 +472,7 @@ Function *FunctionAST::codegen() {
return nullptr;

// Create a new basic block to start insertion into.
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
Builder.SetInsertPoint(BB);

// Record the function arguments in the NamedValues map.
Expand Down Expand Up @@ -577,7 +576,7 @@ int main() {
getNextToken();

// Make the module, which holds all the code.
TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
TheModule = llvm::make_unique<Module>("my cool jit", TheContext);

// Run the main "interpreter loop" now.
MainLoop();
Expand Down
17 changes: 8 additions & 9 deletions examples/Kaleidoscope/Chapter4/toy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ static std::unique_ptr<PrototypeAST> ParseExtern() {
//===----------------------------------------------------------------------===//

static std::unique_ptr<Module> TheModule;
static IRBuilder<> Builder(getGlobalContext());
static LLVMContext TheContext;
static IRBuilder<> Builder(TheContext);
static std::map<std::string, Value *> NamedValues;
static std::unique_ptr<legacy::FunctionPassManager> TheFPM;
static std::unique_ptr<KaleidoscopeJIT> TheJIT;
Expand All @@ -415,7 +416,7 @@ Function *getFunction(std::string Name) {
}

Value *NumberExprAST::codegen() {
return ConstantFP::get(getGlobalContext(), APFloat(Val));
return ConstantFP::get(TheContext, APFloat(Val));
}

Value *VariableExprAST::codegen() {
Expand All @@ -442,8 +443,7 @@ Value *BinaryExprAST::codegen() {
case '<':
L = Builder.CreateFCmpULT(L, R, "cmptmp");
// Convert bool 0/1 to double 0.0 or 1.0
return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
"booltmp");
return Builder.CreateUIToFP(L, Type::getDoubleTy(TheContext), "booltmp");
default:
return LogErrorV("invalid binary operator");
}
Expand Down Expand Up @@ -471,10 +471,9 @@ Value *CallExprAST::codegen() {

Function *PrototypeAST::codegen() {
// Make the function type: double(double,double) etc.
std::vector<Type *> Doubles(Args.size(),
Type::getDoubleTy(getGlobalContext()));
std::vector<Type *> Doubles(Args.size(), Type::getDoubleTy(TheContext));
FunctionType *FT =
FunctionType::get(Type::getDoubleTy(getGlobalContext()), Doubles, false);
FunctionType::get(Type::getDoubleTy(TheContext), Doubles, false);

Function *F =
Function::Create(FT, Function::ExternalLinkage, Name, TheModule.get());
Expand All @@ -497,7 +496,7 @@ Function *FunctionAST::codegen() {
return nullptr;

// Create a new basic block to start insertion into.
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
BasicBlock *BB = BasicBlock::Create(TheContext, "entry", TheFunction);
Builder.SetInsertPoint(BB);

// Record the function arguments in the NamedValues map.
Expand Down Expand Up @@ -529,7 +528,7 @@ Function *FunctionAST::codegen() {

static void InitializeModuleAndPassManager() {
// Open a new module.
TheModule = llvm::make_unique<Module>("my cool jit", getGlobalContext());
TheModule = llvm::make_unique<Module>("my cool jit", TheContext);
TheModule->setDataLayout(TheJIT->getTargetMachine().createDataLayout());

// Create a new pass manager attached to it.
Expand Down
Loading

0 comments on commit 8be7707

Please sign in to comment.