From 44f96ba52433a18204e86e0cad5719d10f230b5a Mon Sep 17 00:00:00 2001 From: Robert Bycul Date: Mon, 3 Jun 2024 12:20:00 +0200 Subject: [PATCH] [M120 Migration][WASM] Compile big functions sequentially Ported changes: Original change: https://review.tizen.org/gerrit/c/platform/framework/web/chromium-efl/+/292708 Fixups: https://review.tizen.org/gerrit/c/platform/framework/web/chromium-efl/+/296434 (changes of unit tests already included in the port of V8 unit tests) https://review.tizen.org/gerrit/c/platform/framework/web/chromium-efl/+/298091 Bug: https://jira-eu.sec.samsung.net/browse/VDWASM-1543 Change-Id: I9964536fcc5f727b469cff7799331a0fab32c6ac Signed-off-by: Robert Bycul --- v8/src/flags/flag-definitions.h | 4 +++ v8/src/wasm/module-compiler.cc | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/v8/src/flags/flag-definitions.h b/v8/src/flags/flag-definitions.h index 9ead38111cb4..86301939249c 100644 --- a/v8/src/flags/flag-definitions.h +++ b/v8/src/flags/flag-definitions.h @@ -1349,6 +1349,10 @@ DEFINE_INT( DEFINE_BOOL(trace_wasm_compilation_times, false, "print how long it took to compile each wasm function") DEFINE_INT(wasm_tier_up_filter, -1, "only tier-up function with this index") +#if defined(BUILDING_V8_FOR_TIZEN_TV) +DEFINE_BOOL(compile_big_functions_with_one_thread, true, + "Compile big WASM functions using only one thread to decrease memory usage") +#endif DEFINE_DEBUG_BOOL(trace_wasm_decoder, false, "trace decoding of wasm code") DEFINE_DEBUG_BOOL(trace_wasm_compiler, false, "trace compiling of wasm code") DEFINE_DEBUG_BOOL(trace_wasm_streaming, false, diff --git a/v8/src/wasm/module-compiler.cc b/v8/src/wasm/module-compiler.cc index db52019f0914..9e62dbf946f1 100644 --- a/v8/src/wasm/module-compiler.cc +++ b/v8/src/wasm/module-compiler.cc @@ -276,6 +276,22 @@ class CompilationUnitQueues { size_t EstimateCurrentMemoryConsumption() const; +#if defined(BUILDING_V8_FOR_TIZEN_TV) + bool MarkTopTierUnitCompiled(int unit_id) { + const auto was_compiling = + big_top_tier_unit_compiling_.compare_exchange_strong( + unit_id, kNoUnitCompiling, std::memory_order_relaxed); + return was_compiling; + } + + static bool IsBigUnit(const NativeModule* module, + const WasmCompilationUnit& unit) { + const auto func_size = + module->module()->functions[unit.func_index()].code.length(); + return func_size > kBigUnitsLimit; + } +#endif // BUILDING_V8_FOR_TIZEN_TV + private: // Functions bigger than {kBigUnitsLimit} will be compiled first, in ascending // order of their function body size. @@ -391,6 +407,16 @@ class CompilationUnitQueues { return {}; } +#if defined(BUILDING_V8_FOR_TIZEN_TV) + bool CanStartTopTierUnitCompilation(int unit_id) { + // A big top tier unit can only be compiled if there isn't any of those + // already being compiled. + int expected_id = kNoUnitCompiling; + return big_top_tier_unit_compiling_.compare_exchange_strong(expected_id, + unit_id, std::memory_order_relaxed); + } +#endif // BUILDING_V8_FOR_TIZEN_TV + base::Optional GetBigUnitOfTier(int tier) { // Fast path without locking. if (!big_units_queue_.has_units[tier].load(std::memory_order_relaxed)) { @@ -399,6 +425,17 @@ class CompilationUnitQueues { base::MutexGuard guard(&big_units_queue_.mutex); if (big_units_queue_.units[tier].empty()) return {}; WasmCompilationUnit unit = big_units_queue_.units[tier].top().unit; + +#if defined(BUILDING_V8_FOR_TIZEN_TV) + // This is a big unit. Check the tier since we should only interact + // with top tier units. + if (v8_flags.compile_big_functions_with_one_thread && + unit.tier() == ExecutionTier::kTurbofan && + !CanStartTopTierUnitCompilation(unit.func_index())) { + return {}; + } +#endif // BUILDING_V8_FOR_TIZEN_TV + big_units_queue_.units[tier].pop(); if (big_units_queue_.units[tier].empty()) { big_units_queue_.has_units[tier].store(false, std::memory_order_relaxed); @@ -520,6 +557,10 @@ class CompilationUnitQueues { std::atomic num_priority_units_{0}; std::unique_ptr[]> top_tier_compiled_; std::atomic next_queue_to_add{0}; +#if defined(BUILDING_V8_FOR_TIZEN_TV) + static constexpr int kNoUnitCompiling{-1}; + std::atomic big_top_tier_unit_compiling_{kNoUnitCompiling}; +#endif // BUILDING_V8_FOR_TIZEN_TV }; size_t CompilationUnitQueues::EstimateCurrentMemoryConsumption() const { @@ -704,6 +745,12 @@ class CompilationStateImpl { size_t EstimateCurrentMemoryConsumption() const; +#if defined(BUILDING_V8_FOR_TIZEN_TV) + bool MarkTopTierUnitCompiled(int unit_id) { + return compilation_unit_queues_.MarkTopTierUnitCompiled(unit_id); + } +#endif // BUILDING_V8_FOR_TIZEN_TV + private: void AddCompilationUnitInternal(CompilationUnitBuilder* builder, int function_index, @@ -1576,6 +1623,16 @@ CompilationExecutionResult ExecuteCompilationUnits( BackgroundCompileScope compile_scope(native_module); if (compile_scope.cancelled()) return kYield; +#if defined(BUILDING_V8_FOR_TIZEN_TV) + if (v8_flags.compile_big_functions_with_one_thread && + unit->tier() == ExecutionTier::kTurbofan && + CompilationUnitQueues::IsBigUnit(compile_scope.native_module(), + *unit)) { + compile_scope.compilation_state()->MarkTopTierUnitCompiled( + unit->func_index()); + } +#endif // BUILDING_V8_FOR_TIZEN_TV + if (!results_to_publish.back().succeeded()) { compile_scope.compilation_state()->SetError(); return kNoMoreUnits; -- 2.34.1