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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lost stack values when entering a loop #678

Open
gtrepta opened this issue Jul 16, 2024 · 0 comments
Open

Lost stack values when entering a loop #678

gtrepta opened this issue Jul 16, 2024 · 0 comments

Comments

@gtrepta
Copy link
Contributor

gtrepta commented Jul 16, 2024

(module
  ;; Iterative factorial without locals.
  (func $pick0 (param i64) (result i64 i64)
    (local.get 0) (local.get 0)
  )
  (func $pick1 (param i64 i64) (result i64 i64 i64)
    (local.get 0) (local.get 1) (local.get 0)
  )
  (func (export "fac-ssa") (param i64) (result i64)
    (i64.const 1) (local.get 0)
    (loop $l (param i64 i64) (result i64)
      (call $pick1) (call $pick1) (i64.mul)
      (call $pick1) (i64.const 1) (i64.sub)
      (call $pick0) (i64.const 0) (i64.gt_u)
      (br_if $l)
      (drop) (return)
    )
  )
)

(assert_return (invoke "fac-ssa" (i64.const 25)) (i64.const 7034535277573963776))

After fac-ssa gets invoked and the loop instruction is reached, the two values that have been pushed onto the stack disappear when we enter the loop, so then the subsequent call to $pick1 doesn't have any parameters to use.

The spec

Our rule:

syntax Instr ::= #loop(VecType, Instrs, BlockMetaData) [symbol(aLoop)]
// ------------------------------------------------------------------------------
rule <instrs> #loop(VECTYP, IS, BLOCKMETA) => sequenceInstrs(IS) ~> label VECTYP { #loop(VECTYP, IS, BLOCKMETA) } VALSTACK ... </instrs>
<valstack> VALSTACK => .ValStack </valstack>

It looks like the spec states that the stack values are prepended to the instructions when entering the block, but we aren't doing that here.

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

No branches or pull requests

1 participant