1
1
use std::hint::black_box ;
2
2
3
3
fn main (a : u32 , b : u32 ) {
4
- // This version unrolls into a number of additions
5
- assert_eq (loop (5 , a ), b );
6
- // This version simplifies into a single `constraint 50 == b`
7
- assert_eq (loop (5 , 10 ), b );
8
- // This version should not simplify down to a single constraint,
9
- // it should treat 10 as opaque:
10
- assert_eq (loop (5 , black_box (10 )), b );
11
-
12
- // Check array handling.
13
- let arr = [a , a , a , a , a ];
14
-
15
- assert_eq (array_sum (arr ), b );
16
- assert_eq (array_sum (black_box (arr )), b );
17
-
18
- assert_eq (slice_sum (arr .as_slice ()), b );
19
- assert_eq (slice_sum (black_box (arr ).as_slice ()), b );
20
-
21
- // This doesn't work because by calling `black_box` on a slice the compiler
22
- // loses track of the length, and then cannot unroll the loop for ACIR.
23
- //assert_eq(slice_sum(black_box(arr.as_slice())), b);
24
-
25
- // But we can pass a blackboxed slice to Brillig.
26
- let s = unsafe {
27
- brillig_slice_sum (black_box (arr .as_slice ()))
28
- };
29
- assert_eq (s , b );
4
+ // // This version unrolls into a number of additions
5
+ // assert_eq(loop(5, a), b);
6
+ // // This version simplifies into a single `constraint 50 == b`
7
+ // assert_eq(loop(5, 10), b);
8
+ // // This version should not simplify down to a single constraint,
9
+ // // it should treat 10 as opaque:
10
+ // assert_eq(loop(5, black_box(10)), b);
11
+
12
+ // // Check array handling.
13
+ // let arr = [a, a, a, a, a];
14
+
15
+ // assert_eq(array_sum(arr), b);
16
+ // assert_eq(array_sum(black_box(arr)), b);
17
+
18
+ // assert_eq(slice_sum(arr.as_slice()), b);
19
+ // assert_eq(slice_sum(black_box(arr).as_slice()), b);
20
+
21
+ // // This doesn't work because by calling `black_box` on a slice the compiler
22
+ // // loses track of the length, and then cannot unroll the loop for ACIR.
23
+ // //assert_eq(slice_sum(black_box(arr.as_slice())), b);
24
+
25
+ // // But we can pass a blackboxed slice to Brillig.
26
+ // let s = unsafe {
27
+ // brillig_slice_sum(black_box(arr.as_slice()))
28
+ // };
29
+ // assert_eq(s, b);
30
+
31
+ let mut d = b ;
32
+ // This gets completely eliminated:
33
+ let mut c = 0 ;
34
+ set_ref (&mut c , &mut d );
35
+ assert_eq (c , b );
36
+
37
+ // This way the constraint is preserved:
38
+ let mut c = 0 ;
39
+ set_ref (&mut c , &mut black_box (d ));
40
+ assert_eq (c , b );
41
+
42
+ // A reference over the output of black box is not the original variable:
43
+ let mut c = 0 ;
44
+ set_ref (&mut black_box (c ), &mut d );
45
+ assert_eq (c , 0 );
46
+
47
+ // This would cause a causes a crash during SSA passes unless it's a Brillig runtime:
48
+ // > Could not resolve some references to the array. All references must be resolved at compile time
49
+ // If we use `--force-brillig` then the it doesn't crash but the assertion fails.
50
+ // let mut c = 0;
51
+ // set_ref(black_box(&mut c), black_box(&mut d));
52
+ // assert_eq(c, d);
30
53
}
31
54
32
55
fn loop (n : u32 , k : u32 ) -> u32 {
@@ -47,16 +70,20 @@ fn array_sum<let N: u32>(xs: [u32; N]) -> u32 {
47
70
48
71
fn slice_sum (xs : [u32 ]) -> u32 {
49
72
let mut sum = 0 ;
50
- for x in xs {
73
+ for x in xs {
51
74
sum = sum + x ;
52
75
}
53
76
sum
54
77
}
55
78
56
79
unconstrained fn brillig_slice_sum (xs : [u32 ]) -> u32 {
57
80
let mut sum = 0 ;
58
- for x in xs {
81
+ for x in xs {
59
82
sum = sum + x ;
60
83
}
61
84
sum
62
- }
85
+ }
86
+
87
+ fn set_ref (c : &mut u32 , b : &mut u32 ) {
88
+ *c = *b ;
89
+ }
0 commit comments