Skip to content

Commit 255476f

Browse files
committed
[move compiler] [CSE Step 3] add test cases for CSE
1 parent 571c79c commit 255476f

19 files changed

+4911
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
module 0x99::FiledAccess {
2+
struct Inner has copy, drop {
3+
value: u64,
4+
}
5+
6+
struct Outer has copy, drop {
7+
inner: Inner,
8+
}
9+
10+
fun get_value(outer: &Outer): u64 {
11+
outer.inner.value
12+
}
13+
14+
fun set_value(outer: &mut Outer, new_value: u64) {
15+
outer.inner.value = new_value;
16+
}
17+
18+
fun get_inner_value1(inner: &Inner): u64 {
19+
inner.value
20+
}
21+
22+
fun get_inner_value2(inner: &Inner): u64 {
23+
inner.value
24+
}
25+
26+
// `&arg1.inner` cannot be reused
27+
// perf_gain: 1 borrow_loc + 1 borrow_field eliminated
28+
// new_cost:
29+
// - `u64` flushed and copied twice
30+
// perf_gain < new_cost, so this optimization is not applied
31+
fun test_field_access_with_function(arg1: Outer): u64 {
32+
get_inner_value1(&arg1.inner) + get_inner_value2(&arg1.inner)
33+
}
34+
35+
// `arg1.inner.value` cannot be reused due to the mutation between the two accesses
36+
fun test_field_access(arg1: Outer, arg2: u64): u64 {
37+
let x = arg1.inner.value;
38+
arg1.inner.value += 1;
39+
x + arg2 + arg1.inner.value
40+
}
41+
42+
// `arg1.inner.value` cannot be reused due to the mutation between the two accesses
43+
fun test_field_access_ref(arg1: Outer, arg2: u64): u64 {
44+
let x = arg1.inner.value;
45+
let ref = &mut arg1.inner.value;
46+
*ref += 1;
47+
x + arg2 + arg1.inner.value
48+
}
49+
50+
// `set_value(&mut arg1, arg2)` may modify `arg1.inner.value`, so the two calls cannot be reused
51+
fun test_field_access_mut_ref(arg1: Outer, arg2: u64): u64 {
52+
set_value(&mut arg1, arg2);
53+
set_value(&mut arg1, arg2);
54+
arg1.inner.value
55+
}
56+
}
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
2+
============ disassembled file-format ==================
3+
// Bytecode version v9
4+
module 0x99::FiledAccess
5+
struct Inner has copy + drop
6+
value: u64
7+
8+
struct Outer has copy + drop
9+
inner: Inner
10+
11+
// Function definition at index 0
12+
fun get_inner_value1(l0: &Inner): u64
13+
move_loc l0
14+
borrow_field Inner, value
15+
read_ref
16+
ret
17+
18+
// Function definition at index 1
19+
fun get_inner_value2(l0: &Inner): u64
20+
move_loc l0
21+
borrow_field Inner, value
22+
read_ref
23+
ret
24+
25+
// Function definition at index 2
26+
fun get_value(l0: &Outer): u64
27+
move_loc l0
28+
borrow_field Outer, inner
29+
borrow_field Inner, value
30+
read_ref
31+
ret
32+
33+
// Function definition at index 3
34+
fun set_value(l0: &mut Outer, l1: u64)
35+
local l2: &mut u64
36+
move_loc l0
37+
mut_borrow_field Outer, inner
38+
mut_borrow_field Inner, value
39+
st_loc l2
40+
move_loc l1
41+
// @5
42+
move_loc l2
43+
write_ref
44+
ret
45+
46+
// Function definition at index 4
47+
fun test_field_access(l0: Outer, l1: u64): u64
48+
local l2: &mut u64
49+
borrow_loc l0
50+
borrow_field Outer, inner
51+
borrow_field Inner, value
52+
read_ref
53+
mut_borrow_loc l0
54+
// @5
55+
mut_borrow_field Outer, inner
56+
mut_borrow_field Inner, value
57+
st_loc l2
58+
copy_loc l2
59+
read_ref
60+
// @10
61+
ld_u64 1
62+
add
63+
move_loc l2
64+
write_ref
65+
move_loc l1
66+
// @15
67+
add
68+
borrow_loc l0
69+
borrow_field Outer, inner
70+
borrow_field Inner, value
71+
read_ref
72+
// @20
73+
add
74+
ret
75+
76+
// Function definition at index 5
77+
fun test_field_access_mut_ref(l0: Outer, l1: u64): u64
78+
mut_borrow_loc l0
79+
copy_loc l1
80+
call set_value
81+
mut_borrow_loc l0
82+
move_loc l1
83+
// @5
84+
call set_value
85+
borrow_loc l0
86+
borrow_field Outer, inner
87+
borrow_field Inner, value
88+
read_ref
89+
// @10
90+
ret
91+
92+
// Function definition at index 6
93+
fun test_field_access_ref(l0: Outer, l1: u64): u64
94+
local l2: &mut u64
95+
borrow_loc l0
96+
borrow_field Outer, inner
97+
borrow_field Inner, value
98+
read_ref
99+
mut_borrow_loc l0
100+
// @5
101+
mut_borrow_field Outer, inner
102+
mut_borrow_field Inner, value
103+
st_loc l2
104+
copy_loc l2
105+
read_ref
106+
// @10
107+
ld_u64 1
108+
add
109+
move_loc l2
110+
write_ref
111+
move_loc l1
112+
// @15
113+
add
114+
borrow_loc l0
115+
borrow_field Outer, inner
116+
borrow_field Inner, value
117+
read_ref
118+
// @20
119+
add
120+
ret
121+
122+
// Function definition at index 7
123+
fun test_field_access_with_function(l0: Outer): u64
124+
borrow_loc l0
125+
borrow_field Outer, inner
126+
call get_inner_value1
127+
borrow_loc l0
128+
borrow_field Outer, inner
129+
// @5
130+
call get_inner_value2
131+
add
132+
ret
133+
134+
135+
============ bytecode verification succeeded ========

0 commit comments

Comments
 (0)