Commit b1583b6c authored by Michael Starzinger's avatar Michael Starzinger Committed by Commit Bot

[wasm] Load {CEntryStub} from the instance object.

This avoids embedding the {CEntryStub} into generated {WasmCode} and
instead loads it from the instance object. It is another step towards
making the generated code independent of the Isolate.

R=clemensh@chromium.org
BUG=v8:7472

Change-Id: Ic6ab7602a77fc11e6ec4a03e1bdba647d54df5e3
Reviewed-on: https://chromium-review.googlesource.com/1084841Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53491}
parent 4ff2cdf3
......@@ -112,12 +112,11 @@ bool ContainsInt64(wasm::FunctionSig* sig) {
WasmGraphBuilder::WasmGraphBuilder(
wasm::ModuleEnv* env, Zone* zone, MachineGraph* mcgraph,
Handle<Code> centry_stub, wasm::FunctionSig* sig,
wasm::FunctionSig* sig,
compiler::SourcePositionTable* source_position_table)
: zone_(zone),
mcgraph_(mcgraph),
env_(env),
centry_stub_(centry_stub),
cur_buffer_(def_buffer_),
cur_bufsize_(kDefaultBufferSize),
has_simd_(ContainsSimd(sig)),
......@@ -216,14 +215,6 @@ Node* WasmGraphBuilder::RefNull() {
return null;
}
Node* WasmGraphBuilder::CEntryStub() {
if (!centry_stub_node_.is_set()) {
centry_stub_node_.set(
graph()->NewNode(mcgraph()->common()->HeapConstant(centry_stub_)));
}
return centry_stub_node_.get();
}
Node* WasmGraphBuilder::NoContextConstant() {
// TODO(titzer): avoiding a dependency on JSGraph here. Refactor.
return mcgraph()->IntPtrConstant(0);
......@@ -2963,16 +2954,18 @@ Node* WasmGraphBuilder::BuildCallToRuntimeWithContext(Runtime::FunctionId f,
auto call_descriptor = Linkage::GetRuntimeCallDescriptor(
mcgraph()->zone(), f, fun->nargs, Operator::kNoProperties,
CallDescriptor::kNoFlags);
// CEntryStubConstant nodes have to be created and cached in the main
// thread. At the moment this is only done for CEntryStubConstant(1).
// The CEntryStub is loaded from the instance_node so that generated code is
// Isolate independent. At the moment this is only done for CEntryStub(1).
DCHECK_EQ(1, fun->result_size);
Node* centry_stub = *effect_ =
LOAD_INSTANCE_FIELD(CEntryStub, MachineType::TaggedPointer());
// At the moment we only allow 4 parameters. If more parameters are needed,
// increase this constant accordingly.
static const int kMaxParams = 4;
DCHECK_GE(kMaxParams, parameter_count);
Node* inputs[kMaxParams + 6];
int count = 0;
inputs[count++] = CEntryStub();
inputs[count++] = centry_stub;
for (int i = 0; i < parameter_count; i++) {
inputs[count++] = parameters[i];
}
......@@ -4002,8 +3995,7 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
WasmWrapperGraphBuilder(Zone* zone, wasm::ModuleEnv* env, JSGraph* jsgraph,
wasm::FunctionSig* sig,
compiler::SourcePositionTable* spt)
: WasmGraphBuilder(env, zone, jsgraph,
CodeFactory::CEntry(jsgraph->isolate()), sig, spt),
: WasmGraphBuilder(env, zone, jsgraph, sig, spt),
isolate_(jsgraph->isolate()),
jsgraph_(jsgraph) {}
......@@ -4353,8 +4345,7 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
// called {WasmExportedFunction} via the {WasmExportedFunctionData}
// structure. since JSToWasm wrappers can be compiled at module compile time
// and patched at instance build time.
DCHECK_NULL(instance_node_);
instance_node_ = BuildLoadInstanceFromExportedFunction(js_closure);
instance_node_.set(BuildLoadInstanceFromExportedFunction(js_closure));
if (!wasm::IsJSCompatibleSignature(sig_)) {
// Throw a TypeError. Use the js_context of the calling javascript
......@@ -4409,12 +4400,13 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
int wasm_count = static_cast<int>(sig_->parameter_count());
// Build the start and the parameter nodes.
CallDescriptor* call_descriptor;
Node* start = Start(wasm_count + 3);
*effect_ = start;
*control_ = start;
// Create the instance_node from the passed parameter.
instance_node_.set(Param(wasm::kWasmInstanceParameterIndex));
Node* callables_node = LOAD_INSTANCE_FIELD(ImportedFunctionCallables,
MachineType::TaggedPointer());
Node* callable_node = LOAD_FIXED_ARRAY_SLOT(callables_node, index);
......@@ -4433,6 +4425,7 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
return false;
}
CallDescriptor* call_descriptor;
Node** args = Buffer(wasm_count + 9);
Node* call = nullptr;
......@@ -4561,6 +4554,9 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
*effect_ = start;
*control_ = start;
// Create the instance_node from the passed parameter.
instance_node_.set(Param(wasm::kWasmInstanceParameterIndex));
// Compute size for the argument buffer.
int args_size_bytes = 0;
for (wasm::ValueType type : sig_->parameters()) {
......@@ -5032,8 +5028,7 @@ SourcePositionTable* TurbofanWasmCompilationUnit::BuildGraphForWasmFunction(
SourcePositionTable* source_position_table =
new (mcgraph_->zone()) SourcePositionTable(mcgraph_->graph());
WasmGraphBuilder builder(wasm_unit_->env_, mcgraph_->zone(), mcgraph_,
wasm_unit_->centry_stub_, wasm_unit_->func_body_.sig,
source_position_table);
wasm_unit_->func_body_.sig, source_position_table);
graph_construction_result_ =
wasm::BuildTFGraph(wasm_unit_->isolate_->allocator(), &builder,
wasm_unit_->func_body_, node_origins);
......
......@@ -161,7 +161,7 @@ class WasmGraphBuilder {
enum UseRetpoline : bool { kRetpoline = true, kNoRetpoline = false };
WasmGraphBuilder(wasm::ModuleEnv* env, Zone* zone, MachineGraph* mcgraph,
Handle<Code> centry_stub, wasm::FunctionSig* sig,
wasm::FunctionSig* sig,
compiler::SourcePositionTable* spt = nullptr);
Node** Buffer(size_t count) {
......@@ -349,12 +349,9 @@ class WasmGraphBuilder {
Node** effect_ = nullptr;
WasmInstanceCacheNodes* instance_cache_ = nullptr;
Handle<Code> centry_stub_;
SetOncePointer<Node> instance_node_;
SetOncePointer<Node> globals_start_;
SetOncePointer<Node> imported_mutable_globals_;
SetOncePointer<Node> centry_stub_node_;
SetOncePointer<Node> stack_check_code_node_;
const Operator* stack_check_call_operator_ = nullptr;
......@@ -371,7 +368,6 @@ class WasmGraphBuilder {
compiler::SourcePositionTable* const source_position_table_ = nullptr;
Node* CEntryStub();
Node* NoContextConstant();
Node* MemBuffer(uint32_t offset);
......
......@@ -4,7 +4,6 @@
#include "src/wasm/function-compiler.h"
#include "src/code-factory.h"
#include "src/compiler/wasm-compiler.h"
#include "src/counters.h"
#include "src/macro-assembler-inl.h"
......@@ -37,7 +36,6 @@ WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, ModuleEnv* env,
wasm::NativeModule* native_module,
wasm::FunctionBody body,
wasm::WasmName name, int index,
Handle<Code> centry_stub,
CompilationMode mode,
Counters* counters, bool lower_simd)
: isolate_(isolate),
......@@ -45,7 +43,6 @@ WasmCompilationUnit::WasmCompilationUnit(Isolate* isolate, ModuleEnv* env,
func_body_(body),
func_name_(name),
counters_(counters ? counters : isolate->counters()),
centry_stub_(centry_stub),
func_index_(index),
native_module_(native_module),
lower_simd_(lower_simd),
......@@ -141,8 +138,7 @@ wasm::WasmCode* WasmCompilationUnit::CompileWasmFunction(
WasmCompilationUnit unit(isolate, env, native_module, function_body,
wire_bytes.GetNameOrNull(function, env->module),
function->func_index,
CodeFactory::CEntry(isolate, 1), mode);
function->func_index, mode);
unit.ExecuteCompilation();
return unit.FinishCompilation(thrower);
}
......
......@@ -63,7 +63,6 @@ class WasmCompilationUnit final {
// only allowed to happen on the foreground thread.
WasmCompilationUnit(Isolate*, ModuleEnv*, wasm::NativeModule*,
wasm::FunctionBody, wasm::WasmName, int index,
Handle<Code> centry_stub,
CompilationMode = GetDefaultCompilationMode(),
Counters* = nullptr, bool lower_simd = false);
......@@ -91,7 +90,6 @@ class WasmCompilationUnit final {
wasm::FunctionBody func_body_;
wasm::WasmName func_name_;
Counters* counters_;
Handle<Code> centry_stub_;
int func_index_;
size_t memory_cost_ = 0;
wasm::NativeModule* native_module_;
......
......@@ -10,7 +10,6 @@
#include "src/base/optional.h"
#include "src/base/template-utils.h"
#include "src/base/utils/random-number-generator.h"
#include "src/code-factory.h"
#include "src/compiler/wasm-compiler.h"
#include "src/counters.h"
#include "src/identity-map.h"
......@@ -530,8 +529,7 @@ const wasm::WasmCode* LazyCompileFunction(
ErrorThrower thrower(isolate, "WasmLazyCompile");
WasmCompilationUnit unit(isolate, &module_env, native_module, body,
CStrVector(func_name.c_str()), func_index,
CodeFactory::CEntry(isolate));
CStrVector(func_name.c_str()), func_index);
unit.ExecuteCompilation();
wasm::WasmCode* wasm_code = unit.FinishCompilation(&thrower);
......@@ -952,11 +950,9 @@ size_t GetMaxUsableMemorySize(Isolate* isolate) {
// CompilationState when {Commit} is called.
class CompilationUnitBuilder {
public:
explicit CompilationUnitBuilder(NativeModule* native_module,
Handle<Code> centry_stub)
explicit CompilationUnitBuilder(NativeModule* native_module)
: native_module_(native_module),
compilation_state_(native_module->compilation_state()),
centry_stub_(centry_stub) {}
compilation_state_(native_module->compilation_state()) {}
void AddUnit(const WasmFunction* function, uint32_t buffer_offset,
Vector<const uint8_t> bytes, WasmName name) {
......@@ -1000,13 +996,12 @@ class CompilationUnitBuilder {
native_module_,
wasm::FunctionBody{function->sig, buffer_offset, bytes.begin(),
bytes.end()},
name, function->func_index, centry_stub_, mode,
name, function->func_index, mode,
compilation_state_->isolate()->async_counters().get());
}
NativeModule* native_module_;
CompilationState* compilation_state_;
Handle<Code> centry_stub_;
std::vector<std::unique_ptr<WasmCompilationUnit>> baseline_units_;
std::vector<std::unique_ptr<WasmCompilationUnit>> tiering_units_;
};
......@@ -1047,12 +1042,11 @@ size_t GetNumFunctionsToCompile(const WasmModule* wasm_module) {
void InitializeCompilationUnits(const std::vector<WasmFunction>& functions,
const ModuleWireBytes& wire_bytes,
const WasmModule* wasm_module,
Handle<Code> centry_stub,
NativeModule* native_module) {
uint32_t start = wasm_module->num_imported_functions;
uint32_t num_funcs = static_cast<uint32_t>(functions.size());
CompilationUnitBuilder builder(native_module, centry_stub);
CompilationUnitBuilder builder(native_module);
for (uint32_t i = start; i < num_funcs; ++i) {
const WasmFunction* func = &functions[i];
uint32_t buffer_offset = func->code.offset();
......@@ -1111,7 +1105,7 @@ void UpdateAllCompiledModulesWithTopTierCode(
void CompileInParallel(Isolate* isolate, NativeModule* native_module,
const ModuleWireBytes& wire_bytes, ModuleEnv* module_env,
Handle<WasmModuleObject> module_object,
Handle<Code> centry_stub, ErrorThrower* thrower) {
ErrorThrower* thrower) {
const WasmModule* module = module_env->module;
// Data structures for the parallel compilation.
......@@ -1149,13 +1143,11 @@ void CompileInParallel(Isolate* isolate, NativeModule* native_module,
compilation_state->SetWireBytes(wire_bytes);
DeferredHandles* deferred_handles = nullptr;
Handle<Code> centry_deferred = centry_stub;
Handle<WasmModuleObject> module_object_deferred;
if (compilation_state->compile_mode() == CompileMode::kTiering) {
// Open a deferred handle scope for the centry_stub, in order to allow
// Open a deferred handle scope for the module_object, in order to allow
// for background tiering compilation.
DeferredHandleScope deferred(isolate);
centry_deferred = Handle<Code>(*centry_stub, isolate);
module_object_deferred = handle(*module_object, isolate);
deferred_handles = deferred.Detach();
}
......@@ -1203,7 +1195,7 @@ void CompileInParallel(Isolate* isolate, NativeModule* native_module,
// {BackgroundCompileTask} instances are spawned which run on
// background threads.
InitializeCompilationUnits(module->functions, compilation_state->wire_bytes(),
module, centry_deferred, native_module);
module, native_module);
// 2.a) The background threads and the main thread pick one compilation
// unit at a time and execute the parallel phase of the compilation
......@@ -1295,7 +1287,6 @@ MaybeHandle<WasmModuleObject> CompileToModuleObjectInternal(
const ModuleWireBytes& wire_bytes, Handle<Script> asm_js_script,
Vector<const byte> asm_js_offset_table_bytes) {
WasmModule* wasm_module = module.get();
Handle<Code> centry_stub = CodeFactory::CEntry(isolate);
TimedHistogramScope wasm_compile_module_time_scope(
SELECT_WASM_COUNTER(isolate->async_counters(), wasm_module->origin,
wasm_compile, module_time));
......@@ -1389,7 +1380,7 @@ MaybeHandle<WasmModuleObject> CompileToModuleObjectInternal(
if (compile_parallel) {
CompileInParallel(isolate, native_module, wire_bytes, &env, module_object,
centry_stub, thrower);
thrower);
} else {
CompileSequentially(isolate, native_module, wire_bytes, &env, thrower);
}
......@@ -3030,10 +3021,6 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
// is done.
job_->background_task_manager_.CancelAndWait();
Isolate* isolate = job_->isolate_;
job_->centry_stub_ = CodeFactory::CEntry(isolate);
DCHECK_LE(module_->num_imported_functions, module_->functions.size());
// Create the compiled module object and populate with compiled functions
// and information needed at instantiation time. This object needs to be
......@@ -3127,7 +3114,7 @@ class AsyncCompileJob::PrepareAndStartCompile : public CompileStep {
compilation_state->SetNumberOfFunctionsToCompile(functions_count);
// Add compilation units and kick off compilation.
InitializeCompilationUnits(module_->functions, job_->wire_bytes_,
env.module, job_->centry_stub_,
env.module,
job_->compiled_module_->GetNativeModule());
}
}
......@@ -3323,8 +3310,7 @@ bool AsyncStreamingProcessor::ProcessCodeSectionHeader(size_t functions_count,
// Set outstanding_finishers_ to 2, because both the AsyncCompileJob and the
// AsyncStreamingProcessor have to finish.
job_->outstanding_finishers_.SetValue(2);
compilation_unit_builder_.reset(
new CompilationUnitBuilder(native_module, job_->centry_stub_));
compilation_unit_builder_.reset(new CompilationUnitBuilder(native_module));
return true;
}
......
......@@ -158,7 +158,6 @@ class AsyncCompileJob {
std::unique_ptr<CompileStep> step_;
CancelableTaskManager background_task_manager_;
Handle<Code> centry_stub_;
std::shared_ptr<v8::TaskRunner> foreground_task_runner_;
......
......@@ -159,6 +159,7 @@ OPTIONAL_ACCESSORS(WasmInstanceObject, managed_indirect_patcher, Foreign,
kManagedIndirectPatcherOffset)
ACCESSORS(WasmInstanceObject, undefined_value, Oddball, kUndefinedValueOffset)
ACCESSORS(WasmInstanceObject, null_value, Oddball, kNullValueOffset)
ACCESSORS(WasmInstanceObject, centry_stub, Code, kCEntryStubOffset)
inline bool WasmInstanceObject::has_indirect_function_table() {
return indirect_function_table_sig_ids() != nullptr;
......
......@@ -7,6 +7,7 @@
#include "src/assembler-inl.h"
#include "src/base/iterator.h"
#include "src/code-factory.h"
#include "src/compiler/wasm-compiler.h"
#include "src/debug/debug-interface.h"
#include "src/objects-inl.h"
......@@ -796,6 +797,9 @@ Handle<WasmInstanceObject> WasmInstanceObject::New(
isolate->factory()->NewFixedArray(num_imported_functions);
instance->set_imported_function_callables(*imported_function_callables);
Handle<Code> centry_stub = CodeFactory::CEntry(isolate);
instance->set_centry_stub(*centry_stub);
instance->SetRawMemory(nullptr, 0);
instance->set_stack_limit_address(
isolate->stack_guard()->address_of_jslimit());
......
......@@ -290,6 +290,7 @@ class WasmInstanceObject : public JSObject {
DECL_OPTIONAL_ACCESSORS(managed_indirect_patcher, Foreign)
DECL_ACCESSORS(undefined_value, Oddball)
DECL_ACCESSORS(null_value, Oddball)
DECL_ACCESSORS(centry_stub, Code)
DECL_PRIMITIVE_ACCESSORS(memory_start, byte*)
DECL_PRIMITIVE_ACCESSORS(memory_size, uint32_t)
DECL_PRIMITIVE_ACCESSORS(memory_mask, uint32_t)
......@@ -323,6 +324,7 @@ class WasmInstanceObject : public JSObject {
V(kManagedIndirectPatcherOffset, kPointerSize) \
V(kUndefinedValueOffset, kPointerSize) \
V(kNullValueOffset, kPointerSize) \
V(kCEntryStubOffset, kPointerSize) \
V(kFirstUntaggedOffset, 0) /* marker */ \
V(kMemoryStartOffset, kPointerSize) /* untagged */ \
V(kMemorySizeOffset, kUInt32Size) /* untagged */ \
......
......@@ -5,7 +5,6 @@
#include "test/cctest/wasm/wasm-run-utils.h"
#include "src/assembler-inl.h"
#include "src/code-factory.h"
#include "src/wasm/wasm-memory.h"
#include "src/wasm/wasm-objects-inl.h"
......@@ -272,7 +271,6 @@ void TestBuildingGraph(Zone* zone, compiler::JSGraph* jsgraph,
compiler::SourcePositionTable* source_position_table,
const byte* start, const byte* end) {
compiler::WasmGraphBuilder builder(module, zone, jsgraph,
CodeFactory::CEntry(jsgraph->isolate(), 1),
sig, source_position_table);
TestBuildingGraphWithBuilder(&builder, zone, sig, start, end);
}
......@@ -436,8 +434,7 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
? WasmCompilationUnit::CompilationMode::kLiftoff
: WasmCompilationUnit::CompilationMode::kTurbofan;
WasmCompilationUnit unit(isolate(), &module_env, native_module, func_body,
func_name, function_->func_index,
CodeFactory::CEntry(isolate(), 1), comp_mode,
func_name, function_->func_index, comp_mode,
isolate()->counters(), builder_->lower_simd());
unit.ExecuteCompilation();
wasm::WasmCode* wasm_code = unit.FinishCompilation(&thrower);
......
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