agx/lower_address: Optimize "shift + constant"
authorAlyssa Rosenzweig <alyssa@rosenzweig.io>
Thu, 2 Mar 2023 05:54:32 +0000 (00:54 -0500)
committerMarge Bot <emma+marge@anholt.net>
Tue, 7 Mar 2023 02:58:35 +0000 (02:58 +0000)
commit6203503196c4837292c8f6501eac3b70e9d7244a
tree44590d0d118640a67070bf0c467370d9cb7a9a9c
parentdccf6f569b6ef63ec20600d844638e561270d6df
agx/lower_address: Optimize "shift + constant"

Optimize address arithmetic of the form

   base + u2u64((index << shift) + const)

into hardware operands

   base, index << (shift - format_shift) + const'

which (if format_shift = shift) can be simply

   base, index + const'

rather than the current naive translation

   base, ((index << shift) + const) >> format_shift

This saves at least one pointless shift. We can't do this optimization with
nir_opt_algebraic, because explicitly optimizing "(a << #b) >> #b" to "a" isn't
sound due to overflow. But there's no overflow issue here, which is what this
whole pass is designed around.

For motivation, this address arithmetic implements "dynamically indexing into an
array inside of a C structure", where the const is the offset of the array
relative to the structure.

Signed-off-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21643>
src/asahi/compiler/agx_nir_lower_address.c