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: c717ac1247 ("gallivm/nir: wrap idiv to avoid divide by 0 (v2)")
Reviewed-by: Roland Scheidegger <sroland@vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3956>
This commit is contained in:
Dave Airlie 2020-02-26 12:10:54 +10:00 committed by Marge Bot
parent 954cf8e86b
commit 5370c685da

View file

@ -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,