Skip to content

Commit

Permalink
Resume with only one tag (#70)
Browse files Browse the repository at this point in the history
- Extend `ContV` with `k: (Stack, Cont[A], MCont[A], Handler[A]) => A`
  • Loading branch information
ahuoguo authored Nov 27, 2024
1 parent 4865eab commit 5b4f1cf
Show file tree
Hide file tree
Showing 18 changed files with 617 additions and 66 deletions.
12 changes: 12 additions & 0 deletions benchmarks/wasm/wasmfx/cont_bind4.bin.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(module definition binary
"\00\61\73\6d\01\00\00\00\01\8e\80\80\80\00\04\60"
"\00\01\7f\5d\00\60\01\7f\01\7f\5d\02\03\83\80\80"
"\80\00\02\02\02\07\88\80\80\80\00\01\04\6d\61\69"
"\6e\00\01\09\85\80\80\80\00\01\03\00\01\00\0a\a0"
"\80\80\80\00\02\87\80\80\80\00\00\20\00\41\2c\6a"
"\0b\8e\80\80\80\00\00\20\00\d2\00\e0\03\e1\03\01"
"\e3\01\00\0b"
)
(module instance)
(assert_return (invoke "main" (i32.const 0x16)) (i32.const 0x42))
(assert_return (invoke "main" (i32.const 0xffff_fe44)) (i32.const 0xffff_fe70))
21 changes: 21 additions & 0 deletions benchmarks/wasm/wasmfx/cont_bind4.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(module
(type $f1 (func (result i32)))
(type $c1 (cont $f1))

(type $f2 (func (param i32) (result i32)))
(type $c2 (cont $f2))

(func $add44 (param i32) (result i32) (i32.add (local.get 0) (i32.const 44)))
(elem declare func $add44)

(func (export "main") (param i32) (result i32)
(resume $c1
(cont.bind $c2 $c1
(local.get 0)
(cont.new $c2 (ref.func $add44))))
)
)

(assert_return (invoke "main" (i32.const 22)) (i32.const 66))
(assert_return (invoke "main" (i32.const -444)) (i32.const -400))

31 changes: 31 additions & 0 deletions benchmarks/wasm/wasmfx/cont_bind5-strip.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(module
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (cont 0))
(type (;2;) (func (param i32 i32) (result i32)))
(type (;3;) (cont 2))
(type (;4;) (func (param i32)))
(type (;5;) (func))
(import "spectest" "print_i32" (func (;0;) (type 4)))
(export "main" (func 3))
(start 3)
(elem (;0;) declare func 1)
(func (;1;) (type 2) (param i32 i32) (result i32)
local.get 0
local.get 1
i32.sub
)
(func (;2;) (type 2) (param i32 i32) (result i32)
local.get 1
local.get 0
ref.func 1
cont.new 3
cont.bind 3 1
resume 1
)
(func (;3;) (type 5)
i32.const 22
i32.const 44
call 2
call 0
)
)
18 changes: 18 additions & 0 deletions benchmarks/wasm/wasmfx/cont_bind5.bin.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(module definition binary
"\00\61\73\6d\01\00\00\00\01\90\80\80\80\00\04\60"
"\01\7f\01\7f\5d\00\60\02\7f\7f\01\7f\5d\02\03\83"
"\80\80\80\00\02\02\02\07\88\80\80\80\00\01\04\6d"
"\61\69\6e\00\01\09\85\80\80\80\00\01\03\00\01\00"
"\0a\a2\80\80\80\00\02\87\80\80\80\00\00\20\00\20"
"\01\6b\0b\90\80\80\80\00\00\20\01\20\00\d2\00\e0"
"\03\e1\03\01\e3\01\00\0b"
)
(module instance)
(assert_return
(invoke "main" (i32.const 0x16) (i32.const 0x2c))
(i32.const 0xffff_ffea)
)
(assert_return
(invoke "main" (i32.const 0xffff_fe44) (i32.const 0x6f))
(i32.const 0xffff_fdd5)
)
25 changes: 25 additions & 0 deletions benchmarks/wasm/wasmfx/cont_bind5.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
(module
;; (type $f1 (func (result i32)))
;; (type $c1 (cont $f1))

(type $f2 (func (param i32) (result i32)))
(type $c2 (cont $f2))

(type $f3 (func (param i32 i32) (result i32)))
(type $c3 (cont $f3))

(func $sub (param i32 i32) (result i32) (i32.sub (local.get 0) (local.get 1)))
(elem declare func $sub)

(func (export "main") (param i32 i32) (result i32)
(resume $c2
(local.get 1)
(cont.bind $c3 $c2
(local.get 0)
(cont.new $c3 (ref.func $sub))))
)
)

;; (assert_return (invoke "main" (i32.const 22) (i32.const 44)) (i32.const -22))
;; (assert_return (invoke "main" (i32.const -444) (i32.const 111)) (i32.const -555))

53 changes: 53 additions & 0 deletions benchmarks/wasm/wasmfx/diff_resume-strip.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(module
(type (;0;) (func (param i32)))
(type (;1;) (cont 0))
(type (;2;) (func (result i32)))
(type (;3;) (func))
(import "spectest" "print_i32" (func (;0;) (type 0)))
(tag (;0;) (type 2) (result i32))
(export "_start" (func 2))
(start 2)
(elem (;0;) declare func 1)
(func (;1;) (type 0) (param i32)
local.get 0
call 0
suspend 0
call 0
)
(func (;2;) (type 3)
(local i32 (ref 1))
ref.func 1
cont.new 1
local.set 1
i32.const 10
local.set 0
block ;; label = @1
block (result (ref 1)) ;; label = @2
local.get 0
local.get 1
resume 1 (on 0 0 (;@2;))
i32.const -2
call 0
br 1 (;@1;)
end
local.set 1
local.get 0
i32.const 1
i32.add
local.set 0
block ;; label = @2
block (result (ref 1)) ;; label = @3
local.get 0
local.get 1
resume 1 (on 0 0 (;@3;))
i32.const 42
call 0
br 1 (;@2;)
end
i32.const 111
call 0
drop
end
end
)
)
52 changes: 52 additions & 0 deletions benchmarks/wasm/wasmfx/diff_resume.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
(module
(import "spectest" "print_i32" (func $print_i32 (param i32)))
(type $task (func (param i32)))
(type $cont (cont $task))

(tag $yield (result i32))

(func $task1 (param $x i32)
(call $print_i32 (local.get $x))
(suspend $yield) ;; DH: levaes a continuation on the stack, jump to the tag $yield
;; when come back, the stack should contains a i32 value, since the return type of $yield is i32
(call $print_i32)
)

(func $main (export "_start")
(local $i i32)
(local $k (ref $cont))
(local.set $k (cont.new $cont (ref.func $task1)))
(local.set $i (i32.const 10))
(block $h
(block $on_yield (result (ref $cont))
(resume $cont
(on $yield $on_yield)
(local.get $i)
(local.get $k)
)
(call $print_i32 (i32.const -2))
(br $h))
;; $on_yield lands here, with the continuation on the stack
(local.set $k)
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(block $h
(block $on_yield2 (result (ref $cont))
(resume $cont
(on $yield $on_yield2)
(local.get $i)
(local.get $k)
)
(call $print_i32 (i32.const 42))
(br $h)
)
;; $on_yield2 lands here, with the continuation on the stack
(call $print_i32 (i32.const 111))
drop
)
)
)

(elem declare func $task1)

(start $main)
)
42 changes: 42 additions & 0 deletions benchmarks/wasm/wasmfx/gen-stripped.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(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 (result i32 (ref 1)) ;; label = @2
local.get 0
resume 1 (on 0 0 (;@2;)) ;; wasmfx ref interpreter has a bug on this, you can add a bracket around `resume ..` to get around
i32.const 42
call 0
br 2 (;@2;)
end
local.set 0
call 0
br 0 (;@1;)
end
)
)
39 changes: 39 additions & 0 deletions benchmarks/wasm/wasmfx/nested_suspend-strip.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
(module
(type (;0;) (func))
(type (;1;) (cont 0))
(type (;2;) (func (param i32)))
(import "spectest" "print_i32" (func (;0;) (type 2)))
(tag (;0;) (type 0))
(export "_start" (func 3))
(start 3)
;; (elem (;0;) declare func 1 2)
(func (;1;) (type 0)
suspend 0
)
(func (;2;) (type 0)
block ;; label = @1
block (result (ref 1)) ;; label = @2
ref.func 1
cont.new 1
resume 1 (on 0 0 (;@2;))
br 1 (;@1;)
end
drop
i32.const 0
call 0
end
)
(func (;3;) (type 0)
block ;; label = @1
block (result (ref 1)) ;; label = @2
ref.func 2
cont.new 1
resume 1 (on 0 0 (;@2;))
br 1 (;@1;)
end
drop
i32.const 1
call 0
end
)
)
9 changes: 9 additions & 0 deletions benchmarks/wasm/wasmfx/resume2.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(module
(type $f1 (func (result i32)))
(type $c1 (cont $f1))
(func $f42 (result i32) (i32.const 42))
(elem declare func $f42)
(func (export "main") (result i32)
(resume $c1 (cont.new $c1 (ref.func $f42)))
)
)
31 changes: 31 additions & 0 deletions benchmarks/wasm/wasmfx/resume_chain1-strip.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(module
(type (;0;) (func (param i32) (result i32)))
(type (;1;) (cont 0))
(export "main" (func 2))
;; (elem (;0;) declare func 0 1)
(func (;0;) (type 0) (param i32) (result i32)
local.get 0
i32.const 10
i32.add
)
(func (;1;) (type 0) (param i32) (result i32)
local.get 0
i32.const 100
i32.add
ref.func 0
cont.new 1
resume 1
i32.const 1000
i32.add
)
(func (;2;) (type 0) (param i32) (result i32)
local.get 0
ref.func 1
cont.new 1
resume 1
)
)

(assert_return (invoke "main" (i32.const 4)) (i32.const 1114))
(assert_return (invoke "main" (i32.const 5)) (i32.const 1115))
(assert_return (invoke "main" (i32.const 9)) (i32.const 1119))
Loading

0 comments on commit 5b4f1cf

Please sign in to comment.