[M120 Migration][WASM] Compile big functions sequentially 25/312625/3
authorRobert Bycul <r.bycul@samsung.com>
Mon, 3 Jun 2024 10:20:00 +0000 (12:20 +0200)
committerBot Blink <blinkbot@samsung.com>
Thu, 13 Jun 2024 13:36:02 +0000 (13:36 +0000)
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 <r.bycul@samsung.com>
v8/src/flags/flag-definitions.h
v8/src/wasm/module-compiler.cc

index 9ead38111cb4d5feae2e7293e2f39cabcd01312d..86301939249ce3872666519acac542ec147e5a44 100644 (file)
@@ -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,
index db52019f0914fbce78b3075569ba0267acafacac..9e62dbf946f1d10c34578c4929f074449328dede 100644 (file)
@@ -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<WasmCompilationUnit> 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<size_t> num_priority_units_{0};
   std::unique_ptr<std::atomic<bool>[]> top_tier_compiled_;
   std::atomic<int> next_queue_to_add{0};
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  static constexpr int kNoUnitCompiling{-1};
+  std::atomic<int> 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;