Skip to content

Conversation

@junxzm1990
Copy link
Contributor

@junxzm1990 junxzm1990 commented Nov 24, 2025

Description

This PR adds reaching definition analysis for temporaries and global resources used in a function. It is the first PR on the stack to introduce Common Subexpression Elimination (CSE) for Move.

For each bytecode at code_offset, the analysis provides a mapping from each local temporary or global resource to the set of code offsets where they may be defined that can reach code_offset:

  • ReachingDefState(code_offset) := Map<Object, Set<CodeOffset>>
  • Object := Local(temp_index) | Global(struct_id)

The analysis is over-approximation, producing may-definitions and may-reaches. A result ReachingDefState(offset_1) := Map<obj1, Set<offset_2, offset_3>> means that obj1 is possibly defined at offset_2 and offset_3 and the definitions may reach offset_1.

================= for temporaries =================

  • Definitions of temporaries are created at Assign, Load instructions, and Call instructions with destinations.
  • Definitions are killed when the temporary is re-defined, or when it is moved.
  • Definitions via dereferences are handled conservatively
    • WriteRef: all temporaries potentially pointed to by the reference are killed and re-defined
    • mut_ref passed to callees: all temporaries potentially pointed to by the reference are killed and re-defined
    • mut_ref passed to invoked closures: all temporaries potentially pointed to by the reference are killed and re-defined

================ for global resources =================

  • Definitions of global resources are killed and re-defined at the following instructions:

    • MoveFrom
    • MoveTo
    • Mutation via dereferencing mut_ref, conservatively handled like temporaries
  • Callees and invoked closures are handled conservatively:

    • On Call to a child function, the child function and its transitively used functions are analyzed to collect the accessed resource structs
    • On Invoke of a closure, the functions used transitively by the current function are analyzed to collect the accessed resource structs

Important Note: The analysis only considers global resources that are declared in the same module of the target function

How Has This Been Tested?

  • Test cases will be shipped with later PRs for CSE optimizations.

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 (specify)

Note

Introduces a reaching definitions analysis for locals and same-module global resources, and extends reference-safety to expose referenced objects used by the new pass.

  • Analysis/IR:
    • Reaching Definitions (new): Add pipeline/reaching_def_analysis_processor.rs implementing forward may-reaching-defs for Object = Local | Global with per-instruction ReachingDefAnnotation and formatter.
      • Handles Assign/Load/Call dest defs; kills on re-def, moves, Drop.
      • Conservatively re-defs via WriteRef, passing mut_ref to Function/Invoke.
      • Tracks globals on MoveFrom/MoveTo and via transitive same-module accesses from callees/closures.
  • Reference Safety API:
    • Introduce reference_safety::Object and add referenced_objects_* helpers to LifetimeAnnotation and LifetimeInfo trait.
    • Implement referenced_objects in both reference_safety_processor_v2 and v3 (borrow-graph–based traversal), plus default no-op in NoLifetimeInfo.
  • Pipeline:
    • Export reaching_def_analysis_processor module in pipeline/mod.rs.

Written by Cursor Bugbot for commit 2d543b1. This will update automatically on new commits. Configure here.

Copy link
Contributor Author

junxzm1990 commented Nov 24, 2025

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

This is the final PR Bugbot will review for you during this billing cycle

Your free Bugbot reviews will reset on December 7

Details

Your team is on the Bugbot Free tier. On this plan, Bugbot will review limited PRs each billing cycle for each member of your team.

To receive Bugbot reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@junxzm1990 junxzm1990 force-pushed the jun/reach-def branch 2 times, most recently from a6827da to 69c4e71 Compare November 24, 2025 20:56
pub mod reference_safety_processor_v3;

#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Object {
Copy link
Contributor

Choose a reason for hiding this comment

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

Object is a little general, maybe use a more specific name?

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. Will update in the next PR.

@junxzm1990 junxzm1990 changed the title [move compiler] add reaching-def analysis [move compiler] [CSE Step 1] add reaching-def analysis Nov 25, 2025
| Operation::MoveFrom(mid, sid, _),
_,
_,
) => Some(Object::Global(mid.qualified(sid))),
Copy link

Choose a reason for hiding this comment

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

Bug: BorrowGlobal incorrectly treated as global resource definition

The collect_potential_global_access closure includes BorrowGlobal operations when collecting global resource definitions. However, BorrowGlobal only creates a reference to a global resource without modifying it, so it should not be treated as a definition. According to the documentation, only MoveFrom, MoveTo, and mutations via mutable references should be considered as definitions of global resources. Including BorrowGlobal causes the analysis to incorrectly mark global resources as redefined when they are merely borrowed.

Fix in Cursor Fix in Web

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.

3 participants