From 9d4cc20dda5fc242e6286ff14b41b2c5735bec3b Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 14 Aug 2025 12:55:09 -0700 Subject: [PATCH 1/3] fix --- src/wasm/wasm-binary.cpp | 4 +++- test/lit/basic/cont-no-gc.wast | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 test/lit/basic/cont-no-gc.wast diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 5d32f52d726..c5d14336123 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -1863,7 +1863,9 @@ void WasmBinaryWriter::writeHeapType(HeapType type, Exactness exactness) { if (!wasm->features.hasCustomDescriptors()) { exactness = Inexact; } - if (!wasm->features.hasGC()) { + // GC can refer to full heap types for many things; Stack Switching can do so + // for functions (continuation types refer to function types). + if (!wasm->features.hasGC() && !wasm->features.hasStackSwitching()) { type = type.getTop(); } assert(!type.isBasic() || exactness == Inexact); diff --git a/test/lit/basic/cont-no-gc.wast b/test/lit/basic/cont-no-gc.wast new file mode 100644 index 00000000000..c70d80c378b --- /dev/null +++ b/test/lit/basic/cont-no-gc.wast @@ -0,0 +1,20 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py and should not be edited. + +;; Test that we can write a binary without erroring when using stack switching +;; without GC. This verifies we can emit continuation types properly even when +;; GC is disabled + +;; RUN: wasm-opt %s --enable-stack-switching --roundtrip -S -o - | filecheck %s + +(module + (type $proc (func)) + (type $cont (cont $proc)) + + (func $main + (drop + (cont.new $cont + (ref.func $main) + ) + ) + ) +) From 997579ec6c2454d74c6b1c6ece655a2032a163c4 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 14 Aug 2025 12:59:02 -0700 Subject: [PATCH 2/3] fix --- test/lit/basic/cont-no-gc.wast | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/lit/basic/cont-no-gc.wast b/test/lit/basic/cont-no-gc.wast index c70d80c378b..f7fe3f27e1d 100644 --- a/test/lit/basic/cont-no-gc.wast +++ b/test/lit/basic/cont-no-gc.wast @@ -4,12 +4,21 @@ ;; without GC. This verifies we can emit continuation types properly even when ;; GC is disabled -;; RUN: wasm-opt %s --enable-stack-switching --roundtrip -S -o - | filecheck %s +;; RUN: wasm-opt %s --enable-stack-switching --enable-reference-types --roundtrip -S -o - | filecheck %s (module + ;; CHECK: (type $proc (func)) (type $proc (func)) + ;; CHECK: (type $cont (cont $proc)) (type $cont (cont $proc)) + ;; CHECK: (func $main + ;; CHECK-NEXT: (drop + ;; CHECK-NEXT: (cont.new $cont + ;; CHECK-NEXT: (ref.func $main) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) + ;; CHECK-NEXT: ) (func $main (drop (cont.new $cont From 1759b0330e398c03e7dd04e405201c2f6e5d1b71 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 14 Aug 2025 13:21:20 -0700 Subject: [PATCH 3/3] bettr --- src/wasm/wasm-binary.cpp | 6 ++---- src/wasm/wasm-stack.cpp | 4 +++- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index c5d14336123..0b03c5824e9 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -310,7 +310,7 @@ void WasmBinaryWriter::writeTypes() { break; case HeapTypeKind::Cont: o << uint8_t(BinaryConsts::EncodedType::Cont); - writeHeapType(type.getContinuation().type, Inexact); + writeIndexedHeapType(type.getContinuation().type); break; case HeapTypeKind::Basic: WASM_UNREACHABLE("unexpected kind"); @@ -1863,9 +1863,7 @@ void WasmBinaryWriter::writeHeapType(HeapType type, Exactness exactness) { if (!wasm->features.hasCustomDescriptors()) { exactness = Inexact; } - // GC can refer to full heap types for many things; Stack Switching can do so - // for functions (continuation types refer to function types). - if (!wasm->features.hasGC() && !wasm->features.hasStackSwitching()) { + if (!wasm->features.hasGC()) { type = type.getTop(); } assert(!type.isBasic() || exactness == Inexact); diff --git a/src/wasm/wasm-stack.cpp b/src/wasm/wasm-stack.cpp index 529bb1db19c..bfc0d8191ae 100644 --- a/src/wasm/wasm-stack.cpp +++ b/src/wasm/wasm-stack.cpp @@ -135,7 +135,9 @@ void BinaryInstWriter::visitCallIndirect(CallIndirect* curr) { Index tableIdx = parent.getTableIndex(curr->table); int8_t op = curr->isReturn ? BinaryConsts::RetCallIndirect : BinaryConsts::CallIndirect; - o << op << U32LEB(parent.getTypeIndex(curr->heapType)) << U32LEB(tableIdx); + o << op; + parent.writeIndexedHeapType(curr->heapType); + o << U32LEB(tableIdx); } void BinaryInstWriter::visitLocalGet(LocalGet* curr) {