From 7bdfbf6f7764ff896f5c8efa2729052316a6eab0 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Fri, 7 Nov 2025 18:11:06 -0800 Subject: [PATCH 1/2] [NFC] Remove HeapType from RefFunc::finalize Since finalization already looks up the function on the module to determine whether it is imported, go further and just take the type directly from the function. --- src/binaryen-c.cpp | 11 +++++------ src/ir/ReFinalize.cpp | 4 +--- src/ir/possible-contents.h | 3 +-- src/ir/table-utils.h | 3 +-- src/passes/FuncCastEmulation.cpp | 2 +- src/passes/JSPI.cpp | 4 +--- src/passes/LegalizeJSInterface.cpp | 2 +- src/passes/MergeSimilarFunctions.cpp | 4 +--- src/tools/fuzzing/fuzzing.cpp | 12 ++++-------- src/tools/wasm-merge.cpp | 5 +---- src/wasm-builder.h | 6 +++--- src/wasm.h | 2 +- src/wasm/wasm-binary.cpp | 3 +-- src/wasm/wasm-ir-builder.cpp | 2 +- src/wasm/wasm.cpp | 6 +----- 15 files changed, 24 insertions(+), 45 deletions(-) diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 1d4077145ec..efd388d3a21 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -1614,10 +1614,11 @@ BinaryenExpressionRef BinaryenRefFunc(BinaryenModuleRef module, // is non-imported if not. TODO: If we want to allow creating imports later, // we would need an API addition or change. auto* wasm = (Module*)module; - if (wasm->getFunctionOrNull(func)) { + if ([[maybe_unused]] auto* f = wasm->getFunctionOrNull(func)) { + assert(f->type.getHeapType() == HeapType(type)); // Use the HeapType constructor, which will do a lookup on the module. return static_cast( - Builder(*(Module*)module).makeRefFunc(func, HeapType(type))); + Builder(*(Module*)module).makeRefFunc(func)); } else { // Assume non-imported, and provide the full type for that. Type full = Type(HeapType(type), NonNullable, Exact); @@ -5299,8 +5300,7 @@ BinaryenAddActiveElementSegment(BinaryenModuleRef module, Fatal() << "invalid function '" << funcNames[i] << "'."; } segment->data.push_back( - Builder(*(Module*)module) - .makeRefFunc(funcNames[i], func->type.getHeapType())); + Builder(*(Module*)module).makeRefFunc(funcNames[i])); } return ((Module*)module)->addElementSegment(std::move(segment)); } @@ -5317,8 +5317,7 @@ BinaryenAddPassiveElementSegment(BinaryenModuleRef module, Fatal() << "invalid function '" << funcNames[i] << "'."; } segment->data.push_back( - Builder(*(Module*)module) - .makeRefFunc(funcNames[i], func->type.getHeapType())); + Builder(*(Module*)module).makeRefFunc(funcNames[i])); } return ((Module*)module)->addElementSegment(std::move(segment)); } diff --git a/src/ir/ReFinalize.cpp b/src/ir/ReFinalize.cpp index ced96c1c357..f7544e3cf51 100644 --- a/src/ir/ReFinalize.cpp +++ b/src/ir/ReFinalize.cpp @@ -115,9 +115,7 @@ void ReFinalize::visitMemorySize(MemorySize* curr) { curr->finalize(); } void ReFinalize::visitMemoryGrow(MemoryGrow* curr) { curr->finalize(); } void ReFinalize::visitRefNull(RefNull* curr) { curr->finalize(); } void ReFinalize::visitRefIsNull(RefIsNull* curr) { curr->finalize(); } -void ReFinalize::visitRefFunc(RefFunc* curr) { - curr->finalize(curr->type.getHeapType(), *getModule()); -} +void ReFinalize::visitRefFunc(RefFunc* curr) { curr->finalize(*getModule()); } void ReFinalize::visitRefEq(RefEq* curr) { curr->finalize(); } void ReFinalize::visitTableGet(TableGet* curr) { curr->finalize(); } void ReFinalize::visitTableSet(TableSet* curr) { curr->finalize(); } diff --git a/src/ir/possible-contents.h b/src/ir/possible-contents.h index 6e0f110adb8..a5fdb51edc0 100644 --- a/src/ir/possible-contents.h +++ b/src/ir/possible-contents.h @@ -327,8 +327,7 @@ class PossibleContents { wasm.getGlobal(info.name)->type); } else { assert(info.kind == ExternalKind::Function); - return builder.makeRefFunc( - info.name, wasm.getFunction(info.name)->type.getHeapType()); + return builder.makeRefFunc(info.name); } } } diff --git a/src/ir/table-utils.h b/src/ir/table-utils.h index 6b9f6b5a8d0..6af41d28517 100644 --- a/src/ir/table-utils.h +++ b/src/ir/table-utils.h @@ -92,8 +92,7 @@ inline Index append(Table& table, Name name, Module& wasm) { auto* func = wasm.getFunctionOrNull(name); assert(func != nullptr && "Cannot append non-existing function to a table."); - segment->data.push_back( - Builder(wasm).makeRefFunc(name, func->type.getHeapType())); + segment->data.push_back(Builder(wasm).makeRefFunc(name)); table.initial++; return tableIndex; } diff --git a/src/passes/FuncCastEmulation.cpp b/src/passes/FuncCastEmulation.cpp index d4f2de4009a..4f85c1ddc13 100644 --- a/src/passes/FuncCastEmulation.cpp +++ b/src/passes/FuncCastEmulation.cpp @@ -178,7 +178,7 @@ struct FuncCastEmulation : public Pass { } auto* thunk = iter->second; ref->func = thunk->name; - ref->finalize(thunk->type.getHeapType(), *module); + ref->finalize(*module); } } diff --git a/src/passes/JSPI.cpp b/src/passes/JSPI.cpp index 65cf944b47c..7d42aad118c 100644 --- a/src/passes/JSPI.cpp +++ b/src/passes/JSPI.cpp @@ -151,9 +151,7 @@ struct JSPI : public Pass { if (iter == wrappedExports.end()) { continue; } - auto* replacementRef = builder.makeRefFunc( - iter->second, - module->getFunction(iter->second)->type.getHeapType()); + auto* replacementRef = builder.makeRefFunc(iter->second); segment->data[i] = replacementRef; } } diff --git a/src/passes/LegalizeJSInterface.cpp b/src/passes/LegalizeJSInterface.cpp index 5cc34680fb5..e3b58f6f8b9 100644 --- a/src/passes/LegalizeJSInterface.cpp +++ b/src/passes/LegalizeJSInterface.cpp @@ -148,7 +148,7 @@ struct LegalizeJSInterface : public Pass { } curr->func = iter->second->name; - curr->finalize(iter->second->type.getHeapType(), *getModule()); + curr->finalize(*getModule()); } }; diff --git a/src/passes/MergeSimilarFunctions.cpp b/src/passes/MergeSimilarFunctions.cpp index 1e908f49abb..6b6aeca09d1 100644 --- a/src/passes/MergeSimilarFunctions.cpp +++ b/src/passes/MergeSimilarFunctions.cpp @@ -131,9 +131,7 @@ struct ParamInfo { if (const auto literals = std::get_if(&values)) { return builder.makeConst((*literals)[index]); } else if (auto callees = std::get_if>(&values)) { - auto fnName = (*callees)[index]; - auto heapType = module->getFunction(fnName)->type.getHeapType(); - return builder.makeRefFunc(fnName, heapType); + return builder.makeRefFunc((*callees)[index]); } else { WASM_UNREACHABLE("unexpected const value type"); } diff --git a/src/tools/fuzzing/fuzzing.cpp b/src/tools/fuzzing/fuzzing.cpp index 02dba6d7962..21d4f62c1ea 100644 --- a/src/tools/fuzzing/fuzzing.cpp +++ b/src/tools/fuzzing/fuzzing.cpp @@ -1694,8 +1694,7 @@ Function* TranslateToFuzzReader::addFunction() { } }); auto& randomElem = compatibleSegments[upTo(compatibleSegments.size())]; - randomElem->data.push_back( - builder.makeRefFunc(func->name, func->type.getHeapType())); + randomElem->data.push_back(builder.makeRefFunc(func->name)); } numAddedFunctions++; return func; @@ -2988,10 +2987,7 @@ Expression* TranslateToFuzzReader::makeCallRef(Type type) { } // TODO: half the time make a completely random item with that type. return builder.makeCallRef( - builder.makeRefFunc(target->name, target->type.getHeapType()), - args, - type, - isReturn); + builder.makeRefFunc(target->name), args, type, isReturn); } Expression* TranslateToFuzzReader::makeLocalGet(Type type) { @@ -3627,7 +3623,7 @@ Expression* TranslateToFuzzReader::makeRefFuncConst(Type type) { do { auto& func = wasm.functions[i]; if (Type::isSubType(func->type, type)) { - return builder.makeRefFunc(func->name, func->type.getHeapType()); + return builder.makeRefFunc(func->name); } i = (i + 1) % wasm.functions.size(); } while (i != start); @@ -3666,7 +3662,7 @@ Expression* TranslateToFuzzReader::makeRefFuncConst(Type type) { Type(heapType, NonNullable, Exact), {}, body)); - return builder.makeRefFunc(func->name, heapType); + return builder.makeRefFunc(func->name); } Expression* TranslateToFuzzReader::makeConst(Type type) { diff --git a/src/tools/wasm-merge.cpp b/src/tools/wasm-merge.cpp index 51af318e7a0..d9a991a58cc 100644 --- a/src/tools/wasm-merge.cpp +++ b/src/tools/wasm-merge.cpp @@ -584,10 +584,7 @@ void updateTypes(Module& wasm) { } } - void visitRefFunc(RefFunc* curr) { - curr->finalize(getModule()->getFunction(curr->func)->type.getHeapType(), - *getModule()); - } + void visitRefFunc(RefFunc* curr) { curr->finalize(*getModule()); } void visitFunction(Function* curr) { ReFinalize().walkFunctionInModule(curr, getModule()); diff --git a/src/wasm-builder.h b/src/wasm-builder.h index b1c10146dac..fef417fc1a5 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -709,10 +709,10 @@ class Builder { ret->type = type; return ret; } - RefFunc* makeRefFunc(Name func, HeapType heapType) { + RefFunc* makeRefFunc(Name func) { auto* ret = wasm.allocator.alloc(); ret->func = func; - ret->finalize(heapType, wasm); + ret->finalize(wasm); return ret; } RefEq* makeRefEq(Expression* left, Expression* right) { @@ -1368,7 +1368,7 @@ class Builder { return makeRefNull(type.getHeapType()); } if (type.isFunction()) { - return makeRefFunc(value.getFunc(), type.getHeapType()); + return makeRefFunc(value.getFunc()); } if (type.isRef() && type.getHeapType().isMaybeShared(HeapType::i31)) { return makeRefI31(makeConst(value.geti31()), diff --git a/src/wasm.h b/src/wasm.h index 3f3ac1d7434..757a0001501 100644 --- a/src/wasm.h +++ b/src/wasm.h @@ -1376,7 +1376,7 @@ class RefFunc : public SpecificExpression { Name func; void finalize(); - void finalize(HeapType heapType, Module& wasm); + void finalize(Module& wasm); }; class RefEq : public SpecificExpression { diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 2e85bfb5e0c..57d034b3c62 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -4962,8 +4962,7 @@ void WasmBinaryReader::readElementSegments() { } else { for (Index j = 0; j < size; j++) { Index index = getU32LEB(); - auto sig = getTypeByFunctionIndex(index); - auto* refFunc = Builder(wasm).makeRefFunc(getFunctionName(index), sig); + auto* refFunc = Builder(wasm).makeRefFunc(getFunctionName(index)); segmentData.push_back(refFunc); } } diff --git a/src/wasm/wasm-ir-builder.cpp b/src/wasm/wasm-ir-builder.cpp index 512551b1eb7..e63bb9d488d 100644 --- a/src/wasm/wasm-ir-builder.cpp +++ b/src/wasm/wasm-ir-builder.cpp @@ -1736,7 +1736,7 @@ Result<> IRBuilder::makeRefIsNull() { } Result<> IRBuilder::makeRefFunc(Name func) { - push(builder.makeRefFunc(func, wasm.getFunction(func)->type.getHeapType())); + push(builder.makeRefFunc(func)); return Ok{}; } diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index 17a3fe1156c..44c1daea622 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -827,11 +827,7 @@ void RefFunc::finalize() { assert(type.isSignature()); } -void RefFunc::finalize(HeapType heapType, Module& wasm) { - type = Type(heapType, - NonNullable, - wasm.getFunction(func)->imported() ? Inexact : Exact); -} +void RefFunc::finalize(Module& wasm) { type = wasm.getFunction(func)->type; } void RefEq::finalize() { if (left->type == Type::unreachable || right->type == Type::unreachable) { From 7f9e82f4e50898e5e0aa0eb7f1f53c581bf55a14 Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Mon, 10 Nov 2025 16:06:29 -0800 Subject: [PATCH 2/2] remove unused variable --- src/ir/table-utils.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ir/table-utils.h b/src/ir/table-utils.h index 6af41d28517..130a9f84947 100644 --- a/src/ir/table-utils.h +++ b/src/ir/table-utils.h @@ -90,8 +90,8 @@ inline Index append(Table& table, Name name, Module& wasm) { wasm.dylinkSection->tableSize++; } - auto* func = wasm.getFunctionOrNull(name); - assert(func != nullptr && "Cannot append non-existing function to a table."); + assert(wasm.getFunctionOrNull(name) != nullptr && + "Cannot append non-existing function to a table."); segment->data.push_back(Builder(wasm).makeRefFunc(name)); table.initial++; return tableIndex;