Skip to content

chore: debug mem2reg for load in loop #6978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 25 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
//!
//! Repeating this algorithm for each block in the function in program order should result in
//! optimizing out most known loads. However, identifying all aliases correctly has been proven
//! undecidable in general (Landi, 1992). So this pass will not always optimize out all loads

Check warning on line 71 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Landi)
//! that could theoretically be optimized out. This pass can be performed at any time in the
//! SSA optimization pipeline, although it will be more successful the simpler the program's CFG is.
//! This pass is currently performed several times to enable other passes - most notably being
Expand Down Expand Up @@ -129,7 +129,7 @@
/// Load and Store instructions that should be removed at the end of the pass.
///
/// We avoid removing individual instructions as we go since removing elements
/// from the middle of Vecs many times will be slower than a single call to `retain`.

Check warning on line 132 in compiler/noirc_evaluator/src/ssa/opt/mem2reg.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (Vecs)
instructions_to_remove: HashSet<InstructionId>,

/// Track a value's last load across all blocks.
Expand Down Expand Up @@ -1228,4 +1228,29 @@
// We expect the program to be unchanged
assert_normalized_ssa_equals(ssa, src);
}

#[test]
fn optimizes_out_load_in_loop_if_var_is_read_only_in_loop() {
let src = "
brillig(inline) fn main f0 {
b0(v0: [u8; 1]):
v2 = allocate -> &mut u32
store u32 1 at v2
jmp b1(u32 0)
b1(v1: u32):
jmpif u1 1 then: b3, else: b2
b2():
return Field 0
b3():
// TODO: this load should be optimized out to `u32 1`
v7 = load v2 -> u32
jmp b1(v7)
}
";
let ssa = Ssa::from_str(src).unwrap();

let mut ssa = ssa.mem2reg();
ssa.normalize_ids();
println!("{ssa}");
}
}
Loading