Skip to content

Cranelift: Infinite loop in cranelift-frontend's variable ssa state machine #2903

@Mrmaxmeier

Description

@Mrmaxmeier

I'm experimenting with some cranelift codegen and I'm seeing a hang/OOM related to the variable ssa resolver.

Here's a minimal reproducer (for cranelift/frontend/src/frontend.rs):

#[test]
fn ssa_state_machine_hang() {
    let sig = Signature::new(CallConv::SystemV);

    let mut fn_ctx = FunctionBuilderContext::new();
    let mut func = Function::with_name_signature(ExternalName::testcase("sample"), sig);
    {
        let mut builder = FunctionBuilder::new(&mut func, &mut fn_ctx);

        let a = Variable::new(0);
        builder.declare_var(a, I32);

        let entry = builder.create_block();
        let block1 = builder.create_block();
        let use_block = builder.create_block();

        // fill entry block
        builder.switch_to_block(entry);
        use cranelift_codegen::ir::TrapCode;
        builder.ins().trap(TrapCode::User(0));

        // fill block1 with back edge
        builder.switch_to_block(block1);
        let cond = builder.ins().iconst(I32, 0);
        builder.ins().brnz(cond, block1, &[]);
        builder.ins().jump(use_block, &[]);

        // use variable in use_block
        builder.switch_to_block(use_block);
        let _ = builder.use_var(a);
        builder.ins().return_(&[]);

        println!("sealing blocks...");
        builder.seal_all_blocks(); // hangs
        builder.finalize();
    }
    println!("{}", func.display(None).to_string());
}

It hangs in seal_all_blocks -> run_state_machine with a huge/growing self.calls vec.

Versions and Environment

Cranelift version or commit: v0.73.0 and current main

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIncorrect behavior in the current implementation that needs fixingcraneliftIssues related to the Cranelift code generator

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions