Commit d94fbbe0 authored by mtrofin's avatar mtrofin Committed by Commit bot

[wasm] Move WasmCompilationUnit to .h file.

Removed wrapper functions.

BUG=

Review-Url: https://codereview.chromium.org/1994963002
Cr-Commit-Position: refs/heads/master@{#36372}
parent a2956f42
...@@ -2939,180 +2939,133 @@ std::pair<JSGraph*, SourcePositionTable*> BuildGraphForWasmFunction( ...@@ -2939,180 +2939,133 @@ std::pair<JSGraph*, SourcePositionTable*> BuildGraphForWasmFunction(
return std::make_pair(jsgraph, source_position_table); return std::make_pair(jsgraph, source_position_table);
} }
class WasmCompilationUnit { WasmCompilationUnit::WasmCompilationUnit(wasm::ErrorThrower* thrower,
public: Isolate* isolate,
WasmCompilationUnit(wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env,
wasm::ModuleEnv* module_env, const wasm::WasmFunction* function,
const wasm::WasmFunction* function, uint32_t index) uint32_t index)
: thrower_(thrower), : thrower_(thrower),
isolate_(isolate), isolate_(isolate),
module_env_(module_env), module_env_(module_env),
function_(function), function_(function),
graph_zone_(new Zone(isolate->allocator())), graph_zone_(new Zone(isolate->allocator())),
jsgraph_(new (graph_zone()) JSGraph( jsgraph_(new (graph_zone()) JSGraph(
isolate, new (graph_zone()) Graph(graph_zone()), isolate, new (graph_zone()) Graph(graph_zone()),
new (graph_zone()) CommonOperatorBuilder(graph_zone()), nullptr, new (graph_zone()) CommonOperatorBuilder(graph_zone()), nullptr,
nullptr, nullptr, new (graph_zone()) MachineOperatorBuilder(
new (graph_zone()) MachineOperatorBuilder( graph_zone(), MachineType::PointerRepresentation(),
graph_zone(), MachineType::PointerRepresentation(), InstructionSelector::SupportedMachineOperatorFlags()))),
InstructionSelector::SupportedMachineOperatorFlags()))), compilation_zone_(isolate->allocator()),
compilation_zone_(isolate->allocator()), info_(function->name_length != 0
info_(function->name_length != 0 ? module_env->module->GetNameOrNull(function->name_offset,
? module_env->module->GetNameOrNull(function->name_offset, function->name_length)
function->name_length) : ArrayVector("wasm"),
: ArrayVector("wasm"), isolate, &compilation_zone_,
isolate, &compilation_zone_, Code::ComputeFlags(Code::WASM_FUNCTION)),
Code::ComputeFlags(Code::WASM_FUNCTION)), job_(),
job_(), index_(index),
index_(index), ok_(true) {
ok_(true) { // Create and cache this node in the main thread.
// Create and cache this node in the main thread. jsgraph_->CEntryStubConstant(1);
jsgraph_->CEntryStubConstant(1); }
}
void WasmCompilationUnit::ExecuteCompilation() {
Zone* graph_zone() { return graph_zone_.get(); } // TODO(ahaas): The counters are not thread-safe at the moment.
// HistogramTimerScope wasm_compile_function_time_scope(
void ExecuteCompilation() { // isolate_->counters()->wasm_compile_function_time());
// TODO(ahaas): The counters are not thread-safe at the moment. if (FLAG_trace_wasm_compiler) {
// HistogramTimerScope wasm_compile_function_time_scope( OFStream os(stdout);
// isolate_->counters()->wasm_compile_function_time()); os << "Compiling WASM function "
if (FLAG_trace_wasm_compiler) { << wasm::WasmFunctionName(function_, module_env_) << std::endl;
OFStream os(stdout); os << std::endl;
os << "Compiling WASM function " }
<< wasm::WasmFunctionName(function_, module_env_) << std::endl;
os << std::endl; double decode_ms = 0;
} size_t node_count = 0;
double decode_ms = 0; base::SmartPointer<Zone> graph_zone(graph_zone_.Detach());
size_t node_count = 0; std::pair<JSGraph*, SourcePositionTable*> graph_result =
BuildGraphForWasmFunction(jsgraph_, thrower_, isolate_, module_env_,
base::SmartPointer<Zone> graph_zone(graph_zone_.Detach()); function_, &decode_ms);
std::pair<JSGraph*, SourcePositionTable*> graph_result = JSGraph* jsgraph = graph_result.first;
BuildGraphForWasmFunction(jsgraph_, thrower_, isolate_, module_env_, SourcePositionTable* source_positions = graph_result.second;
function_, &decode_ms);
JSGraph* jsgraph = graph_result.first; if (jsgraph == nullptr) {
SourcePositionTable* source_positions = graph_result.second; ok_ = false;
return;
if (jsgraph == nullptr) {
ok_ = false;
return;
}
base::ElapsedTimer pipeline_timer;
if (FLAG_trace_wasm_decode_time) {
node_count = jsgraph->graph()->NodeCount();
pipeline_timer.Start();
}
// Run the compiler pipeline to generate machine code.
CallDescriptor* descriptor = wasm::ModuleEnv::GetWasmCallDescriptor(
&compilation_zone_, function_->sig);
if (jsgraph->machine()->Is32()) {
descriptor =
module_env_->GetI32WasmCallDescriptor(&compilation_zone_, descriptor);
}
job_.Reset(Pipeline::NewWasmCompilationJob(&info_, jsgraph->graph(),
descriptor, source_positions));
ok_ = job_->OptimizeGraph() == CompilationJob::SUCCEEDED;
// TODO(bradnelson): Improve histogram handling of size_t.
// TODO(ahaas): The counters are not thread-safe at the moment.
// isolate_->counters()->wasm_compile_function_peak_memory_bytes()
// ->AddSample(
// static_cast<int>(jsgraph->graph()->zone()->allocation_size()));
if (FLAG_trace_wasm_decode_time) {
double pipeline_ms = pipeline_timer.Elapsed().InMillisecondsF();
PrintF(
"wasm-compilation phase 1 ok: %d bytes, %0.3f ms decode, %zu nodes, "
"%0.3f ms pipeline\n",
static_cast<int>(function_->code_end_offset -
function_->code_start_offset),
decode_ms, node_count, pipeline_ms);
}
} }
Handle<Code> FinishCompilation() { base::ElapsedTimer pipeline_timer;
if (!ok_) { if (FLAG_trace_wasm_decode_time) {
return Handle<Code>::null(); node_count = jsgraph->graph()->NodeCount();
} pipeline_timer.Start();
if (job_->GenerateCode() != CompilationJob::SUCCEEDED) { }
return Handle<Code>::null();
} // Run the compiler pipeline to generate machine code.
base::ElapsedTimer compile_timer; CallDescriptor* descriptor = wasm::ModuleEnv::GetWasmCallDescriptor(
if (FLAG_trace_wasm_decode_time) { &compilation_zone_, function_->sig);
compile_timer.Start(); if (jsgraph->machine()->Is32()) {
} descriptor =
Handle<Code> code = info_.code(); module_env_->GetI32WasmCallDescriptor(&compilation_zone_, descriptor);
DCHECK(!code.is_null()); }
DCHECK(code->deoptimization_data() == nullptr || job_.Reset(Pipeline::NewWasmCompilationJob(&info_, jsgraph->graph(),
code->deoptimization_data()->length() == 0); descriptor, source_positions));
Handle<FixedArray> deopt_data = ok_ = job_->OptimizeGraph() == CompilationJob::SUCCEEDED;
isolate_->factory()->NewFixedArray(2, TENURED); // TODO(bradnelson): Improve histogram handling of size_t.
if (!module_env_->instance->js_object.is_null()) { // TODO(ahaas): The counters are not thread-safe at the moment.
deopt_data->set(0, *module_env_->instance->js_object); // isolate_->counters()->wasm_compile_function_peak_memory_bytes()
} // ->AddSample(
deopt_data->set(1, Smi::FromInt(function_->func_index)); // static_cast<int>(jsgraph->graph()->zone()->allocation_size()));
deopt_data->set_length(2);
code->set_deoptimization_data(*deopt_data);
RecordFunctionCompilation(
Logger::FUNCTION_TAG, &info_, "WASM_function", function_->func_index,
module_env_->module->GetName(function_->name_offset,
function_->name_length));
if (FLAG_trace_wasm_decode_time) {
double compile_ms = compile_timer.Elapsed().InMillisecondsF();
PrintF("wasm-code-generation ok: %d bytes, %0.3f ms code generation\n",
static_cast<int>(function_->code_end_offset -
function_->code_start_offset),
compile_ms);
}
return code; if (FLAG_trace_wasm_decode_time) {
double pipeline_ms = pipeline_timer.Elapsed().InMillisecondsF();
PrintF(
"wasm-compilation phase 1 ok: %d bytes, %0.3f ms decode, %zu nodes, "
"%0.3f ms pipeline\n",
static_cast<int>(function_->code_end_offset -
function_->code_start_offset),
decode_ms, node_count, pipeline_ms);
} }
wasm::ErrorThrower* thrower_;
Isolate* isolate_;
wasm::ModuleEnv* module_env_;
const wasm::WasmFunction* function_;
// The graph zone is deallocated at the end of ExecuteCompilation.
base::SmartPointer<Zone> graph_zone_;
JSGraph* jsgraph_;
Zone compilation_zone_;
CompilationInfo info_;
base::SmartPointer<CompilationJob> job_;
uint32_t index_;
bool ok_;
};
WasmCompilationUnit* CreateWasmCompilationUnit(
wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env,
const wasm::WasmFunction* function, uint32_t index) {
return new WasmCompilationUnit(thrower, isolate, module_env, function, index);
}
void ExecuteCompilation(WasmCompilationUnit* unit) {
unit->ExecuteCompilation();
} }
uint32_t GetIndexOfWasmCompilationUnit(WasmCompilationUnit* unit) { Handle<Code> WasmCompilationUnit::FinishCompilation() {
return unit->index_; if (!ok_) {
} return Handle<Code>::null();
}
if (job_->GenerateCode() != CompilationJob::SUCCEEDED) {
return Handle<Code>::null();
}
base::ElapsedTimer compile_timer;
if (FLAG_trace_wasm_decode_time) {
compile_timer.Start();
}
Handle<Code> code = info_.code();
DCHECK(!code.is_null());
DCHECK(code->deoptimization_data() == nullptr ||
code->deoptimization_data()->length() == 0);
Handle<FixedArray> deopt_data =
isolate_->factory()->NewFixedArray(2, TENURED);
if (!module_env_->instance->js_object.is_null()) {
deopt_data->set(0, *module_env_->instance->js_object);
}
deopt_data->set(1, Smi::FromInt(function_->func_index));
deopt_data->set_length(2);
code->set_deoptimization_data(*deopt_data);
RecordFunctionCompilation(
Logger::FUNCTION_TAG, &info_, "WASM_function", function_->func_index,
module_env_->module->GetName(function_->name_offset,
function_->name_length));
Handle<Code> FinishCompilation(WasmCompilationUnit* unit) { if (FLAG_trace_wasm_decode_time) {
Handle<Code> result = unit->FinishCompilation(); double compile_ms = compile_timer.Elapsed().InMillisecondsF();
delete unit; PrintF("wasm-code-generation ok: %d bytes, %0.3f ms code generation\n",
return result; static_cast<int>(function_->code_end_offset -
} function_->code_start_offset),
compile_ms);
}
// Helper function to compile a single function. return code;
Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate,
wasm::ModuleEnv* module_env,
const wasm::WasmFunction* function) {
WasmCompilationUnit* unit =
CreateWasmCompilationUnit(thrower, isolate, module_env, function, 0);
ExecuteCompilation(unit);
return FinishCompilation(unit);
} }
} // namespace compiler } // namespace compiler
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
// Clients of this interface shouldn't depend on lots of compiler internals. // Clients of this interface shouldn't depend on lots of compiler internals.
// Do not include anything from src/compiler here! // Do not include anything from src/compiler here!
#include "src/compiler.h"
#include "src/wasm/wasm-opcodes.h" #include "src/wasm/wasm-opcodes.h"
#include "src/zone.h" #include "src/zone.h"
...@@ -20,7 +21,6 @@ class JSGraph; ...@@ -20,7 +21,6 @@ class JSGraph;
class Graph; class Graph;
class Operator; class Operator;
class SourcePositionTable; class SourcePositionTable;
class WasmCompilationUnit;
} }
namespace wasm { namespace wasm {
...@@ -35,10 +35,41 @@ typedef compiler::JSGraph TFGraph; ...@@ -35,10 +35,41 @@ typedef compiler::JSGraph TFGraph;
} }
namespace compiler { namespace compiler {
// Compiles a single function, producing a code object. class WasmCompilationUnit final {
Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower, Isolate* isolate, public:
wasm::ModuleEnv* module_env, WasmCompilationUnit(wasm::ErrorThrower* thrower, Isolate* isolate,
const wasm::WasmFunction* function); wasm::ModuleEnv* module_env,
const wasm::WasmFunction* function, uint32_t index);
Zone* graph_zone() { return graph_zone_.get(); }
int index() const { return index_; }
void ExecuteCompilation();
Handle<Code> FinishCompilation();
static Handle<Code> CompileWasmFunction(wasm::ErrorThrower* thrower,
Isolate* isolate,
wasm::ModuleEnv* module_env,
const wasm::WasmFunction* function) {
WasmCompilationUnit unit(thrower, isolate, module_env, function, 0);
unit.ExecuteCompilation();
return unit.FinishCompilation();
}
private:
wasm::ErrorThrower* thrower_;
Isolate* isolate_;
wasm::ModuleEnv* module_env_;
const wasm::WasmFunction* function_;
// The graph zone is deallocated at the end of ExecuteCompilation.
base::SmartPointer<Zone> graph_zone_;
JSGraph* jsgraph_;
Zone compilation_zone_;
CompilationInfo info_;
base::SmartPointer<CompilationJob> job_;
uint32_t index_;
bool ok_;
};
// Wraps a JS function, producing a code object that can be called from WASM. // Wraps a JS function, producing a code object that can be called from WASM.
Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, wasm::ModuleEnv* module, Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, wasm::ModuleEnv* module,
...@@ -53,16 +84,6 @@ Handle<JSFunction> CompileJSToWasmWrapper( ...@@ -53,16 +84,6 @@ Handle<JSFunction> CompileJSToWasmWrapper(
Isolate* isolate, wasm::ModuleEnv* module, Handle<String> name, Isolate* isolate, wasm::ModuleEnv* module, Handle<String> name,
Handle<Code> wasm_code, Handle<JSObject> module_object, uint32_t index); Handle<Code> wasm_code, Handle<JSObject> module_object, uint32_t index);
WasmCompilationUnit* CreateWasmCompilationUnit(
wasm::ErrorThrower* thrower, Isolate* isolate, wasm::ModuleEnv* module_env,
const wasm::WasmFunction* function, uint32_t index);
void ExecuteCompilation(WasmCompilationUnit* unit);
Handle<Code> FinishCompilation(WasmCompilationUnit* unit);
uint32_t GetIndexOfWasmCompilationUnit(WasmCompilationUnit* unit);
// Abstracts details of building TurboFan graph nodes for WASM to separate // Abstracts details of building TurboFan graph nodes for WASM to separate
// the WASM decoder from the internal details of TurboFan. // the WASM decoder from the internal details of TurboFan.
class WasmTrapHelper; class WasmTrapHelper;
......
...@@ -411,7 +411,7 @@ bool FetchAndExecuteCompilationUnit( ...@@ -411,7 +411,7 @@ bool FetchAndExecuteCompilationUnit(
compiler::WasmCompilationUnit* unit = compilation_units->at(index); compiler::WasmCompilationUnit* unit = compilation_units->at(index);
if (unit != nullptr) { if (unit != nullptr) {
compiler::ExecuteCompilation(unit); unit->ExecuteCompilation();
{ {
base::LockGuard<base::Mutex> guard(result_mutex); base::LockGuard<base::Mutex> guard(result_mutex);
executed_units->push(unit); executed_units->push(unit);
...@@ -498,7 +498,7 @@ void InitializeParallelCompilation( ...@@ -498,7 +498,7 @@ void InitializeParallelCompilation(
} }
for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) { for (uint32_t i = FLAG_skip_compiling_wasm_funcs; i < functions.size(); i++) {
compilation_units[i] = compiler::CreateWasmCompilationUnit( compilation_units[i] = new compiler::WasmCompilationUnit(
&thrower, isolate, &module_env, &functions[i], i); &thrower, isolate, &module_env, &functions[i], i);
} }
} }
...@@ -553,8 +553,9 @@ void FinishCompilationUnits( ...@@ -553,8 +553,9 @@ void FinishCompilationUnits(
unit = executed_units.front(); unit = executed_units.front();
executed_units.pop(); executed_units.pop();
} }
int j = compiler::GetIndexOfWasmCompilationUnit(unit); int j = unit->index();
results[j] = compiler::FinishCompilation(unit); results[j] = unit->FinishCompilation();
delete unit;
} }
} }
...@@ -580,8 +581,8 @@ bool FinishCompilation(Isolate* isolate, WasmModule* module, ...@@ -580,8 +581,8 @@ bool FinishCompilation(Isolate* isolate, WasmModule* module,
code = results[i]; code = results[i];
} else { } else {
// Compile the function. // Compile the function.
code = code = compiler::WasmCompilationUnit::CompileWasmFunction(
compiler::CompileWasmFunction(&thrower, isolate, &module_env, &func); &thrower, isolate, &module_env, &func);
} }
if (code.is_null()) { if (code.is_null()) {
thrower.Error("Compilation of #%d:%.*s failed.", i, str.length(), thrower.Error("Compilation of #%d:%.*s failed.", i, str.length(),
...@@ -772,7 +773,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate, ...@@ -772,7 +773,7 @@ MaybeHandle<JSObject> WasmModule::Instantiate(Isolate* isolate,
if (!FinishCompilation(isolate, this, ffi, results, instance, code_table, if (!FinishCompilation(isolate, this, ffi, results, instance, code_table,
thrower, factory, module_env, total_code_size, thrower, factory, module_env, total_code_size,
desc)) { desc)) {
return MaybeHandle<JSObject>(); instance.js_object = Handle<JSObject>::null();
} }
// Patch all direct call sites. // Patch all direct call sites.
...@@ -930,8 +931,8 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) { ...@@ -930,8 +931,8 @@ int32_t CompileAndRunWasmModule(Isolate* isolate, WasmModule* module) {
for (const WasmFunction& func : module->functions) { for (const WasmFunction& func : module->functions) {
DCHECK_EQ(index, func.func_index); DCHECK_EQ(index, func.func_index);
// Compile the function and install it in the code table. // Compile the function and install it in the code table.
Handle<Code> code = Handle<Code> code = compiler::WasmCompilationUnit::CompileWasmFunction(
compiler::CompileWasmFunction(&thrower, isolate, &module_env, &func); &thrower, isolate, &module_env, &func);
if (!code.is_null()) { if (!code.is_null()) {
if (func.exported) { if (func.exported) {
main_code = code; main_code = code;
......
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