Skip to content

Commit f6fd861

Browse files
authored
[NFC] Remove HeapType from RefFunc::finalize (#8032)
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.
1 parent f202450 commit f6fd861

15 files changed

+26
-47
lines changed

src/binaryen-c.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,10 +1614,11 @@ BinaryenExpressionRef BinaryenRefFunc(BinaryenModuleRef module,
16141614
// is non-imported if not. TODO: If we want to allow creating imports later,
16151615
// we would need an API addition or change.
16161616
auto* wasm = (Module*)module;
1617-
if (wasm->getFunctionOrNull(func)) {
1617+
if ([[maybe_unused]] auto* f = wasm->getFunctionOrNull(func)) {
1618+
assert(f->type.getHeapType() == HeapType(type));
16181619
// Use the HeapType constructor, which will do a lookup on the module.
16191620
return static_cast<Expression*>(
1620-
Builder(*(Module*)module).makeRefFunc(func, HeapType(type)));
1621+
Builder(*(Module*)module).makeRefFunc(func));
16211622
} else {
16221623
// Assume non-imported, and provide the full type for that.
16231624
Type full = Type(HeapType(type), NonNullable, Exact);
@@ -5299,8 +5300,7 @@ BinaryenAddActiveElementSegment(BinaryenModuleRef module,
52995300
Fatal() << "invalid function '" << funcNames[i] << "'.";
53005301
}
53015302
segment->data.push_back(
5302-
Builder(*(Module*)module)
5303-
.makeRefFunc(funcNames[i], func->type.getHeapType()));
5303+
Builder(*(Module*)module).makeRefFunc(funcNames[i]));
53045304
}
53055305
return ((Module*)module)->addElementSegment(std::move(segment));
53065306
}
@@ -5317,8 +5317,7 @@ BinaryenAddPassiveElementSegment(BinaryenModuleRef module,
53175317
Fatal() << "invalid function '" << funcNames[i] << "'.";
53185318
}
53195319
segment->data.push_back(
5320-
Builder(*(Module*)module)
5321-
.makeRefFunc(funcNames[i], func->type.getHeapType()));
5320+
Builder(*(Module*)module).makeRefFunc(funcNames[i]));
53225321
}
53235322
return ((Module*)module)->addElementSegment(std::move(segment));
53245323
}

src/ir/ReFinalize.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,7 @@ void ReFinalize::visitMemorySize(MemorySize* curr) { curr->finalize(); }
115115
void ReFinalize::visitMemoryGrow(MemoryGrow* curr) { curr->finalize(); }
116116
void ReFinalize::visitRefNull(RefNull* curr) { curr->finalize(); }
117117
void ReFinalize::visitRefIsNull(RefIsNull* curr) { curr->finalize(); }
118-
void ReFinalize::visitRefFunc(RefFunc* curr) {
119-
curr->finalize(curr->type.getHeapType(), *getModule());
120-
}
118+
void ReFinalize::visitRefFunc(RefFunc* curr) { curr->finalize(*getModule()); }
121119
void ReFinalize::visitRefEq(RefEq* curr) { curr->finalize(); }
122120
void ReFinalize::visitTableGet(TableGet* curr) { curr->finalize(); }
123121
void ReFinalize::visitTableSet(TableSet* curr) { curr->finalize(); }

src/ir/possible-contents.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,8 +327,7 @@ class PossibleContents {
327327
wasm.getGlobal(info.name)->type);
328328
} else {
329329
assert(info.kind == ExternalKind::Function);
330-
return builder.makeRefFunc(
331-
info.name, wasm.getFunction(info.name)->type.getHeapType());
330+
return builder.makeRefFunc(info.name);
332331
}
333332
}
334333
}

src/ir/table-utils.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ inline Index append(Table& table, Name name, Module& wasm) {
9090
wasm.dylinkSection->tableSize++;
9191
}
9292

93-
auto* func = wasm.getFunctionOrNull(name);
94-
assert(func != nullptr && "Cannot append non-existing function to a table.");
95-
segment->data.push_back(
96-
Builder(wasm).makeRefFunc(name, func->type.getHeapType()));
93+
assert(wasm.getFunctionOrNull(name) != nullptr &&
94+
"Cannot append non-existing function to a table.");
95+
segment->data.push_back(Builder(wasm).makeRefFunc(name));
9796
table.initial++;
9897
return tableIndex;
9998
}

