From 29eb163beda752eddd88948483f47c13a74e402a Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 5 Dec 2025 13:12:07 -0800 Subject: [PATCH 1/4] work --- src/ir/lubs.cpp | 68 +++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/src/ir/lubs.cpp b/src/ir/lubs.cpp index f9c04cb36e4..a33c59762c1 100644 --- a/src/ir/lubs.cpp +++ b/src/ir/lubs.cpp @@ -15,7 +15,6 @@ */ #include "ir/lubs.h" -#include "ir/find_all.h" #include "ir/utils.h" #include "wasm-type.h" #include "wasm.h" @@ -51,51 +50,42 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { return lub; } - // Scan the body and look at the returns. First, return expressions. - for (auto* ret : FindAll(func->body).list) { - lub.note(ret->value->type); - if (lub.getLUB() == originalType) { - return lub; - } - } + // Scan the body and look at the returns. + struct Finder : public PostWalker { + Module& wasm; + LUBFinder& lub; - // Process return_calls and call_refs. Unlike return expressions which we - // just handled, these only get a type to update, not a value. - auto processReturnType = [&](Type type) { - // Return whether we still look ok to do the optimization. If this is - // false then we can stop here. - lub.note(type); - return lub.getLUB() != originalType; - }; + Finder(Module& wasm, LUBFinder& lub) : wasm(wasm), lub(lub) {} - for (auto* call : FindAll(func->body).list) { - if (call->isReturn && - !processReturnType(wasm.getFunction(call->target)->getResults())) { - return lub; - } - } - for (auto* call : FindAll(func->body).list) { - if (call->isReturn && - !processReturnType(call->heapType.getSignature().results)) { - return lub; + void visitReturn(Return* curr) { + lub.note(ret->value->type); } - } - for (auto* call : FindAll(func->body).list) { - if (call->isReturn) { - auto targetType = call->target->type; - // We can skip unreachable code and calls to bottom types, as both trap. - if (targetType == Type::unreachable) { - continue; + void visitCall(Call* curr) { + if (curr->isReturn) { + lub.note(wasm.getFunction(curr->target)->getResults()); } - auto targetHeapType = targetType.getHeapType(); - if (targetHeapType.isBottom()) { - continue; + } + void visitCallIndirect(CallIndirect* curr) { + if (curr->isReturn && + lub.note(curr->heapType.getSignature().results); } - if (!processReturnType(targetHeapType.getSignature().results)) { - return lub; + } + void visitCallRef(CallRef* curr) { + if (curr->isReturn) { + auto targetType = curr->target->type; + // We can skip unreachable code and calls to bottom types, as both trap. + if (targetType == Type::unreachable) { + return; + } + auto targetHeapType = targetType.getHeapType(); + if (targetHeapType.isBottom()) { + return; + } + lub.note(targetHeapType.getSignature().results); } } - } + } finder(wasm, lub); + finder.walk(func->body); return lub; } From dee1e17c0bdc53d9b4fc612bea2c09db64d2accd Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 5 Dec 2025 13:12:38 -0800 Subject: [PATCH 2/4] work --- src/ir/lubs.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/ir/lubs.cpp b/src/ir/lubs.cpp index a33c59762c1..6019c326a9c 100644 --- a/src/ir/lubs.cpp +++ b/src/ir/lubs.cpp @@ -57,20 +57,18 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { Finder(Module& wasm, LUBFinder& lub) : wasm(wasm), lub(lub) {} - void visitReturn(Return* curr) { - lub.note(ret->value->type); - } + void visitReturn(Return* curr) { lub.note(ret->value->type); } void visitCall(Call* curr) { if (curr->isReturn) { lub.note(wasm.getFunction(curr->target)->getResults()); } } void visitCallIndirect(CallIndirect* curr) { - if (curr->isReturn && + if (curr->isReturn) { lub.note(curr->heapType.getSignature().results); } } - void visitCallRef(CallRef* curr) { + void visitCallRef(CallRef * curr) { if (curr->isReturn) { auto targetType = curr->target->type; // We can skip unreachable code and calls to bottom types, as both trap. @@ -84,7 +82,8 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { lub.note(targetHeapType.getSignature().results); } } - } finder(wasm, lub); + } + finder(wasm, lub); finder.walk(func->body); return lub; From 3a0460ba30854199f1e6e59c333b64ecb024502e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 5 Dec 2025 13:12:54 -0800 Subject: [PATCH 3/4] work --- src/ir/lubs.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/ir/lubs.cpp b/src/ir/lubs.cpp index 6019c326a9c..1049f7cff29 100644 --- a/src/ir/lubs.cpp +++ b/src/ir/lubs.cpp @@ -68,7 +68,7 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { lub.note(curr->heapType.getSignature().results); } } - void visitCallRef(CallRef * curr) { + void visitCallRef(CallRef* curr) { if (curr->isReturn) { auto targetType = curr->target->type; // We can skip unreachable code and calls to bottom types, as both trap. @@ -82,8 +82,7 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { lub.note(targetHeapType.getSignature().results); } } - } - finder(wasm, lub); + } finder(wasm, lub); finder.walk(func->body); return lub; From 0a8ae798a2d53982c1ccb21603117305949a27f7 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 5 Dec 2025 13:14:34 -0800 Subject: [PATCH 4/4] fix --- src/ir/lubs.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ir/lubs.cpp b/src/ir/lubs.cpp index 1049f7cff29..98d12df0671 100644 --- a/src/ir/lubs.cpp +++ b/src/ir/lubs.cpp @@ -57,7 +57,7 @@ LUBFinder getResultsLUB(Function* func, Module& wasm) { Finder(Module& wasm, LUBFinder& lub) : wasm(wasm), lub(lub) {} - void visitReturn(Return* curr) { lub.note(ret->value->type); } + void visitReturn(Return* curr) { lub.note(curr->value->type); } void visitCall(Call* curr) { if (curr->isReturn) { lub.note(wasm.getFunction(curr->target)->getResults());