Skip to content
Merged
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
311 changes: 286 additions & 25 deletions src/asahi/compiler/agx_opt_register_cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ agx3_remove_redundant_physical_copies(agx_context *ctx)
}
}

/* agx_opt_break_if moves a conditional break into the block after its if,
* but the edge to the break target stays on the emptied then block. The
* target's live-in is live at the break itself. */
static agx_block *
fused_break_target(const agx_instr *I)
{
return (I->op == AGX_OPCODE_BREAK_IF_ICMP ||
I->op == AGX_OPCODE_BREAK_IF_FCMP)
? I->target
: NULL;
}

static void
postra_liveness_ins(BITSET_WORD *live, agx_instr *I)
{
Expand All @@ -125,40 +137,223 @@ postra_liveness(agx_context *ctx)
u_worklist worklist;
agx_worklist_init(ctx, &worklist);

bool fused_breaks = false;
agx_foreach_block(ctx, block) {
BITSET_ZERO(block->reg_live_in);
BITSET_ZERO(block->reg_live_out);

agx_worklist_push_tail(&worklist, block);
agx_foreach_instr_in_block(block, I)
fused_breaks |= fused_break_target(I) != NULL;
}

while (!u_worklist_is_empty(&worklist)) {
/* Pop off in reverse order since liveness is backwards */
agx_block *blk = agx_worklist_pop_tail(&worklist);
/* A fused break's block is not a predecessor of its target, so with
* fused breaks the whole analysis repeats until nothing changes. */
bool changed;
do {
changed = false;
agx_foreach_block(ctx, block)
agx_worklist_push_tail(&worklist, block);

while (!u_worklist_is_empty(&worklist)) {
/* Pop off in reverse order since liveness is backwards */
agx_block *blk = agx_worklist_pop_tail(&worklist);

/* Calculate liveness locally */
agx_foreach_successor(blk, succ) {
BITSET_OR(blk->reg_live_out, blk->reg_live_out, succ->reg_live_in);
}

BITSET_DECLARE(live, AGX_NUM_REGS);
memcpy(live, blk->reg_live_out, sizeof(live));

agx_foreach_instr_in_block_rev(blk, ins) {
agx_block *target = fused_break_target(ins);
if (target)
BITSET_OR(live, live, target->reg_live_in);

postra_liveness_ins(live, ins);
}

if (BITSET_EQUAL(blk->reg_live_in, live))
continue;

/* Calculate liveness locally */
agx_foreach_successor(blk, succ) {
BITSET_OR(blk->reg_live_out, blk->reg_live_out, succ->reg_live_in);
/* We made progress, so we need to reprocess the predecessors */
memcpy(blk->reg_live_in, live, sizeof(live));
changed = true;
agx_foreach_predecessor(blk, pred) {
agx_worklist_push_head(&worklist, *pred);
}
}
} while (fused_breaks && changed);

BITSET_DECLARE(live, AGX_NUM_REGS);
memcpy(live, blk->reg_live_out, sizeof(live));
u_worklist_fini(&worklist);
}

/*
* What the whole SIMD-group needs in divergent control flow.
*
* Divergent control flow runs every side of a branch, and every remaining
* loop iteration, in program order with the other threads masked off. A
* thread masked off in block B waits at a block that program order has not
* reached yet: the target of an edge that jumps over B, or, inside a loop, a
* block the loop leaves to and every remaining iteration jumps over. Call these B's pending blocks. Such a thread next
* reads what the physical liveness into its pending block says, so B must
* keep those registers (B's inactive set): its writes reach only the active
* threads and do not end them, and no read in B is their last use.
* Everything else in B is liveness as usual. Inactive sets and liveness feed
* each other, so both are solved together; they only grow, so the iteration
* ends.
*/
struct physical_live {
unsigned words; /* BITSET_WORDS(AGX_NUM_REGS) */
unsigned *pos; /* program position by block index */
BITSET_WORD *inactive; /* per position */
BITSET_WORD *live_out; /* per position, inactive set included */
BITSET_WORD *live_in; /* per position */
};

static void
physical_live_free(struct physical_live *p)
{
free(p->pos);
free(p->inactive);
free(p->live_out);
free(p->live_in);
memset(p, 0, sizeof(*p));
}

agx_foreach_instr_in_block_rev(blk, ins) {
postra_liveness_ins(live, ins);
/* Bounds the quadratic pending sets; larger shaders keep no divergent hints. */
#define PHYSICAL_LIVE_MAX_BLOCKS 4096

static bool
physical_liveness(agx_context *ctx, struct physical_live *p)
{
const unsigned n = ctx->num_blocks;
if (n == 0 || n > PHYSICAL_LIVE_MAX_BLOCKS)
return false;

const unsigned rw = BITSET_WORDS(AGX_NUM_REGS);
const unsigned bw = BITSET_WORDS(n);
agx_block **order = calloc(n, sizeof(*order));
unsigned *loop_end = calloc(n, sizeof(unsigned));
BITSET_WORD *pending = calloc((size_t)n * bw, sizeof(BITSET_WORD));
BITSET_WORD *live_in = calloc((size_t)n * rw, sizeof(BITSET_WORD));
p->live_in = live_in;
p->words = rw;
p->pos = calloc(n, sizeof(unsigned));
p->inactive = calloc((size_t)n * rw, sizeof(BITSET_WORD));
p->live_out = calloc((size_t)n * rw, sizeof(BITSET_WORD));
bool ok = order && loop_end && pending && live_in && p->pos && p->inactive &&
p->live_out;

unsigned count = 0;
if (ok) {
agx_foreach_block(ctx, block) {
if (block->index >= n) {
ok = false;
break;
}
order[count] = block;
p->pos[block->index] = count++;
}
}

if (BITSET_EQUAL(blk->reg_live_in, live))
continue;
if (ok) {
/* A loop runs again from its header up to its last back edge. */
for (unsigned a = 0; a < count; ++a)
loop_end[a] = a;
for (unsigned a = 0; a < count; ++a) {
agx_foreach_successor(order[a], succ) {
const unsigned t = p->pos[succ->index];
if (t <= a)
loop_end[t] = MAX2(loop_end[t], a);
}
}

/* We made progress, so we need to reprocess the predecessors */
memcpy(blk->reg_live_in, live, sizeof(live));
agx_foreach_predecessor(blk, pred) {
agx_worklist_push_head(&worklist, *pred);
/* An edge A -> T leaves its threads waiting at T until program order
* reaches T: the blocks between them, or, for a back edge, the rest of
* the loop before it runs again. */
for (unsigned a = 0; a < count; ++a) {
agx_foreach_successor(order[a], succ) {
const unsigned t = p->pos[succ->index];
const unsigned last = t > a ? t - 1 : loop_end[t];
for (unsigned b = a + 1; b <= last && b < count; ++b)
BITSET_SET(pending + (size_t)b * bw, t);
}
}

/* Threads waiting outside a loop keep waiting while it runs again. */
bool changed;
do {
changed = false;
for (unsigned h = 0; h < count; ++h) {
const unsigned l = loop_end[h];
if (l == h)
continue;
const BITSET_WORD *tail = pending + (size_t)l * bw;
unsigned t;
BITSET_FOREACH_SET(t, tail, count) {
if (t >= h && t <= l)
continue;
for (unsigned b = h; b <= l; ++b) {
BITSET_WORD *set = pending + (size_t)b * bw;
if (!BITSET_TEST(set, t)) {
BITSET_SET(set, t);
changed = true;
}
}
}
}
} while (changed);

do {
changed = false;
for (unsigned a = count; a-- > 0;) {
agx_block *block = order[a];
BITSET_WORD *inactive = p->inactive + (size_t)a * rw;
BITSET_WORD *out = p->live_out + (size_t)a * rw;
memset(inactive, 0, rw * sizeof(BITSET_WORD));

if (block->cf_divergent) {
unsigned t;
BITSET_FOREACH_SET(t, pending + (size_t)a * bw, count) {
__bitset_or(inactive, inactive, live_in + (size_t)t * rw, rw);
}
}

BITSET_DECLARE(live, AGX_NUM_REGS);
memcpy(live, inactive, sizeof(live));
agx_foreach_successor(block, succ) {
__bitset_or(live, live,
live_in + (size_t)p->pos[succ->index] * rw, rw);
}
memcpy(out, live, sizeof(live));

agx_foreach_instr_in_block_rev(block, I) {
agx_block *target = fused_break_target(I);
if (target) {
__bitset_or(live, live,
live_in + (size_t)p->pos[target->index] * rw, rw);
}
postra_liveness_ins(live, I);
__bitset_or(live, live, inactive, rw);
}

BITSET_WORD *in = live_in + (size_t)a * rw;
if (memcmp(in, live, sizeof(live))) {
memcpy(in, live, sizeof(live));
changed = true;
}
}
} while (changed);
}

u_worklist_fini(&worklist);
free(order);
free(loop_end);
free(pending);
if (!ok)
physical_live_free(p);
return ok;
}

static bool
Expand All @@ -174,6 +369,17 @@ writes_reg(const agx_instr *I, unsigned reg)
return false;
}

/* Last-use hints in divergent control flow, from physical liveness. They
* are an AGX3 instruction-set feature: AGX3 releases a register at a source
* marked as its last use, for the whole SIMD-group. AGX2's hint is a
* register-cache discard only and keeps the old rule.
*/
static bool
agx_divergent_last_use(const agx_context *ctx)
{
return agx_isa_is_agx3(ctx->key->dev.encoding);
}

/*
* Mark last-use sources to allow the hardware to discard from the register
* cache. Last use information follows immediately from (post-RA) liveness
Expand All @@ -187,20 +393,63 @@ agx_opt_register_cache(agx_context *ctx)
{
agx3_remove_redundant_physical_copies(ctx);

/* A last-use hint releases the register for the whole SIMD-group (a
* wrong one destroys the value), so divergent blocks take their last uses
* from physical liveness (physical_liveness()), which knows what
* masked-off threads still need. Last uses inside divergent control flow
* are then marked as densely as outside it.
*/
struct physical_live phys = {0};
bool have_phys = false;
if (agx_divergent_last_use(ctx)) {
bool any = false;
agx_foreach_block(ctx, block)
any |= block->cf_divergent;

have_phys = any && physical_liveness(ctx, &phys);
}

/* agx_lower_divergent_shuffle routes register lane indices through a
* scratch register that must read zero in every inactive thread: the
* shuffle adds the index up over the whole quad. Never release it. */
const bool agx3 = ctx->key->dev.encoding != AGX_ISA_ENCODING_AGX2;
const unsigned scratch_lo = agx3 ? 2 : 1, scratch_hi = agx3 ? 3 : 1;
const bool keep_scratch = have_phys && ctx->any_quad_divergent_shuffle;

/* Analyze the shader globally */
postra_liveness(ctx);

agx_foreach_block(ctx, block) {
/* Blocks outside divergent control flow keep logical liveness. */
const bool physical = have_phys && block->cf_divergent;
const BITSET_WORD *inactive =
physical ? phys.inactive + (size_t)phys.pos[block->index] * phys.words
: NULL;

/* Live-set at each point in the program */
BITSET_DECLARE(live, AGX_NUM_REGS);
memcpy(live, block->reg_live_out, sizeof(live));
if (physical) {
memcpy(live,
phys.live_out + (size_t)phys.pos[block->index] * phys.words,
sizeof(live));
}

/* Set of registers read "soon" by an ALU instruction. These are
* candidates for the .cache bit.
*/
BITSET_DECLARE(alu_reads, AGX_NUM_REGS) = {0};

agx_foreach_instr_in_block_rev(block, I) {
agx_block *target = fused_break_target(I);
if (target && physical) {
__bitset_or(live, live,
phys.live_in + (size_t)phys.pos[target->index] * phys.words,
phys.words);
} else if (target) {
BITSET_OR(live, live, target->reg_live_in);
}

agx_foreach_reg_dest(I, d) {
uint64_t reg = I->dest[d].value;
unsigned nr = agx_index_size_16(I->dest[d]);
Expand All @@ -219,15 +468,23 @@ agx_opt_register_cache(agx_context *ctx)

/* If the register dead after this instruction, it's the last use.
* That includes if the register is overwritten this cycle, but that
* won't show up in the liveness analysis.
* won't show up in the liveness analysis, unless masked-off
* threads still need the register (its inactive set).
*/
bool lu = !BITSET_TEST_COUNT(live, reg, nr) || writes_reg(I, reg);

/* Handling divergent blocks would require physical CFG awareness.
* Just bail for now, skipping this pass won't affect correctness.
* Subgroup-uniform ifs/loops have no physical divergence.
bool lu = !BITSET_TEST_COUNT(live, reg, nr) ||
(writes_reg(I, reg) &&
!(inactive && BITSET_TEST_COUNT(inactive, reg, nr)));

if (keep_scratch && reg <= scratch_hi && reg + nr > scratch_lo)
lu = false;

/* Without physical liveness, divergent blocks get no last-use
* hints: another side of the branch, run later with these threads
* masked off, may still read the register. Skipping hints never
* affects correctness. Subgroup-uniform ifs/loops have no
* physical divergence.
*/
I->src[s].discard = lu && !block->cf_divergent;
I->src[s].discard = lu && (!block->cf_divergent || physical);

/* Mark any source read by an ALU instruction in the same block as
* wanting a .cache hint. This is better than just marking
Expand Down Expand Up @@ -269,9 +526,13 @@ agx_opt_register_cache(agx_context *ctx)
}

postra_liveness_ins(live, I);
if (inactive)
__bitset_or(live, live, inactive, BITSET_WORDS(AGX_NUM_REGS));
}
}

physical_live_free(&phys);

/* Apple's G17P fragment loop test keeps
* its counter operand in the register cache (the f11 hint on the icmp's
* counter source). The block-local analysis above cannot: the counter's next
Expand Down
Loading