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
130 changes: 118 additions & 12 deletions src/asahi/compiler/agx_pressure_schedule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1213,6 +1213,31 @@ ilp_instr_pipe(const agx_instr *I)
}
}

/* Mark hoisted loads feeding SSA value v as consumed, walking through any
* transitive COLLECT chains (which regroup split/collected channels without
* consuming them). */
static void
ilp_mark_consumed_ssa(unsigned v, const int *load_node,
agx_instr *const *collect_def, bool *consumed,
unsigned *consumed_loads)
{
const int l = load_node[v];
if (l >= 0) {
if (!consumed[l]) {
consumed[l] = true;
(*consumed_loads)++;
}
return;
}
const agx_instr *col = collect_def[v];
if (!col)
return;
agx_foreach_ssa_src(col, cj) {
ilp_mark_consumed_ssa(col->src[cj].value, load_node, collect_def,
consumed, consumed_loads);
}
}

static bool
ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
bool native_uniforms, unsigned alu_latency,
Expand Down Expand Up @@ -1317,6 +1342,7 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
unsigned *uses_left = NULL;
bool *dies = NULL;
signed live = 0;
signed eff_fwd_cap = fwd_cap;
if (fwd_cap > 0) {
uses_left = rzalloc_array(memctx, unsigned, ctx->alloc);
dies = rzalloc_array(memctx, bool, ctx->alloc);
Expand All @@ -1338,6 +1364,21 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
agx_foreach_ssa_src(I, si)
dies[I->src[si].value] = false;
}
/* Loop-carried PHIs in loop headers are already live on block entry; do
* not charge live ones against the forward budget for temporaries added
* inside this block. Dead PHIs contribute 0 net live halves, and non-loop
* merge PHIs keep the original fwd_cap brake intact. */
if (block->loop_header) {
for (unsigned i = 0; i < n; ++i) {
if (nodes[i]->instr->op != AGX_OPCODE_PHI)
continue;
agx_foreach_ssa_dest(nodes[i]->instr, d) {
const unsigned v = nodes[i]->instr->dest[d].value;
if (uses_left[v] > 0 || !dies[v])
eff_fwd_cap += agx_index_size_16(nodes[i]->instr->dest[d]);
}
}
}
}

