From 588393c907cadaf2f8c965ad7f2b2dbcc4129779 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 29 Jun 2026 12:58:26 -0700 Subject: [PATCH 1/2] fix --- src/ir/table-utils.h | 13 ++++-- test/lit/passes/directize_all-features.wast | 47 +++++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/ir/table-utils.h b/src/ir/table-utils.h index 884f2309797..70482688130 100644 --- a/src/ir/table-utils.h +++ b/src/ir/table-utils.h @@ -17,7 +17,6 @@ #ifndef wasm_ir_table_h #define wasm_ir_table_h -#include "ir/element-utils.h" #include "ir/literal-utils.h" #include "ir/module-utils.h" #include "support/stdckdint.h" @@ -51,8 +50,16 @@ struct FlatTable { if (end > names.size()) { names.resize(end); } - ElementUtils::iterElementSegmentFunctionNames( - segment, [&](Name entry, Index i) { names[start + i] = entry; }); + for (Index i = 0; i < segment->data.size(); i++) { + auto* item = segment->data[i]; + if (auto* refFunc = item->dynCast()) { + names[start + i] = refFunc->func; + } else if (item->is()) { + names[start + i] = Name(); + } else { + WASM_UNREACHABLE("invalid elem segment func content"); + } + } }); } }; diff --git a/test/lit/passes/directize_all-features.wast b/test/lit/passes/directize_all-features.wast index 8cbe6092dbc..8973492c671 100644 --- a/test/lit/passes/directize_all-features.wast +++ b/test/lit/passes/directize_all-features.wast @@ -2148,3 +2148,50 @@ (func $target ) ) + +;; Set a function using one elem, then trample it with a null. We should use the +;; null for the call_indirect. +(module + ;; CHECK: (type $func (func)) + ;; IMMUT: (type $func (func)) + (type $func (func)) + + ;; CHECK: (table $table 6 funcref) + ;; IMMUT: (table $table 6 funcref) + (table $table 6 funcref) + + ;; CHECK: (elem $5 (i32.const 0) $nop) + ;; IMMUT: (elem $5 (i32.const 0) $nop) + (elem $5 (i32.const 0) $nop) + + ;; CHECK: (elem $6 (table $table) (i32.const 0) funcref (item (ref.null nofunc))) + ;; IMMUT: (elem $6 (table $table) (i32.const 0) funcref (item (ref.null nofunc))) + (elem $6 (i32.const 0) funcref (item (ref.null nofunc))) + + ;; CHECK: (export "call" (func $call)) + + ;; CHECK: (func $nop (type $func) + ;; CHECK-NEXT: (nop) + ;; CHECK-NEXT: ) + ;; IMMUT: (export "call" (func $call)) + + ;; IMMUT: (func $nop (type $func) + ;; IMMUT-NEXT: (nop) + ;; IMMUT-NEXT: ) + (func $nop (type $func) + (nop) + ) + + ;; CHECK: (func $call (type $func) + ;; CHECK-NEXT: (unreachable) + ;; CHECK-NEXT: ) + ;; IMMUT: (func $call (type $func) + ;; IMMUT-NEXT: (unreachable) + ;; IMMUT-NEXT: ) + (func $call (export "call") + ;; This can be optimized to an unreachable. + (call_indirect $table (type $func) + (i32.const 0) + ) + ) +) From 48f3e36b1b436d2a4e45ebaddc94f4a719d61aba Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 29 Jun 2026 13:30:34 -0700 Subject: [PATCH 2/2] fix --- src/ir/table-utils.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/ir/table-utils.h b/src/ir/table-utils.h index 70482688130..c1035c008d5 100644 --- a/src/ir/table-utils.h +++ b/src/ir/table-utils.h @@ -57,7 +57,10 @@ struct FlatTable { } else if (item->is()) { names[start + i] = Name(); } else { - WASM_UNREACHABLE("invalid elem segment func content"); + // Anything else, we can't represent. + // TODO: mark only this index as unoptimizable? + valid = false; + return; } } });