Commit cbaa5682 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[wasm] Fix function compilation profiling

We had dangling pointers by storing a raw pointer and then discarding
the unique_ptr holding it alive, and we had lots of redundant
information there.
This CL refactors the interface to take a format string and a variable
number of argument.

R=titzer@chromium.org

Change-Id: I8eb6ccd19d307e2477c97a3e5e7f537b5671a891
Reviewed-on: https://chromium-review.googlesource.com/690196
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#48223}
parent 04afb10f
...@@ -4068,17 +4068,23 @@ Node* WasmGraphBuilder::AtomicOp(wasm::WasmOpcode opcode, Node* const* inputs, ...@@ -4068,17 +4068,23 @@ Node* WasmGraphBuilder::AtomicOp(wasm::WasmOpcode opcode, Node* const* inputs,
return node; return node;
} }
static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, namespace {
Isolate* isolate, Handle<Code> code, bool must_record_function_compilation(Isolate* isolate) {
const char* message, uint32_t index, return isolate->logger()->is_logging_code_events() || isolate->is_profiling();
const wasm::WasmName& module_name, }
const wasm::WasmName& func_name) {
DCHECK(isolate->logger()->is_logging_code_events() || PRINTF_FORMAT(4, 5)
isolate->is_profiling()); void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag,
Isolate* isolate, Handle<Code> code,
const char* format, ...) {
DCHECK(must_record_function_compilation(isolate));
ScopedVector<char> buffer(128); ScopedVector<char> buffer(128);
SNPrintF(buffer, "%s#%d:%.*s:%.*s", message, index, module_name.length(), va_list arguments;
module_name.start(), func_name.length(), func_name.start()); va_start(arguments, format);
int len = VSNPrintF(buffer, format, arguments);
CHECK_LT(0, len);
va_end(arguments);
Handle<String> name_str = Handle<String> name_str =
isolate->factory()->NewStringFromAsciiChecked(buffer.start()); isolate->factory()->NewStringFromAsciiChecked(buffer.start());
Handle<String> script_str = Handle<String> script_str =
...@@ -4088,6 +4094,7 @@ static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag, ...@@ -4088,6 +4094,7 @@ static void RecordFunctionCompilation(CodeEventListener::LogEventsAndTags tag,
PROFILE(isolate, CodeCreateEvent(tag, AbstractCode::cast(*code), *shared, PROFILE(isolate, CodeCreateEvent(tag, AbstractCode::cast(*code), *shared,
*script_str, 0, 0)); *script_str, 0, 0));
} }
} // namespace
Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::WasmModule* module, Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::WasmModule* module,
Handle<Code> wasm_code, uint32_t index, Handle<Code> wasm_code, uint32_t index,
...@@ -4164,13 +4171,12 @@ Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::WasmModule* module, ...@@ -4164,13 +4171,12 @@ Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::WasmModule* module,
buffer.Dispose(); buffer.Dispose();
} }
if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) { if (must_record_function_compilation(isolate)) {
char func_name[32];
SNPrintF(ArrayVector(func_name), "js-to-wasm#%d", func->func_index);
RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code, RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code,
"js-to-wasm", index, wasm::WasmName("export"), "js-to-wasm#%d:%.*s", func->func_index,
CStrVector(func_name)); func_name.length(), func_name.start());
} }
return code; return code;
} }
...@@ -4207,8 +4213,8 @@ void ValidateImportWrapperReferencesImmovables(Handle<Code> wrapper) { ...@@ -4207,8 +4213,8 @@ void ValidateImportWrapperReferencesImmovables(Handle<Code> wrapper) {
Handle<Code> CompileWasmToJSWrapper( Handle<Code> CompileWasmToJSWrapper(
Isolate* isolate, Handle<JSReceiver> target, wasm::FunctionSig* sig, Isolate* isolate, Handle<JSReceiver> target, wasm::FunctionSig* sig,
uint32_t index, Handle<String> module_name, MaybeHandle<String> import_name, uint32_t index, wasm::ModuleOrigin origin,
wasm::ModuleOrigin origin, Handle<FixedArray> global_js_imports_table) { Handle<FixedArray> global_js_imports_table) {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// Create the Graph // Create the Graph
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
...@@ -4297,19 +4303,9 @@ Handle<Code> CompileWasmToJSWrapper( ...@@ -4297,19 +4303,9 @@ Handle<Code> CompileWasmToJSWrapper(
buffer.Dispose(); buffer.Dispose();
} }
} }
if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) { if (must_record_function_compilation(isolate)) {
const char* function_name = nullptr;
size_t function_name_size = 0;
if (!import_name.is_null()) {
Handle<String> handle = import_name.ToHandleChecked();
function_name = handle->ToCString().get();
function_name_size = static_cast<size_t>(handle->length());
}
RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code, RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code,
"wasm-to-js", index, "wasm-to-js#%d", index);
{module_name->ToCString().get(),
static_cast<size_t>(module_name->length())},
{function_name, function_name_size});
} }
return code; return code;
...@@ -4317,8 +4313,6 @@ Handle<Code> CompileWasmToJSWrapper( ...@@ -4317,8 +4313,6 @@ Handle<Code> CompileWasmToJSWrapper(
Handle<Code> CompileWasmToWasmWrapper(Isolate* isolate, Handle<Code> target, Handle<Code> CompileWasmToWasmWrapper(Isolate* isolate, Handle<Code> target,
wasm::FunctionSig* sig, uint32_t index, wasm::FunctionSig* sig, uint32_t index,
Handle<String> module_name,
MaybeHandle<String> import_name,
Address new_wasm_context_address) { Address new_wasm_context_address) {
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
// Create the Graph // Create the Graph
...@@ -4377,18 +4371,8 @@ Handle<Code> CompileWasmToWasmWrapper(Isolate* isolate, Handle<Code> target, ...@@ -4377,18 +4371,8 @@ Handle<Code> CompileWasmToWasmWrapper(Isolate* isolate, Handle<Code> target,
buffer.Dispose(); buffer.Dispose();
} }
if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) { if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) {
std::unique_ptr<char[]> function_name;
size_t function_name_size = 0;
if (!import_name.is_null()) {
Handle<String> handle = import_name.ToHandleChecked();
function_name = handle->ToCString();
function_name_size = static_cast<size_t>(handle->length());
}
RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code, RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code,
"wasm-to-wasm", index, "wasm-to-wasm#%d", index);
{module_name->ToCString().get(),
static_cast<size_t>(module_name->length())},
{function_name.get(), function_name_size});
} }
return code; return code;
...@@ -4422,7 +4406,7 @@ Handle<Code> CompileWasmInterpreterEntry(Isolate* isolate, uint32_t func_index, ...@@ -4422,7 +4406,7 @@ Handle<Code> CompileWasmInterpreterEntry(Isolate* isolate, uint32_t func_index,
{ {
if (FLAG_trace_turbo_graph) { // Simple textual RPO. if (FLAG_trace_turbo_graph) { // Simple textual RPO.
OFStream os(stdout); OFStream os(stdout);
os << "-- Wasm to interpreter graph -- " << std::endl; os << "-- Wasm interpreter entry graph -- " << std::endl;
os << AsRPO(graph); os << AsRPO(graph);
} }
...@@ -4433,7 +4417,8 @@ Handle<Code> CompileWasmInterpreterEntry(Isolate* isolate, uint32_t func_index, ...@@ -4433,7 +4417,8 @@ Handle<Code> CompileWasmInterpreterEntry(Isolate* isolate, uint32_t func_index,
} }
Code::Flags flags = Code::ComputeFlags(Code::WASM_INTERPRETER_ENTRY); Code::Flags flags = Code::ComputeFlags(Code::WASM_INTERPRETER_ENTRY);
EmbeddedVector<char, 32> debug_name; EmbeddedVector<char, 32> debug_name;
int name_len = SNPrintF(debug_name, "wasm-to-interpreter#%d", func_index); int name_len =
SNPrintF(debug_name, "wasm-interpreter-entry#%d", func_index);
DCHECK(name_len > 0 && name_len < debug_name.length()); DCHECK(name_len > 0 && name_len < debug_name.length());
debug_name.Truncate(name_len); debug_name.Truncate(name_len);
DCHECK_EQ('\0', debug_name.start()[debug_name.length()]); DCHECK_EQ('\0', debug_name.start()[debug_name.length()]);
...@@ -4447,11 +4432,9 @@ Handle<Code> CompileWasmInterpreterEntry(Isolate* isolate, uint32_t func_index, ...@@ -4447,11 +4432,9 @@ Handle<Code> CompileWasmInterpreterEntry(Isolate* isolate, uint32_t func_index,
} }
#endif #endif
if (isolate->logger()->is_logging_code_events() || if (must_record_function_compilation(isolate)) {
isolate->is_profiling()) {
RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code, RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate, code,
"wasm-to-interpreter", func_index, "%s", debug_name.start());
wasm::WasmName("module"), debug_name);
} }
} }
...@@ -4715,11 +4698,10 @@ MaybeHandle<Code> WasmCompilationUnit::FinishCompilation( ...@@ -4715,11 +4698,10 @@ MaybeHandle<Code> WasmCompilationUnit::FinishCompilation(
Handle<Code> code = info_->code(); Handle<Code> code = info_->code();
DCHECK(!code.is_null()); DCHECK(!code.is_null());
if (isolate_->logger()->is_logging_code_events() || if (must_record_function_compilation(isolate_)) {
isolate_->is_profiling()) {
RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate_, code, RecordFunctionCompilation(CodeEventListener::FUNCTION_TAG, isolate_, code,
"WASM_function", func_index_, "wasm_function#%d:%.*s", func_index_,
wasm::WasmName("module"), func_name_); func_name_.length(), func_name_.start());
} }
if (FLAG_trace_wasm_decode_time) { if (FLAG_trace_wasm_decode_time) {
......
...@@ -139,8 +139,6 @@ class WasmCompilationUnit final { ...@@ -139,8 +139,6 @@ class WasmCompilationUnit final {
// GCable) in the generated code so that it can reside outside of GCed heap. // GCable) in the generated code so that it can reside outside of GCed heap.
Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, Handle<JSReceiver> target, Handle<Code> CompileWasmToJSWrapper(Isolate* isolate, Handle<JSReceiver> target,
wasm::FunctionSig* sig, uint32_t index, wasm::FunctionSig* sig, uint32_t index,
Handle<String> module_name,
MaybeHandle<String> import_name,
wasm::ModuleOrigin origin, wasm::ModuleOrigin origin,
Handle<FixedArray> global_js_imports_table); Handle<FixedArray> global_js_imports_table);
...@@ -153,8 +151,6 @@ Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::WasmModule* module, ...@@ -153,8 +151,6 @@ Handle<Code> CompileJSToWasmWrapper(Isolate* isolate, wasm::WasmModule* module,
// wasm instances (the WasmContext address must be changed). // wasm instances (the WasmContext address must be changed).
Handle<Code> CompileWasmToWasmWrapper(Isolate* isolate, Handle<Code> target, Handle<Code> CompileWasmToWasmWrapper(Isolate* isolate, Handle<Code> target,
wasm::FunctionSig* sig, uint32_t index, wasm::FunctionSig* sig, uint32_t index,
Handle<String> module_name,
MaybeHandle<String> import_name,
Address new_wasm_context_address); Address new_wasm_context_address);
// Compiles a stub that redirects a call to a wasm function to the wasm // Compiles a stub that redirects a call to a wasm function to the wasm
......
...@@ -1343,7 +1343,6 @@ using WasmInstanceMap = ...@@ -1343,7 +1343,6 @@ using WasmInstanceMap =
Handle<Code> UnwrapExportOrCompileImportWrapper( Handle<Code> UnwrapExportOrCompileImportWrapper(
Isolate* isolate, int index, FunctionSig* sig, Handle<JSReceiver> target, Isolate* isolate, int index, FunctionSig* sig, Handle<JSReceiver> target,
Handle<String> module_name, MaybeHandle<String> import_name,
ModuleOrigin origin, WasmInstanceMap* imported_instances, ModuleOrigin origin, WasmInstanceMap* imported_instances,
Handle<FixedArray> js_imports_table, Handle<WasmInstanceObject> instance) { Handle<FixedArray> js_imports_table, Handle<WasmInstanceObject> instance) {
WasmFunction* other_func = GetWasmFunctionForExport(isolate, target); WasmFunction* other_func = GetWasmFunctionForExport(isolate, target);
...@@ -1367,8 +1366,7 @@ Handle<Code> UnwrapExportOrCompileImportWrapper( ...@@ -1367,8 +1366,7 @@ Handle<Code> UnwrapExportOrCompileImportWrapper(
Address new_wasm_context = Address new_wasm_context =
reinterpret_cast<Address>(imported_instance->wasm_context()); reinterpret_cast<Address>(imported_instance->wasm_context());
Handle<Code> wrapper_code = compiler::CompileWasmToWasmWrapper( Handle<Code> wrapper_code = compiler::CompileWasmToWasmWrapper(
isolate, wasm_code, sig, index, module_name, import_name, isolate, wasm_code, sig, index, new_wasm_context);
new_wasm_context);
// Set the deoptimization data for the WasmToWasm wrapper. // Set the deoptimization data for the WasmToWasm wrapper.
// TODO(wasm): Remove the deoptimization data when we will use tail calls // TODO(wasm): Remove the deoptimization data when we will use tail calls
// for WasmToWasm wrappers. // for WasmToWasm wrappers.
...@@ -1382,8 +1380,7 @@ Handle<Code> UnwrapExportOrCompileImportWrapper( ...@@ -1382,8 +1380,7 @@ Handle<Code> UnwrapExportOrCompileImportWrapper(
} }
// No wasm function or being debugged. Compile a new wrapper for the new // No wasm function or being debugged. Compile a new wrapper for the new
// signature. // signature.
return compiler::CompileWasmToJSWrapper(isolate, target, sig, index, return compiler::CompileWasmToJSWrapper(isolate, target, sig, index, origin,
module_name, import_name, origin,
js_imports_table); js_imports_table);
} }
...@@ -2255,9 +2252,8 @@ int InstanceBuilder::ProcessImports(Handle<FixedArray> code_table, ...@@ -2255,9 +2252,8 @@ int InstanceBuilder::ProcessImports(Handle<FixedArray> code_table,
Handle<Code> import_code = UnwrapExportOrCompileImportWrapper( Handle<Code> import_code = UnwrapExportOrCompileImportWrapper(
isolate_, index, module_->functions[import.index].sig, isolate_, index, module_->functions[import.index].sig,
Handle<JSReceiver>::cast(value), module_name, import_name, Handle<JSReceiver>::cast(value), module_->origin(),
module_->origin(), &imported_wasm_instances, js_imports_table, &imported_wasm_instances, js_imports_table, instance);
instance);
if (import_code.is_null()) { if (import_code.is_null()) {
ReportLinkError("imported function does not match the expected type", ReportLinkError("imported function does not match the expected type",
index, module_name, import_name); index, module_name, import_name);
......
...@@ -97,8 +97,7 @@ uint32_t TestingModuleBuilder::AddJsFunction( ...@@ -97,8 +97,7 @@ uint32_t TestingModuleBuilder::AddJsFunction(
uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr); uint32_t index = AddFunction(sig, Handle<Code>::null(), nullptr);
js_imports_table->set(0, *isolate_->native_context()); js_imports_table->set(0, *isolate_->native_context());
Handle<Code> code = compiler::CompileWasmToJSWrapper( Handle<Code> code = compiler::CompileWasmToJSWrapper(
isolate_, jsfunc, sig, index, Handle<String>::null(), isolate_, jsfunc, sig, index, test_module_.origin(), js_imports_table);
Handle<String>::null(), test_module_.origin(), js_imports_table);
function_code_[index] = code; function_code_[index] = code;
return index; return index;
} }
......
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