Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/asahi/compiler/agx_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/asahi/compiler/agx_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/asahi/compiler/agx_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/asahi/compiler/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
159 changes: 159 additions & 0 deletions src/asahi/compiler/test/test-g16-pixel-hoist.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* Copyright 2026 Dylan Ironside
* SPDX-License-Identifier: MIT
*/

#include <vector>
#include <gtest/gtest.h>
#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<agx_instr *>
order(agx_context *ctx)
{
std::vector<agx_instr *> 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<agx_instr *> 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<agx_instr *> 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<agx_instr *> 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<agx_instr *> 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);
}
3 changes: 3 additions & 0 deletions src/asahi/lib/agx_linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
1 change: 1 addition & 0 deletions src/asahi/lib/agx_nir_prolog_epilog.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/asahi/vulkan/hk_cmd_draw.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions src/compiler/nir/nir_lower_blend.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions src/compiler/nir/nir_lower_blend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down