From 12c82aaa82a6792ffe5e96a9fcf6fa60b289f261 Mon Sep 17 00:00:00 2001 From: Icenowy Zheng Date: Fri, 3 Oct 2025 18:40:58 +0800 Subject: [PATCH] gallivm: orcjit: remember Context in addition to ThreadSafeContext The llvm::orc::ThreadSafeContext object wraps an llvm::Context and keeps its reference. As we are no longer able to squeeze out Context from ThreadSafeContext in LLVM 21, do not let ThreadSafeContext create Context implicitly for LLVM 21, instead explicitly create Context and then remember it. This also eliminates the code creating a Context that is never disposed. Fixes: cd129dbf8af2 ("gallivm: support LLVM 21") Signed-off-by: Icenowy Zheng (cherry picked from commit cc60a7a39d11251c96dafcc4c017be502c06abf5) Part-of: --- .pick_status.json | 2 +- src/gallium/auxiliary/gallivm/lp_bld.h | 20 ++++++++++--------- .../auxiliary/gallivm/lp_bld_init_orc.cpp | 4 ++-- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index fffc0853488..a49d0a62ef9 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -14,7 +14,7 @@ "description": "gallivm: orcjit: remember Context in addition to ThreadSafeContext", "nominated": true, "nomination_type": 2, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "cd129dbf8af2d16b1243f2ce287ff69c6a5dc557", "notes": null diff --git a/src/gallium/auxiliary/gallivm/lp_bld.h b/src/gallium/auxiliary/gallivm/lp_bld.h index 29b5db92410..5bdd1a25e95 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld.h +++ b/src/gallium/auxiliary/gallivm/lp_bld.h @@ -88,10 +88,9 @@ #define LLVMCreateBuilder ILLEGAL_LLVM_FUNCTION typedef struct lp_context_ref { -#if GALLIVM_USE_ORCJIT - LLVMOrcThreadSafeContextRef ref; -#else LLVMContextRef ref; +#if GALLIVM_USE_ORCJIT + LLVMOrcThreadSafeContextRef tsref; #endif bool owned; } lp_context_ref; @@ -101,18 +100,21 @@ lp_context_create(lp_context_ref *context) { assert(context != NULL); #if GALLIVM_USE_ORCJIT - context->ref = LLVMOrcCreateNewThreadSafeContext(); +#if LLVM_VERSION_MAJOR >= 21 + context->ref = LLVMContextCreate(); + /* Ownership of ref is then transferred to tsref */ + context->tsref = LLVMOrcCreateNewThreadSafeContextFromLLVMContext(context->ref); +#else + context->tsref = LLVMOrcCreateNewThreadSafeContext(); + context->ref = LLVMOrcThreadSafeContextGetContext(context->tsref); +#endif #else context->ref = LLVMContextCreate(); #endif context->owned = true; #if LLVM_VERSION_MAJOR == 15 if (context->ref) { -#if GALLIVM_USE_ORCJIT - LLVMContextSetOpaquePointers(LLVMOrcThreadSafeContextGetContext(context->ref), false); -#else LLVMContextSetOpaquePointers(context->ref, false); -#endif } #endif } @@ -123,7 +125,7 @@ lp_context_destroy(lp_context_ref *context) assert(context != NULL); if (context->owned) { #if GALLIVM_USE_ORCJIT - LLVMOrcDisposeThreadSafeContext(context->ref); + LLVMOrcDisposeThreadSafeContext(context->tsref); #else LLVMContextDispose(context->ref); #endif diff --git a/src/gallium/auxiliary/gallivm/lp_bld_init_orc.cpp b/src/gallium/auxiliary/gallivm/lp_bld_init_orc.cpp index e5d8e461dd6..d2a435c0512 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_init_orc.cpp +++ b/src/gallium/auxiliary/gallivm/lp_bld_init_orc.cpp @@ -555,8 +555,8 @@ init_gallivm_state(struct gallivm_state *gallivm, const char *name, gallivm->cache = cache; - gallivm->_ts_context = context->ref; - gallivm->context = LLVMContextCreate(); + gallivm->_ts_context = context->tsref; + gallivm->context = context->ref; gallivm->module_name = LPJit::get_unique_name(name); gallivm->module = LLVMModuleCreateWithNameInContext(gallivm->module_name,