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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request][compiler-v2] Generate more efficient code in the presence of asserts #12613

Open
vineethk opened this issue Mar 20, 2024 · 0 comments
Labels
compiler-v2 enhancement New feature or request stale-exempt Prevents issues from being automatically marked and closed as stale

Comments

@vineethk
Copy link
Contributor

馃殌 Feature Request

Originally reported by @georgemitenkov.

Consider the following Move code:

struct Decimal has copy, drop, store {
    val: u128,
  }

 public fun add(arg0: Decimal, arg1: Decimal) : Decimal {
  assert!(arg0.val + arg1.val <= 100, 1);
  Decimal{val: arg0.val + arg1.val}
}

Compiling it with v1 compiler (similar issue exists with v2 as well) generates the following bytecode:

0: ImmBorrowLoc(0)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   ImmBorrowLoc(1)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   Add
   LdConst(6)
   Le
   BrFalse(11)

10:Branch(13)

11: LdConst(1)
   Abort

13: ImmBorrowLoc(0)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   ImmBorrowLoc(1)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   Add
   Pack(0)
   Ret

Ideally, we want to have:

0: ImmBorrowLoc(0)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   ImmBorrowLoc(1)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   Add
   LdConst(6)
   Le
   BrFalse(19)

10: ImmBorrowLoc(0)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   ImmBorrowLoc(1)
   ImmBorrowField(FieldHandleIndex(0))
   ReadRef
   Add
   Pack(0)
   Ret

19: LdConst(1)
    Abort
@vineethk vineethk added the enhancement New feature or request label Mar 20, 2024
@sausagee sausagee added the stale-exempt Prevents issues from being automatically marked and closed as stale label Mar 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler-v2 enhancement New feature or request stale-exempt Prevents issues from being automatically marked and closed as stale
Projects
Status: 馃搵 Backlog
Development

No branches or pull requests

3 participants