Skip to content

Commit 3ee60c5

Browse files
committed
[llvm-c] Deprecate functions working on the global context
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 functions of that kind. However, the fact that these still exist (and have shorter names) continues to cause issues. This PR proposes to deprecate these APIs, with intent of future removal.
1 parent ac40b78 commit 3ee60c5

File tree

16 files changed

+352
-178
lines changed

16 files changed

+352
-178
lines changed

llvm/docs/ReleaseNotes.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,40 @@ Changes to the C API
153153

154154
* Add `LLVMGetOrInsertFunction` to get or insert a function, replacing the combination of `LLVMGetNamedFunction` and `LLVMAddFunction`.
155155
* Allow `LLVMGetVolatile` to work with any kind of Instruction.
156+
* Functions working on the global context have been deprecated. Use the
157+
functions that work on a specific context instead.
158+
159+
* `LLVMGetGlobalContext` -> use `LLVMContextCreate` context instead
160+
* `LLVMInt1Type` -> `LLVMInt1TypeInContext`
161+
* `LLVMInt8Type` -> `LLVMInt8TypeInContext`
162+
* `LLVMInt16Type` -> `LLVMInt16TypeInContext`
163+
* `LLVMInt32Type` -> `LLVMInt32TypeInContext`
164+
* `LLVMInt64Type` -> `LLVMInt64TypeInContext`
165+
* `LLVMInt128Type` -> `LLVMInt128TypeInContext`
166+
* `LLVMIntType` -> `LLVMIntTypeInContext`
167+
* `LLVMHalfType` -> `LLVMHalfTypeInContext`
168+
* `LLVMBFloatType` -> `LLVMBFloatTypeInContext`
169+
* `LLVMFloatType` -> `LLVMFloatTypeInContext`
170+
* `LLVMDoubleType` -> `LLVMDoubleTypeInContext`
171+
* `LLVMX86FP80Type` -> `LLVMX86FP80TypeInContext`
172+
* `LLVMFP128Type` -> `LLVMFP128TypeInContext`
173+
* `LLVMPPCFP128Type` -> `LLVMPPCFP128TypeInContext`
174+
* `LLVMStructType` -> `LLVMStructTypeInContext`
175+
* `LLVMVoidType` -> `LLVMVoidTypeInContext`
176+
* `LLVMLabelType` -> `LLVMLabelTypeInContext`
177+
* `LLVMX86AMXType` -> `LLVMX86AMXTypeInContext`
178+
* `LLVMConstString` -> `LLVMConstStringInContext2`
179+
* `LLVMConstStruct` -> `LLVMConstStructInContext`
180+
* `LLVMMDString` -> `LLVMMDStringInContext2`
181+
* `LLVMMDNode` -> `LLVMMDNodeInContext2`
182+
* `LLVMAppendBasicBlock` -> `LLVMAppendBasicBlockInContext`
183+
* `LLVMInsertBasicBlock` -> `LLVMInsertBasicBlockInContext`
184+
* `LLVMIntPtrType` -> `LLVMIntPtrTypeInContext`
185+
* `LLVMIntPtrTypeForAS` -> `LLVMIntPtrTypeForASInContext`
186+
* `LLVMParseBitcode` -> `LLVMParseBitcodeInContext2`
187+
* `LLVMParseBitcode2` -> `LLVMParseBitcodeInContext2`
188+
* `LLVMGetBitcodeModule` -> `LLVMGetBitcodeModuleInContext2`
189+
* `LLVMGetBitcodeModule2` -> `LLVMGetBitcodeModuleInContext2`
156190

157191
Changes to the CodeGen infrastructure
158192
-------------------------------------

llvm/examples/OrcV2Examples/OrcV2CBindingsAddObjectFile/OrcV2CBindingsAddObjectFile.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ 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.
4041
LLVMBuilderRef Builder = LLVMCreateBuilder();

llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ 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.
4243
LLVMBuilderRef Builder = LLVMCreateBuilder();

llvm/examples/OrcV2Examples/OrcV2CBindingsDumpObjects/OrcV2CBindingsDumpObjects.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,12 @@ 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");
40+
LLVMBasicBlockRef EntryBB =
41+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
4142
LLVMBuilderRef Builder = LLVMCreateBuilder();
4243
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4344
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);

llvm/examples/OrcV2Examples/OrcV2CBindingsIRTransforms/OrcV2CBindingsIRTransforms.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ 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");
41+
LLVMBasicBlockRef EntryBB =
42+
LLVMAppendBasicBlockInContext(Ctx, SumFunction, "entry");
4243
LLVMBuilderRef Builder = LLVMCreateBuilder();
4344
LLVMPositionBuilderAtEnd(Builder, EntryBB);
4445
LLVMValueRef SumArg0 = LLVMGetParam(SumFunction, 0);

llvm/examples/OrcV2Examples/OrcV2CBindingsMCJITLikeMemoryManager/OrcV2CBindingsMCJITLikeMemoryManager.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,14 @@ 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.
170171
LLVMBuilderRef Builder = LLVMCreateBuilder();

llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ 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.
4243
LLVMBuilderRef Builder = LLVMCreateBuilder();

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)