src/passes/FuncCastEmulation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ struct FuncCastEmulation : public Pass {
178178
}
179179
auto* thunk = iter->second;
180180
ref->func = thunk->name;
181-
ref->finalize(thunk->type.getHeapType(), *module);
181+
ref->finalize(*module);
182182
}
183183
}
184184

src/passes/JSPI.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,7 @@ struct JSPI : public Pass {
151151
if (iter == wrappedExports.end()) {
152152
continue;
153153
}
154-
auto* replacementRef = builder.makeRefFunc(
155-
iter->second,
156-
module->getFunction(iter->second)->type.getHeapType());
154+
auto* replacementRef = builder.makeRefFunc(iter->second);
157155
segment->data[i] = replacementRef;
158156
}
159157
}

src/passes/LegalizeJSInterface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ struct LegalizeJSInterface : public Pass {
148148
}
149149

150150
curr->func = iter->second->name;
151-
curr->finalize(iter->second->type.getHeapType(), *getModule());
151+
curr->finalize(*getModule());
152152
}
153153
};
154154

src/passes/MergeSimilarFunctions.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ struct ParamInfo {
131131
if (const auto literals = std::get_if<Literals>(&values)) {
132132
return builder.makeConst((*literals)[index]);
133133
} else if (auto callees = std::get_if<std::vector<Name>>(&values)) {
134-
auto fnName = (*callees)[index];
135-
auto heapType = module->getFunction(fnName)->type.getHeapType();
136-
return builder.makeRefFunc(fnName, heapType);
134+
return builder.makeRefFunc((*callees)[index]);
137135
} else {
138136
WASM_UNREACHABLE("unexpected const value type");
139137
}

src/tools/fuzzing/fuzzing.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,8 +1697,7 @@ Function* TranslateToFuzzReader::addFunction() {
16971697
}
16981698
});
16991699
auto& randomElem = compatibleSegments[upTo(compatibleSegments.size())];
1700-
randomElem->data.push_back(
1701-
builder.makeRefFunc(func->name, func->type.getHeapType()));
1700+
randomElem->data.push_back(builder.makeRefFunc(func->name));
17021701
}
17031702
numAddedFunctions++;
17041703
return func;
@@ -2991,10 +2990,7 @@ Expression* TranslateToFuzzReader::makeCallRef(Type type) {
29912990
}
29922991
// TODO: half the time make a completely random item with that type.
29932992
return builder.makeCallRef(
2994-
builder.makeRefFunc(target->name, target->type.getHeapType()),
2995-
args,
2996-
type,
2997-
isReturn);
2993+
builder.makeRefFunc(target->name), args, type, isReturn);
29982994
}
29992995

30002996
Expression* TranslateToFuzzReader::makeLocalGet(Type type) {
@@ -3630,7 +3626,7 @@ Expression* TranslateToFuzzReader::makeRefFuncConst(Type type) {
36303626
do {
36313627
auto& func = wasm.functions[i];
36323628
if (Type::isSubType(func->type, type)) {
3633-
return builder.makeRefFunc(func->name, func->type.getHeapType());
3629+
return builder.makeRefFunc(func->name);
36343630
}
36353631
i = (i + 1) % wasm.functions.size();
36363632
} while (i != start);
@@ -3669,7 +3665,7 @@ Expression* TranslateToFuzzReader::makeRefFuncConst(Type type) {
36693665
Type(heapType, NonNullable, Exact),
36703666
{},
36713667
body));
3672-
return builder.makeRefFunc(func->name, heapType);
3668+
return builder.makeRefFunc(func->name);
36733669
}
36743670

36753671
Expression* TranslateToFuzzReader::makeConst(Type type) {

src/tools/wasm-merge.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,10 +584,7 @@ void updateTypes(Module& wasm) {
584584
}
585585
}
586586

587-
void visitRefFunc(RefFunc* curr) {
588-
curr->finalize(getModule()->getFunction(curr->func)->type.getHeapType(),
589-
*getModule());
590-
}
587+
void visitRefFunc(RefFunc* curr) { curr->finalize(*getModule()); }
591588

592589
void visitFunction(Function* curr) {
593590
ReFinalize().walkFunctionInModule(curr, getModule());

0 commit comments

Comments
 (0)