From 5370c685da4790834671e88bedbb0f15610e9bef Mon Sep 17 00:00:00 2001 From: Dave Airlie Date: Wed, 26 Feb 2020 12:10:54 +1000 Subject: [PATCH] gallivm/nir: fix integer divide SIGFPE Blender was crashing with a SIGFPE even though the divide by 0 logic was kicking in. I'm not sure why TGSI doesn't get into this state. The problem was is the numerator was INT_MIN we'd replace the div by 0 with a divide by -1, which is an exception for INT_MIN as INT_MIN/-1 == INT_MAX + 1 (too large for 32-bits). Instead for integer divides just replace the mask values with 0x7fffffff. Also fix up the result handling so it aligns with TGSI usage. (gives 0) Fixes: c717ac1247c3 ("gallivm/nir: wrap idiv to avoid divide by 0 (v2)") Reviewed-by: Roland Scheidegger Part-of: --- src/gallium/auxiliary/gallivm/lp_bld_nir.c | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/gallium/auxiliary/gallivm/lp_bld_nir.c b/src/gallium/auxiliary/gallivm/lp_bld_nir.c index f6e84855bf5..c9f67226dcc 100644 --- a/src/gallium/auxiliary/gallivm/lp_bld_nir.c +++ b/src/gallium/auxiliary/gallivm/lp_bld_nir.c @@ -389,15 +389,26 @@ do_int_divide(struct lp_build_nir_context *bld_base, struct gallivm_state *gallivm = bld_base->base.gallivm; LLVMBuilderRef builder = gallivm->builder; struct lp_build_context *int_bld = get_int_bld(bld_base, is_unsigned, src_bit_size); - LLVMValueRef div_mask = lp_build_cmp(int_bld, PIPE_FUNC_EQUAL, src2, - int_bld->zero); + struct lp_build_context *mask_bld = get_int_bld(bld_base, true, src_bit_size); + LLVMValueRef div_mask = lp_build_cmp(mask_bld, PIPE_FUNC_EQUAL, src2, + mask_bld->zero); + + if (!is_unsigned) { + /* INT_MIN (0x80000000) / -1 (0xffffffff) causes sigfpe, seen with blender. */ + div_mask = LLVMBuildAnd(builder, div_mask, lp_build_const_int_vec(gallivm, int_bld->type, 0x7fffffff), ""); + } LLVMValueRef divisor = LLVMBuildOr(builder, div_mask, src2, ""); LLVMValueRef result = lp_build_div(int_bld, src, divisor); - /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10 - * may as well do same for idiv */ - return LLVMBuildOr(builder, div_mask, result, ""); + + if (!is_unsigned) { + LLVMValueRef not_div_mask = LLVMBuildNot(builder, div_mask, ""); + return LLVMBuildAnd(builder, not_div_mask, result, ""); + } else + /* udiv by zero is guaranteed to return 0xffffffff at least with d3d10 + * may as well do same for idiv */ + return LLVMBuildOr(builder, div_mask, result, ""); } static LLVMValueRef do_alu_action(struct lp_build_nir_context *bld_base, -- 2.30.2