Mesa (main): nir/algebraic: optimize more 64-bit imul with constant source

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Dec 17 19:40:57 UTC 2021


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

Author: Rhys Perry <pendingchaos02 at gmail.com>
Date:   Tue Mar  9 16:51:25 2021 +0000

nir/algebraic: optimize more 64-bit imul with constant source

Two 64-bit shifts and an addition are usually faster than the several
multiplications nir_lower_int64 creates.

No fossil-db changes.

Signed-off-by: Rhys Perry <pendingchaos02 at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/14227>

---

 src/compiler/nir/nir_opt_algebraic.py |  3 +++
 src/compiler/nir/nir_search_helpers.h | 18 ++++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py
index 58d5ee29ae5..1fd7aa4ce7a 100644
--- a/src/compiler/nir/nir_opt_algebraic.py
+++ b/src/compiler/nir/nir_opt_algebraic.py
@@ -100,6 +100,9 @@ optimizations = [
    (('imul', a, '#b(is_neg_power_of_two)'), ('ineg', ('ishl', a, ('find_lsb', ('iabs', b)))), '!options->lower_bitops'),
    (('ishl', a, '#b'), ('imul', a, ('ishl', 1, b)), 'options->lower_bitops'),
 
+   (('imul at 64', a, '#b(is_bitcount2)'), ('iadd', ('ishl', a, ('ufind_msb', b)), ('ishl', a, ('find_lsb', b))),
+    '!options->lower_bitops && (options->lower_int64_options & (nir_lower_imul64 | nir_lower_shift64)) == nir_lower_imul64'),
+
    (('unpack_64_2x32_split_x', ('imul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
    (('unpack_64_2x32_split_x', ('umul_2x32_64(is_used_once)', a, b)), ('imul', a, b)),
    (('imul_2x32_64', a, b), ('pack_64_2x32_split', ('imul', a, b), ('imul_high', a, b)), 'options->lower_mul_2x32_64'),
diff --git a/src/compiler/nir/nir_search_helpers.h b/src/compiler/nir/nir_search_helpers.h
index 63531291c96..5048167bde6 100644
--- a/src/compiler/nir/nir_search_helpers.h
+++ b/src/compiler/nir/nir_search_helpers.h
@@ -93,6 +93,24 @@ is_neg_power_of_two(UNUSED struct hash_table *ht, const nir_alu_instr *instr,
    return true;
 }
 
+static inline bool
+is_bitcount2(UNUSED struct hash_table *ht, const nir_alu_instr *instr,
+             unsigned src, unsigned num_components,
+             const uint8_t *swizzle)
+{
+   /* only constant srcs: */
+   if (!nir_src_is_const(instr->src[src].src))
+      return false;
+
+   for (unsigned i = 0; i < num_components; i++) {
+      uint64_t val = nir_src_comp_as_uint(instr->src[src].src, swizzle[i]);
+      if (util_bitcount64(val) != 2)
+         return false;
+   }
+
+   return true;
+}
+
 #define MULTIPLE(test)                                                  \
 static inline bool                                                      \
 is_unsigned_multiple_of_ ## test(UNUSED struct hash_table *ht,          \



More information about the mesa-commit mailing list