From a9193c4dee0b4ce4c9260e5a4e479c7893459324 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 13 Jun 2024 16:08:57 -0700 Subject: [PATCH 1/2] work --- src/ir/module-utils.cpp | 20 ++++++++--- test/lit/passes/unsubtyping-open.wast | 52 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/lit/passes/unsubtyping-open.wast diff --git a/src/ir/module-utils.cpp b/src/ir/module-utils.cpp index 5791ed77c66..250147d2ea3 100644 --- a/src/ir/module-utils.cpp +++ b/src/ir/module-utils.cpp @@ -574,9 +574,9 @@ InsertOrderedSet getPublicTypeSet(Module& wasm) { // TODO: Consider Tags as well, but they should store HeapTypes instead of // Signatures first. + std::unordered_set publicTables; ModuleUtils::iterImportedTables(wasm, [&](Table* table) { - assert(table->type.isRef()); - notePublic(table->type.getHeapType()); + publicTables.insert(table); }); ModuleUtils::iterImportedGlobals(wasm, [&](Global* global) { if (global->type.isRef()) { @@ -599,8 +599,7 @@ InsertOrderedSet getPublicTypeSet(Module& wasm) { } case ExternalKind::Table: { auto* table = wasm.getTable(ex->value); - assert(table->type.isRef()); - notePublic(table->type.getHeapType()); + publicTables.insert(table); continue; } case ExternalKind::Memory: @@ -622,6 +621,19 @@ InsertOrderedSet getPublicTypeSet(Module& wasm) { WASM_UNREACHABLE("unexpected export kind"); } + for (auto* table : publicTables) { + assert(table->type.isRef()); + notePublic(table->type.getHeapType()); + + // All functions added to the table are public as well. + // TODO: we should also scan for table.set + iterTableSegments(wasm, table->name, [&](ElementSegment* segment) { + for (auto* data : segment->data) { + notePublic(data->type.getHeapType()); + } + }); + } + // Ignorable public types are public. for (auto type : getIgnorablePublicTypes()) { notePublic(type); diff --git a/test/lit/passes/unsubtyping-open.wast b/test/lit/passes/unsubtyping-open.wast new file mode 100644 index 00000000000..1318758aa0c --- /dev/null +++ b/test/lit/passes/unsubtyping-open.wast @@ -0,0 +1,52 @@ +;; NOTE: Assertions have been generated by update_lit_checks.py --all-items and should not be edited. +;; RUN: foreach %s %t wasm-opt --unsubtyping -all -S -o - | filecheck %s + +;; Test unsubtyping *without* closed world. No public types should be modified. + +;; One of the function types, $some, must both remain in its own singleton rec +;; groups, as it is public because it is on an elem of an imported table. The +;; other function type $none, can move into the new rec group (it is private). +;; +;; There are also struct types which unsubtyping can optimize (so that it has +;; work to do, and does not exit early, which would pass this test incorrectly). +;; Specifically $sub1 and $sub2 should stop being subtypes of $super. +(module + ;; CHECK: (rec + ;; CHECK-NEXT: (type $super (sub (struct ))) + (type $super (sub (struct))) + + ;; CHECK: (type $none (func)) + (type $none (func)) + + ;; CHECK: (type $sub2 (sub (struct (field f32)))) + (type $sub2 (sub $super (struct f32))) + + ;; CHECK: (type $sub1 (sub (struct (field i32)))) + (type $sub1 (sub $super (struct i32))) + + ;; CHECK: (type $some (func (param f32 i32 f32))) + (type $some (func (param f32 i32 f32))) + + ;; CHECK: (import "a" "b" (table $table 1 funcref)) + (import "a" "b" (table $table 1 funcref)) + + ;; CHECK: (global $sub1 (ref $sub1) (struct.new_default $sub1)) + (global $sub1 (ref $sub1) (struct.new_default $sub1)) + ;; CHECK: (global $sub2 (ref $sub2) (struct.new_default $sub2)) + (global $sub2 (ref $sub2) (struct.new_default $sub2)) + + ;; CHECK: (elem $elem (i32.const 0) $some) + (elem $elem (i32.const 0) $some) + + ;; CHECK: (func $none (type $none) + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: ) + (func $none (type $none) + ) + + ;; CHECK: (func $some (type $some) (param $0 f32) (param $1 i32) (param $2 f32) + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: ) + (func $some (type $some) (param $0 f32) (param $1 i32) (param $2 f32) + ) +) From 3b4800478be4be358fed016a59295222b06f42dd Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 13 Jun 2024 16:23:50 -0700 Subject: [PATCH 2/2] format --- src/ir/module-utils.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ir/module-utils.cpp b/src/ir/module-utils.cpp index 250147d2ea3..0c82cd43c1c 100644 --- a/src/ir/module-utils.cpp +++ b/src/ir/module-utils.cpp @@ -575,9 +575,8 @@ InsertOrderedSet getPublicTypeSet(Module& wasm) { // TODO: Consider Tags as well, but they should store HeapTypes instead of // Signatures first. std::unordered_set publicTables; - ModuleUtils::iterImportedTables(wasm, [&](Table* table) { - publicTables.insert(table); - }); + ModuleUtils::iterImportedTables( + wasm, [&](Table* table) { publicTables.insert(table); }); ModuleUtils::iterImportedGlobals(wasm, [&](Global* global) { if (global->type.isRef()) { notePublic(global->type.getHeapType());