Mesa (main): nir/search: don't consider INT_MIN a negative power-of-two

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Mon Aug 9 11:39:13 UTC 2021


Module: Mesa
Branch: main
Commit: 2bb49e45873d29331e12221ee51ce2644ff83863
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=2bb49e45873d29331e12221ee51ce2644ff83863

Author: Rhys Perry <pendingchaos02 at gmail.com>
Date:   Wed Jul 21 17:13:40 2021 +0100

nir/search: don't consider INT_MIN a negative power-of-two

ineg(INT_MIN)/iabs(INT_MIN) won't work as expected.

No fossil-db changes.

Signed-off-by: Rhys Perry <pendingchaos02 at gmail.com>
Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/12039>

---

 src/compiler/nir/nir_search_helpers.h | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
index a49b1edaa41..24938484377 100644
--- a/src/compiler/nir/nir_search_helpers.h
+++ b/src/compiler/nir/nir_search_helpers.h
@@ -73,12 +73,15 @@ is_neg_power_of_two(UNUSED struct hash_table *ht, const nir_alu_instr *instr,
    if (!nir_src_is_const(instr->src[src].src))
       return false;
 
+   int64_t int_min = u_intN_min(instr->src[src].src.ssa->bit_size);
+
    for (unsigned i = 0; i < num_components; i++) {
       nir_alu_type type = nir_op_infos[instr->op].input_types[src];
       switch (nir_alu_type_get_base_type(type)) {
       case nir_type_int: {
          int64_t val = nir_src_comp_as_int(instr->src[src].src, swizzle[i]);
-         if (val >= 0 || !util_is_power_of_two_or_zero64(-val))
+         /* "int_min" is a power-of-two, but negation can cause overflow. */
+         if (val == int_min || val >= 0 || !util_is_power_of_two_or_zero64(-val))
             return false;
          break;
       }



More information about the mesa-commit mailing list