From 08adb7bb9da342e835449b3f3be444281e2ccdcc Mon Sep 17 00:00:00 2001 From: Dave Airlie Date: Wed, 3 Aug 2022 14:50:19 +1000 Subject: [PATCH] gallivm: fix printf hook for cached shaders. I've noticed this before but never tracked it down, but it's annoying. The printf hooks would crash with debug shaders when they were loaded from the cache. This was because nothing was initing the printf hook in the cached path so the global was never set. No problems just always creating this afaics. Fixes: 333ee94285ac ("gallivm: rework debug printf hook to use global mapping.") Reviewed-by: Mike Blumenkrantz Part-of: (cherry picked from commit 4c0a7a169dd3b929352d8c61f3e47abc2b5628ea) --- .pick_status.json | 2 +- src/gallium/auxiliary/gallivm/lp_bld_init.c | 6 ++++-- src/gallium/auxiliary/gallivm/lp_bld_printf.c | 13 +++++++++---- src/gallium/auxiliary/gallivm/lp_bld_printf.h | 1 + 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/.pick_status.json b/.pick_status.json index 421b56bfc56..04649fa6f2d 100644 --- a/.pick_status.json +++ b/.pick_status.json @@ -310,7 +310,7 @@ "description": "gallivm: fix printf hook for cached shaders.", "nominated": true, "nomination_type": 1, - "resolution": 0, + "resolution": 1, "main_sha": null, "because_sha": "333ee94285ac453b9d75ce93b01bc26e48bf96d7" }, diff --git a/src/gallium/auxiliary/gallivm/lp_bld_init.c b/src/gallium/auxiliary/gallivm/lp_bld_init.c index 2ed81d9ae5d..ca50a6debbd 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_init.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_init.c @@ -36,6 +36,7 @@ #include "lp_bld_debug.h" #include "lp_bld_misc.h" #include "lp_bld_init.h" +#include "lp_bld_printf.h" #include #include @@ -689,8 +690,9 @@ gallivm_compile_module(struct gallivm_state *gallivm) ++gallivm->compiled; - if (gallivm->debug_printf_hook) - LLVMAddGlobalMapping(gallivm->engine, gallivm->debug_printf_hook, debug_printf); + lp_init_printf_hook(gallivm); + LLVMAddGlobalMapping(gallivm->engine, gallivm->debug_printf_hook, debug_printf); + if (gallivm_debug & GALLIVM_DEBUG_ASM) { LLVMValueRef llvm_func = LLVMGetFirstFunction(gallivm->module); diff --git a/src/gallium/auxiliary/gallivm/lp_bld_printf.c b/src/gallium/auxiliary/gallivm/lp_bld_printf.c index 5e08a0c2fe6..6c514838f0e 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_printf.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_printf.c @@ -38,6 +38,13 @@ #include "lp_bld_printf.h" #include "lp_bld_type.h" +void lp_init_printf_hook(struct gallivm_state *gallivm) +{ + if (gallivm->debug_printf_hook) + return; + LLVMTypeRef printf_type = LLVMFunctionType(LLVMInt32TypeInContext(gallivm->context), NULL, 0, 1); + gallivm->debug_printf_hook = LLVMAddFunction(gallivm->module, "debug_printf", printf_type); +} /** * Generates LLVM IR to call debug_printf. @@ -63,10 +70,8 @@ lp_build_print_args(struct gallivm_state* gallivm, args[i] = LLVMBuildFPExt(builder, args[i], LLVMDoubleTypeInContext(context), ""); } - LLVMTypeRef printf_type = LLVMFunctionType(LLVMInt32TypeInContext(context), NULL, 0, 1); - if (!gallivm->debug_printf_hook) { - gallivm->debug_printf_hook = LLVMAddFunction(gallivm->module, "debug_printf", printf_type); - } + lp_init_printf_hook(gallivm); + LLVMTypeRef printf_type = LLVMFunctionType(LLVMInt32TypeInContext(gallivm->context), NULL, 0, 1); return LLVMBuildCall2(builder, printf_type, gallivm->debug_printf_hook, args, argcount, ""); } diff --git a/src/gallium/auxiliary/gallivm/lp_bld_printf.h b/src/gallium/auxiliary/gallivm/lp_bld_printf.h index 298b4033f08..e0191264f11 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_printf.h +++ b/src/gallium/auxiliary/gallivm/lp_bld_printf.h @@ -36,6 +36,7 @@ extern "C" { #include "lp_bld.h" #include "lp_bld_init.h" +void lp_init_printf_hook(struct gallivm_state *gallivm); LLVMValueRef lp_build_printf(struct gallivm_state *gallivm,