Skip to content

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

Merged
xushiwei merged 2 commits into
xgofrom
main
Aug 31, 2026
Merged

go-llvm: use context-aware LLVM C APIs for functions deprecated in LLVM 22#50
xushiwei merged 2 commits into
xgofrom
main

Conversation

@xushiwei

Copy link
Copy Markdown

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.

deadprogram and others added 2 commits July 21, 2026 09:29
…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>

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review: Wrap deprecated LLVMGetGlobalContext + delegation refactor

Overall: Well-scoped, high-quality change. The deprecated.c/deprecated.h shim cleanly isolates the single deprecated LLVM 22 call (LLVMGetGlobalContext) that has no non-deprecated replacement, using a properly balanced #pragma GCC diagnostic push/pop so the suppression doesn't leak. The ir.go refactor centralizes global-context logic by delegating the package-level functions (MDKindID, StructType, ConstString, ConstStruct, AddBasicBlock, InsertBasicBlock) to their Context-receiver counterparts.

Verified:

  • Delegation is behaviorally equivalent to the removed inline implementations — the ...InContext variants with the global context match what the old non-context C APIs did internally. target.go IntPtrType via LLVMIntPtrTypeInContext likewise matches the deprecated LLVMIntPtrType.
  • No leftover references to the raw deprecated symbol outside the wrapper; both ir.go and target.go include deprecated.h.
  • cgo C.CString/defer C.free handling and header guards/extern "C" are correct. No memory-safety or performance concerns introduced (the added Go indirection is on cold IR-construction paths; cgo crossing count is unchanged).

Two minor, non-blocking nits are noted inline.

Findings without inline locations

  • target.go:148: Idiom consistency nit: this site calls C.LLVMGetGlobalContext_wrap() directly, whereas the ir.go refactor routes everything through the GlobalContext() helper. For a single point of truth, consider t.C = C.LLVMIntPtrTypeInContext(GlobalContext().C, td.C). Purely stylistic — not a defect.

Comment thread deprecated.h
//===----------------------------------------------------------------------===//

#ifndef LLVM_BINDINGS_GO_LLVM_DEPRECATED_H
#define LLVM_BINDINGS_GO_LLVM_DEPRECATED_H

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Minor documentation nit: "All other wrapped functions are handled by calling their context-aware counterparts from Go" is a bit misleading — this file wraps exactly one function (LLVMGetGlobalContext_wrap) and contains no other wrappers. The intent is that other deprecated context-less C APIs are avoided by calling their ...InContext counterparts directly from Go, so no additional wrappers are needed here. Consider rewording, e.g.: "The other deprecated context-less C APIs are avoided by calling their context-aware counterparts directly from Go, so no additional wrappers are needed." (Same text in deprecated.c.)

@xushiwei
xushiwei merged commit 67d4e35 into xgo Aug 31, 2026
60 checks passed
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