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

Reland "[wasm] Add a separate CodeTracer to the WasmEngine."

This is a reland of b7f0951f

Original change's description:
> [wasm] Add a separate CodeTracer to the WasmEngine.
> 
> This makes sure the TurboFan pipeline is independent of the Isolate by
> getting the CodeTracer from the WasmEngine for WebAssembly compilations.
> 
> R=clemensh@chromium.org
> 
> Change-Id: I343af1a2bfaeff77e2f41ef0c53fbfe165e2e202
> Reviewed-on: https://chromium-review.googlesource.com/1134997
> Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
> Reviewed-by: Clemens Hammacher <clemensh@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#54455}

Change-Id: I48b161b5f35dd388fd3ef299afe04214a666b5a6
Reviewed-on: https://chromium-review.googlesource.com/1138114
Commit-Queue: Michael Starzinger <mstarzinger@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54462}
parent 0fb4f6a2
......@@ -21,11 +21,13 @@ class CodeTracer final : public Malloced {
return;
}
if (FLAG_redirect_code_traces_to == nullptr) {
if (FLAG_redirect_code_traces_to != nullptr) {
StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
} else if (isolate_id >= 0) {
SNPrintF(filename_, "code-%d-%d.asm", base::OS::GetCurrentProcessId(),
isolate_id);
} else {
StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
SNPrintF(filename_, "code-%d.asm", base::OS::GetCurrentProcessId());
}
WriteChars(filename_.start(), "", 0, false);
......
......@@ -141,14 +141,15 @@ class PipelineData {
// For WebAssembly compile entry point.
PipelineData(ZoneStats* zone_stats, Isolate* isolate,
OptimizedCompilationInfo* info, MachineGraph* mcgraph,
PipelineStatistics* pipeline_statistics,
wasm::WasmEngine* wasm_engine, OptimizedCompilationInfo* info,
MachineGraph* mcgraph, PipelineStatistics* pipeline_statistics,
SourcePositionTable* source_positions,
NodeOriginTable* node_origins,
WasmCompilationData* wasm_compilation_data,
int wasm_function_index,
const AssemblerOptions& assembler_options)
: isolate_(isolate),
wasm_engine_(wasm_engine),
info_(info),
debug_name_(info_->GetDebugName()),
wasm_function_index_(wasm_function_index),
......@@ -295,6 +296,11 @@ class PipelineData {
return jump_optimization_info_;
}
CodeTracer* GetCodeTracer() const {
return wasm_engine_ == nullptr ? isolate_->GetCodeTracer()
: wasm_engine_->GetCodeTracer();
}
void DeleteGraphZone() {
if (graph_zone_ == nullptr) return;
graph_zone_scope_.Destroy();
......@@ -407,6 +413,7 @@ class PipelineData {
private:
Isolate* const isolate_;
wasm::WasmEngine* const wasm_engine_ = nullptr;
OptimizedCompilationInfo* const info_;
std::unique_ptr<char[]> debug_name_;
int wasm_function_index_ = -1;
......@@ -650,7 +657,7 @@ struct TurboCfgFile : public std::ofstream {
std::ios_base::app) {}
};
void TraceSchedule(OptimizedCompilationInfo* info, Isolate* isolate,
void TraceSchedule(OptimizedCompilationInfo* info, PipelineData* data,
Schedule* schedule, const char* phase_name) {
if (info->trace_turbo_json_enabled()) {
AllowHandleDereference allow_deref;
......@@ -667,7 +674,7 @@ void TraceSchedule(OptimizedCompilationInfo* info, Isolate* isolate,
}
if (info->trace_turbo_graph_enabled() || FLAG_trace_turbo_scheduler) {
AllowHandleDereference allow_deref;
CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "-- Schedule --------------------------------------\n" << *schedule;
}
......@@ -987,9 +994,10 @@ class PipelineWasmCompilationJob final : public OptimizedCompilationJob {
zone_stats_(isolate->allocator()),
pipeline_statistics_(CreatePipelineStatistics(
wasm_engine, function_body, wasm_module, info, &zone_stats_)),
data_(&zone_stats_, isolate, info, mcgraph, pipeline_statistics_.get(),
source_positions, node_origins, wasm_compilation_data,
function_index, WasmAssemblerOptions(isolate)),
data_(&zone_stats_, isolate, wasm_engine, info, mcgraph,
pipeline_statistics_.get(), source_positions, node_origins,
wasm_compilation_data, function_index,
WasmAssemblerOptions(isolate)),
pipeline_(&data_),
linkage_(call_descriptor),
native_module_(native_module),
......@@ -1446,7 +1454,7 @@ struct EffectControlLinearizationPhase {
Schedule* schedule = Scheduler::ComputeSchedule(temp_zone, data->graph(),
Scheduler::kTempSchedule);
if (FLAG_turbo_verify) ScheduleVerifier::Run(schedule);
TraceSchedule(data->info(), data->isolate(), schedule,
TraceSchedule(data->info(), data, schedule,
"effect linearization schedule");
EffectControlLinearizer::MaskArrayIndexEnable mask_array_index =
......@@ -1894,13 +1902,13 @@ struct PrintGraphPhase {
}
AllowHandleDereference allow_deref;
CodeTracer::Scope tracing_scope(data->isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "-- Graph after " << phase << " -- " << std::endl;
os << AsScheduledGraph(schedule);
} else if (info->trace_turbo_graph_enabled()) { // Simple textual RPO.
AllowHandleDereference allow_deref;
CodeTracer::Scope tracing_scope(data->isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "-- Graph after " << phase << " -- " << std::endl;
os << AsRPO(*graph);
......@@ -1949,7 +1957,7 @@ bool PipelineImpl::CreateGraph() {
if (info()->trace_turbo_json_enabled() ||
info()->trace_turbo_graph_enabled()) {
CodeTracer::Scope tracing_scope(isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "---------------------------------------------------\n"
<< "Begin compiling method " << info()->GetDebugName().get()
......@@ -2133,7 +2141,7 @@ MaybeHandle<Code> Pipeline::GenerateCodeForCodeStub(
DCHECK_NOT_NULL(data.schedule());
if (info.trace_turbo_json_enabled() || info.trace_turbo_graph_enabled()) {
CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
CodeTracer::Scope tracing_scope(data.GetCodeTracer());
OFStream os(tracing_scope.file());
os << "---------------------------------------------------\n"
<< "Begin compiling " << debug_name << " using Turbofan" << std::endl;
......@@ -2148,7 +2156,7 @@ MaybeHandle<Code> Pipeline::GenerateCodeForCodeStub(
pipeline.Run<PrintGraphPhase>("Machine");
}
TraceSchedule(data.info(), data.isolate(), data.schedule(), "schedule");
TraceSchedule(data.info(), &data, data.schedule(), "schedule");
pipeline.Run<VerifyGraphPhase>(false, true);
return pipeline.GenerateCode(call_descriptor);
......@@ -2269,7 +2277,7 @@ void PipelineImpl::ComputeScheduledGraph() {
RunPrintAndVerify(LateGraphTrimmingPhase::phase_name(), true);
Run<ComputeSchedulePhase>();
TraceSchedule(data->info(), data->isolate(), data->schedule(), "schedule");
TraceSchedule(data->info(), data, data->schedule(), "schedule");
}
bool PipelineImpl::SelectInstructions(Linkage* linkage) {
......@@ -2300,7 +2308,7 @@ bool PipelineImpl::SelectInstructions(Linkage* linkage) {
!strcmp(FLAG_turbo_verify_machine_graph, data->debug_name())))) {
if (FLAG_trace_verify_csa) {
AllowHandleDereference allow_deref;
CodeTracer::Scope tracing_scope(data->isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "--------------------------------------------------\n"
<< "--- Verifying " << data->debug_name() << " generated by TurboFan\n"
......@@ -2504,7 +2512,7 @@ MaybeHandle<Code> PipelineImpl::FinalizeCode() {
}
if (info()->trace_turbo_json_enabled() ||
info()->trace_turbo_graph_enabled()) {
CodeTracer::Scope tracing_scope(isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "---------------------------------------------------\n"
<< "Finished compiling method " << info()->GetDebugName().get()
......@@ -2556,7 +2564,7 @@ void PipelineImpl::AllocateRegisters(const RegisterConfiguration* config,
Run<BuildLiveRangesPhase>();
if (info()->trace_turbo_graph_enabled()) {
AllowHandleDereference allow_deref;
CodeTracer::Scope tracing_scope(isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "----- Instruction sequence before register allocation -----\n"
<< PrintableInstructionSequence({config, data->sequence()});
......@@ -2600,7 +2608,7 @@ void PipelineImpl::AllocateRegisters(const RegisterConfiguration* config,
if (info()->trace_turbo_graph_enabled()) {
AllowHandleDereference allow_deref;
CodeTracer::Scope tracing_scope(isolate()->GetCodeTracer());
CodeTracer::Scope tracing_scope(data->GetCodeTracer());
OFStream os(tracing_scope.file());
os << "----- Instruction sequence after register allocation -----\n"
<< PrintableInstructionSequence({config, data->sequence()});
......
......@@ -3344,7 +3344,9 @@ int Shell::Main(int argc, char* argv[]) {
if (i::FLAG_trace_turbo_cfg_file == nullptr) {
SetFlagsFromString("--trace-turbo-cfg-file=turbo.cfg");
}
SetFlagsFromString("--redirect-code-traces-to=code.asm");
if (i::FLAG_redirect_code_traces_to == nullptr) {
SetFlagsFromString("--redirect-code-traces-to=code.asm");
}
int result = 0;
Isolate::CreateParams create_params;
ShellArrayBufferAllocator shell_array_buffer_allocator;
......
......@@ -4,6 +4,7 @@
#include "src/wasm/wasm-engine.h"
#include "src/code-tracer.h"
#include "src/compilation-statistics.h"
#include "src/objects-inl.h"
#include "src/objects/js-promise.h"
......@@ -158,6 +159,7 @@ std::shared_ptr<StreamingDecoder> WasmEngine::StartStreamingCompilation(
}
CompilationStatistics* WasmEngine::GetOrCreateTurboStatistics() {
base::LockGuard<base::Mutex> guard(&mutex_);
if (compilation_stats_ == nullptr) {
compilation_stats_.reset(new CompilationStatistics());
}
......@@ -165,6 +167,7 @@ CompilationStatistics* WasmEngine::GetOrCreateTurboStatistics() {
}
void WasmEngine::DumpAndResetTurboStatistics() {
base::LockGuard<base::Mutex> guard(&mutex_);
if (compilation_stats_ != nullptr) {
StdoutStream os;
os << AsPrintableStatistics{*compilation_stats_.get(), false} << std::endl;
......@@ -172,6 +175,12 @@ void WasmEngine::DumpAndResetTurboStatistics() {
compilation_stats_.reset();
}
CodeTracer* WasmEngine::GetCodeTracer() {
base::LockGuard<base::Mutex> guard(&mutex_);
if (code_tracer_ == nullptr) code_tracer_.reset(new CodeTracer(-1));
return code_tracer_.get();
}
void WasmEngine::Register(CancelableTaskManager* task_manager) {
task_managers_.emplace_back(task_manager);
}
......
......@@ -14,6 +14,7 @@
namespace v8 {
namespace internal {
class CodeTracer;
class CompilationStatistics;
class WasmModuleObject;
class WasmInstanceObject;
......@@ -99,6 +100,9 @@ class V8_EXPORT_PRIVATE WasmEngine {
// Prints the gathered compilation statistics, then resets them.
void DumpAndResetTurboStatistics();
// Used to redirect tracing output from {stdout} to a file.
CodeTracer* GetCodeTracer();
// We register and unregister CancelableTaskManagers that run engine-dependent
// tasks. These tasks need to be shutdown if the engine is shut down.
void Register(CancelableTaskManager* task_manager);
......@@ -127,7 +131,6 @@ class V8_EXPORT_PRIVATE WasmEngine {
// job from the map when it is finished.
std::unordered_map<AsyncCompileJob*, std::unique_ptr<AsyncCompileJob>> jobs_;
std::unique_ptr<WasmCodeManager> code_manager_;
std::unique_ptr<CompilationStatistics> compilation_stats_;
WasmMemoryTracker memory_tracker_;
AccountingAllocator allocator_;
......@@ -135,6 +138,19 @@ class V8_EXPORT_PRIVATE WasmEngine {
// on the isolate.
std::list<CancelableTaskManager*> task_managers_;
// This mutex protects all information which is mutated concurrently or
// fields that are initialized lazily on the first access.
base::Mutex mutex_;
//////////////////////////////////////////////////////////////////////////////
// Protected by {mutex_}:
std::unique_ptr<CompilationStatistics> compilation_stats_;
std::unique_ptr<CodeTracer> code_tracer_;
// End of fields protected by {mutex_}.
//////////////////////////////////////////////////////////////////////////////
DISALLOW_COPY_AND_ASSIGN(WasmEngine);
};
......
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