Skip to content

Commit 03b42e4

Browse files
committed
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
1 parent 3d1c1de commit 03b42e4

File tree

80 files changed

+1108
-1079
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1108
-1079
lines changed

llvm/bindings/ocaml/llvm/llvm.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ val create_context : unit -> llcontext
434434
[llvm::LLVMContext::~LLVMContext]. *)
435435
val dispose_context : llcontext -> unit
436436

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

440440
(** [mdkind_id context name] returns the MDKind ID that corresponds to the

llvm/docs/ProgrammersManual.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,11 +2419,6 @@ determine what context they belong to by looking at their own ``Type``. If you
24192419
are adding new entities to LLVM IR, please try to maintain this interface
24202420
design.
24212421

2422-
For clients that do *not* require the benefits of isolation, LLVM provides a
2423-
convenience API ``getGlobalContext()``. This returns a global, lazily
2424-
initialized ``LLVMContext`` that may be used in situations where isolation is
2425-
not a concern.
2426-
24272422
.. _jitthreading:
24282423

24292424
Threads and the JIT

llvm/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Non-comprehensive list of changes in this release
3838
(other than GlobalValue). This is intended to be used in release builds by
3939
clients that are interested in saving CPU/memory as much as possible.
4040

41+
* There is no longer a "global context" available in LLVM, except for the C API.
42+
4143
* .. note about autoconf build having been removed.
4244

4345
* .. note about C API functions LLVMParseBitcode,

llvm/docs/tutorial/LangImpl3.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ parser, which will be used to report errors found during code generation
7474
.. code-block:: c++
7575

7676
static std::unique_ptr<Module> *TheModule;
77-
static IRBuilder<> Builder(getGlobalContext());
77+
static IRBuilder<> Builder(LLVMContext);
7878
static std::map<std::string, Value*> NamedValues;
7979

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

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

342342
// Create a new basic block to start insertion into.
343-
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
343+
BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
344344
Builder.SetInsertPoint(BB);
345345
346346
// Record the function arguments in the NamedValues map.

llvm/docs/tutorial/LangImpl4.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ for us:
131131

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

137138
// Create a new pass manager attached to it.

llvm/docs/tutorial/LangImpl5.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ for ``IfExprAST``:
292292
293293
// Convert condition to a bool by comparing equal to 0.0.
294294
CondV = Builder.CreateFCmpONE(
295-
CondV, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "ifcond");
295+
CondV, ConstantFP::get(LLVMContext, APFloat(0.0)), "ifcond");
296296

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

@@ -400,7 +400,7 @@ code:
400400
TheFunction->getBasicBlockList().push_back(MergeBB);
401401
Builder.SetInsertPoint(MergeBB);
402402
PHINode *PN =
403-
Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()), 2, "iftmp");
403+
Builder.CreatePHI(Type::getDoubleTy(LLVMContext), 2, "iftmp");
404404
405405
PN->addIncoming(ThenV, ThenBB);
406406
PN->addIncoming(ElseV, ElseBB);
@@ -625,7 +625,7 @@ expression).
625625
Function *TheFunction = Builder.GetInsertBlock()->getParent();
626626
BasicBlock *PreheaderBB = Builder.GetInsertBlock();
627627
BasicBlock *LoopBB =
628-
BasicBlock::Create(getGlobalContext(), "loop", TheFunction);
628+
BasicBlock::Create(LLVMContext, "loop", TheFunction);
629629
630630
// Insert an explicit fall through from the current block to the LoopBB.
631631
Builder.CreateBr(LoopBB);
@@ -642,7 +642,7 @@ the two blocks.
642642
Builder.SetInsertPoint(LoopBB);
643643

