-
Notifications
You must be signed in to change notification settings - Fork 721
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Monomorphize dropped functions (#6734)
We now consider a drop to be part of the call context: If we see (drop (call $foo) ) (func $foo (result i32) (i32.const 42) ) Then we'd monomorphize to this: (call $foo_1) ;; call the specialized function instead (func $foo_1 ;; the specialized function returns nothing (drop ;; the drop was moved into here (i32.const 42) ) ) With the drop now in the called function, we may be able to optimize out unused work. Refactor a bit of code out of DAE that we can reuse here, into a new return-utils.h.
- Loading branch information
Showing
6 changed files
with
1,418 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
* Copyright 2024 WebAssembly Community Group participants | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "ir/return-utils.h" | ||
#include "ir/module-utils.h" | ||
#include "wasm-builder.h" | ||
#include "wasm-traversal.h" | ||
#include "wasm.h" | ||
|
||
namespace wasm::ReturnUtils { | ||
|
||
namespace { | ||
|
||
struct ReturnValueRemover : public PostWalker<ReturnValueRemover> { | ||
void visitReturn(Return* curr) { | ||
auto* value = curr->value; | ||
assert(value); | ||
curr->value = nullptr; | ||
Builder builder(*getModule()); | ||
replaceCurrent(builder.makeSequence(builder.makeDrop(value), curr)); | ||
} | ||
|
||
void visitCall(Call* curr) { handleReturnCall(curr); } | ||
void visitCallIndirect(CallIndirect* curr) { handleReturnCall(curr); } | ||
void visitCallRef(CallRef* curr) { handleReturnCall(curr); } | ||
|
||
template<typename T> void handleReturnCall(T* curr) { | ||
if (curr->isReturn) { | ||
Fatal() << "Cannot remove return_calls in ReturnValueRemover"; | ||
} | ||
} | ||
|
||
void visitFunction(Function* curr) { | ||
if (curr->body->type.isConcrete()) { | ||
curr->body = Builder(*getModule()).makeDrop(curr->body); | ||
} | ||
} | ||
}; | ||
|
||
} // anonymous namespace | ||
|
||
void removeReturns(Function* func, Module& wasm) { | ||
ReturnValueRemover().walkFunctionInModule(func, &wasm); | ||
} | ||
|
||
std::unordered_map<Function*, bool> findReturnCallers(Module& wasm) { | ||
ModuleUtils::ParallelFunctionAnalysis<bool> analysis( | ||
wasm, [&](Function* func, bool& hasReturnCall) { | ||
if (func->imported()) { | ||
return; | ||
} | ||
|
||
struct Finder : PostWalker<Finder> { | ||
bool hasReturnCall = false; | ||
|
||
void visitCall(Call* curr) { | ||
if (curr->isReturn) { | ||
hasReturnCall = true; | ||
} | ||
} | ||
void visitCallIndirect(CallIndirect* curr) { | ||
if (curr->isReturn) { | ||
hasReturnCall = true; | ||
} | ||
} | ||
void visitCallRef(CallRef* curr) { | ||
if (curr->isReturn) { | ||
hasReturnCall = true; | ||
} | ||
} | ||
} finder; | ||
|
||
finder.walk(func->body); | ||
hasReturnCall = finder.hasReturnCall; | ||
}); | ||
|
||
// Convert to an unordered map for fast lookups. TODO: Avoid a copy here. | ||
std::unordered_map<Function*, bool> ret; | ||
ret.reserve(analysis.map.size()); | ||
for (auto& [k, v] : analysis.map) { | ||
ret[k] = v; | ||
} | ||
return ret; | ||
} | ||
|
||
} // namespace wasm::ReturnUtils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2024 WebAssembly Community Group participants | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#ifndef wasm_ir_return_h | ||
#define wasm_ir_return_h | ||
|
||
#include "wasm.h" | ||
|
||
namespace wasm::ReturnUtils { | ||
|
||
// Removes values from both explicit returns and implicit ones (values that flow | ||
// from the body). This is useful after changing a function's type to no longer | ||
// return anything. | ||
// | ||
// This does *not* handle return calls, and will error on them. Removing a | ||
// return call may change the semantics of the program, so we do not do it | ||
// automatically here. | ||
void removeReturns(Function* func, Module& wasm); | ||
|
||
// Return a map of every function to whether it does a return call. | ||
using ReturnCallersMap = std::unordered_map<Function*, bool>; | ||
ReturnCallersMap findReturnCallers(Module& wasm); | ||
|
||
} // namespace wasm::ReturnUtils | ||
|
||
#endif // wasm_ir_return_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.