Skip to content

Commit 5def442

Browse files
committed
fixup do_not_hoist_unsafe_div test
1 parent 38e12f2 commit 5def442

File tree

1 file changed

+73
-51
lines changed

1 file changed

+73
-51
lines changed

compiler/noirc_evaluator/src/ssa/opt/loop_invariant.rs

+73-51
Original file line numberDiff line numberDiff line change
@@ -957,32 +957,44 @@ mod test {
957957

958958
#[test]
959959
fn do_not_hoist_unsafe_div() {
960-
// This test is similar to `nested_loop_invariant_code_motion`, the operation
961-
// in question we are trying to hoist is `v7 = div i32 10, v0`.
962-
// Check that the lower bound of the outer loop it checked and that we not
960+
// This test is similar to `nested_loop_invariant_code_motion`, except that
961+
// the loop logic is under a dynamic predicate.
962+
// Divisions are only reliant upon predicates and do not have other side effects.
963+
// If an unsafe division occurs in a loop block that is not control dependent,
964+
// we can still safely hoist that division as that instruction is always going to be hit.
965+
// Thus, we place the unsafe division under a predicate to ensure that we are testing
966+
// division hoisting based upon loop bounds and nothing else.
967+
//
968+
// The operation in question we are trying to hoist is `v12 = div u32 10, v1`.
969+
// Check that the lower bound of the outer loop is checked and that we do not
963970
// hoist an operation that can potentially error with a division by zero.
964971
let src = "
965972
brillig(inline) fn main f0 {
966-
b0():
967-
jmp b1(i32 0)
968-
b1(v0: i32):
969-
v4 = lt v0, i32 4
970-
jmpif v4 then: b3, else: b2
973+
b0(v0: u32):
974+
v4 = eq v0, u32 5
975+
jmp b1(u32 0)
976+
b1(v1: u32):
977+
v7 = lt v1, u32 4
978+
jmpif v7 then: b2, else: b3
971979
b2():
972-
return
980+
jmp b4(u32 0)
973981
b3():
974-
jmp b4(i32 0)
975-
b4(v1: i32):
976-
v5 = lt v1, i32 4
977-
jmpif v5 then: b6, else: b5
982+
return
983+
b4(v2: u32):
984+
v8 = lt v2, u32 4
985+
jmpif v8 then: b5, else: b6
978986
b5():
979-
v11 = unchecked_add v0, i32 1
980-
jmp b1(v11)
987+
jmpif v4 then: b7, else: b8
981988
b6():
982-
v7 = div i32 10, v0
983-
constrain v7 == i32 6
984-
v10 = unchecked_add v1, i32 1
985-
jmp b4(v10)
989+
v10 = unchecked_add v1, u32 1
990+
jmp b1(v10)
991+
b7():
992+
v12 = div u32 10, v1
993+
constrain v12 == u32 6
994+
jmp b8()
995+
b8():
996+
v14 = unchecked_add v2, u32 1
997+
jmp b4(v14)
986998
}
987999
";
9881000

@@ -998,26 +1010,31 @@ mod test {
9981010
// in this test starts with a lower bound of `1`.
9991011
let src = "
10001012
brillig(inline) fn main f0 {
1001-
b0():
1002-
jmp b1(i32 1)
1003-
b1(v0: i32):
1004-
v4 = lt v0, i32 4
1005-
jmpif v4 then: b3, else: b2
1013+
b0(v0: u32):
1014+
v4 = eq v0, u32 5
1015+
jmp b1(u32 1)
1016+
b1(v1: u32):
1017+
v7 = lt v1, u32 4
1018+
jmpif v7 then: b2, else: b3
10061019
b2():
1007-
return
1020+
jmp b4(u32 0)
10081021
b3():
1009-
jmp b4(i32 0)
1010-
b4(v1: i32):
1011-
v5 = lt v1, i32 4
1012-
jmpif v5 then: b6, else: b5
1022+
return
1023+
b4(v2: u32):
1024+
v9 = lt v2, u32 4
1025+
jmpif v9 then: b5, else: b6
10131026
b5():
1014-
v7 = unchecked_add v0, i32 1
1015-
jmp b1(v7)
1027+
jmpif v4 then: b7, else: b8
10161028
b6():
1017-
v9 = div i32 10, v0
1018-
constrain v9 == i32 6
1019-
v11 = unchecked_add v1, i32 1
1020-
jmp b4(v11)
1029+
v10 = unchecked_add v1, u32 1
1030+
jmp b1(v10)
1031+
b7():
1032+
v12 = div u32 10, v1
1033+
constrain v12 == u32 6
1034+
jmp b8()
1035+
b8():
1036+
v14 = unchecked_add v2, u32 1
1037+
jmp b4(v14)
10211038
}
10221039
";
10231040

@@ -1026,26 +1043,31 @@ mod test {
10261043
let ssa = ssa.loop_invariant_code_motion();
10271044
let expected = "
10281045
brillig(inline) fn main f0 {
1029-
b0():
1030-
jmp b1(i32 1)
1031-
b1(v0: i32):
1032-
v4 = lt v0, i32 4
1033-
jmpif v4 then: b3, else: b2
1046+
b0(v0: u32):
1047+
v4 = eq v0, u32 5
1048+
jmp b1(u32 1)
1049+
b1(v1: u32):
1050+
v7 = lt v1, u32 4
1051+
jmpif v7 then: b2, else: b3
10341052
b2():
1035-
return
1053+
v9 = div u32 10, v1
1054+
jmp b4(u32 0)
10361055
b3():
1037-
v6 = div i32 10, v0
1038-
jmp b4(i32 0)
1039-
b4(v1: i32):
1040-
v8 = lt v1, i32 4
1041-
jmpif v8 then: b6, else: b5
1056+
return
1057+
b4(v2: u32):
1058+
v11 = lt v2, u32 4
1059+
jmpif v11 then: b5, else: b6
10421060
b5():
1043-
v11 = unchecked_add v0, i32 1
1044-
jmp b1(v11)
1061+
jmpif v4 then: b7, else: b8
10451062
b6():
1046-
constrain v6 == i32 6
1047-
v10 = unchecked_add v1, i32 1
1048-
jmp b4(v10)
1063+
v12 = unchecked_add v1, u32 1
1064+
jmp b1(v12)
1065+
b7():
1066+
constrain v9 == u32 6
1067+
jmp b8()
1068+
b8():
1069+
v14 = unchecked_add v2, u32 1
1070+
jmp b4(v14)
10491071
}
10501072
";
10511073

0 commit comments

Comments
 (0)