Skip to content
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

[compiler-v2] Test cases reduced from the framework showcasing need for stack optimizations #14800

Merged
merged 2 commits into from
Oct 28, 2024
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
112 changes: 112 additions & 0 deletions third_party/move/move-compiler-v2/tests/eager-pushes/eager_load_03.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
============ initial bytecode ================

[variant baseline]
fun m::bar($t0: &mut u64) {
0: return ()
}


[variant baseline]
fun m::baz($t0: u64, $t1: u64) {
0: return ()
}


[variant baseline]
public fun m::foo($t0: u64) {
var $t1: u64
var $t2: &mut u64
0: $t1 := m::one()
1: $t2 := borrow_local($t0)
2: m::bar($t2)
3: m::baz($t0, $t1)
4: return ()
}


[variant baseline]
fun m::one(): u64 {
var $t0: u64
0: $t0 := 1
1: return $t0
}

============ after LiveVarAnalysisProcessor: ================

[variant baseline]
fun m::bar($t0: &mut u64) {
# live vars: $t0
0: drop($t0)
# live vars:
1: return ()
}


[variant baseline]
fun m::baz($t0: u64, $t1: u64) {
# live vars: $t0, $t1
0: return ()
}


[variant baseline]
public fun m::foo($t0: u64) {
var $t1: u64
var $t2: &mut u64
# live vars: $t0
0: $t1 := m::one()
# live vars: $t0, $t1
1: $t2 := borrow_local($t0)
# live vars: $t0, $t1, $t2
2: m::bar($t2)
# live vars: $t0, $t1
3: m::baz($t0, $t1)
# live vars:
4: return ()
}


[variant baseline]
fun m::one(): u64 {
var $t0: u64
# live vars:
0: $t0 := 1
# live vars: $t0
1: return $t0
}


