Skip to content

fix decompiler assignment optimization issue #17008 #17120

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-issue-17087
Choose a base branch
from

Conversation

junxzm1990
Copy link
Contributor

@junxzm1990 junxzm1990 commented Jul 22, 2025

Description

The decompiler optimizes the following pattern

t1 = rhs;
...
use(t1)

===>

...
use(rhs)

If t1 is never used (i.e., use(t1) does not exist), the decompiler will eliminate t1 = rhs entirely. This is not safe as rhs can have side effects (e.g., rhs being a function call). This PR fixes this issue by checking if rhs carries potential side effects. Currently, it only checks if rhs is a Call(Operation::MoveFunction) or Invoke.

Close issue #17008.

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

junxzm1990 commented Jul 22, 2025

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 marked this pull request as ready for review July 22, 2025 02:53
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-issue-17087 branch from c90d427 to 2d404c8 Compare July 22, 2025 03:17
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-assignment-opt branch 2 times, most recently from 31332e9 to 1a86009 Compare July 22, 2025 15:38
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-issue-17087 branch from 2d404c8 to e6cf841 Compare July 22, 2025 15:41
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-assignment-opt branch from 1a86009 to 6bacdef Compare July 22, 2025 15:41
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-issue-17087 branch from e6cf841 to 5cc0a94 Compare July 22, 2025 15:44
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-assignment-opt branch from 6bacdef to 1d06027 Compare July 22, 2025 15:44
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-issue-17087 branch from 5cc0a94 to c60be0d Compare July 22, 2025 15:48
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-assignment-opt branch from 1d06027 to 9b9a80d Compare July 22, 2025 15:48
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-issue-17087 branch from c60be0d to f7078a2 Compare July 22, 2025 15:51
@junxzm1990 junxzm1990 force-pushed the jun/decompiler-assignment-opt branch from 9b9a80d to 8f239c3 Compare July 22, 2025 15:51
impl AssignTransformer<'_> {
/// Check if an expression is safe to eliminate, assuming its result is never used.
/// [TODO]: refine the rules to consider other side-effect-carrying operations.
fn safe_to_eliminate(&self, exp: &Exp) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this leaves out whole lot of cases: e.g., addition would be considered safe to eliminate (even though it is not), blocks with side-effects would be disregarded.

I wonder if a a better approach would be to have an allow-list for those constructs that are known to be safe: e.g., Value, LocalVar, Temporary, etc. Otherwise, it would lead to subtle bugs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. Using an allow-list is easier to maintain safety. At this stage, the AST is fairly simple: it should not have nested expressions and complex ones like blocks. So, by even just considering Value, LocalVar, and Temporary, the optimization effects should largely remain. Let me try.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done. It matched my expectations.

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.

2 participants