diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 5443f4e509f0..2d013c1b73ec 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -8872,6 +8872,89 @@ agx3_coalesce_uniform_half_stores(agx_context *ctx) * adjacent run of non-coherent loads with identical operation and base; SSA * keeps their consumers attached to the correct result and no serialization * is introduced. */ +/* G16G: after scheduling, move + * float arithmetic computed only from uniforms ahead of the first + * pixel-ordering wait of its block, so it executes outside the ordered colour + * transaction instead of between WAIT_PIX and the tile store. Only fadd, fmul, + * ffma and mov_imm move, and only when every source is a uniform, an + * immediate, or the result of another such instruction. Moved instructions + * keep their relative order and their exact operations, modifiers and + * operands, so results are bit-identical. + * + * A moved instruction reads its uniforms earlier than scheduled, so it must not + * cross a write to the uniform file. The scan of a block stops at the first + * such write after the wait; nothing after it moves. */ +static bool +agx_writes_uniform(const agx_instr *I) +{ + if (I->op == AGX_OPCODE_UNIFORM_STORE) + return true; + agx_foreach_dest(I, d) { + if (I->dest[d].type == AGX_INDEX_UNIFORM) + return true; + } + return false; +} + +bool +agx3_g16_hoist_pixel_independent(agx_context *ctx) +{ + if (ctx->key->dev.chip != AGX_CHIP_G16G || + ctx->stage != MESA_SHADER_FRAGMENT || ctx->is_preamble) + return false; + + /* Values computed only from uniforms and immediates by the movable ops, + * wherever they are in the block. Texture, varying and tile results never + * qualify, so per-pixel work keeps its scheduled place. */ + BITSET_WORD *uniform = BITSET_CALLOC(ctx->alloc); + if (!uniform) + return false; + + bool progress = false; + agx_foreach_block(ctx, block) { + agx_instr *wait = NULL; + agx_foreach_instr_in_block_safe(block, I) { + if (!wait && I->op == AGX_OPCODE_WAIT_PIX) { + wait = I; + continue; + } + + if (wait && agx_writes_uniform(I)) + break; + + bool uniform_only = (I->op == AGX_OPCODE_FADD || + I->op == AGX_OPCODE_FMUL || + I->op == AGX_OPCODE_FFMA || + I->op == AGX_OPCODE_MOV_IMM) && + I->nr_dests == 1 && + I->dest[0].type == AGX_INDEX_NORMAL; + agx_foreach_src(I, s) { + const agx_index src = I->src[s]; + if (src.type == AGX_INDEX_NORMAL) { + if (!BITSET_TEST(uniform, src.value)) + uniform_only = false; + } else if (src.type != AGX_INDEX_UNIFORM && + src.type != AGX_INDEX_IMMEDIATE && + src.type != AGX_INDEX_NULL) { + uniform_only = false; + } + } + + if (!uniform_only) + continue; + BITSET_SET(uniform, I->dest[0].value); + if (wait) { + list_del(&I->link); + list_addtail(&I->link, &wait->link); + progress = true; + } + } + } + + free(uniform); + return progress; +} + static bool agx3_order_fragment_device_loads(agx_context *ctx) { @@ -10383,6 +10466,7 @@ agx_compile_function_nir_attempt(nir_shader *nir, nir_function_impl *impl, } AGX_PASS(ctx, agx3_order_fragment_device_loads); + AGX_PASS(ctx, agx3_g16_hoist_pixel_independent); if (agx_should_dump(nir, AGX_DBG_SHADERS)) agx_print_shader(ctx, stdout); diff --git a/src/asahi/compiler/agx_compiler.h b/src/asahi/compiler/agx_compiler.h index 20461fb035fd..2cf7bbbabd65 100644 --- a/src/asahi/compiler/agx_compiler.h +++ b/src/asahi/compiler/agx_compiler.h @@ -1207,6 +1207,7 @@ void agx_lower_spill(agx_context *ctx); void agx3_split_vector_stack_access(agx_context *ctx); bool agx3_lower_texture_publications(agx_context *ctx); bool agx3_fuse_fma_texture_publications(agx_context *ctx); +bool agx3_g16_hoist_pixel_independent(agx_context *ctx); bool agx3_lower_scalar_texture_publications(agx_context *ctx); bool agx3_lower_scalar_cube_coordinates(agx_context *ctx); bool agx3_split_texture_coordinates(agx_context *ctx); diff --git a/src/asahi/compiler/agx_debug.h b/src/asahi/compiler/agx_debug.h index a1cc739e60b4..8e45d1b4e3f5 100644 --- a/src/asahi/compiler/agx_debug.h +++ b/src/asahi/compiler/agx_debug.h @@ -34,6 +34,7 @@ enum agx_compiler_dbg { uint64_t agx_get_compiler_debug(void); + /* Why the last agx_compile_shader_nir() on this thread returned false * ("" when it succeeded), for the driver's "cannot encode" error. */ const char *agx_last_compile_refusal(void); diff --git a/src/asahi/compiler/meson.build b/src/asahi/compiler/meson.build index 2b766a4f14d3..3c0033d7efc6 100644 --- a/src/asahi/compiler/meson.build +++ b/src/asahi/compiler/meson.build @@ -227,6 +227,7 @@ if with_tests 'test/test-agx3-integer-fusion.cpp', 'test/test-agx3-integer-lowering.cpp', 'test/test-agx3-unary-lowering.cpp', + 'test/test-g16-pixel-hoist.cpp', 'test/test-agx3-fsel-uniform.cpp', 'test/test-fminmax-constant-denorm.cpp', 'test/test-shared-byte-offsets.cpp', diff --git a/src/asahi/compiler/test/test-g16-pixel-hoist.cpp b/src/asahi/compiler/test/test-g16-pixel-hoist.cpp new file mode 100644 index 000000000000..3931a1d1ff00 --- /dev/null +++ b/src/asahi/compiler/test/test-g16-pixel-hoist.cpp @@ -0,0 +1,159 @@ +/* + * Copyright 2026 Dylan Ironside + * SPDX-License-Identifier: MIT + */ + +#include +#include +#include "agx_builder.h" +#include "agx_test.h" + +/* agx3_g16_hoist_pixel_independent runs after scheduling, so these cases pin + * its dependency rules and its gates directly. */ + +namespace { + +struct hoist_case { + void *mem; + agx_shader_key key; + agx_builder *b; + agx_instr *wait; + agx_instr *mul; /* uniform * uniform */ + agx_instr *add; /* mul + immediate */ + agx_instr *fma; /* add * uniform + mov_imm */ + agx_instr *imm; /* mov_imm */ + agx_instr *tile; /* ld_tile */ + agx_instr *mix; /* tile * add: tile-dependent */ + agx_instr *mix2; /* mix + uniform: depends on a tile-dependent value */ +}; + +static void +build(hoist_case &c, enum agx_chip chip, mesa_shader_stage stage, + bool preamble = false) +{ + c.mem = ralloc_context(nullptr); + c.key = {}; + c.key.dev.chip = chip; + c.key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + c.b = agx_test_builder(c.mem); + c.b->shader->key = &c.key; + c.b->shader->stage = stage; + c.b->shader->is_preamble = preamble; + + agx_builder *b = c.b; + agx_context *ctx = b->shader; + const agx_index u0 = agx_uniform(4, AGX_SIZE_32); + const agx_index u1 = agx_uniform(6, AGX_SIZE_32); + + c.wait = agx_wait_pix(b, 1); + c.tile = agx_ld_tile_to(b, agx_temp(ctx, AGX_SIZE_32), agx_immediate(0xff), + agx_null(), AGX_FORMAT_I32, 1, 0, false, 0); + c.mul = agx_fmul_to(b, agx_temp(ctx, AGX_SIZE_32), u0, u1); + c.imm = agx_mov_imm_to(b, agx_temp(ctx, AGX_SIZE_32), 0x3f800000); + c.mix = agx_fmul_to(b, agx_temp(ctx, AGX_SIZE_32), c.tile->dest[0], + c.mul->dest[0]); + c.add = agx_fadd_to(b, agx_temp(ctx, AGX_SIZE_32), c.mul->dest[0], + agx_immediate_f(0.5f)); + c.mix2 = agx_fadd_to(b, agx_temp(ctx, AGX_SIZE_32), c.mix->dest[0], u0); + c.fma = agx_ffma_to(b, agx_temp(ctx, AGX_SIZE_32), c.add->dest[0], u1, + c.imm->dest[0]); +} + +static std::vector +order(agx_context *ctx) +{ + std::vector v; + agx_foreach_instr_global(ctx, I) + v.push_back(I); + return v; +} + +static void +expect_unchanged(enum agx_chip chip, mesa_shader_stage stage, bool preamble) +{ + hoist_case c; + build(c, chip, stage, preamble); + const std::vector before = order(c.b->shader); + EXPECT_FALSE(agx3_g16_hoist_pixel_independent(c.b->shader)); + EXPECT_EQ(order(c.b->shader), before); + ralloc_free(c.mem); +} + +} /* namespace */ + +/* The uniform-only chain mul -> add -> fma (with the mov_imm it reads) moves + * ahead of WAIT_PIX in its scheduled order. The tile load, the operation that + * reads it and the operation that reads that result keep their places after + * the wait, even though they also read uniforms. */ +TEST(G16PixelHoist, UniformChainMovesTileDependentStays) +{ + hoist_case c; + build(c, AGX_CHIP_G16G, MESA_SHADER_FRAGMENT); + + EXPECT_TRUE(agx3_g16_hoist_pixel_independent(c.b->shader)); + const std::vector expected = { + c.mul, c.imm, c.add, c.fma, c.wait, c.tile, c.mix, c.mix2, + }; + EXPECT_EQ(order(c.b->shader), expected); + + /* Nothing about the moved operations changes. */ + EXPECT_EQ(c.add->src[0].value, c.mul->dest[0].value); + EXPECT_EQ(c.fma->src[2].value, c.imm->dest[0].value); + + /* A second run finds nothing left to move. */ + EXPECT_FALSE(agx3_g16_hoist_pixel_independent(c.b->shader)); + EXPECT_EQ(order(c.b->shader), expected); + ralloc_free(c.mem); +} + +/* Uniform-only operations already ahead of the wait are not reordered, and a + * block without WAIT_PIX is left alone. */ +TEST(G16PixelHoist, NothingAfterWaitOrNoWait) +{ + void *mem = ralloc_context(nullptr); + agx_shader_key key = {}; + key.dev.chip = AGX_CHIP_G16G; + key.dev.encoding = AGX_ISA_ENCODING_AGX3B; + agx_builder *b = agx_test_builder(mem); + b->shader->key = &key; + b->shader->stage = MESA_SHADER_FRAGMENT; + agx_fmul_to(b, agx_temp(b->shader, AGX_SIZE_32), + agx_uniform(4, AGX_SIZE_32), agx_uniform(6, AGX_SIZE_32)); + agx_fadd_to(b, agx_temp(b->shader, AGX_SIZE_32), + agx_uniform(8, AGX_SIZE_32), agx_immediate_f(1.0f)); + + std::vector before = order(b->shader); + EXPECT_FALSE(agx3_g16_hoist_pixel_independent(b->shader)); + EXPECT_EQ(order(b->shader), before); + + agx_wait_pix(b, 1); + before = order(b->shader); + EXPECT_FALSE(agx3_g16_hoist_pixel_independent(b->shader)); + EXPECT_EQ(order(b->shader), before); + ralloc_free(mem); +} + +/* Only G16G fragment main programs are touched. */ +TEST(G16PixelHoist, OtherChipsStagesAndPreamblesUnchanged) +{ + for (enum agx_chip chip : {AGX_CHIP_G13G, AGX_CHIP_G13X, AGX_CHIP_G14G, + AGX_CHIP_G14X, AGX_CHIP_G17P}) + expect_unchanged(chip, MESA_SHADER_FRAGMENT, false); + expect_unchanged(AGX_CHIP_G16G, MESA_SHADER_COMPUTE, false); + expect_unchanged(AGX_CHIP_G16G, MESA_SHADER_VERTEX, false); + expect_unchanged(AGX_CHIP_G16G, MESA_SHADER_FRAGMENT, true); +} + +/* A uniform write after the wait stops the scan: nothing after it reads its + * uniforms earlier than scheduled. */ +TEST(G16PixelHoist, StopsAtUniformWrite) +{ + hoist_case c; + build(c, AGX_CHIP_G16G, MESA_SHADER_FRAGMENT); + agx_builder sb = agx_init_builder(c.b->shader, agx_after_instr(c.wait)); + agx_uniform_store(&sb, agx_immediate(0), agx_immediate(4), 0x3); + const std::vector before = order(c.b->shader); + EXPECT_FALSE(agx3_g16_hoist_pixel_independent(c.b->shader)); + EXPECT_EQ(order(c.b->shader), before); + ralloc_free(c.mem); +} diff --git a/src/asahi/lib/agx_linker.h b/src/asahi/lib/agx_linker.h index e2993ee182a0..8dfaa4cbbae5 100644 --- a/src/asahi/lib/agx_linker.h +++ b/src/asahi/lib/agx_linker.h @@ -370,6 +370,9 @@ struct agx_fs_epilog_key { * the constants after the main shader's uniform footprint instead. */ uint16_t blend_constant_base; + /* Emit the blend destination term first (nir_lower_blend_rt::dst_term_first). */ + bool blend_dst_first; + /* Render targets whose tile value must be stored back unchanged (colour * write disabled while raster coverage still makes the tile nonempty). * Independent of blending, so it also holds for pure-integer formats. */ diff --git a/src/asahi/lib/agx_nir_prolog_epilog.c b/src/asahi/lib/agx_nir_prolog_epilog.c index d1bfa069d340..5e26c2677325 100644 --- a/src/asahi/lib/agx_nir_prolog_epilog.c +++ b/src/asahi/lib/agx_nir_prolog_epilog.c @@ -807,6 +807,7 @@ agx_nir_fs_epilog_native(nir_builder *b, const void *key_) .alpha.src_factor = blend.alpha_src_factor, .alpha.dst_factor = blend.alpha_dst_factor, + .dst_term_first = key->blend_dst_first, .alpha.func = blend.alpha_func, .colormask = key->blend.rt[i].colormask, diff --git a/src/asahi/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 81c7c77c5d5e..799494f49219 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -9982,6 +9982,8 @@ hk_fs_link_key_build(const struct hk_device *dev, struct hk_shader *fs, key->epilog.link.already_ran_zs |= nontrivial_force_early; key->epilog.native_imageblock = in->tib->native_imageblock; key->epilog.max_sample_size_B = in->tib->max_sample_size_B; + key->epilog.blend_dst_first = + dev->dev.chip == AGX_CHIP_G16G; key->epilog.resident_mask = in->tib->resident_mask; key->epilog.max_bytes_per_tile = in->tib->max_bytes_per_tile; key->epilog.max_samples_per_tile = in->tib->max_samples_per_tile; diff --git a/src/compiler/nir/nir_lower_blend.c b/src/compiler/nir/nir_lower_blend.c index a5099116af4c..7f6e5a001f21 100644 --- a/src/compiler/nir/nir_lower_blend.c +++ b/src/compiler/nir/nir_lower_blend.c @@ -51,11 +51,11 @@ static nir_def * nir_blend_func( nir_builder *b, enum pipe_blend_func func, - nir_def *src, nir_def *dst) + nir_def *src, nir_def *dst, bool dst_first) { switch (func) { case PIPE_BLEND_ADD: - return nir_fadd(b, src, dst); + return dst_first ? nir_fadd(b, dst, src) : nir_fadd(b, src, dst); case PIPE_BLEND_SUBTRACT: return nir_fsub(b, src, dst); case PIPE_BLEND_REVERSE_SUBTRACT: @@ -1006,7 +1006,8 @@ nir_color_blend(nir_builder *b, nir_def *src, nir_def *src1, nir_def *dst, chan.dst_factor, rt->format); } - channels[c] = nir_blend_func(b, chan.func, psrc, pdst); + channels[c] = nir_blend_func(b, chan.func, psrc, pdst, + rt->dst_term_first); } return nir_vec(b, channels, 4); diff --git a/src/compiler/nir/nir_lower_blend.h b/src/compiler/nir/nir_lower_blend.h index bc979a79efc9..f5b865337a5f 100644 --- a/src/compiler/nir/nir_lower_blend.h +++ b/src/compiler/nir/nir_lower_blend.h @@ -53,6 +53,12 @@ typedef struct { bool src_premultiplied; bool dst_premultiplied; enum pipe_blend_overlap_mode overlap; + + /* Emit the destination term first in additive blending, so contraction + * fuses dst * dst_factor into the final multiply-add and the source + * product stays a separate multiply that does not depend on the + * destination. */ + bool dst_term_first; } nir_lower_blend_rt; typedef struct {