============ disassembled file-format ==================
// Move bytecode v7
module c0ffee.m {


bar(Arg0: &mut u64) /* def_idx: 0 */ {
B0:
0: MoveLoc[0](Arg0: &mut u64)
1: Pop
2: Ret
}
baz(Arg0: u64, Arg1: u64) /* def_idx: 1 */ {
B0:
0: Ret
}
public foo(Arg0: u64) /* def_idx: 2 */ {
L1: loc0: u64
B0:
0: Call one(): u64
1: MutBorrowLoc[0](Arg0: u64)
2: Call bar(&mut u64)
3: StLoc[1](loc0: u64)
4: MoveLoc[0](Arg0: u64)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without interprocedural analysis, this code looks pretty optimal. You can't load Arg0 earlier because of the call to bar(&mut Arg0). Meanwhile, loc0 comes from the call to one() and you have to rearrange the two arguments before calling baz. A swap operation would be useful here, but without that this looks good.

Of course, if you can inline the other calls you can do much better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was a test case to show that you should not pre-load Arg0 because of the &mut to it, like you point out.

I should have mentioned in the PR description that there are some negative test cases as well - this one is actually handcrafted and not reduced from framework code.

5: MoveLoc[1](loc0: u64)
6: Call baz(u64, u64)
7: Ret
}
one(): u64 /* def_idx: 3 */ {
B0:
0: LdU64(1)
1: Ret
}
}
============ bytecode verification succeeded ========
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module 0xc0ffee::m {
fun one(): u64 {
1
}

fun bar(_x: &mut u64) {}

fun baz(_x: u64, _y: u64) {}

public fun foo(x: u64) {
let t = one();
bar(&mut x);
baz(x, t);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
============ initial bytecode ================

[variant baseline]
fun m::foo() {
0: return ()
}


[variant baseline]
fun m::one(): u64 {
var $t0: u64
0: $t0 := 1
1: return $t0
}


[variant baseline]
public fun m::test($t0: u64) {
var $t1: u64
var $t2: bool
var $t3: u64
var $t4: u64
var $t5: u64
0: $t1 := m::two()
1: $t4 := infer($t0)
2: $t5 := m::one()
3: $t3 := -($t4, $t5)
4: $t2 := >($t3, $t1)
5: if ($t2) goto 6 else goto 9
6: label L0
7: m::foo()
8: goto 10
9: label L1
10: label L2
11: return ()
}


[variant baseline]
fun m::two(): u64 {
var $t0: u64
0: $t0 := 2
1: return $t0
}

============ after LiveVarAnalysisProcessor: ================

[variant baseline]
fun m::foo() {
# live vars:
0: return ()
}


[variant baseline]
fun m::one(): u64 {
var $t0: u64
# live vars:
0: $t0 := 1
# live vars: $t0
1: return $t0
}


[variant baseline]
public fun m::test($t0: u64) {
var $t1: u64
var $t2: bool
var $t3: u64 [unused]
var $t4: u64 [unused]
var $t5: u64
# live vars: $t0
0: $t1 := m::two()
# live vars: $t0, $t1
1: $t5 := m::one()
# live vars: $t0, $t1, $t5
2: $t0 := -($t0, $t5)
# live vars: $t0, $t1
3: $t2 := >($t0, $t1)
# live vars: $t2
4: if ($t2) goto 7 else goto 5
# live vars:
5: label L3
# live vars:
6: goto 9
# live vars:
7: label L0
# live vars:
8: m::foo()
# live vars:
9: label L2
# live vars:
10: return ()
}


[variant baseline]
fun m::two(): u64 {
var $t0: u64
# live vars:
0: $t0 := 2
# live vars: $t0
1: return $t0
}


============ disassembled file-format ==================
// Move bytecode v7
module c0ffee.m {


foo() /* def_idx: 0 */ {
B0:
0: Ret
}
one(): u64 /* def_idx: 1 */ {
B0:
0: LdU64(1)
1: Ret
}
public test(Arg0: u64) /* def_idx: 2 */ {
L1: loc0: u64
L2: loc1: u64
B0:
0: Call two(): u64
1: StLoc[1](loc0: u64)
2: Call one(): u64
3: StLoc[2](loc1: u64)
4: MoveLoc[0](Arg0: u64)
5: MoveLoc[2](loc1: u64)
6: Sub
7: MoveLoc[1](loc0: u64)
8: Gt
9: BrTrue(11)
B1:
10: Branch(12)
B2:
11: Call foo()
B3:
12: Ret
}
two(): u64 /* def_idx: 3 */ {
B0:
0: LdU64(2)
1: Ret
}
}
============ bytecode verification succeeded ========
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module 0xc0ffee::m {
fun one(): u64 {
1
}

fun two(): u64 {
2
}

fun foo() {}

public fun test(p: u64) {
let e = two();
if (p - one() > e) {
foo();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
============ initial bytecode ================

[variant baseline]
public fun m::make($t0: u64, $t1: u64, $t2: u64, $t3: &0xc0ffee::m::S, $t4: u64): 0xc0ffee::m::Wrap {
var $t5: 0xc0ffee::m::Wrap
var $t6: u64
var $t7: &u64
0: $t7 := borrow_field<0xc0ffee::m::S>.x($t3)
1: $t6 := read_ref($t7)
2: $t5 := pack 0xc0ffee::m::Wrap($t0, $t1, $t2, $t6, $t4)
3: return $t5
}

============ after LiveVarAnalysisProcessor: ================

[variant baseline]
public fun m::make($t0: u64, $t1: u64, $t2: u64, $t3: &0xc0ffee::m::S, $t4: u64): 0xc0ffee::m::Wrap {
var $t5: 0xc0ffee::m::Wrap
var $t6: u64
var $t7: &u64
# live vars: $t0, $t1, $t2, $t3, $t4
0: $t7 := borrow_field<0xc0ffee::m::S>.x($t3)
# live vars: $t0, $t1, $t2, $t4, $t7
1: $t6 := read_ref($t7)
# live vars: $t0, $t1, $t2, $t4, $t6
2: $t5 := pack 0xc0ffee::m::Wrap($t0, $t1, $t2, $t6, $t4)
# live vars: $t5
3: return $t5
}


============ disassembled file-format ==================
// Move bytecode v7
module c0ffee.m {
struct S {
x: u64
}
struct Wrap {
a: u64,
b: u64,
c: u64,
d: u64,
e: u64
}

public make(Arg0: u64, Arg1: u64, Arg2: u64, Arg3: &S, Arg4: u64): Wrap /* def_idx: 0 */ {
L5: loc0: u64
B0:
0: MoveLoc[3](Arg3: &S)
1: ImmBorrowField[0](S.x: u64)
2: ReadRef
3: StLoc[5](loc0: u64)
4: MoveLoc[0](Arg0: u64)
5: MoveLoc[1](Arg1: u64)
6: MoveLoc[2](Arg2: u64)
7: MoveLoc[5](loc0: u64)
8: MoveLoc[4](Arg4: u64)
9: Pack[1](Wrap)
10: Ret
}
}
============ bytecode verification succeeded ========
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module 0xc0ffee::m {
struct Wrap {
a: u64,
b: u64,
c: u64,
d: u64,
e: u64,
}

struct S {
x: u64,
}

public fun make(a: u64, b: u64, c: u64, d: &S, e: u64): Wrap {
Wrap {
a,
b,
c,
d: d.x,
e,
}
}
}
Loading
Loading