Commit dde3166b authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[wasm] Remove one {NewNativeModule} method

This makes the {code_size_estimate} computation explicit in the caller,
and removes one of the two {NewNativeModule} constructors. It turns out
that the calculation is totally off in the streaming calculation phase,
since no function bodies have been parsed yet. So all
{WasmFunction::code} fields are still empty, and we compute an estimate
that is way too low.
This CL prepares the actual fix for that (by computing a better estimate
at specific call sites).

R=ahaas@chromium.org

Bug: v8:9950
Change-Id: I68a891c97e5f65a9c7e73e21684bdfa7e261e216
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1901273
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarAndreas Haas <ahaas@chromium.org>
Cr-Commit-Position: refs/heads/master@{#64845}
parent 43ad81f3
...@@ -1365,8 +1365,10 @@ std::shared_ptr<NativeModule> CompileToNativeModule( ...@@ -1365,8 +1365,10 @@ std::shared_ptr<NativeModule> CompileToNativeModule(
OwnedVector<uint8_t>::Of(wire_bytes.module_bytes()); OwnedVector<uint8_t>::Of(wire_bytes.module_bytes());
// Create a new {NativeModule} first. // Create a new {NativeModule} first.
size_t code_size_estimate =
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(module.get());
auto native_module = isolate->wasm_engine()->NewNativeModule( auto native_module = isolate->wasm_engine()->NewNativeModule(
isolate, enabled, std::move(module)); isolate, enabled, std::move(module), code_size_estimate);
native_module->SetWireBytes(std::move(wire_bytes_copy)); native_module->SetWireBytes(std::move(wire_bytes_copy));
CompileNativeModule(isolate, thrower, wasm_module, native_module.get()); CompileNativeModule(isolate, thrower, wasm_module, native_module.get());
...@@ -1498,8 +1500,10 @@ void AsyncCompileJob::CreateNativeModule( ...@@ -1498,8 +1500,10 @@ void AsyncCompileJob::CreateNativeModule(
// breakpoints on a (potentially empty) subset of the instances. // breakpoints on a (potentially empty) subset of the instances.
// Create the module object. // Create the module object.
size_t code_size_estimate =
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(module.get());
native_module_ = isolate_->wasm_engine()->NewNativeModule( native_module_ = isolate_->wasm_engine()->NewNativeModule(
isolate_, enabled_features_, std::move(module)); isolate_, enabled_features_, std::move(module), code_size_estimate);
native_module_->SetWireBytes({std::move(bytes_copy_), wire_bytes_.length()}); native_module_->SetWireBytes({std::move(bytes_copy_), wire_bytes_.length()});
if (stream_) stream_->NotifyNativeModuleCreated(native_module_); if (stream_) stream_->NotifyNativeModuleCreated(native_module_);
......
...@@ -1555,7 +1555,7 @@ size_t WasmCodeManager::EstimateNativeModuleCodeSize(const WasmModule* module) { ...@@ -1555,7 +1555,7 @@ size_t WasmCodeManager::EstimateNativeModuleCodeSize(const WasmModule* module) {
} }
// static // static
size_t WasmCodeManager::EstimateNativeModuleNonCodeSize( size_t WasmCodeManager::EstimateNativeModuleMetaDataSize(
const WasmModule* module) { const WasmModule* module) {
size_t wasm_module_estimate = EstimateStoredSize(module); size_t wasm_module_estimate = EstimateStoredSize(module);
......
...@@ -692,8 +692,11 @@ class V8_EXPORT_PRIVATE WasmCodeManager final { ...@@ -692,8 +692,11 @@ class V8_EXPORT_PRIVATE WasmCodeManager final {
return total_committed_code_space_.load(); return total_committed_code_space_.load();
} }
// Estimate the needed code space from a completely decoded module.
static size_t EstimateNativeModuleCodeSize(const WasmModule* module); static size_t EstimateNativeModuleCodeSize(const WasmModule* module);
static size_t EstimateNativeModuleNonCodeSize(const WasmModule* module); // Estimate the size of meta data needed for the NativeModule, excluding
// generated code. This data still be stored on the C++ heap.
static size_t EstimateNativeModuleMetaDataSize(const WasmModule* module);
private: private:
friend class WasmCodeAllocator; friend class WasmCodeAllocator;
......
...@@ -673,18 +673,11 @@ void WasmEngine::LogOutstandingCodesForIsolate(Isolate* isolate) { ...@@ -673,18 +673,11 @@ void WasmEngine::LogOutstandingCodesForIsolate(Isolate* isolate) {
std::shared_ptr<NativeModule> WasmEngine::NewNativeModule( std::shared_ptr<NativeModule> WasmEngine::NewNativeModule(
Isolate* isolate, const WasmFeatures& enabled, Isolate* isolate, const WasmFeatures& enabled,
std::shared_ptr<const WasmModule> module) { std::shared_ptr<const WasmModule> module, size_t code_size_estimate) {
size_t code_size_estimate = // TODO(clemensb): Remove --wasm-far-jump-table and {can_request_more}.
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(module.get()); bool can_request_more =
return NewNativeModule(isolate, enabled, code_size_estimate, !wasm::NativeModule::kNeedsFarJumpsBetweenCodeSpaces ||
!wasm::NativeModule::kNeedsFarJumpsBetweenCodeSpaces || FLAG_wasm_far_jump_table;
FLAG_wasm_far_jump_table,
std::move(module));
}
std::shared_ptr<NativeModule> WasmEngine::NewNativeModule(
Isolate* isolate, const WasmFeatures& enabled, size_t code_size_estimate,
bool can_request_more, std::shared_ptr<const WasmModule> module) {
std::shared_ptr<NativeModule> native_module = std::shared_ptr<NativeModule> native_module =
code_manager_.NewNativeModule(this, isolate, enabled, code_size_estimate, code_manager_.NewNativeModule(this, isolate, enabled, code_size_estimate,
can_request_more, std::move(module)); can_request_more, std::move(module));
......
...@@ -175,16 +175,12 @@ class V8_EXPORT_PRIVATE WasmEngine { ...@@ -175,16 +175,12 @@ class V8_EXPORT_PRIVATE WasmEngine {
// Create a new NativeModule. The caller is responsible for its // Create a new NativeModule. The caller is responsible for its
// lifetime. The native module will be given some memory for code, // lifetime. The native module will be given some memory for code,
// which will be page size aligned. The size of the initial memory // which will be page size aligned. The size of the initial memory
// is determined with a heuristic based on the total size of wasm // is determined by {code_size_estimate}. The native module may later request
// code. The native module may later request more memory. // more memory.
// TODO(titzer): isolate is only required here for CompilationState. // TODO(wasm): isolate is only required here for CompilationState.
std::shared_ptr<NativeModule> NewNativeModule( std::shared_ptr<NativeModule> NewNativeModule(
Isolate* isolate, const WasmFeatures& enabled_features, Isolate* isolate, const WasmFeatures& enabled_features,
std::shared_ptr<const WasmModule> module); std::shared_ptr<const WasmModule> module, size_t code_size_estimate);
std::shared_ptr<NativeModule> NewNativeModule(
Isolate* isolate, const WasmFeatures& enabled_features,
size_t code_size_estimate, bool can_request_more,
std::shared_ptr<const WasmModule> module);
void FreeNativeModule(NativeModule*); void FreeNativeModule(NativeModule*);
......
...@@ -209,7 +209,7 @@ Handle<WasmModuleObject> WasmModuleObject::New( ...@@ -209,7 +209,7 @@ Handle<WasmModuleObject> WasmModuleObject::New(
// allocating a new {Managed<T>} that the {WasmModuleObject} references. // allocating a new {Managed<T>} that the {WasmModuleObject} references.
size_t memory_estimate = size_t memory_estimate =
code_size_estimate + code_size_estimate +
wasm::WasmCodeManager::EstimateNativeModuleNonCodeSize(module); wasm::WasmCodeManager::EstimateNativeModuleMetaDataSize(module);
Handle<Managed<wasm::NativeModule>> managed_native_module = Handle<Managed<wasm::NativeModule>> managed_native_module =
Managed<wasm::NativeModule>::FromSharedPtr(isolate, memory_estimate, Managed<wasm::NativeModule>::FromSharedPtr(isolate, memory_estimate,
std::move(native_module)); std::move(native_module));
...@@ -2032,7 +2032,7 @@ Handle<AsmWasmData> AsmWasmData::New( ...@@ -2032,7 +2032,7 @@ Handle<AsmWasmData> AsmWasmData::New(
const WasmModule* module = native_module->module(); const WasmModule* module = native_module->module();
size_t memory_estimate = size_t memory_estimate =
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(module) + wasm::WasmCodeManager::EstimateNativeModuleCodeSize(module) +
wasm::WasmCodeManager::EstimateNativeModuleNonCodeSize(module); wasm::WasmCodeManager::EstimateNativeModuleMetaDataSize(module);
Handle<Managed<wasm::NativeModule>> managed_native_module = Handle<Managed<wasm::NativeModule>> managed_native_module =
Managed<wasm::NativeModule>::FromSharedPtr(isolate, memory_estimate, Managed<wasm::NativeModule>::FromSharedPtr(isolate, memory_estimate,
std::move(native_module)); std::move(native_module));
......
...@@ -614,13 +614,15 @@ MaybeHandle<WasmModuleObject> DeserializeNativeModule( ...@@ -614,13 +614,15 @@ MaybeHandle<WasmModuleObject> DeserializeNativeModule(
false, i::wasm::kWasmOrigin, isolate->counters(), false, i::wasm::kWasmOrigin, isolate->counters(),
isolate->wasm_engine()->allocator()); isolate->wasm_engine()->allocator());
if (decode_result.failed()) return {}; if (decode_result.failed()) return {};
CHECK_NOT_NULL(decode_result.value()); std::shared_ptr<WasmModule> module = std::move(decode_result.value());
WasmModule* module = decode_result.value().get(); CHECK_NOT_NULL(module);
Handle<Script> script = CreateWasmScript( Handle<Script> script = CreateWasmScript(
isolate, wire_bytes, module->source_map_url, module->name); isolate, wire_bytes, module->source_map_url, module->name);
size_t code_size_estimate =
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(module.get());
auto shared_native_module = isolate->wasm_engine()->NewNativeModule( auto shared_native_module = isolate->wasm_engine()->NewNativeModule(
isolate, enabled_features, std::move(decode_result.value())); isolate, enabled_features, std::move(module), code_size_estimate);
shared_native_module->SetWireBytes(OwnedVector<uint8_t>::Of(wire_bytes_vec)); shared_native_module->SetWireBytes(OwnedVector<uint8_t>::Of(wire_bytes_vec));
Handle<FixedArray> export_wrappers; Handle<FixedArray> export_wrappers;
......
...@@ -126,7 +126,7 @@ std::shared_ptr<wasm::NativeModule> AllocateNativeModule(Isolate* isolate, ...@@ -126,7 +126,7 @@ std::shared_ptr<wasm::NativeModule> AllocateNativeModule(Isolate* isolate,
// WasmCallDescriptor assumes that code is on the native heap and not // WasmCallDescriptor assumes that code is on the native heap and not
// within a code object. // within a code object.
return isolate->wasm_engine()->NewNativeModule( return isolate->wasm_engine()->NewNativeModule(
isolate, wasm::kAllWasmFeatures, code_size, false, std::move(module)); isolate, wasm::kAllWasmFeatures, std::move(module), code_size);
} }
void TestReturnMultipleValues(MachineType type) { void TestReturnMultipleValues(MachineType type) {
......
...@@ -20,10 +20,9 @@ namespace test_wasm_import_wrapper_cache { ...@@ -20,10 +20,9 @@ namespace test_wasm_import_wrapper_cache {
std::shared_ptr<NativeModule> NewModule(Isolate* isolate) { std::shared_ptr<NativeModule> NewModule(Isolate* isolate) {
std::shared_ptr<WasmModule> module(new WasmModule); std::shared_ptr<WasmModule> module(new WasmModule);
bool can_request_more = false; constexpr size_t kCodeSizeEstimate = 16384;
size_t size = 16384;
return isolate->wasm_engine()->NewNativeModule( return isolate->wasm_engine()->NewNativeModule(
isolate, kAllWasmFeatures, size, can_request_more, std::move(module)); isolate, kAllWasmFeatures, std::move(module), kCodeSizeEstimate);
} }
TEST(CacheHit) { TEST(CacheHit) {
......
...@@ -318,8 +318,10 @@ Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() { ...@@ -318,8 +318,10 @@ Handle<WasmInstanceObject> TestingModuleBuilder::InitInstanceObject() {
isolate_->factory()->NewScript(isolate_->factory()->empty_string()); isolate_->factory()->NewScript(isolate_->factory()->empty_string());
script->set_type(Script::TYPE_WASM); script->set_type(Script::TYPE_WASM);
size_t code_size_estimate =
wasm::WasmCodeManager::EstimateNativeModuleCodeSize(test_module_.get());
auto native_module = isolate_->wasm_engine()->NewNativeModule( auto native_module = isolate_->wasm_engine()->NewNativeModule(
isolate_, enabled_features_, test_module_); isolate_, enabled_features_, test_module_, code_size_estimate);
native_module->SetWireBytes(OwnedVector<const uint8_t>()); native_module->SetWireBytes(OwnedVector<const uint8_t>());
Handle<WasmModuleObject> module_object = Handle<WasmModuleObject> module_object =
......
...@@ -143,7 +143,7 @@ std::shared_ptr<wasm::NativeModule> AllocateNativeModule(i::Isolate* isolate, ...@@ -143,7 +143,7 @@ std::shared_ptr<wasm::NativeModule> AllocateNativeModule(i::Isolate* isolate,
// WasmCallDescriptor assumes that code is on the native heap and not // WasmCallDescriptor assumes that code is on the native heap and not
// within a code object. // within a code object.
return isolate->wasm_engine()->NewNativeModule( return isolate->wasm_engine()->NewNativeModule(
isolate, i::wasm::kAllWasmFeatures, code_size, false, std::move(module)); isolate, i::wasm::kAllWasmFeatures, std::move(module), code_size);
} }
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
......
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