Skip to content

[GR-61178] Adopt new StackValue API - #1120

Merged
graalvmbot merged 5 commits into
masterfrom
mdsouza/GR-61178-adopt-stackvalues
Sep 1, 2026
Merged

[GR-61178] Adopt new StackValue API#1120
graalvmbot merged 5 commits into
masterfrom
mdsouza/GR-61178-adopt-stackvalues

Conversation

@graalvmbot

Copy link
Copy Markdown
Collaborator

This change migrates temporary local usages to stack values wherever possible (the remaining temporary locals are used with LocalAccessors). Changes can be reviewed commit-by-commit.

The translation is mostly straightforward, but did require an overhaul of pattern matching:

// BEFORE: chained IfThenElses with short-circuit Ands to check patterns
IfThenElse(
  And(
    <case 1 pattern checks, which bind temp_1, ..., temp_n>
    Block(
      <copy temp_1 into var_1>
      ...
      <copy temp_n into var_n>,
      true   // continue unconditionally
    ),
    <guard, if exists>
  ),
  <case 1 body>,
  IfThenElse(
    And(...), // <case 2 condition>
    <case 2 body>,
    ... // <more cases>
  )
)

// NOW: Since StackValues need to live until they can be copied into real variables (see the Block above), they cannot be bound inside the And operand. Instead, we desugar the control flow so that all binds happen in a separate Block for each case.
Block(
  evaluate subject
  Block( // case 1
    // check each subpattern, binding stack values for each named subpattern
    if (!checkPattern) {
       branch afterCase1
    }
    <copy stack values bound by pattern into python variables>
    if (guard != null && !guard) {
      branch afterCase1
    }
    <case 1 body>
    branch endMatch
  )
  afterCase1:
  Block( // case 2
    ...
  )
  afterCase2:
  ...
  Block( // case n
    ...
  )
  endMatch:
)

This approach allows us to lazily BindStackValue(x) in most cases. However, sometimes the StackValues need to be pre-allocated (with BindStackValue(LoadNull)) and then overwritten (with StoreStackValue(x)):

  • OR patterns, because the stack values take on different values in each OR branch.
  • Patterns that span multiple lines, because new lines open up new Tag[Statement] and Block operations (which restricts the lifetime of the stack values)

@oracle-contributor-agreement oracle-contributor-agreement Bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Sep 1, 2026
@graalvmbot
graalvmbot merged commit 1ddfdb0 into master Sep 1, 2026
37 of 41 checks passed
@graalvmbot
graalvmbot deleted the mdsouza/GR-61178-adopt-stackvalues branch September 1, 2026 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

OCA Verified All contributors have signed the Oracle Contributor Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants