Commit 4ff2cdf3 authored by Stephan Herhut's avatar Stephan Herhut Committed by Commit Bot

[wasm] Produce WasmCode as result of the compilation pipeline

This moves the generation of the WasmCode structure into the
PipelineWasmCompilationJob, removing WasmCodeDesc in the process.
WasmCodeDesc was a structure that was not understood by other parts of
the compiler, including the disassembler. Using WasmCode right away
enables printing code comments for turbolizer.

Change-Id: Ie5cca131829bc842c51c999ea14d0dc339b3e028
Reviewed-on: https://chromium-review.googlesource.com/1073312
Commit-Queue: Stephan Herhut <herhut@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#53490}
parent 8c7d4fb8
......@@ -35,6 +35,16 @@ class CodeReference {
return kind_ == JS ? js_code_.is_null() : wasm_code_ == nullptr;
}
Handle<Code> as_js_code() const {
CHECK_EQ(JS, kind_);
return js_code_;
}
const wasm::WasmCode* as_wasm_code() const {
CHECK_EQ(WASM, kind_);
return wasm_code_;
}
private:
enum { JS, WASM } kind_;
union {
......
......@@ -69,6 +69,7 @@
#include "src/compiler/typer.h"
#include "src/compiler/value-numbering-reducer.h"
#include "src/compiler/verifier.h"
#include "src/compiler/wasm-compiler.h"
#include "src/compiler/zone-stats.h"
#include "src/disassembler.h"
#include "src/isolate-inl.h"
......@@ -137,10 +138,12 @@ class PipelineData {
PipelineStatistics* pipeline_statistics,
SourcePositionTable* source_positions,
NodeOriginTable* node_origins,
WasmCompilationData* wasm_compilation_data)
WasmCompilationData* wasm_compilation_data,
int wasm_function_index)
: isolate_(isolate),
info_(info),
debug_name_(info_->GetDebugName()),
wasm_function_index_(wasm_function_index),
zone_stats_(zone_stats),
pipeline_statistics_(pipeline_statistics),
graph_zone_scope_(zone_stats_, ZONE_NAME),
......@@ -378,10 +381,17 @@ class PipelineData {
const char* debug_name() const { return debug_name_.get(); }
WasmCompilationData* wasm_compilation_data() const {
return wasm_compilation_data_;
}
int wasm_function_index() const { return wasm_function_index_; }
private:
Isolate* const isolate_;
OptimizedCompilationInfo* const info_;
std::unique_ptr<char[]> debug_name_;
int wasm_function_index_ = -1;
bool may_have_unverifiable_graph_ = true;
ZoneStats* const zone_stats_;
PipelineStatistics* pipeline_statistics_ = nullptr;
......@@ -939,16 +949,18 @@ class PipelineWasmCompilationJob final : public OptimizedCompilationJob {
CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data,
wasm::FunctionBody function_body, wasm::WasmModule* wasm_module,
bool asmjs_origin)
wasm::NativeModule* native_module, int function_index, bool asmjs_origin)
: OptimizedCompilationJob(isolate->stack_guard()->real_climit(), info,
"TurboFan", State::kReadyToExecute),
zone_stats_(isolate->allocator()),
pipeline_statistics_(CreatePipelineStatistics(
function_body, wasm_module, info, isolate, &zone_stats_)),
data_(&zone_stats_, isolate, info, mcgraph, pipeline_statistics_.get(),
source_positions, node_origins, wasm_compilation_data),
source_positions, node_origins, wasm_compilation_data,
function_index),
pipeline_(&data_),
linkage_(call_descriptor),
native_module_(native_module),
asmjs_origin_(asmjs_origin) {}
protected:
......@@ -964,6 +976,7 @@ class PipelineWasmCompilationJob final : public OptimizedCompilationJob {
PipelineData data_;
PipelineImpl pipeline_;
Linkage linkage_;
wasm::NativeModule* native_module_;
bool asmjs_origin_;
};
......@@ -1015,27 +1028,27 @@ size_t PipelineWasmCompilationJob::AllocatedMemory() const {
PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::FinalizeJobImpl(
Isolate* isolate) {
CodeGenerator* code_generator = pipeline_.data_->code_generator();
OptimizedCompilationInfo::WasmCodeDesc* wasm_code_desc =
compilation_info()->wasm_code_desc();
code_generator->tasm()->GetCode(isolate, &wasm_code_desc->code_desc);
wasm_code_desc->safepoint_table_offset =
code_generator->GetSafepointTableOffset();
wasm_code_desc->handler_table_offset =
code_generator->GetHandlerTableOffset();
wasm_code_desc->frame_slot_count =
code_generator->frame()->GetTotalFrameSlotCount();
wasm_code_desc->source_positions_table =
code_generator->GetSourcePositionTable();
CodeDesc code_desc;
code_generator->tasm()->GetCode(isolate, &code_desc);
wasm::WasmCode* code = native_module_->AddCode(
code_desc, code_generator->frame()->GetTotalFrameSlotCount(),
data_.wasm_function_index(), code_generator->GetSafepointTableOffset(),
code_generator->GetHandlerTableOffset(),
data_.wasm_compilation_data()->ReleaseProtectedInstructions(),
code_generator->GetSourcePositionTable(), wasm::WasmCode::kTurbofan);
if (!code) return FAILED;
if (data_.info()->trace_turbo_json_enabled()) {
TurboJsonFile json_of(data_.info(), std::ios_base::app);
json_of << "{\"name\":\"disassembly\",\"type\":\"disassembly\",\"data\":\"";
#ifdef ENABLE_DISASSEMBLER
std::stringstream disassembler_stream;
CodeDesc& code_desc = wasm_code_desc->code_desc;
Disassembler::Decode(
isolate, &disassembler_stream, code_desc.buffer,
code_desc.buffer + wasm_code_desc->safepoint_table_offset);
isolate, &disassembler_stream, code->instructions().start(),
code->instructions().start() + code->safepoint_table_offset(),
CodeReference(code));
for (auto const c : disassembler_stream.str()) {
json_of << AsEscapedUC16ForJSON(c);
}
......@@ -1044,6 +1057,8 @@ PipelineWasmCompilationJob::Status PipelineWasmCompilationJob::FinalizeJobImpl(
json_of << "\n}";
}
compilation_info()->SetCode(code);
return SUCCEEDED;
}
......@@ -2136,10 +2151,12 @@ OptimizedCompilationJob* Pipeline::NewWasmCompilationJob(
CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data,
wasm::FunctionBody function_body, wasm::WasmModule* wasm_module,
wasm::NativeModule* native_module, int function_index,
wasm::ModuleOrigin asmjs_origin) {
return new PipelineWasmCompilationJob(
info, isolate, mcgraph, call_descriptor, source_positions, node_origins,
wasm_compilation_data, function_body, wasm_module, asmjs_origin);
wasm_compilation_data, function_body, wasm_module, native_module,
function_index, asmjs_origin);
}
bool Pipeline::AllocateRegistersForTesting(const RegisterConfiguration* config,
......
......@@ -23,6 +23,7 @@ class JumpOptimizationInfo;
namespace wasm {
enum ModuleOrigin : uint8_t;
struct FunctionBody;
class NativeModule;
struct WasmModule;
} // namespace wasm
......@@ -49,6 +50,7 @@ class Pipeline : public AllStatic {
CallDescriptor* call_descriptor, SourcePositionTable* source_positions,
NodeOriginTable* node_origins, WasmCompilationData* wasm_compilation_data,
wasm::FunctionBody function_body, wasm::WasmModule* wasm_module,
wasm::NativeModule* native_module, int function_index,
wasm::ModuleOrigin wasm_origin);
// Run the pipeline on a machine graph and generate code. The {schedule} must
......
......@@ -5147,6 +5147,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation() {
source_positions, node_origins, &wasm_compilation_data_,
wasm_unit_->func_body_,
const_cast<wasm::WasmModule*>(wasm_unit_->env_->module),
wasm_unit_->native_module_, wasm_unit_->func_index_,
wasm_unit_->env_->module->origin));
ok_ = job_->ExecuteJob() == CompilationJob::SUCCEEDED;
// TODO(bradnelson): Improve histogram handling of size_t.
......@@ -5200,19 +5201,6 @@ wasm::WasmCode* TurbofanWasmCompilationUnit::FinishCompilation(
return nullptr;
}
// TODO(mtrofin): when we crystalize a design in lieu of WasmCodeDesc, that
// works for both wasm and non-wasm, we can simplify AddCode to just take
// that as a parameter.
const CodeDesc& desc = job_->compilation_info()->wasm_code_desc()->code_desc;
wasm::WasmCode* code = wasm_unit_->native_module_->AddCode(
desc, job_->compilation_info()->wasm_code_desc()->frame_slot_count,
wasm_unit_->func_index_,
job_->compilation_info()->wasm_code_desc()->safepoint_table_offset,
job_->compilation_info()->wasm_code_desc()->handler_table_offset,
wasm_compilation_data_.ReleaseProtectedInstructions(),
job_->compilation_info()->wasm_code_desc()->source_positions_table,
wasm::WasmCode::kTurbofan);
if (!code) return code;
if (FLAG_trace_wasm_decode_time) {
double codegen_ms = codegen_timer.Elapsed().InMillisecondsF();
PrintF("wasm-code-generation ok: %u bytes, %0.3f ms code generation\n",
......@@ -5221,7 +5209,7 @@ wasm::WasmCode* TurbofanWasmCompilationUnit::FinishCompilation(
codegen_ms);
}
return code;
return job_->compilation_info()->wasm_code();
}
namespace {
......
......@@ -8,6 +8,7 @@
#include <memory>
#include "src/bailout-reason.h"
#include "src/code-reference.h"
#include "src/compilation-dependencies.h"
#include "src/feedback-vector.h"
#include "src/frames.h"
......@@ -58,18 +59,6 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
kTraceTurboScheduled = 1 << 16,
};
// TODO(mtrofin): investigate if this might be generalized outside wasm, with
// the goal of better separating the compiler from where compilation lands. At
// that point, the Handle<Code> member of OptimizedCompilationInfo would also
// be removed.
struct WasmCodeDesc {
CodeDesc code_desc;
size_t safepoint_table_offset = 0;
size_t handler_table_offset = 0;
uint32_t frame_slot_count = 0;
Handle<ByteArray> source_positions_table;
};
// Construct a compilation info for optimized compilation.
OptimizedCompilationInfo(Zone* zone, Isolate* isolate,
Handle<SharedFunctionInfo> shared,
......@@ -85,7 +74,11 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
Handle<SharedFunctionInfo> shared_info() const { return shared_info_; }
bool has_shared_info() const { return !shared_info().is_null(); }
Handle<JSFunction> closure() const { return closure_; }
Handle<Code> code() const { return code_; }
Handle<Code> code() const { return code_.as_js_code(); }
wasm::WasmCode* wasm_code() const {
return const_cast<wasm::WasmCode*>(code_.as_wasm_code());
}
AbstractCode::Kind abstract_code_kind() const { return code_kind_; }
Code::Kind code_kind() const {
DCHECK(code_kind_ < static_cast<AbstractCode::Kind>(Code::NUMBER_OF_KINDS));
......@@ -184,7 +177,10 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
// Code getters and setters.
void SetCode(Handle<Code> code) { code_ = code; }
template <typename T>
void SetCode(T code) {
code_ = CodeReference(code);
}
bool has_context() const;
Context* context() const;
......@@ -270,8 +266,6 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
StackFrame::Type GetOutputStackFrameType() const;
WasmCodeDesc* wasm_code_desc() { return &wasm_code_desc_; }
private:
OptimizedCompilationInfo(Vector<const char> debug_name,
AbstractCode::Kind code_kind, Zone* zone);
......@@ -295,8 +289,7 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
Handle<JSFunction> closure_;
// The compiled code.
Handle<Code> code_;
WasmCodeDesc wasm_code_desc_;
CodeReference code_;
// Entry point when compiling for OSR, {BailoutId::None} otherwise.
BailoutId osr_offset_;
......
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