From 2168ba208f79ca40aa2fc7948cc245d79fa76f12 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Tue, 17 Nov 2026 18:00:08 +0000 Subject: [PATCH 1/5] nir/lower_blend: add an option to emit the destination term first Additive blending currently emits src * src_factor + dst * dst_factor. With dst_term_first the destination term comes first, so contraction fuses the destination product into the final multiply-add and the source product becomes a separate multiply that a backend can schedule independently of the destination load. Off by default; no driver changes behaviour. --- src/compiler/nir/nir_lower_blend.c | 7 ++++--- src/compiler/nir/nir_lower_blend.h | 6 ++++++ 2 files changed, 10 insertions(+), 3 deletions(-) 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 { From 39fd16ed11671a8c583e3b2401461143882fcb1e Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Tue, 17 Nov 2026 18:00:09 +0000 Subject: [PATCH 2/5] asahi: emit the blend destination term first on G16G On G16G the epilog's additive blend now fuses dst * dst_factor into the final multiply-add, so each channel needs one dependent operation after the tilebuffer load instead of a multiply followed by a multiply-add. The source product no longer depends on the destination and is scheduled in the load's shadow. The association of the last add changes, so results may differ by one UNORM step from the previous order. The choice is carried in the epilog key, so it applies only to G16G and separates cached shaders. ASAHI_G16_BLEND_DST_FIRST=0 restores the previous order. --- src/asahi/compiler/agx_compile.c | 6 ++++++ src/asahi/compiler/agx_debug.h | 3 +++ src/asahi/lib/agx_linker.h | 3 +++ src/asahi/lib/agx_nir_prolog_epilog.c | 1 + src/asahi/vulkan/hk_cmd_draw.c | 2 ++ 5 files changed, 15 insertions(+) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 5443f4e509f0..917a557cdf43 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -82,6 +82,12 @@ agx_get_compiler_debug(void) return debug_get_option_agx_compiler_debug(); } +bool +agx_g16_blend_dst_first_enabled(void) +{ + return debug_get_bool_option("ASAHI_G16_BLEND_DST_FIRST", true); +} + static __thread char agx_refusal[192]; const char * diff --git a/src/asahi/compiler/agx_debug.h b/src/asahi/compiler/agx_debug.h index a1cc739e60b4..440b9a440f03 100644 --- a/src/asahi/compiler/agx_debug.h +++ b/src/asahi/compiler/agx_debug.h @@ -34,6 +34,9 @@ enum agx_compiler_dbg { uint64_t agx_get_compiler_debug(void); +/* Qualified on G16G; ASAHI_G16_BLEND_DST_FIRST=0 restores the source-first blend add. */ +bool agx_g16_blend_dst_first_enabled(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/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..0d084597ec2d 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 && agx_g16_blend_dst_first_enabled(); 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; From 02437426637f3fecec1bfa71eaaccd0c5e7154c1 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Tue, 17 Nov 2026 18:55:21 +0000 Subject: [PATCH 3/5] asahi: issue uniform-only G16 fragment arithmetic before the pixel wait After scheduling, move fadd, fmul, ffma and mov_imm computed only from uniforms and immediates ahead of the first wait_pix of a G16G fragment block. With the blend destination term first, the source terms of a uniform-colour blend (clamps, 1 - alpha and the source products) are then computed before the ordered colour section, which holds only the tile load, unpack, one multiply-add per channel, pack and store. Texture, varying and tile-dependent work keeps its scheduled place. Operations, modifiers and operands are unchanged, so results are bit-identical. The choice is part of the Vulkan compiler flags and the Gallium disk-cache identity. ASAHI_G16_PIX_HOIST=0 restores the previous placement. --- src/asahi/compiler/agx_compile.c | 72 ++++++++++++++++++++++ src/asahi/compiler/agx_debug.h | 2 + src/asahi/vulkan/hk_shader.c | 3 +- src/gallium/drivers/asahi/agx_disk_cache.c | 2 + 4 files changed, 78 insertions(+), 1 deletion(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 917a557cdf43..60e1d52d08ad 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -82,6 +82,12 @@ agx_get_compiler_debug(void) return debug_get_option_agx_compiler_debug(); } +bool +agx_g16_pix_hoist_enabled(void) +{ + return debug_get_bool_option("ASAHI_G16_PIX_HOIST", true); +} + bool agx_g16_blend_dst_first_enabled(void) { @@ -8878,6 +8884,71 @@ 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 (ASAHI_G16_PIX_HOIST=0 opts out): 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. */ +static 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 || + !agx_g16_pix_hoist_enabled()) + 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; + } + + 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) { @@ -10389,6 +10460,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_debug.h b/src/asahi/compiler/agx_debug.h index 440b9a440f03..0184f4fec68a 100644 --- a/src/asahi/compiler/agx_debug.h +++ b/src/asahi/compiler/agx_debug.h @@ -34,6 +34,8 @@ enum agx_compiler_dbg { uint64_t agx_get_compiler_debug(void); +/* Qualified on G16G; ASAHI_G16_PIX_HOIST=0 restores the scheduler placement. */ +bool agx_g16_pix_hoist_enabled(void); /* Qualified on G16G; ASAHI_G16_BLEND_DST_FIRST=0 restores the source-first blend add. */ bool agx_g16_blend_dst_first_enabled(void); diff --git a/src/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index 77d3dbdf13bc..b26c3beb50a1 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -187,7 +187,8 @@ hk_physical_device_compiler_flags(const struct hk_physical_device *pdev) { /* This could be optimized but it doesn't matter. */ return pdev->dev.debug | - hk_fs_structure_key_bits(); + hk_fs_structure_key_bits() | + ((uint64_t)!!agx_g16_pix_hoist_enabled() << 33); } const nir_shader_compiler_options * diff --git a/src/gallium/drivers/asahi/agx_disk_cache.c b/src/gallium/drivers/asahi/agx_disk_cache.c index 660795aec160..0141fdc73e7c 100644 --- a/src/gallium/drivers/asahi/agx_disk_cache.c +++ b/src/gallium/drivers/asahi/agx_disk_cache.c @@ -237,6 +237,8 @@ agx_disk_cache_init(struct agx_screen *screen) _mesa_sha1_format(timestamp, id_sha1); uint64_t driver_flags = screen->dev.debug; + if (screen->dev.chip == AGX_CHIP_G16G && agx_g16_pix_hoist_enabled()) + driver_flags |= UINT64_C(1) << 61; screen->disk_cache = disk_cache_create(renderer, timestamp, driver_flags); #endif } From 5f81ba4474dc66190cc56249b9fd776cfa121133 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Fri, 20 Nov 2026 18:08:42 +0000 Subject: [PATCH 4/5] asahi: test the G16G pixel-independent hoist agx3_g16_hoist_pixel_independent reorders instructions after scheduling, so cover its rules directly instead of relying on hardware readbacks: - a chain of uniform-only fmul, fadd, ffma and mov_imm moves ahead of WAIT_PIX and keeps its scheduled order; - a tile load, the operation reading it and the operation reading that result stay after the wait, although they also read uniforms; - nothing moves without a wait, on other chips, in other stages or in preambles; - ASAHI_G16_PIX_HOIST=0 keeps the scheduled order. Export the pass so the test can call it. --- src/asahi/compiler/agx_compile.c | 2 +- src/asahi/compiler/agx_compiler.h | 1 + src/asahi/compiler/meson.build | 1 + .../compiler/test/test-g16-pixel-hoist.cpp | 157 ++++++++++++++++++ 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 src/asahi/compiler/test/test-g16-pixel-hoist.cpp diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index 60e1d52d08ad..fefcfccad03e 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -8892,7 +8892,7 @@ agx3_coalesce_uniform_half_stores(agx_context *ctx) * 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. */ -static bool +bool agx3_g16_hoist_pixel_independent(agx_context *ctx) { if (ctx->key->dev.chip != AGX_CHIP_G16G || 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/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..cd2ca22d9d8e --- /dev/null +++ b/src/asahi/compiler/test/test-g16-pixel-hoist.cpp @@ -0,0 +1,157 @@ +/* + * Copyright 2026 Dylan Ironside + * SPDX-License-Identifier: MIT + */ + +#include +#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) +{ + unsetenv("ASAHI_G16_PIX_HOIST"); + 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) +{ + unsetenv("ASAHI_G16_PIX_HOIST"); + 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) +{ + unsetenv("ASAHI_G16_PIX_HOIST"); + 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); +} + +/* ASAHI_G16_PIX_HOIST=0 keeps the scheduled order on G16G. */ +TEST(G16PixelHoist, OptOut) +{ + setenv("ASAHI_G16_PIX_HOIST", "0", 1); + expect_unchanged(AGX_CHIP_G16G, MESA_SHADER_FRAGMENT, false); + unsetenv("ASAHI_G16_PIX_HOIST"); +} From 6f13b2d1a70c15932ded7541050fa6643008f3c3 Mon Sep 17 00:00:00 2001 From: Dylan Ironside Date: Sat, 21 Nov 2026 01:02:03 +0000 Subject: [PATCH 5/5] asahi: drop the G16G blend and hoist switches, stop at uniform writes Merged code cannot carry runtime switches. Remove ASAHI_G16_BLEND_DST_FIRST and ASAHI_G16_PIX_HOIST with their helpers: on G16G the blend destination term is always emitted first and uniform-only arithmetic is always issued ahead of the first WAIT_PIX. The Vulkan compiler-flag bit and the Gallium disk-cache bit only kept the settings apart, so they go too. agx3_g16_hoist_pixel_independent() moved uniform reads ahead of the wait without looking for a uniform write between the two. Stop the scan of a block at the first instruction after the wait that writes the uniform file (uniform_store or a uniform destination); nothing after it moves. Tests: the opt-out test is replaced by StopsAtUniformWrite; the other-chip test already checks the scheduled program is left as it was where the pass does not run. Both changes stay limited to G16G because that is the only AGX3b part they were measured on. G16S and G16C were not measured; nothing known rules them out. Assisted-by: LLM --- src/asahi/compiler/agx_compile.c | 38 +++++++++++-------- src/asahi/compiler/agx_debug.h | 4 -- .../compiler/test/test-g16-pixel-hoist.cpp | 20 +++++----- src/asahi/vulkan/hk_cmd_draw.c | 2 +- src/asahi/vulkan/hk_shader.c | 3 +- src/gallium/drivers/asahi/agx_disk_cache.c | 2 - 6 files changed, 35 insertions(+), 34 deletions(-) diff --git a/src/asahi/compiler/agx_compile.c b/src/asahi/compiler/agx_compile.c index fefcfccad03e..2d013c1b73ec 100644 --- a/src/asahi/compiler/agx_compile.c +++ b/src/asahi/compiler/agx_compile.c @@ -82,18 +82,6 @@ agx_get_compiler_debug(void) return debug_get_option_agx_compiler_debug(); } -bool -agx_g16_pix_hoist_enabled(void) -{ - return debug_get_bool_option("ASAHI_G16_PIX_HOIST", true); -} - -bool -agx_g16_blend_dst_first_enabled(void) -{ - return debug_get_bool_option("ASAHI_G16_BLEND_DST_FIRST", true); -} - static __thread char agx_refusal[192]; const char * @@ -8884,20 +8872,35 @@ 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 (ASAHI_G16_PIX_HOIST=0 opts out): after scheduling, move +/* 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. */ + * 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 || - !agx_g16_pix_hoist_enabled()) + ctx->stage != MESA_SHADER_FRAGMENT || ctx->is_preamble) return false; /* Values computed only from uniforms and immediates by the movable ops, @@ -8916,6 +8919,9 @@ agx3_g16_hoist_pixel_independent(agx_context *ctx) 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 || diff --git a/src/asahi/compiler/agx_debug.h b/src/asahi/compiler/agx_debug.h index 0184f4fec68a..8e45d1b4e3f5 100644 --- a/src/asahi/compiler/agx_debug.h +++ b/src/asahi/compiler/agx_debug.h @@ -34,10 +34,6 @@ enum agx_compiler_dbg { uint64_t agx_get_compiler_debug(void); -/* Qualified on G16G; ASAHI_G16_PIX_HOIST=0 restores the scheduler placement. */ -bool agx_g16_pix_hoist_enabled(void); -/* Qualified on G16G; ASAHI_G16_BLEND_DST_FIRST=0 restores the source-first blend add. */ -bool agx_g16_blend_dst_first_enabled(void); /* Why the last agx_compile_shader_nir() on this thread returned false * ("" when it succeeded), for the driver's "cannot encode" error. */ diff --git a/src/asahi/compiler/test/test-g16-pixel-hoist.cpp b/src/asahi/compiler/test/test-g16-pixel-hoist.cpp index cd2ca22d9d8e..3931a1d1ff00 100644 --- a/src/asahi/compiler/test/test-g16-pixel-hoist.cpp +++ b/src/asahi/compiler/test/test-g16-pixel-hoist.cpp @@ -3,7 +3,6 @@ * SPDX-License-Identifier: MIT */ -#include #include #include #include "agx_builder.h" @@ -88,7 +87,6 @@ expect_unchanged(enum agx_chip chip, mesa_shader_stage stage, bool preamble) * the wait, even though they also read uniforms. */ TEST(G16PixelHoist, UniformChainMovesTileDependentStays) { - unsetenv("ASAHI_G16_PIX_HOIST"); hoist_case c; build(c, AGX_CHIP_G16G, MESA_SHADER_FRAGMENT); @@ -112,7 +110,6 @@ TEST(G16PixelHoist, UniformChainMovesTileDependentStays) * block without WAIT_PIX is left alone. */ TEST(G16PixelHoist, NothingAfterWaitOrNoWait) { - unsetenv("ASAHI_G16_PIX_HOIST"); void *mem = ralloc_context(nullptr); agx_shader_key key = {}; key.dev.chip = AGX_CHIP_G16G; @@ -139,7 +136,6 @@ TEST(G16PixelHoist, NothingAfterWaitOrNoWait) /* Only G16G fragment main programs are touched. */ TEST(G16PixelHoist, OtherChipsStagesAndPreamblesUnchanged) { - unsetenv("ASAHI_G16_PIX_HOIST"); 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); @@ -148,10 +144,16 @@ TEST(G16PixelHoist, OtherChipsStagesAndPreamblesUnchanged) expect_unchanged(AGX_CHIP_G16G, MESA_SHADER_FRAGMENT, true); } -/* ASAHI_G16_PIX_HOIST=0 keeps the scheduled order on G16G. */ -TEST(G16PixelHoist, OptOut) +/* A uniform write after the wait stops the scan: nothing after it reads its + * uniforms earlier than scheduled. */ +TEST(G16PixelHoist, StopsAtUniformWrite) { - setenv("ASAHI_G16_PIX_HOIST", "0", 1); - expect_unchanged(AGX_CHIP_G16G, MESA_SHADER_FRAGMENT, false); - unsetenv("ASAHI_G16_PIX_HOIST"); + 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/vulkan/hk_cmd_draw.c b/src/asahi/vulkan/hk_cmd_draw.c index 0d084597ec2d..799494f49219 100644 --- a/src/asahi/vulkan/hk_cmd_draw.c +++ b/src/asahi/vulkan/hk_cmd_draw.c @@ -9983,7 +9983,7 @@ hk_fs_link_key_build(const struct hk_device *dev, struct hk_shader *fs, 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 && agx_g16_blend_dst_first_enabled(); + 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/asahi/vulkan/hk_shader.c b/src/asahi/vulkan/hk_shader.c index b26c3beb50a1..77d3dbdf13bc 100644 --- a/src/asahi/vulkan/hk_shader.c +++ b/src/asahi/vulkan/hk_shader.c @@ -187,8 +187,7 @@ hk_physical_device_compiler_flags(const struct hk_physical_device *pdev) { /* This could be optimized but it doesn't matter. */ return pdev->dev.debug | - hk_fs_structure_key_bits() | - ((uint64_t)!!agx_g16_pix_hoist_enabled() << 33); + hk_fs_structure_key_bits(); } const nir_shader_compiler_options * diff --git a/src/gallium/drivers/asahi/agx_disk_cache.c b/src/gallium/drivers/asahi/agx_disk_cache.c index 0141fdc73e7c..660795aec160 100644 --- a/src/gallium/drivers/asahi/agx_disk_cache.c +++ b/src/gallium/drivers/asahi/agx_disk_cache.c @@ -237,8 +237,6 @@ agx_disk_cache_init(struct agx_screen *screen) _mesa_sha1_format(timestamp, id_sha1); uint64_t driver_flags = screen->dev.debug; - if (screen->dev.chip == AGX_CHIP_G16G && agx_g16_pix_hoist_enabled()) - driver_flags |= UINT64_C(1) << 61; screen->disk_cache = disk_cache_create(renderer, timestamp, driver_flags); #endif }