[wasm] Update permitted calls in unrolled loops

We prevent unrolling of loops with indirect calls. We expand the set of
permitted wasm builtins in unrolled loops.

Bug: v8:11298
Change-Id: I70b8ff3b16d9b0d3a4ea2d103f8ffb74083fd2a9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3289152
Commit-Queue: Manos Koukoutos <manoskouk@chromium.org>
Reviewed-by: Nico Hartmann <nicohartmann@chromium.org>
Reviewed-by: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/main@{#77965}
This commit is contained in:
Manos Koukoutos 2021-11-18 11:00:21 +00:00 committed by V8 LUCI CQ
parent 9c8741567d
commit 95c1647a45
2 changed files with 77 additions and 10 deletions

View File

@ -589,18 +589,33 @@ ZoneUnorderedSet<Node*>* LoopFinder::FindSmallUnnestedLoopFromHeader(
case IrOpcode::kTailCall:
case IrOpcode::kJSWasmCall:
case IrOpcode::kJSCall:
// Call nodes are considered to have unbounded size, i.e. >max_size.
// An exception is the call to the stack guard builtin at the beginning
// of many loops.
// Call nodes are considered to have unbounded size, i.e. >max_size,
// with the exception of certain wasm builtins.
return nullptr;
case IrOpcode::kCall: {
Node* callee = node->InputAt(0);
if (callee->opcode() == IrOpcode::kRelocatableInt32Constant ||
callee->opcode() == IrOpcode::kRelocatableInt64Constant) {
auto info = OpParameter<RelocatablePtrConstantInfo>(callee->op());
if (info.value() != v8::internal::wasm::WasmCode::kWasmStackGuard) {
return nullptr;
}
if (callee->opcode() != IrOpcode::kRelocatableInt32Constant &&
callee->opcode() != IrOpcode::kRelocatableInt64Constant) {
return nullptr;
}
intptr_t info =
OpParameter<RelocatablePtrConstantInfo>(callee->op()).value();
using WasmCode = v8::internal::wasm::WasmCode;
constexpr intptr_t unrollable_builtins[] = {
WasmCode::kWasmStackGuard,
WasmCode::kWasmTableGet,
WasmCode::kWasmTableSet,
WasmCode::kWasmTableGrow,
WasmCode::kWasmThrow,
WasmCode::kWasmRethrow,
WasmCode::kWasmRethrowExplicitContext,
WasmCode::kWasmRefFunc,
WasmCode::kWasmAllocateRtt,
WasmCode::kWasmAllocateFreshRtt};
if (std::count(unrollable_builtins,
unrollable_builtins + arraysize(unrollable_builtins),
info) == 0) {
return nullptr;
}
V8_FALLTHROUGH;
}

View File

@ -3,7 +3,7 @@
// found in the LICENSE file.
// Flags: --experimental-wasm-typed-funcref --experimental-wasm-eh
// Flags: --wasm-loop-unrolling --experimental-wasm-return-call
// Flags: --experimental-wasm-return-call --no-liftoff
// Needed for exceptions-utils.js.
// Flags: --allow-natives-syntax
@ -194,3 +194,55 @@ d8.file.execute("test/mjsunit/wasm/exceptions-utils.js");
let instance = builder.instantiate();
assertEquals(11, instance.exports.throw_catch(0));
})();
// Test that loops are unrolled in the presence of builtins.
(function UnrollWithBuiltinsTest() {
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
builder.addTable(kWasmFuncRef, 10, 10);
let callee = builder.addFunction("callee", kSig_i_i)
.addBody([kExprLocalGet, 0, kExprI32Const, 1, kExprI32Add])
.exportFunc();
builder.addFunction("main", makeSig([kWasmI32], []))
.addBody([
kExprLoop, kWasmVoid,
kExprLocalGet, 0, kExprI32Const, 0, kExprI32LtS, kExprBrIf, 1,
kExprLocalGet, 0,
kExprRefFunc, callee.index,
kExprTableSet, 0,
kExprBr, 0,
kExprEnd])
.exportFunc();
builder.instantiate();
})();
// Test that loops are *not* unrolled in the presence of direct/indirect calls.
(function LoopWithCallsTest() {
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
let callee = builder.addFunction("callee", kSig_i_i)
.addBody([kExprLocalGet, 0, kExprI32Const, 1, kExprI32Add])
.exportFunc();
builder.addFunction("main", makeSig([kWasmI32], []))
.addBody([
kExprLoop, kWasmVoid,
kExprLocalGet, 0,
kExprRefFunc, callee.index,
kExprCallRef,
kExprBrIf, 0,
kExprEnd,
kExprLoop, kWasmVoid,
kExprLocalGet, 0,
kExprCallFunction, callee.index,
kExprBrIf, 0,
kExprEnd])
.exportFunc();
builder.instantiate();
})();