diff options
author | Dave Airlie <[email protected]> | 2020-02-26 12:10:54 +1000 |
---|---|---|
committer | Marge Bot <[email protected]> | 2020-02-26 23:20:01 +0000 |
commit | 5370c685da4790834671e88bedbb0f15610e9bef (patch) | |
tree | f73b218bf2d7d722afc13c0df937b0926f36d1d8 /src/gallium/auxiliary/gallivm/lp_bld_nir.c | |
parent | 954cf8e86b6e0d52c04098604d2daa4305bf6f70 (diff) |
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 <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3956>
Diffstat (limited to 'src/gallium/auxiliary/gallivm/lp_bld_nir.c')
-rw-r--r-- | src/gallium/auxiliary/gallivm/lp_bld_nir.c | 21 |
1 files 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, |