From 057c7c9f530be05559755f1ae125ac4184ae3f61 Mon Sep 17 00:00:00 2001 From: Ian Romanick Date: Wed, 4 Sep 2024 14:16:12 -0700 Subject: [PATCH] nir/algebraic: Recognize open-coded bitfield_reverse in XCOM 2 The XCOM 2 shaders in my shader-db use iadd instead of ior. No fossil-db changes on any Intel platform. shader-db: All Intel platforms had similar results. (Meteor Lake shown) total instructions in shared programs: 19787210 -> 19787034 (<.01%) instructions in affected programs: 1187 -> 1011 (-14.83%) helped: 6 / HURT: 0 total cycles in shared programs: 906024436 -> 906012612 (<.01%) cycles in affected programs: 72978 -> 61154 (-16.20%) helped: 6 / HURT: 0 Reviewed-by: Georg Lehmann Part-of: --- src/compiler/nir/nir_opt_algebraic.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/compiler/nir/nir_opt_algebraic.py b/src/compiler/nir/nir_opt_algebraic.py index 927e53afc12..87a11f4fdbc 100644 --- a/src/compiler/nir/nir_opt_algebraic.py +++ b/src/compiler/nir/nir_opt_algebraic.py @@ -2759,6 +2759,16 @@ optimizations += [ (('ldexp@64', 'x', 'exp'), ldexp('x', 'exp', 64), 'options->lower_ldexp'), ] +# XCOM 2 (OpenGL) open-codes bitfieldReverse() +def bitfield_reverse_xcom2(u): + step1 = ('iadd', ('ishl', u, 16), ('ushr', u, 16)) + step2 = ('iadd', ('iand', ('ishl', step1, 1), 0xaaaaaaaa), ('iand', ('ushr', step1, 1), 0x55555555)) + step3 = ('iadd', ('iand', ('ishl', step2, 2), 0xcccccccc), ('iand', ('ushr', step2, 2), 0x33333333)) + step4 = ('iadd', ('iand', ('ishl', step3, 4), 0xf0f0f0f0), ('iand', ('ushr', step3, 4), 0x0f0f0f0f)) + step5 = ('iadd(many-comm-expr)', ('iand', ('ishl', step4, 8), 0xff00ff00), ('iand', ('ushr', step4, 8), 0x00ff00ff)) + + return step5 + # Unreal Engine 4 demo applications open-codes bitfieldReverse() def bitfield_reverse_ue4(u): step1 = ('ior', ('ishl', u, 16), ('ushr', u, 16)) @@ -2779,6 +2789,7 @@ def bitfield_reverse_cp2077(u): return step5 +optimizations += [(bitfield_reverse_xcom2('x@32'), ('bitfield_reverse', 'x'), '!options->lower_bitfield_reverse')] optimizations += [(bitfield_reverse_ue4('x@32'), ('bitfield_reverse', 'x'), '!options->lower_bitfield_reverse')] optimizations += [(bitfield_reverse_cp2077('x@32'), ('bitfield_reverse', 'x'), '!options->lower_bitfield_reverse')]