/* With loads_in_flight, the window starts at the number of hoisted loads
Expand All @@ -1348,11 +1389,13 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
const bool in_flight = hoist_samples && loads_in_flight;
unsigned consumed_loads = 0;
int *load_node = NULL;
agx_instr **collect_def = NULL;
bool *consumed = NULL;
if (in_flight) {
load_node = ralloc_array(memctx, int, MAX2(ctx->alloc, 1));
collect_def = rzalloc_array(memctx, agx_instr *, MAX2(ctx->alloc, 1));
consumed = rzalloc_array(memctx, bool, n);
if (!load_node || !consumed)
if (!load_node || !collect_def || !consumed)
return false;
for (unsigned v = 0; v < ctx->alloc; ++v)
load_node[v] = -1;
Expand All @@ -1362,10 +1405,51 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
load_node[nodes[i]->instr->dest[d].value] = i;
} else if (nodes[i]->instr->op == AGX_OPCODE_SPLIT &&
nodes[i]->instr->src[0].type == AGX_INDEX_NORMAL) {
const int l = load_node[nodes[i]->instr->src[0].value];
const unsigned sv = nodes[i]->instr->src[0].value;
const int l = load_node[sv];
if (l >= 0) {
agx_foreach_ssa_dest(nodes[i]->instr, d)
load_node[nodes[i]->instr->dest[d].value] = l;
} else if (collect_def[sv]) {
agx_instr *col = collect_def[sv];
bool exact_channels = col->nr_srcs == nodes[i]->instr->nr_dests;
if (exact_channels) {
agx_foreach_ssa_dest(nodes[i]->instr, d) {
if (agx_index_size_16(col->src[d]) !=
agx_index_size_16(nodes[i]->instr->dest[d])) {
exact_channels = false;
break;
}
}
}
if (exact_channels) {
agx_foreach_ssa_dest(nodes[i]->instr, d) {
if (col->src[d].type != AGX_INDEX_NORMAL)
continue;
const unsigned cv = col->src[d].value;
const unsigned dv = nodes[i]->instr->dest[d].value;
if (load_node[cv] >= 0)
load_node[dv] = load_node[cv];
else if (collect_def[cv])
collect_def[dv] = collect_def[cv];
}
} else {
agx_foreach_ssa_dest(nodes[i]->instr, d)
collect_def[nodes[i]->instr->dest[d].value] = col;
}
}
} else if (nodes[i]->instr->op == AGX_OPCODE_COLLECT) {
bool from_load = false;
agx_foreach_ssa_src(nodes[i]->instr, si) {
const unsigned sv = nodes[i]->instr->src[si].value;
if (load_node[sv] >= 0 || collect_def[sv]) {
from_load = true;
break;
}
}
if (from_load) {
agx_foreach_ssa_dest(nodes[i]->instr, d)
collect_def[nodes[i]->instr->dest[d].value] = nodes[i]->instr;
}
}
}
Expand All @@ -1389,16 +1473,39 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
int best = -1;
unsigned best_t = 0;
signed best_grow = 0;
signed best_dest_grow = 0;
bool best_fits = true;
unsigned i;
BITSET_FOREACH_SET(i, ready_bits, n) {
const unsigned t = MAX2(ready[i], cycle);
signed dest_grow = 0;
signed grow = 0;
if (fwd_cap > 0) {
agx_foreach_ssa_dest(nodes[i]->instr, d)
grow += agx_index_size_16(nodes[i]->instr->dest[d]);
const agx_instr *I = nodes[i]->instr;
agx_foreach_ssa_dest(I, d)
dest_grow += agx_index_size_16(I->dest[d]);
grow = dest_grow;
agx_foreach_ssa_src(I, si) {
const unsigned v = I->src[si].value;
if (!dies[v])
continue;
unsigned occ = 0;
bool seen_before = false;
agx_foreach_ssa_src(I, sj) {
if (I->src[sj].value == v) {
if (sj < si) {
seen_before = true;
break;
}
occ++;
}
}
if (!seen_before && uses_left[v] == occ)
grow -= agx_index_size_16(I->src[si]);
}
}
const bool fits = fwd_cap <= 0 || grow == 0 || live + grow <= fwd_cap;
const bool fits =
fwd_cap <= 0 || grow <= 0 || live + grow <= eff_fwd_cap;
bool better;
if (best < 0)
better = true;
Expand Down Expand Up @@ -1467,6 +1574,7 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,
best = i;
best_t = t;
best_grow = grow;
best_dest_grow = dest_grow;
best_fits = fits;
}
}
Expand All @@ -1477,7 +1585,7 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,

if (fwd_cap > 0) {
const agx_instr *B = nodes[best]->instr;
live += best_grow;
live += best_dest_grow;
agx_foreach_ssa_src(B, si) {
const unsigned v = B->src[si].value;
if (--uses_left[v] == 0 && dies[v])
Expand All @@ -1492,13 +1600,11 @@ ilp_schedule_block(agx_context *ctx, agx_block *block, void *memctx,

order[step] = nodes[best];
issued_samples += ilp_is_hoisted(ctx, nodes[best]->instr, hoist_loads);
if (in_flight && nodes[best]->instr->op != AGX_OPCODE_SPLIT) {
if (in_flight && nodes[best]->instr->op != AGX_OPCODE_SPLIT &&
nodes[best]->instr->op != AGX_OPCODE_COLLECT) {
agx_foreach_ssa_src(nodes[best]->instr, si) {
const int l = load_node[nodes[best]->instr->src[si].value];
if (l >= 0 && !consumed[l]) {
consumed[l] = true;
consumed_loads++;
}
ilp_mark_consumed_ssa(nodes[best]->instr->src[si].value, load_node,
collect_def, consumed, &consumed_loads);
}
}
issued_writes += nodes[best]->instr->op == AGX_OPCODE_IMAGE_WRITE ||
Expand Down
Loading