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

Bug in parsing instructions after resume #52

Open
ahuoguo opened this issue Nov 4, 2024 · 2 comments
Open

Bug in parsing instructions after resume #52

ahuoguo opened this issue Nov 4, 2024 · 2 comments
Assignees
Labels
bug Something isn't working

Comments

@ahuoguo
Copy link

ahuoguo commented Nov 4, 2024

Hi,

The following gen.wast taken from stack-switching explainer can't be parsed in the reference interpreter when unfolded

(module
  (type (;0;) (func))
  (type (;1;) (cont 0))
  (type (;2;) (func (param i32)))
  (type (;3;) (func (result i32 (ref 1))))
  (import "spectest" "print_i32" (func (;0;) (type 2)))
  (tag (;0;) (type 2) (param i32))
  (start 2)
  (elem (;0;) declare func 1)
  (func (;1;) (type 0)
    (local i32)
    i32.const 100
    local.set 0
    loop ;; label = @1
      local.get 0
      suspend 0
      local.get 0
      i32.const 1
      i32.sub
      local.tee 0
      br_if 0 (;@1;)
    end
  )
  (func (;2;) (type 0)
    (local (ref 1))
    ref.func 1
    cont.new 1
    local.set 0
    loop ;; label = @1
      block (type 3) (result i32 (ref 1)) ;; label = @2
        local.get 0
        resume 1 (on 0 0 (;@2;))
        i32.const 42
        call 0
        return
      end
      local.set 0
      call 0
      br 0 (;@1;)
    end
  )
)

It gives me the following syntax error:

gen-stripped.wast:34.9-34.13: syntax error: unexpected token

Here's the working folded version

(module $generator
  (type $ft (func))
  ;; Types of continuations used by the generator:
  ;; No need for param or result types: No data passed back to the
  ;; generator when resuming it, and $generator function has no return
  ;; values.
  (type $ct (cont $ft))
  (func $print (import "spectest" "print_i32") (param i32))
  
  ;; Tag used to coordinate between generator and consumer: The i32 param
  ;; corresponds to the generated values passed; no values passed back from
  ;; generator to consumer.
  (tag $gen (param i32))

  ;; Simple generator yielding values from 100 down to 1
  (func $generator
    (local $i i32)
    (local.set $i (i32.const 100))
    (loop $l
      ;; Suspend execution, pass current value of $i to consumer
      (suspend $gen (local.get $i))
      ;; Decrement $i and exit loop once $i reaches 0
      (local.tee $i (i32.sub (local.get $i) (i32.const 1)))
      (br_if $l)
    )
  )
  (elem declare func $generator)

  (func $consumer
    (local $c (ref $ct))
    ;; Create continuation executing function $generator.
    ;; Execution only starts when resumed for the first time.
    (local.set $c (cont.new $ct (ref.func $generator)))
    ;; (call $print (i32.const 42))
    (loop $loop
      (block $on_gen (result i32 (ref $ct))
        ;; Resume continuation $c
        (resume $ct (on $gen $on_gen) (local.get $c))
        (call $print (i32.const 42))
        ;; $generator returned: no more data
        (return)
      )
      ;; Generator suspended, stack now contains [i32 (ref $ct)]
      ;; Save continuation to resume it in the next iteration
      (local.set $c)
      ;; Stack now contains the i32 value produced by $generator
      (call $print)

      (br $loop)
    )
  )
  (start $consumer)
)
@dhil
Copy link
Member

dhil commented Nov 4, 2024

Thanks for the report. I think this is an instance of the same issue as wasmfx/wasmfx-tools#96 -- I'll eventually get around to fixing it. Just to check, is it blocking progress for whatever you are working on (I suppose the workaround is to rewrite everything to folded form)?

@dhil dhil self-assigned this Nov 4, 2024
@dhil dhil added the bug Something isn't working label Nov 4, 2024
@ahuoguo
Copy link
Author

ahuoguo commented Nov 4, 2024

Thanks for the report. I think this is an instance of the same issue as wasmfx/wasmfx-tools#96 -- I'll eventually get around to fixing it. Just to check, is it blocking progress for whatever you are working on (I suppose the workaround is to rewrite everything to folded form)?

Thanks for the pointer. I believe it's a parsing issue in the reference interpreter. It's not blocking progress on my side. A workaround is to wrap resume 1 (on 0 0 (;@2;)) with another pair of brackets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants