diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index b3d7a67433b..8e2c4390dd6 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -2411,7 +2411,18 @@ agx_compile_shader_nir(nir_shader *nir, struct agx_shader_key *key, */ if (key->fs.nr_samples) { assert(nir->info.stage == MESA_SHADER_FRAGMENT); - NIR_PASS_V(nir, agx_nir_lower_sample_mask, key->fs.nr_samples); + + if (agx_nir_lower_sample_mask(nir, key->fs.nr_samples)) { + /* Clean up ixor(bcsel) patterns created from sample mask lowering. + * If this succeeds, we'll have expressions to constant fold to get the + * benefit. We need to rescalarize after folding constants. + */ + if (agx_nir_opt_ixor_bcsel(nir)) { + NIR_PASS_V(nir, nir_opt_constant_folding); + NIR_PASS_V(nir, nir_lower_load_const_to_scalar); + NIR_PASS_V(nir, nir_opt_dce); + } + } } /* Must be last since NIR passes can remap driver_location freely */ diff --git a/src/asahi/compiler/agx_nir.h b/src/asahi/compiler/agx_nir.h index 8775f2c1846..293ab4a341d 100644 --- a/src/asahi/compiler/agx_nir.h +++ b/src/asahi/compiler/agx_nir.h @@ -9,6 +9,7 @@ struct nir_shader; +bool agx_nir_opt_ixor_bcsel(struct nir_shader *shader); bool agx_nir_lower_algebraic_late(struct nir_shader *shader); bool agx_nir_fuse_algebraic_late(struct nir_shader *shader); diff --git a/src/asahi/compiler/agx_nir_algebraic.py b/src/asahi/compiler/agx_nir_algebraic.py index 9017589ad6d..639cba5fd07 100644 --- a/src/asahi/compiler/agx_nir_algebraic.py +++ b/src/asahi/compiler/agx_nir_algebraic.py @@ -121,6 +121,12 @@ for s in range(1, 5): (('ishl', a, s), iaddshl(0, a, s)), ] +# Discard lowering generates this pattern, clean it up +ixor_bcsel = [ + (('ixor', ('bcsel', a, '#b', '#c'), '#d'), + ('bcsel', a, ('ixor', b, d), ('ixor', c, d))), +] + def main(): parser = argparse.ArgumentParser() parser.add_argument('-p', '--import-path', required=True) @@ -137,6 +143,8 @@ def run(): lower_sm5_shift + lower_pack).render()) print(nir_algebraic.AlgebraicPass("agx_nir_fuse_algebraic_late", fuse_imad).render()) + print(nir_algebraic.AlgebraicPass("agx_nir_opt_ixor_bcsel", + ixor_bcsel).render()) if __name__ == '__main__':