Skip to content

push local variable declarations to their proper scope #17141

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

Open
wants to merge 1 commit into
base: jun/decompiler-assignment-opt
Choose a base branch
from

Conversation

junxzm1990
Copy link
Contributor

@junxzm1990 junxzm1990 commented Jul 23, 2025

Description

The decompiler declares every local variables at the beginning of a function, making them hard to track. This PR pushes the declarations down to the desired scope.

For example,

    fun id2(self: Entity): u64 {
        let _t10;
        let _t9;
        let _t2;
        let _t20;
        let _t1;
        _t1 = &self;
        if ((_t1 is Person) && *&_t1.id > 0) {
            Entity::Person{id: _t20} = self;
            _t2 = _t20
        } else if (_t1 is Institution) {
            Entity::Institution{id: _t9,admin: _t10} = self;
            _t2 = _t9
        } else _t2 = 0;
        _t2
    }

will be optimized into

    fun id2(self: Entity): u64 {
        let _t2;
        let _t1;
        _t1 = &self;
        if ((_t1 is Person) && *&_t1.id > 0) {
            let _t20;
            Entity::Person{id: _t20} = self;
            _t2 = _t20
        } else if (_t1 is Institution) {
            let _t10;
            let _t9;
            Entity::Institution{id: _t9,admin: _t10} = self;
            _t2 = _t9
        } else _t2 = 0;
        _t2
    }

The basic idea: Given a free var, if it is only used in a Sequence, we try to declare the var in this Sequence unless we can do the same thing inside a nested-Sequence.

How Has This Been Tested?

  • Existing decompiler test cases.

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Performance improvement
  • Refactoring
  • Dependency update
  • Documentation update
  • Tests

Which Components or Systems Does This Change Impact?

  • Validator Node
  • Full Node (API, Indexer, etc.)
  • Move/Aptos Virtual Machine
  • Aptos Framework
  • Aptos CLI/SDK
  • Developer Infrastructure
  • Move Compiler
  • Other (Move Decompiler)

Copy link
Contributor Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

This stack of pull requests is managed by Graphite. Learn more about stacking.

@junxzm1990 junxzm1990 force-pushed the jun/decompiler-locals-opt branch 2 times, most recently from d55042d to e19aa8f Compare July 24, 2025 05:58
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-locals-opt branch from e19aa8f to aeee8ec Compare July 24, 2025 06:28
@junxzm1990 junxzm1990 marked this pull request as ready for review July 24, 2025 06:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant