Skip to content

Commit 20b871e

Browse files
danlehv8-scoped@luci-project-accounts.iam.gserviceaccount.com
authored andcommitted
[wasm] Implement more simple ops in Wasm-in-JS body inlining
* Implemented missing simple constants and operations in WasmInJsInliningInterface: `I64Const`, `RefNull`, `RefFunc`, and `RefAsNonNull`. * Fixed the signature of `WasmRefFunc` in `assembler.h` to take `V<WasmTrustedInstanceData>`. * Added tests for `i64Const`, `refNull`, and `refAsNonNull` to `wasm-in-js-inlining-turboshaft.js`. * Added a test for `RefFunc` by exposing it via an exported global. Bug: 353475584 Change-Id: Ie63cb8328b596f29107bf872e48dd2122d1cad07 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/7796002 Reviewed-by: Matthias Liedtke <mliedtke@chromium.org> Commit-Queue: Daniel Lehmann <dlehmann@chromium.org> Cr-Commit-Position: refs/heads/main@{#106860}
1 parent 855e1a2 commit 20b871e

3 files changed

Lines changed: 55 additions & 6 deletions

File tree

src/compiler/turboshaft/assembler.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5299,7 +5299,8 @@ class AssemblerOpInterface : public Next {
52995299
return ReduceIfReachableWasmAllocateStruct(rtt, struct_type, is_shared);
53005300
}
53015301

5302-
V<WasmFuncRef> WasmRefFunc(V<Object> wasm_instance, uint32_t function_index) {
5302+
V<WasmFuncRef> WasmRefFunc(V<WasmTrustedInstanceData> wasm_instance,
5303+
uint32_t function_index) {
53035304
return ReduceIfReachableWasmRefFunc(wasm_instance, function_index);
53045305
}
53055306

src/compiler/turboshaft/wasm-in-js-inlining-reducer-inl.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,10 @@ class WasmInJsInliningInterface {
628628
result->op = __ Word32Constant(value);
629629
}
630630

631+
void I64Const(FullDecoder* decoder, Value* result, int64_t value) {
632+
result->op = __ Word64Constant(value);
633+
}
634+
631635
void F32Const(FullDecoder* decoder, Value* result, float value) {
632636
result->op = __ Float32Constant(value);
633637
}
@@ -636,11 +640,18 @@ class WasmInJsInliningInterface {
636640
result->op = __ Float64Constant(value);
637641
}
638642

639-
// TODO(dlehmann,353475584): Support more of these simple constants.
640-
BAILOUT_WASM_OP(I64Const)
641-
BAILOUT_WASM_OP(RefNull)
642-
BAILOUT_WASM_OP(RefFunc)
643-
BAILOUT_WASM_OP(RefAsNonNull)
643+
void RefNull(FullDecoder* decoder, ValueType type, Value* result) {
644+
result->op = __ Null(type);
645+
}
646+
647+
void RefFunc(FullDecoder* decoder, uint32_t function_index, Value* result) {
648+
result->op = __ WasmRefFunc(trusted_instance_data_, function_index);
649+
}
650+
651+
void RefAsNonNull(FullDecoder* decoder, const Value& arg, Value* result) {
652+
result->op = __ AssertNotNull(arg.get<Object>(), frame_state_, arg.type,
653+
TrapId::kTrapNullDereference);
654+
}
644655

645656
void Drop(FullDecoder* decoder) {}
646657

test/filecheck/wasm-in-js-inlining-turboshaft.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ d8.file.execute("test/mjsunit/wasm/wasm-module-builder.js");
1717
d8.file.execute("test/mjsunit/mjsunit.js");
1818

1919
const builder = new WasmModuleBuilder();
20+
const funcVoid = builder.addFunction('func_void', kSig_v_v).addBody([]).exportFunc();
21+
const globalFuncRef = builder.addGlobal(kWasmFuncRef, true, false).exportAs('globalFuncRef');
2022
const array = builder.addArray(kWasmI32);
2123
const struct = builder.addStruct([makeField(kWasmI32, true)]);
2224
const array8 = builder.addArray(kWasmI8);
@@ -76,6 +78,15 @@ addTestcase('f32Const', kSig_f_v, [], [...wasmF32Const(42.0)]);
7678
// CHECK: Considering Wasm function [{{[0-9]+}}] f64Const of module {{.*}} for inlining
7779
// CHECK-NEXT: - inlining Wasm function
7880
addTestcase('f64Const', kSig_d_v, [], [...wasmF64Const(42.0)]);
81+
// CHECK: Considering Wasm function [{{[0-9]+}}] i64Const of module {{.*}} for inlining
82+
// CHECK-NEXT: - inlining Wasm function
83+
// Use large constant to potentially test int64 lowering on 32-bit platforms in the future.
84+
const kValueExceedingI32 = 0x80000000n;
85+
addTestcase('i64Const', makeSig([], [kWasmI64]), [], [...wasmI64Const(kValueExceedingI32)]);
86+
87+
// CHECK: Considering Wasm function [{{[0-9]+}}] refNull of module {{.*}} for inlining
88+
// CHECK-NEXT: - inlining Wasm function
89+
addTestcase('refNull', makeSig([], [kWasmExternRef]), [], [kExprRefNull, kExternRefCode]);
7990

8091
function addUnaryTestcase(op, wasmSignature, wasmArgument) {
8192
addTestcase(op, wasmSignature, [wasmArgument], [
@@ -441,6 +452,32 @@ addTestcase('refTestAlwaysSucceedsButNull', kSig_i_v, [], [
441452
kGCPrefix, kExprRefTest, array,
442453
]);
443454

455+
// CHECK: Considering Wasm function [{{[0-9]+}}] refAsNonNull of module {{.*}} for inlining
456+
// CHECK-NEXT: - inlining Wasm function
457+
addTestcase('refAsNonNull', makeSig([kWasmExternRef], [kWasmExternRef]), [{}], [
458+
kExprLocalGet, 0,
459+
kExprRefAsNonNull,
460+
]);
461+
462+
// CHECK: Considering Wasm function [{{[0-9]+}}] refFuncNonNull of module {{.*}} for inlining
463+
// CHECK-NEXT: - inlining Wasm function
464+
addTestcase('refFuncNonNull', makeSig([], [kWasmI32]), [], [
465+
kExprRefFunc, funcVoid.index,
466+
kExprRefIsNull,
467+
]);
468+
469+
// CHECK: Considering Wasm function [{{[0-9]+}}] refFuncGlobal of module {{.*}} for inlining
470+
// CHECK-NEXT: - inlining Wasm function
471+
addTestcase('refFuncGlobal', kSig_v_v, [], [
472+
kExprRefFunc, funcVoid.index,
473+
kExprGlobalSet, globalFuncRef.index,
474+
], (wasmExports) => {
475+
return function js_refFuncGlobal() {
476+
wasmExports.refFuncGlobal();
477+
assertEquals('function', typeof wasmExports.globalFuncRef.value);
478+
};
479+
});
480+
444481
const globalAnyRef = builder.addGlobal(kWasmAnyRef, true, false);
445482

446483
builder.addFunction('initGlobalAnyRefArray', makeSig([], [])).addBody([

0 commit comments

Comments
 (0)