nir: Model AGX-specific multiply-shift-add
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Tue, 25 Apr 2023 18:37:07 +0000 (14:37 -0400)
committerAlyssa Rosenzweig <alyssa@rosenzweig.io>
Thu, 11 May 2023 13:23:09 +0000 (09:23 -0400)
Models `(a * b) + (c << d)` in general, as implemented in various forms on AGX.
This will be fused with backend NIR opt algebraic rules, both for the literal
pattern as well as to strength reduce certain multiplications, e.g. replacing
a * 5 with `a + (a << 2)` expressed as imadshl_agx(a, 1, a, 2).

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Reviewed-by: Asahi Lina <lina@asahilina.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22695>

src/compiler/nir/nir_opcodes.py

index c3c4ea2..3621ea2 100644 (file)
@@ -1298,6 +1298,14 @@ opcode("extr_agx", 0, tuint32,
     }
 """);
 
+# AGX multiply-shift-add. Corresponds to iadd/isub/imad/imsub instructions.
+# The shift must be <= 4 (domain restriction). For performance, it should be
+# constant.
+opcode("imadshl_agx", 0, tint, [0, 0, 0, 0], [tint, tint, tint, tint], False,
+       "", f"(src0 * src1) + (src2 << src3)")
+opcode("imsubshl_agx", 0, tint, [0, 0, 0, 0], [tint, tint, tint, tint], False,
+       "", f"(src0 * src1) - (src2 << src3)")
+
 # 24b multiply into 32b result (with sign extension)
 binop("imul24", tint32, _2src_commutative + associative,
       "(((int32_t)src0 << 8) >> 8) * (((int32_t)src1 << 8) >> 8)")