Skip to content

go-llvm: use context-aware LLVM C APIs for functions deprecated in LLVM 22 - #76

Merged
deadprogram merged 1 commit into
mainfrom
llvm22-deprecated-warnings
Jul 21, 2026
Merged

go-llvm: use context-aware LLVM C APIs for functions deprecated in LLVM 22#76
deadprogram merged 1 commit into
mainfrom
llvm22-deprecated-warnings

Conversation

@deadprogram

@deadprogram deadprogram commented Jul 20, 2026

Copy link
Copy Markdown
Member

LLVM 22 deprecated 8 global-context C API functions in favor of their
context-aware counterparts. Instead of suppressing the warnings, call
the non-deprecated InContext APIs directly.

The non-context Go functions (MDKindID, StructType, ConstString,
ConstStruct, AddBasicBlock, InsertBasicBlock) now delegate to
GlobalContext().(), which already calls the InContext C APIs.
IntPtrType calls LLVMIntPtrTypeInContext directly.

Only LLVMGetGlobalContext still needs a warning-suppression wrapper
in deprecated.c since it has no non-deprecated replacement.

The context-aware C APIs have been available since well before LLVM 14,
so this works across all supported LLVM versions.

Here is an example:

# tinygo.org/x/go-llvm
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMIntPtrType’:
cgo-gcc-prolog:477:2: warning: ‘LLVMIntPtrType’ is deprecated [-Wdeprecated-declarations]
In file included from ../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:18,
                 from ../../../go/pkg/mod/tinygo.org/x/go-llvm@v0.0.0-20260707200325-ddd595b68360/target.go:16:
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Target.h:234:41: note: declared here
  234 | LLVM_ATTRIBUTE_C_DEPRECATED(LLVMTypeRef LLVMIntPtrType(LLVMTargetDataRef TD),
      |                                         ^~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
# tinygo.org/x/go-llvm
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMAppendBasicBlock’:
cgo-gcc-prolog:285:2: warning: ‘LLVMAppendBasicBlock’ is deprecated [-Wdeprecated-declarations]
In file included from ../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:18,
                 from ../../../go/pkg/mod/tinygo.org/x/go-llvm@v0.0.0-20260707200325-ddd595b68360/ir.go:16:
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:3717:23: note: declared here
 3717 |     LLVMBasicBlockRef LLVMAppendBasicBlock(LLVMValueRef Fn, const char *Name),
      |                       ^~~~~~~~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMConstString’:
cgo-gcc-prolog:2925:2: warning: ‘LLVMConstString’ is deprecated [-Wdeprecated-declarations]
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:2489:18: note: declared here
 2489 |     LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
      |                  ^~~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMConstStruct’:
cgo-gcc-prolog:2966:2: warning: ‘LLVMConstStruct’ is deprecated [-Wdeprecated-declarations]
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:2538:18: note: declared here
 2538 |     LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
      |                  ^~~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMGetGlobalContext’:
cgo-gcc-prolog:4229:2: warning: ‘LLVMGetGlobalContext’ is deprecated [-Wdeprecated-declarations]
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:593:44: note: declared here
  593 | LLVM_ATTRIBUTE_C_DEPRECATED(LLVMContextRef LLVMGetGlobalContext(void),
      |                                            ^~~~~~~~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMGetMDKindID’:
cgo-gcc-prolog:4597:2: warning: ‘LLVMGetMDKindID’ is deprecated [-Wdeprecated-declarations]
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:669:14: note: declared here
  669 |     unsigned LLVMGetMDKindID(const char *Name, unsigned SLen),
      |              ^~~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMInsertBasicBlock’:
cgo-gcc-prolog:5664:2: warning: ‘LLVMInsertBasicBlock’ is deprecated [-Wdeprecated-declarations]
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:3739:23: note: declared here
 3739 |     LLVMBasicBlockRef LLVMInsertBasicBlock(LLVMBasicBlockRef InsertBeforeBB,
      |                       ^~~~~~~~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
cgo-gcc-prolog: In function ‘_cgo_2ad2ad839789_Cfunc_LLVMStructType’:
cgo-gcc-prolog:8039:2: warning: ‘LLVMStructType’ is deprecated [-Wdeprecated-declarations]
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Core.h:1580:17: note: declared here
 1580 |     LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes, unsigned ElementCount,
      |                 ^~~~~~~~~~~~~~
../../../work/tinygo/tinygo/llvm-project/llvm/include/llvm-c/Deprecated.h:29:3: note: in definition of macro ‘LLVM_ATTRIBUTE_C_DEPRECATED’
   29 |   decl __attribute__((deprecated))
      |   ^~~~
ok  	github.com/tinygo-org/tinygo/builder	48.021s

…VM 22

LLVM 22 deprecated 8 global-context C API functions in favor of their
context-aware counterparts. Instead of suppressing the warnings, call
the non-deprecated InContext APIs directly.

The non-context Go functions (MDKindID, StructType, ConstString,
ConstStruct, AddBasicBlock, InsertBasicBlock) now delegate to
GlobalContext().<method>(), which already calls the InContext C APIs.
IntPtrType calls LLVMIntPtrTypeInContext directly.

Only LLVMGetGlobalContext still needs a warning-suppression wrapper
in deprecated.c since it has no non-deprecated replacement.

The context-aware C APIs have been available since well before LLVM 14,
so this works across all supported LLVM versions.

Signed-off-by: deadprogram <ron@hybridgroup.com>
@deadprogram
deadprogram force-pushed the llvm22-deprecated-warnings branch from 8a2253c to 60f7e53 Compare July 21, 2026 05:35
@deadprogram deadprogram changed the title go-llvm: suppress deprecated LLVM C API warnings go-llvm: use context-aware LLVM C APIs for functions deprecated in LLVM 22 Jul 21, 2026
@deadprogram

Copy link
Copy Markdown
Member Author

Updated this PR to actually call the context-aware functions, not just surpress the warnings.

@dgryski dgryski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@deadprogram

Copy link
Copy Markdown
Member Author

Thank you for the quick review @dgryski now merging.

@deadprogram
deadprogram merged commit 185673e into main Jul 21, 2026
42 checks passed
@deadprogram
deadprogram deleted the llvm22-deprecated-warnings branch July 21, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants