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
22 changes: 22 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,28 @@ impl Function {
}
})
}

/// Asserts that the [`Function`] is well formed.
///
/// Panics on malformed functions.
pub(crate) fn assert_valid(&self) {
let reachable_blocks = self.reachable_blocks();

// We assume that all functions have a single block which terminates with a `return` instruction.
let return_blocks: BTreeSet<_> = reachable_blocks
.iter()
.filter(|block| {
// All blocks must have a terminator instruction of some sort.
let terminator = self.dfg[**block].terminator().unwrap_or_else(|| {
panic!("Function {} has no terminator in block {block}", self.id())
});
matches!(terminator, TerminatorInstruction::Return { .. })
})
.collect();
if return_blocks.len() > 1 {
panic!("Function {} has multiple return blocks {return_blocks:?}", self.id())
}
}
}

impl Clone for Function {
Expand Down
24 changes: 21 additions & 3 deletions compiler/noirc_evaluator/src/ssa/opt/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,12 +816,14 @@ mod test {
v2 = lt v1, u32 1
jmpif v2 then: b1, else: b2
b1():
return u32 1
jmp b3(u32 1)
b2():
v4 = sub v1, u32 1
v5 = call f1(v4) -> u32
v6 = mul v1, v5
return v6
jmp b3(v6)
b3(v7: u32):
return v7
}
";
let ssa = Ssa::from_str(src).unwrap();
Expand All @@ -842,7 +844,23 @@ mod test {
b5():
jmp b6()
b6():
return u32 120
jmp b7(u32 1)
b7(v0: u32):
jmp b8(v0)
b8(v1: u32):
v8 = mul u32 2, v1
jmp b9(v8)
b9(v2: u32):
v10 = mul u32 3, v2
jmp b10(v10)
b10(v3: u32):
v12 = mul u32 4, v3
jmp b11(v12)
b11(v4: u32):
v14 = mul u32 5, v4
jmp b12(v14)
b12(v5: u32):
return v5
}
");
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/opt/simplify_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod test {
b1():
return Field 1
b2():
return Field 2
jmp b1()
}
";
let ssa = Ssa::from_str(src).unwrap();
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,10 @@ impl Translator {
// before each print.
ssa.normalize_ids();

for function in ssa.functions.values() {
function.assert_valid();
}

ssa
}

Expand Down
8 changes: 5 additions & 3 deletions compiler/noirc_evaluator/src/ssa/parser/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ fn test_jmpif() {
b0(v0: Field):
jmpif v0 then: b1, else: b2
b1():
return
jmp b2()
b2():
return
}
Expand All @@ -182,7 +182,7 @@ fn test_jmpif() {
b0(v0: Field):
jmpif v0 then: b2, else: b1
b1():
return
jmp b2()
b2():
return
}
Expand All @@ -197,10 +197,12 @@ fn test_multiple_jmpif() {
b0(v0: Field, v1: Field):
jmpif v0 then: b1, else: b2
b1():
return
jmp b4()
b2():
jmpif v1 then: b3, else: b1
b3():
jmp b4()
b4():
return
}
";
Expand Down
Loading