From fc8a83c96dfa25f0f1fe841c98ed48d668cfde03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C5=82a=C5=BCej=20Szczygie=C5=82?= Date: Fri, 26 Jan 2024 14:07:12 +0100 Subject: [PATCH] gallivm/ssbo: mask offset with exec_mask instead of building the 'if' It prevents reading the invalid out-of-bounds offset if exec_mask is 0 without building the 'if' condition. We can do it only when reading the memory. It's faster than run time 'if' condition Reviewed-by: Konstantin Seurer Part-of: --- src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c b/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c index aa97744c2c2..44d9921aff4 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_nir_soa.c @@ -1432,13 +1432,12 @@ static void emit_load_mem(struct lp_build_nir_context *bld_base, result[c] = lp_build_alloca(gallivm, load_bld->vec_type, ""); LLVMValueRef exec_mask = mask_vec(bld_base); - LLVMValueRef cond = LLVMBuildICmp(gallivm->builder, LLVMIntNE, exec_mask, uint_bld->zero, ""); + + /* mask the offset to prevent invalid reads */ + offset = LLVMBuildAnd(gallivm->builder, offset, exec_mask, ""); + for (unsigned i = 0; i < uint_bld->type.length; i++) { LLVMValueRef counter = lp_build_const_int32(gallivm, i); - LLVMValueRef loop_cond = LLVMBuildExtractElement(gallivm->builder, cond, counter, ""); - - struct lp_build_if_state exec_ifthen; - lp_build_if(&exec_ifthen, gallivm, loop_cond); LLVMValueRef ssbo_limit; LLVMValueRef mem_ptr = mem_access_base_pointer(bld_base, load_bld, bit_size, payload, index, @@ -1472,8 +1471,6 @@ static void emit_load_mem(struct lp_build_nir_context *bld_base, LLVMBuildStore(builder, temp_res, result[c]); lp_build_endif(&ifthen); } - - lp_build_endif(&exec_ifthen); } for (unsigned c = 0; c < nc; c++) outval[c] = LLVMBuildLoad2(gallivm->builder, load_bld->vec_type, result[c], "");