Commit d6b5ac8d authored by Stephan Herhut's avatar Stephan Herhut Committed by Commit Bot

[wasm] Use wasm-function#%d consistently as debug name.

This also fixes a use where it should be a public name. For public
names, we use what is defined in the module or wasm-function[%d] as per
the wasm names spec.

Bug: v8:8015
Change-Id: Ie102db4e1114b20caeb4a990cb9e07cacf0666bc
Reviewed-on: https://chromium-review.googlesource.com/1215627Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Commit-Queue: Stephan Herhut <herhut@chromium.org>
Cr-Commit-Position: refs/heads/master@{#55827}
parent 4bbb7c4e
......@@ -4776,6 +4776,23 @@ class WasmWrapperGraphBuilder : public WasmGraphBuilder {
StubCallMode stub_mode_;
SetOncePointer<const Operator> allocate_heap_number_operator_;
};
void AppendSignature(char* buffer, size_t max_name_len,
wasm::FunctionSig* sig) {
size_t name_len = strlen(buffer);
auto append_name_char = [&](char c) {
if (name_len + 1 < max_name_len) buffer[name_len++] = c;
};
for (wasm::ValueType t : sig->parameters()) {
append_name_char(wasm::ValueTypes::ShortNameOf(t));
}
append_name_char(':');
for (wasm::ValueType t : sig->returns()) {
append_name_char(wasm::ValueTypes::ShortNameOf(t));
}
buffer[name_len] = '\0';
}
} // namespace
MaybeHandle<Code> CompileJSToWasmWrapper(
......@@ -4811,13 +4828,9 @@ MaybeHandle<Code> CompileJSToWasmWrapper(
//----------------------------------------------------------------------------
// Run the compilation pipeline.
//----------------------------------------------------------------------------
#ifdef DEBUG
EmbeddedVector<char, 32> func_name;
static unsigned id = 0;
func_name.Truncate(SNPrintF(func_name, "js-to-wasm#%d", id++));
#else
Vector<const char> func_name = CStrVector("js-to-wasm");
#endif
static constexpr size_t kMaxNameLen = 128;
char debug_name[kMaxNameLen] = "js_to_wasm:";
AppendSignature(debug_name, kMaxNameLen, sig);
// Schedule and compile to machine code.
int params = static_cast<int>(sig->parameter_count());
......@@ -4825,7 +4838,7 @@ MaybeHandle<Code> CompileJSToWasmWrapper(
&zone, false, params + 1, CallDescriptor::kNoFlags);
MaybeHandle<Code> maybe_code = Pipeline::GenerateCodeForWasmStub(
isolate, incoming, &graph, Code::JS_TO_WASM_FUNCTION, func_name.start(),
isolate, incoming, &graph, Code::JS_TO_WASM_FUNCTION, debug_name,
WasmAssemblerOptions());
Handle<Code> code;
if (!maybe_code.ToHandle(&code)) {
......@@ -4835,13 +4848,13 @@ MaybeHandle<Code> CompileJSToWasmWrapper(
if (FLAG_print_opt_code) {
CodeTracer::Scope tracing_scope(isolate->GetCodeTracer());
OFStream os(tracing_scope.file());
code->Disassemble(func_name.start(), os);
code->Disassemble(debug_name, os);
}
#endif
if (must_record_function_compilation(isolate)) {
RecordFunctionCompilation(CodeEventListener::STUB_TAG, isolate, code,
"%.*s", func_name.length(), func_name.start());
RecordFunctionCompilation(CodeEventListener::STUB_TAG, isolate, code, "%s",
debug_name);
}
return code;
......@@ -4882,13 +4895,8 @@ MaybeHandle<Code> CompileWasmToJSWrapper(
builder.set_effect_ptr(&effect);
builder.BuildWasmToJSWrapper(target, index);
#ifdef DEBUG
EmbeddedVector<char, 32> func_name;
static unsigned id = 0;
func_name.Truncate(SNPrintF(func_name, "wasm-to-js#%d", id++));
#else
Vector<const char> func_name = CStrVector("wasm-to-js");
#endif
func_name.Truncate(SNPrintF(func_name, "wasm-to-js#%d", index));
// Schedule and compile to machine code.
CallDescriptor* incoming = GetWasmCallDescriptor(&zone, sig);
......@@ -4947,13 +4955,10 @@ MaybeHandle<Code> CompileWasmInterpreterEntry(Isolate* isolate,
if (machine.Is32()) {
incoming = GetI32WasmCallDescriptor(&zone, incoming);
}
#ifdef DEBUG
EmbeddedVector<char, 32> func_name;
func_name.Truncate(
SNPrintF(func_name, "wasm-interpreter-entry#%d", func_index));
#else
Vector<const char> func_name = CStrVector("wasm-interpreter-entry");
#endif
MaybeHandle<Code> maybe_code = Pipeline::GenerateCodeForWasmStub(
isolate, incoming, &graph, Code::WASM_INTERPRETER_ENTRY,
......@@ -5005,18 +5010,7 @@ MaybeHandle<Code> CompileCWasmEntry(Isolate* isolate, wasm::FunctionSig* sig) {
// Build a name in the form "c-wasm-entry:<params>:<returns>".
static constexpr size_t kMaxNameLen = 128;
char debug_name[kMaxNameLen] = "c-wasm-entry:";
size_t name_len = strlen(debug_name);
auto append_name_char = [&](char c) {
if (name_len + 1 < kMaxNameLen) debug_name[name_len++] = c;
};
for (wasm::ValueType t : sig->parameters()) {
append_name_char(wasm::ValueTypes::ShortNameOf(t));
}
append_name_char(':');
for (wasm::ValueType t : sig->returns()) {
append_name_char(wasm::ValueTypes::ShortNameOf(t));
}
debug_name[name_len] = '\0';
AppendSignature(debug_name, kMaxNameLen, sig);
MaybeHandle<Code> maybe_code = Pipeline::GenerateCodeForWasmStub(
isolate, incoming, &graph, Code::C_WASM_ENTRY, debug_name,
......@@ -5091,23 +5085,17 @@ SourcePositionTable* TurbofanWasmCompilationUnit::BuildGraphForWasmFunction(
}
namespace {
Vector<const char> GetDebugName(Zone* zone, wasm::WasmName name, int index) {
if (!name.is_empty()) {
return name;
}
#ifdef DEBUG
constexpr int kBufferLength = 15;
Vector<const char> GetDebugName(Zone* zone, int index) {
// TODO(herhut): Use name from module if available.
constexpr int kBufferLength = 24;
EmbeddedVector<char, kBufferLength> name_vector;
int name_len = SNPrintF(name_vector, "wasm#%d", index);
int name_len = SNPrintF(name_vector, "wasm-function#%d", index);
DCHECK(name_len > 0 && name_len < name_vector.length());
char* index_name = zone->NewArray<char>(name_len);
memcpy(index_name, name_vector.start(), name_len);
return Vector<const char>(index_name, name_len);
#else
return {};
#endif
}
} // namespace
......@@ -5133,8 +5121,7 @@ void TurbofanWasmCompilationUnit::ExecuteCompilation(
Zone compilation_zone(wasm_unit_->wasm_engine_->allocator(), ZONE_NAME);
OptimizedCompilationInfo info(
GetDebugName(&compilation_zone, wasm_unit_->func_name_,
wasm_unit_->func_index_),
GetDebugName(&compilation_zone, wasm_unit_->func_index_),
&compilation_zone, Code::WASM_FUNCTION);
if (wasm_unit_->env_->runtime_exception_support) {
info.SetWasmRuntimeExceptionSupport();
......@@ -5200,16 +5187,22 @@ wasm::WasmCode* TurbofanWasmCompilationUnit::FinishCompilation(
wasm::ErrorThrower* thrower) {
if (!ok_) {
if (graph_construction_result_.failed()) {
// Add the function as another context for the exception.
// Add the function as another context for the exception. This is
// user-visible, so use official format.
EmbeddedVector<char, 128> message;
if (wasm_unit_->func_name_.start() == nullptr) {
SNPrintF(message, "Compiling wasm function #%d failed",
wasm_unit_->func_index_);
wasm::ModuleWireBytes wire_bytes(
wasm_unit_->native_module()->wire_bytes());
wasm::WireBytesRef name_ref =
wasm_unit_->native_module()->module()->LookupFunctionName(
wire_bytes, wasm_unit_->func_index_);
if (name_ref.is_set()) {
wasm::WasmName name = wire_bytes.GetNameOrNull(name_ref);
SNPrintF(message, "Compiling wasm function \"%.*s\" failed",
name.length(), name.start());
} else {
wasm::TruncatedUserString<> trunc_name(wasm_unit_->func_name_);
SNPrintF(message, "Compiling wasm function #%d:%.*s failed",
wasm_unit_->func_index_, trunc_name.length(),
trunc_name.start());
SNPrintF(message,
"Compiling wasm function \"wasm-function[%d]\" failed",
wasm_unit_->func_index_);
}
thrower->CompileFailed(message.start(), graph_construction_result_);
}
......
......@@ -45,13 +45,11 @@ ExecutionTier WasmCompilationUnit::GetDefaultExecutionTier() {
WasmCompilationUnit::WasmCompilationUnit(WasmEngine* wasm_engine,
ModuleEnv* env,
NativeModule* native_module,
FunctionBody body, WasmName name,
int index, Counters* counters,
ExecutionTier mode)
FunctionBody body, int index,
Counters* counters, ExecutionTier mode)
: env_(env),
wasm_engine_(wasm_engine),
func_body_(body),
func_name_(name),
counters_(counters),
func_index_(index),
native_module_(native_module),
......@@ -155,7 +153,6 @@ WasmCode* WasmCompilationUnit::CompileWasmFunction(
WasmCompilationUnit unit(isolate->wasm_engine(), env, native_module,
function_body,
wire_bytes.GetNameOrNull(function, env->module),
function->func_index, isolate->counters(), mode);
unit.ExecuteCompilation(detected);
return unit.FinishCompilation(thrower);
......
......@@ -86,7 +86,7 @@ class WasmCompilationUnit final {
// If used exclusively from a foreground thread, Isolate::counters() may be
// used by callers to pass Counters.
WasmCompilationUnit(WasmEngine* wasm_engine, ModuleEnv*, NativeModule*,
FunctionBody, WasmName, int index, Counters*,
FunctionBody, int index, Counters*,
ExecutionTier = GetDefaultExecutionTier());
~WasmCompilationUnit();
......@@ -109,7 +109,6 @@ class WasmCompilationUnit final {
ModuleEnv* env_;
WasmEngine* wasm_engine_;
FunctionBody func_body_;
WasmName func_name_;
Counters* counters_;
int func_index_;
NativeModule* native_module_;
......
......@@ -349,18 +349,8 @@ WasmCode* LazyCompileFunction(Isolate* isolate, NativeModule* native_module,
compilation_timer.Start();
ModuleEnv* module_env = native_module->compilation_state()->module_env();
// TODO(wasm): Refactor this to only get the name if it is really needed for
// tracing / debugging.
WasmName func_name;
{
ModuleWireBytes wire_bytes(native_module->wire_bytes());
WireBytesRef name_ref =
module_env->module->LookupFunctionName(wire_bytes, func_index);
func_name = wire_bytes.GetNameOrNull(name_ref);
}
TRACE_LAZY("Compiling function '%.*s' (#%d).\n", func_name.length(),
func_name.start(), func_index);
TRACE_LAZY("Compiling wasm-function#%d.\n", func_index);
const uint8_t* module_start = native_module->wire_bytes().start();
......@@ -371,7 +361,7 @@ WasmCode* LazyCompileFunction(Isolate* isolate, NativeModule* native_module,
ErrorThrower thrower(isolate, "WasmLazyCompile");
WasmCompilationUnit unit(isolate->wasm_engine(), module_env, native_module,
body, func_name, func_index, isolate->counters());
body, func_index, isolate->counters());
unit.ExecuteCompilation(
native_module->compilation_state()->detected_features());
WasmCode* wasm_code = unit.FinishCompilation(&thrower);
......@@ -461,17 +451,17 @@ class CompilationUnitBuilder {
compilation_state_(native_module->compilation_state()) {}
void AddUnit(const WasmFunction* function, uint32_t buffer_offset,
Vector<const uint8_t> bytes, WasmName name) {
Vector<const uint8_t> bytes) {
switch (compilation_state_->compile_mode()) {
case CompileMode::kTiering:
tiering_units_.emplace_back(CreateUnit(
function, buffer_offset, bytes, name, ExecutionTier::kOptimized));
baseline_units_.emplace_back(CreateUnit(
function, buffer_offset, bytes, name, ExecutionTier::kBaseline));
tiering_units_.emplace_back(CreateUnit(function, buffer_offset, bytes,
ExecutionTier::kOptimized));
baseline_units_.emplace_back(CreateUnit(function, buffer_offset, bytes,
ExecutionTier::kBaseline));
return;
case CompileMode::kRegular:
baseline_units_.emplace_back(
CreateUnit(function, buffer_offset, bytes, name,
CreateUnit(function, buffer_offset, bytes,
WasmCompilationUnit::GetDefaultExecutionTier()));
return;
}
......@@ -494,13 +484,12 @@ class CompilationUnitBuilder {
std::unique_ptr<WasmCompilationUnit> CreateUnit(const WasmFunction* function,
uint32_t buffer_offset,
Vector<const uint8_t> bytes,
WasmName name,
ExecutionTier mode) {
return base::make_unique<WasmCompilationUnit>(
compilation_state_->wasm_engine(), compilation_state_->module_env(),
native_module_,
FunctionBody{function->sig, buffer_offset, bytes.begin(), bytes.end()},
name, function->func_index,
function->func_index,
compilation_state_->isolate()->async_counters().get(), mode);
}
......@@ -546,10 +535,8 @@ void InitializeCompilationUnits(NativeModule* native_module) {
Vector<const uint8_t> bytes(wire_bytes.start() + func->code.offset(),
func->code.end_offset() - func->code.offset());
// TODO(herhut): Use a more useful name if none exists.
WasmName name = wire_bytes.GetNameOrNull(func, module);
DCHECK_NOT_NULL(native_module);
builder.AddUnit(func, buffer_offset, bytes, name);
builder.AddUnit(func, buffer_offset, bytes);
}
builder.Commit();
}
......@@ -2741,13 +2728,12 @@ bool AsyncStreamingProcessor::ProcessFunctionBody(Vector<const uint8_t> bytes,
uint32_t offset) {
TRACE_STREAMING("Process function body %d ...\n", next_function_);
decoder_.DecodeFunctionBody(
next_function_, static_cast<uint32_t>(bytes.length()), offset, false);
decoder_.DecodeFunctionBody(
next_function_, static_cast<uint32_t>(bytes.length()), offset, false);
uint32_t index = next_function_ + decoder_.module()->num_imported_functions;
const WasmFunction* func = &decoder_.module()->functions[index];
WasmName name = {nullptr, 0};
compilation_unit_builder_->AddUnit(func, offset, bytes, name);
uint32_t index = next_function_ + decoder_.module()->num_imported_functions;
const WasmFunction* func = &decoder_.module()->functions[index];
compilation_unit_builder_->AddUnit(func, offset, bytes);
++next_function_;
// This method always succeeds. The return value is necessary to comply with
// the StreamingProcessor interface.
......
......@@ -419,18 +419,13 @@ void WasmFunctionCompiler::Build(const byte* start, const byte* end) {
ScopedVector<uint8_t> func_wire_bytes(function_->code.length());
memcpy(func_wire_bytes.start(), wire_bytes.start() + function_->code.offset(),
func_wire_bytes.length());
WireBytesRef func_name_ref =
module_env.module->LookupFunctionName(wire_bytes, function_->func_index);
ScopedVector<char> func_name(func_name_ref.length());
memcpy(func_name.start(), wire_bytes.start() + func_name_ref.offset(),
func_name_ref.length());
FunctionBody func_body{function_->sig, function_->code.offset(),
func_wire_bytes.start(), func_wire_bytes.end()};
NativeModule* native_module =
builder_->instance_object()->module_object()->native_module();
WasmCompilationUnit unit(isolate()->wasm_engine(), &module_env, native_module,
func_body, func_name, function_->func_index,
func_body, function_->func_index,
isolate()->counters(), tier);
WasmFeatures unused_detected_features;
unit.ExecuteCompilation(&unused_detected_features);
......
......@@ -52,7 +52,7 @@ checkExports('☺☺mul☺☺', '☺☺mul☺☺', '☺☺add☺☺', '☺☺add
builder.addFunction('three snowmen: ☃☃☃', kSig_i_v).addBody([]).exportFunc();
assertThrows(
() => builder.instantiate(), WebAssembly.CompileError,
/Compiling wasm function #0:three snowmen: ☃☃☃ failed: /);
/Compiling wasm function "three snowmen: ☃☃☃" failed: /);
})();
(function errorMessageUnicodeInImportModuleName() {
......
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