Mesa (staging/20.0): gallivm/nir: fix integer divide SIGFPE

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Feb 28 22:44:47 UTC 2020


Module: Mesa
Branch: staging/20.0
Commit: 981b2b6241a6b45c88d72f614d4ac66bc45dbf76
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=981b2b6241a6b45c88d72f614d4ac66bc45dbf76

Author: Dave Airlie <airlied at redhat.com>
Date:   Wed Feb 26 12:10:54 2020 +1000

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 <sroland at vmware.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/3956>
(cherry picked from commit 5370c685da4790834671e88bedbb0f15610e9bef)

---

 .pick_status.json                          |  2 +-
 src/gallium/auxiliary/gallivm/lp_bld_nir.c | 21 ++++++++++++++++-----
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/.pick_status.json b/.pick_status.json
index 79ad3f2d509..d039970d049 100644
--- a/.pick_status.json
+++ b/.pick_status.json
@@ -1102,7 +1102,7 @@
         "description": "gallivm/nir: fix integer divide SIGFPE",
         "nominated": true,
         "nomination_type": 1,
-        "resolution": 0,
+        "resolution": 1,
         "master_sha": null,
         "because_sha": "c717ac1247c3c7243af2f1b2732cccb882b75f11"
     },
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,



More information about the mesa-commit mailing list