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 <konstantin.seurer@gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/27302>
This commit is contained in:
Błażej Szczygieł 2024-01-26 14:07:12 +01:00 committed by Marge Bot
parent 9ff3bec484
commit fc8a83c96d

View file

@ -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, ""); result[c] = lp_build_alloca(gallivm, load_bld->vec_type, "");
LLVMValueRef exec_mask = mask_vec(bld_base); 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++) { for (unsigned i = 0; i < uint_bld->type.length; i++) {
LLVMValueRef counter = lp_build_const_int32(gallivm, 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 ssbo_limit;
LLVMValueRef mem_ptr = mem_access_base_pointer(bld_base, load_bld, bit_size, payload, index, 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]); LLVMBuildStore(builder, temp_res, result[c]);
lp_build_endif(&ifthen); lp_build_endif(&ifthen);
} }
lp_build_endif(&exec_ifthen);
} }
for (unsigned c = 0; c < nc; c++) for (unsigned c = 0; c < nc; c++)
outval[c] = LLVMBuildLoad2(gallivm->builder, load_bld->vec_type, result[c], ""); outval[c] = LLVMBuildLoad2(gallivm->builder, load_bld->vec_type, result[c], "");