Commit cffaee55 authored by Bill Budge's avatar Bill Budge Committed by Commit Bot

[wasm] Move fast path of wasm RefFunc to a builtin

- Use a builtin for the fast path of Wasm RefFunc.
- Simplify the runtime function by passing instance as first
  argument.

Change-Id: I5f6993cae21a878cee21a391a25c4d574243058b
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2144533
Commit-Queue: Bill Budge <bbudge@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67346}
parent bf5312b6
...@@ -850,6 +850,7 @@ namespace internal { ...@@ -850,6 +850,7 @@ namespace internal {
TFC(WasmI64AtomicWait32, WasmI64AtomicWait32) \ TFC(WasmI64AtomicWait32, WasmI64AtomicWait32) \
TFC(WasmI64AtomicWait64, WasmI64AtomicWait64) \ TFC(WasmI64AtomicWait64, WasmI64AtomicWait64) \
TFC(WasmMemoryGrow, WasmMemoryGrow) \ TFC(WasmMemoryGrow, WasmMemoryGrow) \
TFC(WasmRefFunc, WasmRefFunc) \
TFC(WasmTableInit, WasmTableInit) \ TFC(WasmTableInit, WasmTableInit) \
TFC(WasmTableCopy, WasmTableCopy) \ TFC(WasmTableCopy, WasmTableCopy) \
TFC(WasmTableGet, WasmTableGet) \ TFC(WasmTableGet, WasmTableGet) \
......
...@@ -256,6 +256,32 @@ TF_BUILTIN(WasmMemoryGrow, WasmBuiltinsAssembler) { ...@@ -256,6 +256,32 @@ TF_BUILTIN(WasmMemoryGrow, WasmBuiltinsAssembler) {
Return(Int32Constant(-1)); Return(Int32Constant(-1));
} }
TF_BUILTIN(WasmRefFunc, WasmBuiltinsAssembler) {
TNode<WasmInstanceObject> instance = LoadInstanceFromFrame();
Label call_runtime(this, Label::kDeferred);
TNode<Uint32T> raw_index =
UncheckedCast<Uint32T>(Parameter(Descriptor::kFunctionIndex));
TNode<FixedArray> table = LoadObjectField<FixedArray>(
instance, WasmInstanceObject::kWasmExternalFunctionsOffset);
GotoIf(IsUndefined(table), &call_runtime);
TNode<IntPtrT> function_index =
UncheckedCast<IntPtrT>(ChangeUint32ToWord(raw_index));
// Function index should be in range.
TNode<Object> result = LoadFixedArrayElement(table, function_index);
GotoIf(IsUndefined(result), &call_runtime);
Return(result);
BIND(&call_runtime);
// Fall back to the runtime call for more complex cases.
// function_index is known to be in Smi range.
TailCallRuntime(Runtime::kWasmRefFunc, LoadContextFromInstance(instance),
instance, SmiFromUint32(raw_index));
}
TF_BUILTIN(WasmTableInit, WasmBuiltinsAssembler) { TF_BUILTIN(WasmTableInit, WasmBuiltinsAssembler) {
TNode<Uint32T> dst_raw = TNode<Uint32T> dst_raw =
UncheckedCast<Uint32T>(Parameter(Descriptor::kDestination)); UncheckedCast<Uint32T>(Parameter(Descriptor::kDestination));
......
...@@ -411,6 +411,11 @@ void WasmMemoryGrowDescriptor::InitializePlatformSpecific( ...@@ -411,6 +411,11 @@ void WasmMemoryGrowDescriptor::InitializePlatformSpecific(
DefaultInitializePlatformSpecific(data, kParameterCount); DefaultInitializePlatformSpecific(data, kParameterCount);
} }
void WasmRefFuncDescriptor::InitializePlatformSpecific(
CallInterfaceDescriptorData* data) {
DefaultInitializePlatformSpecific(data, kParameterCount);
}
void WasmTableInitDescriptor::InitializePlatformSpecific( void WasmTableInitDescriptor::InitializePlatformSpecific(
CallInterfaceDescriptorData* data) { CallInterfaceDescriptorData* data) {
DefaultInitializePlatformSpecific(data, DefaultInitializePlatformSpecific(data,
......
...@@ -101,6 +101,7 @@ namespace internal { ...@@ -101,6 +101,7 @@ namespace internal {
V(WasmI64AtomicWait32) \ V(WasmI64AtomicWait32) \
V(WasmI64AtomicWait64) \ V(WasmI64AtomicWait64) \
V(WasmMemoryGrow) \ V(WasmMemoryGrow) \
V(WasmRefFunc) \
V(WasmTableInit) \ V(WasmTableInit) \
V(WasmTableCopy) \ V(WasmTableCopy) \
V(WasmTableGet) \ V(WasmTableGet) \
...@@ -1363,6 +1364,14 @@ class WasmMemoryGrowDescriptor final : public CallInterfaceDescriptor { ...@@ -1363,6 +1364,14 @@ class WasmMemoryGrowDescriptor final : public CallInterfaceDescriptor {
DECLARE_DESCRIPTOR(WasmMemoryGrowDescriptor, CallInterfaceDescriptor) DECLARE_DESCRIPTOR(WasmMemoryGrowDescriptor, CallInterfaceDescriptor)
}; };
class WasmRefFuncDescriptor final : public CallInterfaceDescriptor {
public:
DEFINE_PARAMETERS_NO_CONTEXT(kFunctionIndex)
DEFINE_RESULT_AND_PARAMETER_TYPES(MachineType::AnyTagged(), // result
MachineType::Uint32()) // kFunctionIndex
DECLARE_DESCRIPTOR(WasmRefFuncDescriptor, CallInterfaceDescriptor)
};
class WasmTableInitDescriptor final : public CallInterfaceDescriptor { class WasmTableInitDescriptor final : public CallInterfaceDescriptor {
public: public:
DEFINE_PARAMETERS_NO_CONTEXT(kDestination, kSource, kSize, kTableIndex, DEFINE_PARAMETERS_NO_CONTEXT(kDestination, kSource, kSize, kTableIndex,
......
...@@ -144,6 +144,19 @@ bool ContainsInt64(const wasm::FunctionSig* sig) { ...@@ -144,6 +144,19 @@ bool ContainsInt64(const wasm::FunctionSig* sig) {
} }
return false; return false;
} }
template <typename BuiltinDescriptor>
CallDescriptor* GetBuiltinCallDescriptor(WasmGraphBuilder* builder,
StubCallMode stub_mode) {
BuiltinDescriptor interface_descriptor;
return Linkage::GetStubCallDescriptor(
builder->mcgraph()->zone(), // zone
interface_descriptor, // descriptor
interface_descriptor.GetStackParameterCount(), // stack parameter count
CallDescriptor::kNoFlags, // flags
Operator::kNoProperties, // properties
stub_mode); // stub call mode
}
} // namespace } // namespace
class WasmGraphAssembler : public GraphAssembler { class WasmGraphAssembler : public GraphAssembler {
...@@ -262,11 +275,16 @@ Node* WasmGraphBuilder::RefNull() { ...@@ -262,11 +275,16 @@ Node* WasmGraphBuilder::RefNull() {
} }
Node* WasmGraphBuilder::RefFunc(uint32_t function_index) { Node* WasmGraphBuilder::RefFunc(uint32_t function_index) {
Node* args[] = { auto call_descriptor = GetBuiltinCallDescriptor<WasmRefFuncDescriptor>(
graph()->NewNode(mcgraph()->common()->NumberConstant(function_index))}; this, StubCallMode::kCallWasmRuntimeStub);
Node* result = // A direct call to a wasm runtime stub defined in this module.
BuildCallToRuntime(Runtime::kWasmRefFunc, args, arraysize(args)); // Just encode the stub index. This will be patched at relocation.
return result; Node* call_target = mcgraph()->RelocatableIntPtrConstant(
wasm::WasmCode::kWasmRefFunc, RelocInfo::WASM_STUB_CALL);
return SetEffectControl(
graph()->NewNode(mcgraph()->common()->Call(call_descriptor), call_target,
Uint32Constant(function_index), effect(), control()));
} }
Node* WasmGraphBuilder::NoContextConstant() { Node* WasmGraphBuilder::NoContextConstant() {
...@@ -1938,19 +1956,6 @@ ExternalReference convert_ccall_ref(WasmGraphBuilder* builder, ...@@ -1938,19 +1956,6 @@ ExternalReference convert_ccall_ref(WasmGraphBuilder* builder,
} }
} }
template <typename BuiltinDescriptor>
CallDescriptor* GetBuiltinCallDescriptor(WasmGraphBuilder* builder,
StubCallMode stub_mode) {
BuiltinDescriptor interface_descriptor;
return Linkage::GetStubCallDescriptor(
builder->mcgraph()->zone(), // zone
interface_descriptor, // descriptor
interface_descriptor.GetStackParameterCount(), // stack parameter count
CallDescriptor::kNoFlags, // flags
Operator::kNoProperties, // properties
stub_mode); // stub call mode
}
} // namespace } // namespace
Node* WasmGraphBuilder::BuildCcallConvertFloat(Node* input, Node* WasmGraphBuilder::BuildCcallConvertFloat(Node* input,
......
...@@ -447,12 +447,9 @@ Object ThrowTableOutOfBounds(Isolate* isolate, ...@@ -447,12 +447,9 @@ Object ThrowTableOutOfBounds(Isolate* isolate,
RUNTIME_FUNCTION(Runtime_WasmRefFunc) { RUNTIME_FUNCTION(Runtime_WasmRefFunc) {
ClearThreadInWasmScope flag_scope; ClearThreadInWasmScope flag_scope;
HandleScope scope(isolate); HandleScope scope(isolate);
DCHECK_EQ(1, args.length()); DCHECK_EQ(2, args.length());
auto instance = CONVERT_ARG_HANDLE_CHECKED(WasmInstanceObject, instance, 0);
Handle<WasmInstanceObject>(GetWasmInstanceOnStackTop(isolate), isolate); CONVERT_UINT32_ARG_CHECKED(function_index, 1);
DCHECK(isolate->context().is_null());
isolate->set_context(instance->native_context());
CONVERT_UINT32_ARG_CHECKED(function_index, 0);
Handle<WasmExternalFunction> function = Handle<WasmExternalFunction> function =
WasmInstanceObject::GetOrCreateWasmExternalFunction(isolate, instance, WasmInstanceObject::GetOrCreateWasmExternalFunction(isolate, instance,
......
...@@ -60,6 +60,7 @@ struct WasmModule; ...@@ -60,6 +60,7 @@ struct WasmModule;
V(WasmI32AtomicWait64) \ V(WasmI32AtomicWait64) \
V(WasmI64AtomicWait32) \ V(WasmI64AtomicWait32) \
V(WasmI64AtomicWait64) \ V(WasmI64AtomicWait64) \
V(WasmRefFunc) \
V(WasmMemoryGrow) \ V(WasmMemoryGrow) \
V(WasmTableInit) \ V(WasmTableInit) \
V(WasmTableCopy) \ V(WasmTableCopy) \
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment