Skip to content

Conversation

@amfaber
Copy link
Contributor

@amfaber amfaber commented Nov 16, 2025

Ensure we propagate the return index when a return block is folded into an existing block

Without this change, my trivial program that I scaled down to in order to debug something failed to compile.
The trivial program was something like this

#[cube(launch_unchecked)]
pub fn minimal_reproduction(
    mut output: Tensor<Line<f32>>,
){
    let row = ABSOLUTE_POS;
    let rows = output.shape(0);
    let cols = output.shape(1);
    if row >= rows {
        terminate!()
    }
}

During cubecl's optimization passes, this would eventually look for post dominators during GVN

impl Analysis for PostDominators {
    fn init(opt: &mut crate::Optimizer) -> Self {
        let mut reversed = opt.program.graph.clone();
        reversed.reverse();
        PostDominators(dominators::simple_fast(&reversed, opt.ret))
    }
}

Since the whole program had been folded into a single basic block, but the opt.ret index had not been updated, this would attempt to traverse the graph starting from a non-existent index, leading to a panic.

This change ensures that the opt.ret index is updated if we remove the block it was previously pointing to, at least in this pass.

return block is folded into an existing block
for merge_successor in merge_successors {
opt.program.add_edge(block, merge_successor, 0);
}
if opt.ret == merge {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should put this in update_references so it applies to all merge passes. I did indeed forget about the that value 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants