Skip to content

Commit ec1ea0a

Browse files
authored
[llvm-c] Deprecate functions working on the global context (#163979)
One of the most common mistakes when working with the LLVM C API is to mix functions that work on the global context and those that work on an explicit context. This often results in seemingly nonsensical errors because types from different contexts are mixed. We have considered the APIs working on the global context to be obsolete for a long time already, and do not add any new APIs using the global context. However, the fact that these still exist (and have shorter names) continues to cause issues. This PR proposes to deprecate these APIs, with intent to remove them at some point in the future. RFC: https://discourse.llvm.org/t/rfc-deprecate-c-api-functions-using-the-global-context/88639
1 parent ec78750 commit ec1ea0a

File tree

25 files changed

+435
-235
lines changed

25 files changed

+435
-235
lines changed

llvm/bindings/ocaml/llvm/llvm_ocaml.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ value llvm_dispose_context(value C) {
239239
}
240240

241241
/* unit -> llcontext */
242-
value llvm_global_context(value Unit) { return to_val(LLVMGetGlobalContext()); }
242+
value llvm_global_context(value Unit) {
243+
return to_val(getGlobalContextForCAPI());
244+
}
243245

244246
/* llcontext -> string -> int */
245247
value llvm_mdkind_id(value C, value Name) {

llvm/docs/ReleaseNotes.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,41 @@ Changes to the C API
178178
* Add `LLVMGetOrInsertFunction` to get or insert a function, replacing the combination of `LLVMGetNamedFunction` and `LLVMAddFunction`.
179179
* Allow `LLVMGetVolatile` to work with any kind of Instruction.
180180
* Add `LLVMConstFPFromBits` to get a constant floating-point value from an array of 64 bit values.
181+
* Functions working on the global context have been deprecated. Use the
182+
functions that work on a specific context instead.
183+
184+
* `LLVMGetGlobalContext` -> use `LLVMContextCreate` context instead
185+
* `LLVMInt1Type` -> `LLVMInt1TypeInContext`
186+
* `LLVMInt8Type` -> `LLVMInt8TypeInContext`
187+
* `LLVMInt16Type` -> `LLVMInt16TypeInContext`
188+
* `LLVMInt32Type` -> `LLVMInt32TypeInContext`
189+
* `LLVMInt64Type` -> `LLVMInt64TypeInContext`
190+
* `LLVMInt128Type` -> `LLVMInt128TypeInContext`
191+
* `LLVMIntType` -> `LLVMIntTypeInContext`
192+
* `LLVMHalfType` -> `LLVMHalfTypeInContext`
193+
* `LLVMBFloatType` -> `LLVMBFloatTypeInContext`
194+
* `LLVMFloatType` -> `LLVMFloatTypeInContext`
195+
* `LLVMDoubleType` -> `LLVMDoubleTypeInContext`
196+
* `LLVMX86FP80Type` -> `LLVMX86FP80TypeInContext`
197+
* `LLVMFP128Type` -> `LLVMFP128TypeInContext`
198+
* `LLVMPPCFP128Type` -> `LLVMPPCFP128TypeInContext`
199+
* `LLVMStructType` -> `LLVMStructTypeInContext`
200+
* `LLVMVoidType` -> `LLVMVoidTypeInContext`
201+
* `LLVMLabelType` -> `LLVMLabelTypeInContext`
202+
* `LLVMX86AMXType` -> `LLVMX86AMXTypeInContext`
203+
* `LLVMConstString` -> `LLVMConstStringInContext2`
204+
* `LLVMConstStruct` -> `LLVMConstStructInContext`
205+
* `LLVMMDString` -> `LLVMMDStringInContext2`
206+
* `LLVMMDNode` -> `LLVMMDNodeInContext2`
207+
* `LLVMAppendBasicBlock` -> `LLVMAppendBasicBlockInContext`
208+
* `LLVMInsertBasicBlock` -> `LLVMInsertBasicBlockInContext`
209+
* `LLVMCreateBuilder` -> `LLVMCreateBuilderInContext`
210+
* `LLVMIntPtrType` -> `LLVMIntPtrTypeInContext`
211+
* `LLVMIntPtrTypeForAS` -> `LLVMIntPtrTypeForASInContext`
212+
* `LLVMParseBitcode` -> `LLVMParseBitcodeInContext2`
213+
* `LLVMParseBitcode2` -> `LLVMParseBitcodeInContext2`
214+
* `LLVMGetBitcodeModule` -> `LLVMGetBitcodeModuleInContext2`
215+
* `LLVMGetBitcodeModule2` -> `LLVMGetBitcodeModuleInContext2`
181216

182217
Changes to the CodeGen infrastructure
183218
-------------------------------------

llvm/examples/OrcV2Examples/OrcV2CBindingsAddObjectFile/OrcV2CBindingsAddObjectFile.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@ LLVMModuleRef createDemoModule(LLVMContextRef Ctx) {
2828

2929
// Add a "sum" function":
3030
// - Create the function type and function instance.
31-
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
32-
LLVMTypeRef SumFunctionType =
33-
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
31+
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
32+
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
33+
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
3434
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
3535

3636
// - Add a basic block to the function.
37-
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
37+
LLVMBasicBlockRef EntryBB =
38+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
3839

3940
// - Add an IR builder and point it at the end of the basic block.
40-
LLVMBuilderRef Builder = LLVMCreateBuilder();
41+
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
4142
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4243

4344
// - Get the two function arguments and use them co construct an "add"

llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
3030

3131
// Add a "sum" function":
3232
// - Create the function type and function instance.
33-
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
34-
LLVMTypeRef SumFunctionType =
35-
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
33+
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
34+
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
35+
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
3636
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
3737

3838
// - Add a basic block to the function.
39-
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
39+
LLVMBasicBlockRef EntryBB =
40+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
4041

4142
// - Add an IR builder and point it at the end of the basic block.
42-
LLVMBuilderRef Builder = LLVMCreateBuilder();
43+
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
4344
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4445

4546
// - Get the two function arguments and use them co construct an "add"

llvm/examples/OrcV2Examples/OrcV2CBindingsDumpObjects/OrcV2CBindingsDumpObjects.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,13 @@ int handleError(LLVMErrorRef Err) {
3333
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
3434
LLVMContextRef Ctx = LLVMContextCreate();
3535
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
36-
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
37-
LLVMTypeRef SumFunctionType =
38-
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
36+
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
37+
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
38+
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
3939
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
40-
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
41-
LLVMBuilderRef Builder = LLVMCreateBuilder();
40+
LLVMBasicBlockRef EntryBB =
41+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
42+
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
4243
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4344
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
4445
LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);

llvm/examples/OrcV2Examples/OrcV2CBindingsIRTransforms/OrcV2CBindingsIRTransforms.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ int handleError(LLVMErrorRef Err) {
3434
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
3535
LLVMContextRef Ctx = LLVMContextCreate();
3636
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
37-
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
38-
LLVMTypeRef SumFunctionType =
39-
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
37+
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
38+
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
39+
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
4040
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
41-
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
42-
LLVMBuilderRef Builder = LLVMCreateBuilder();
41+
LLVMBasicBlockRef EntryBB =
42+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
43+
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
4344
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4445
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);
4546
LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);

llvm/examples/OrcV2Examples/OrcV2CBindingsMCJITLikeMemoryManager/OrcV2CBindingsMCJITLikeMemoryManager.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,17 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
158158

159159
// Add a "sum" function":
160160
// - Create the function type and function instance.
161-
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
162-
LLVMTypeRef SumFunctionType =
163-
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
161+
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
162+
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
163+
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
164164
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
165165

166166
// - Add a basic block to the function.
167-
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
167+
LLVMBasicBlockRef EntryBB =
168+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
168169

169170
// - Add an IR builder and point it at the end of the basic block.
170-
LLVMBuilderRef Builder = LLVMCreateBuilder();
171+
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
171172
LLVMPositionBuilderAtEnd(Builder, EntryBB);
172173

173174
// - Get the two function arguments and use them co construct an "add"

llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,17 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
3030

3131
// Add a "sum" function":
3232
// - Create the function type and function instance.
33-
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
34-
LLVMTypeRef SumFunctionType =
35-
LLVMFunctionType(LLVMInt32Type(), ParamTypes, 2, 0);
33+
LLVMTypeRef Int32Type = LLVMInt32TypeInContext(Ctx);
34+
LLVMTypeRef ParamTypes[] = {Int32Type, Int32Type};
35+
LLVMTypeRef SumFunctionType = LLVMFunctionType(Int32Type, ParamTypes, 2, 0);
3636
LLVMValueRef SumFunction = LLVMAddFunction(M, "sum", SumFunctionType);
3737

3838
// - Add a basic block to the function.
39-
LLVMBasicBlockRef EntryBB = LLVMAppendBasicBlock(SumFunction, "entry");
39+
LLVMBasicBlockRef EntryBB =
40+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
4041

4142
// - Add an IR builder and point it at the end of the basic block.
42-
LLVMBuilderRef Builder = LLVMCreateBuilder();
43+
LLVMBuilderRef Builder = LLVMCreateBuilderInContext(Ctx);
4344
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4445

4546
// - Get the two function arguments and use them co construct an "add"

llvm/include/llvm-c/BitReader.h

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef LLVM_C_BITREADER_H
2020
#define LLVM_C_BITREADER_H
2121

22+
#include "llvm-c/Deprecated.h"
2223
#include "llvm-c/ExternC.h"
2324
#include "llvm-c/Types.h"
2425
#include "llvm-c/Visibility.h"
@@ -37,14 +38,19 @@ LLVM_C_EXTERN_C_BEGIN
3738
Optionally returns a human-readable error message via OutMessage.
3839
3940
This is deprecated. Use LLVMParseBitcode2. */
40-
LLVM_C_ABI LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
41-
LLVMModuleRef *OutModule,
42-
char **OutMessage);
41+
LLVM_C_ABI LLVM_ATTRIBUTE_C_DEPRECATED(
42+
LLVMBool LLVMParseBitcode(LLVMMemoryBufferRef MemBuf,
43+
LLVMModuleRef *OutModule, char **OutMessage),
44+
"Use of the global context is deprecated, use LLVMParseBitcodeInContext2 "
45+
"instead");
4346

4447
/* Builds a module from the bitcode in the specified memory buffer, returning a
4548
reference to the module via the OutModule parameter. Returns 0 on success. */
46-
LLVM_C_ABI LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
47-
LLVMModuleRef *OutModule);
49+
LLVM_C_ABI LLVM_ATTRIBUTE_C_DEPRECATED(
50+
LLVMBool LLVMParseBitcode2(LLVMMemoryBufferRef MemBuf,
51+
LLVMModuleRef *OutModule),
52+
"Use of the global context is deprecated, use LLVMParseBitcodeInContext2 "
53+
"instead");
4854

4955
/* This is deprecated. Use LLVMParseBitcodeInContext2. */
5056
LLVM_C_ABI LLVMBool LLVMParseBitcodeInContext(LLVMContextRef ContextRef,
@@ -77,12 +83,17 @@ LLVM_C_ABI LLVMBool LLVMGetBitcodeModuleInContext2(LLVMContextRef ContextRef,
7783
LLVMModuleRef *OutM);
7884

7985
/* This is deprecated. Use LLVMGetBitcodeModule2. */
80-
LLVM_C_ABI LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf,
81-
LLVMModuleRef *OutM,
82-
char **OutMessage);
83-
84-
LLVM_C_ABI LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
85-
LLVMModuleRef *OutM);
86+
LLVM_C_ABI LLVM_ATTRIBUTE_C_DEPRECATED(
87+
LLVMBool LLVMGetBitcodeModule(LLVMMemoryBufferRef MemBuf,
88+
LLVMModuleRef *OutM, char **OutMessage),
89+
"Use of the global context is deprecated, use "
90+
"LLVMGetBitcodeModuleInContext2 instead");
91+
92+
LLVM_C_ABI LLVM_ATTRIBUTE_C_DEPRECATED(
93+
LLVMBool LLVMGetBitcodeModule2(LLVMMemoryBufferRef MemBuf,
94+
LLVMModuleRef *OutM),
95+
"Use of the global context is deprecated, use "
96+
"LLVMGetBitcodeModuleInContext2 instead");
8697

8798
/**
8899
* @}

0 commit comments

Comments
 (0)