From e84980dffb62d672c991960a701a3c7de8f8aa74 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Thu, 2 Sep 2021 09:17:37 -0700 Subject: Support specialized function types in element segments (#4109) Before this, the element segments would be printed as having type funcref, and then if their table had a specialized type, the element type would not be a subtype of the table and validation would fail. --- src/ir/table-utils.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/ir/table-utils.cpp') diff --git a/src/ir/table-utils.cpp b/src/ir/table-utils.cpp index 80ef885f9..79b90d249 100644 --- a/src/ir/table-utils.cpp +++ b/src/ir/table-utils.cpp @@ -64,6 +64,25 @@ std::set getFunctionsNeedingElemDeclare(Module& wasm) { return ret; } +bool usesExpressions(ElementSegment* curr, Module* module) { + // Binaryen IR always has ref.funcs for functions in tables for uniformity, + // so that by itself does not indicate if expressions should be used when + // emitting the table or not. But definitely anything that is not a ref.func + // implies we are post-MVP and must use expressions. + bool allElementsRefFunc = + std::all_of(curr->data.begin(), curr->data.end(), [](Expression* entry) { + return entry->is(); + }); + + // If the table has a specialized (non-MVP) type, then the segment must + // declare a type that is a subtype of that, so it must use the post-MVP form + // of using expressions. + bool hasTableOfSpecializedType = + curr->table.is() && module->getTable(curr->table)->type != Type::funcref; + + return !allElementsRefFunc || hasTableOfSpecializedType; +} + } // namespace TableUtils } // namespace wasm -- cgit v1.2.3