asahi: Promote constants to uniforms
Certain constants can be inlined into instructions, agx_optimizer
handles these constants. After optimizing, we may be left with some constants that cannot be optimized away. Currently, the AGX compiler emits explicit moves for these. However, most AGX instructions can access "uniform registers" for free. Uniform registers are mainly allocated by agx_uniforms
and our implementation of nir_opt_preamble
(grep src/asahi/compiler/
for these keywords). However, after instruction selection and optimization, we may have unused uniform registers and outstanding constants. In this case, the AGX compiler should promote the constant to a uniform register, eliminating the mov_imm
instruction.
Concretely, this requires:
-
GTest-based unit tests ( src/asahi/compiler/test/
) to test the new compiler pass you're about to write. The tests should cover both positive (promoting constants when it is beneficial) and negative (not promoting constants when it is impossible or harmful) cases. In particular, if a constant is only used by instructions that cannot accept uniform registers (seeagx_optimizer
for that determination), it's not beneficial to promote. Also, if there are too many uniform registers, we have to stop promoting. -
A compiler pass implementing the promotion and rewriting. In addition to rewriting the program, the pass should update ctx->out->push
to tell the driver about the needed uniforms. You'll need to extend agx_push to accept a block of constants. Ideally, you could push all the constants with a singleagx_push
record, using contiguous uniform registers. That will reduce driver overhead. Seesrc/asahi/compiler/agx_compile.h
andsrc/asahi/compiler/agx_uniforms.c
-
Extending the driver to handle the "constant data as uniforms" type AGX_PUSH_CONSTANTS
now passed inagx_shader_info::push[]
from your compiler pass. This just needs to copy the constant into the memory to be pushed as uniforms, so the hardware gets the constants now that they're not passed in the shader. That's a small addition tosrc/gallium/drivers/asahi/agx_uniforms.c
.