From 3a73c76373a3553f9c928210a540a5fa747003ca Mon Sep 17 00:00:00 2001 From: Alyssa Rosenzweig Date: Wed, 15 Apr 2026 17:06:44 -0400 Subject: [PATCH] jay: fix spiller coupling code Signed-off-by: Alyssa Rosenzweig Part-of: --- src/intel/compiler/jay/jay_spill.c | 60 +++++++++++++++++------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/src/intel/compiler/jay/jay_spill.c b/src/intel/compiler/jay/jay_spill.c index 9654a5b986f..0cf92d30f00 100644 --- a/src/intel/compiler/jay/jay_spill.c +++ b/src/intel/compiler/jay/jay_spill.c @@ -322,36 +322,30 @@ insert_coupling_code(struct spill_ctx *ctx, jay_block *pred, jay_block *succ) struct spill_block *sp = &ctx->blocks[pred->index]; struct spill_block *ss = &ctx->blocks[succ->index]; - /* Insert spill/fill at phi sources to match their destination */ + /* Insert spills at phi sources to match their destination */ jay_foreach_phi_src_in_block(pred, phi_src) { jay_inst *phi_dst = ctx->defs[jay_phi_src_index(phi_src)]; unsigned src = jay_index(phi_src->src[0]); - if (phi_src->src[0].file == ctx->file) { - if (jay_is_mem(phi_dst->dst)) { - if (!u_sparse_bitset_test(&sp->S_out, src)) { - /* Spill the phi source. TODO: avoid redundant spills here */ - b.cursor = jay_after_block_logical(pred); - insert_spill(&b, ctx, src); - } - - if (can_remat_node(ctx, jay_index(phi_src->src[0]))) { - jay_def idx = jay_scalar(ctx->file, src); - jay_def tmp = jay_alloc_def(&b, ctx->file, 1); - - b.cursor = jay_before_function(ctx->func); - remat_to(&b, tmp, ctx, src); - jay_MOV(&b, jay_def_as_mem(ctx, idx), tmp); - } - - /* Use the spilled version */ - phi_src->src[0] = jay_def_as_mem(ctx, phi_src->src[0]); - jay_set_phi_src_index(phi_src, jay_index(phi_dst->dst)); - } else if (!u_sparse_bitset_test(&sp->W_out, src)) { - /* Fill the phi source in the predecessor */ - jay_block *reload_block = jay_edge_to_block(pred, succ); - insert_reload(ctx, reload_block, jay_along_edge(pred, succ), src); + if (phi_src->src[0].file == ctx->file && jay_is_mem(phi_dst->dst)) { + if (!u_sparse_bitset_test(&sp->S_out, src)) { + /* Spill the phi source. TODO: avoid redundant spills here */ + b.cursor = jay_after_block_logical(pred); + insert_spill(&b, ctx, src); } + + if (can_remat_node(ctx, jay_index(phi_src->src[0]))) { + jay_def idx = jay_scalar(ctx->file, src); + jay_def tmp = jay_alloc_def(&b, ctx->file, 1); + + b.cursor = jay_before_function(ctx->func); + remat_to(&b, tmp, ctx, src); + jay_MOV(&b, jay_def_as_mem(ctx, idx), tmp); + } + + /* Use the spilled version */ + phi_src->src[0] = jay_def_as_mem(ctx, phi_src->src[0]); + jay_set_phi_src_index(phi_src, jay_index(phi_dst->dst)); } } @@ -367,6 +361,22 @@ insert_coupling_code(struct spill_ctx *ctx, jay_block *pred, jay_block *succ) u_sparse_bitset_set(&ctx->phi_set, jay_index(phi->dst)); } + /* Now insert fills at phi sources to match their destination. Note that we + * must do all spilling before any filling to ensure we stay under the limit. + */ + jay_foreach_phi_src_in_block(pred, phi_src) { + unsigned src = jay_index(phi_src->src[0]); + + if (phi_src->src[0].file == ctx->file && + !jay_is_mem(ctx->defs[jay_phi_src_index(phi_src)]->dst) && + !u_sparse_bitset_test(&sp->W_out, src)) { + + /* Fill the phi source in the predecessor */ + jay_block *reload_block = jay_edge_to_block(pred, succ); + insert_reload(ctx, reload_block, jay_along_edge(pred, succ), src); + } + } + /* Variables in W at the start of succ must be defined along the edge. * If not live at the end of the predecessor (and it's not a phi defined in * the successor), insert a reload.