644644
// Start the PHI node with an entry for Start.
645-
PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(getGlobalContext()),
645+
PHINode *Variable = Builder.CreatePHI(Type::getDoubleTy(LLVMContext),
646646
2, VarName.c_str());
647647
Variable->addIncoming(StartVal, PreheaderBB);
648648
@@ -693,7 +693,7 @@ table.
693693
return nullptr;
694694
} else {
695695
// If not specified, use 1.0.
696-
StepVal = ConstantFP::get(getGlobalContext(), APFloat(1.0));
696+
StepVal = ConstantFP::get(LLVMContext, APFloat(1.0));
697697
}
698698
699699
Value *NextVar = Builder.CreateFAdd(Variable, StepVal, "nextvar");
@@ -712,7 +712,7 @@ iteration of the loop.
712712
713713
// Convert condition to a bool by comparing equal to 0.0.
714714
EndCond = Builder.CreateFCmpONE(
715-
EndCond, ConstantFP::get(getGlobalContext(), APFloat(0.0)), "loopcond");
715+
EndCond, ConstantFP::get(LLVMContext, APFloat(0.0)), "loopcond");
716716

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

753753
// for expr always returns 0.0.
754-
return Constant::getNullValue(Type::getDoubleTy(getGlobalContext()));
754+
return Constant::getNullValue(Type::getDoubleTy(LLVMContext));
755755
}
756756

757757
The final code handles various cleanups: now that we have the "NextVar"

llvm/docs/tutorial/LangImpl6.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ default case for our existing binary operator node:
251251
case '<':
252252
L = Builder.CreateFCmpULT(L, R, "cmptmp");
253253
// Convert bool 0/1 to double 0.0 or 1.0
254-
return Builder.CreateUIToFP(L, Type::getDoubleTy(getGlobalContext()),
254+
return Builder.CreateUIToFP(L, Type::getDoubleTy(LLVMContext),
255255
"booltmp");
256256
default:
257257
break;
@@ -288,7 +288,7 @@ The final piece of code we are missing, is a bit of top-level magic:
288288
BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
289289

290290
// Create a new basic block to start insertion into.
291-
BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction);
291+
BasicBlock *BB = BasicBlock::Create(LLVMContext, "entry", TheFunction);
292292
Builder.SetInsertPoint(BB);
293293
294294
if (Value *RetVal = Body->codegen()) {

llvm/docs/tutorial/LangImpl7.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ the function:
339339
const std::string &VarName) {
340340
IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
341341
TheFunction->getEntryBlock().begin());
342-
return TmpB.CreateAlloca(Type::getDoubleTy(getGlobalContext()), 0,
342+
return TmpB.CreateAlloca(Type::getDoubleTy(LLVMContext), 0,
343343
VarName.c_str());
344344
}
345345
@@ -812,7 +812,7 @@ previous value that we replace in OldBindings.
812812
if (!InitVal)
813813
return nullptr;
814814
} else { // If not specified, use 0.0.
815-
InitVal = ConstantFP::get(getGlobalContext(), APFloat(0.0));
815+
InitVal = ConstantFP::get(LLVMContext, APFloat(0.0));
816816
}
817817
818818
AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);

llvm/examples/BrainF/BrainFDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void addMainFunction(Module *mod) {
8888
int main(int argc, char **argv) {
8989
cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
9090

91-
LLVMContext &Context = getGlobalContext();
91+
LLVMContext Context;
9292

9393
if (InputFilename == "") {
9494
errs() << "Error: You must specify the filename of the program to "

llvm/examples/ExceptionDemo/ExceptionDemo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,12 +1951,12 @@ int main(int argc, char *argv[]) {
19511951

19521952
llvm::InitializeNativeTarget();
19531953
llvm::InitializeNativeTargetAsmPrinter();
1954-
llvm::LLVMContext &context = llvm::getGlobalContext();
1955-
llvm::IRBuilder<> theBuilder(context);
1954+
llvm::LLVMContext Context;
1955+
llvm::IRBuilder<> theBuilder(Context);
19561956

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

19621962
std::unique_ptr<llvm::RTDyldMemoryManager> MemMgr(new llvm::SectionMemoryManager());

0 commit comments

Comments
 (0)