[M130 Migration][V8][WASM] Compilation zone stats flag introduced 74/325174/4
authorRobert Bycul <r.bycul@samsung.com>
Wed, 21 May 2025 08:07:56 +0000 (10:07 +0200)
committerBot Blink <blinkbot@samsung.com>
Wed, 4 Jun 2025 09:07:12 +0000 (09:07 +0000)
Ported change:
https://archive.tizen.org/gerrit/c/platform/framework/web/chromium-efl/+/313983

Bug: https://jira-eu.sec.samsung.net/browse/VDWASM-2364
Change-Id: I134e63f13629c98cc9460467d871a8726bb53bbc
Signed-off-by: Robert Bycul <r.bycul@samsung.com>
v8/src/codegen/optimized-compilation-info.cc
v8/src/codegen/optimized-compilation-info.h
v8/src/compiler/graph-visualizer.cc
v8/src/compiler/graph-visualizer.h
v8/src/compiler/pipeline.cc
v8/src/compiler/wasm-compiler.cc
v8/src/diagnostics/compilation-statistics.cc
v8/src/flags/flag-definitions.h

index 8ba94b870e52d13f2091daddcd0b1f4f31f779a0..f31052dc3f5646b1d79bb56fcb49d9c953285948 100644 (file)
@@ -251,6 +251,9 @@ void OptimizedCompilationInfo::SetTracingFlags(bool passes_filter) {
   if (v8_flags.trace_turbo_alloc) set_trace_turbo_allocation();
   if (v8_flags.trace_heap_broker) set_trace_heap_broker();
   if (v8_flags.turboshaft_trace_reduction) set_turboshaft_trace_reduction();
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  if (v8_flags.trace_turbo_stats) set_trace_turbo_stats();
+#endif
 }
 
 OptimizedCompilationInfo::InlinedFunctionHolder::InlinedFunctionHolder(
index 7fb6b965e0250310c71654e2cf0a016db0782b6e..5c3622536d012a40835fb5c0c33dfe9c61753b6b 100644 (file)
@@ -56,6 +56,12 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
   // Various configuration flags for a compilation, as well as some properties
   // of the compiled code produced by a compilation.
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+#define TRACE_TURBO_STATS(V, num) V(TraceTurboStats, trace_turbo_stats, num)
+#else
+#define TRACE_TURBO_STATS(V, num)
+#endif
+
 #define FLAGS(V)                                                      \
   V(FunctionContextSpecializing, function_context_specializing, 0)    \
   V(Inlining, inlining, 1)                                            \
@@ -77,7 +83,8 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
   V(InlineJSWasmCalls, inline_js_wasm_calls, 17)                      \
   V(TurboshaftTraceReduction, turboshaft_trace_reduction, 18)         \
   V(CouldNotInlineAllCandidates, could_not_inline_all_candidates, 19) \
-  V(ShadowStackCompliantLazyDeopt, shadow_stack_compliant_lazy_deopt, 20)
+  V(ShadowStackCompliantLazyDeopt, shadow_stack_compliant_lazy_deopt, 20) \
+  TRACE_TURBO_STATS(V, 21)
 
   enum Flag {
 #define DEF_ENUM(Camel, Lower, Bit) k##Camel = 1 << Bit,
@@ -268,6 +275,24 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
     return std::move(canonical_handles_);
   }
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  const char* stats_turbo_filename() const {
+    return stats_turbo_filename_.get();
+  }
+
+  void set_stats_turbo_filename(std::unique_ptr<char[]> filename) {
+    stats_turbo_filename_ = std::move(filename);
+  }
+
+  size_t source_size() const {
+    return source_size_;
+  }
+
+  void set_source_size(size_t size) {
+    source_size_ = size;
+  }
+#endif
+
  private:
   void ConfigureFlags();
 
@@ -329,6 +354,10 @@ class V8_EXPORT_PRIVATE OptimizedCompilationInfo final {
 
   base::Vector<const char> debug_name_;
   std::unique_ptr<char[]> trace_turbo_filename_;
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  std::unique_ptr<char[]> stats_turbo_filename_;
+  size_t source_size_{};
+#endif
 
   TickCounter tick_counter_;
 
index 582a05acbedc2b91a4543ebaa830358d8782e1b6..94782051cb27014a2a431f5d0c6c36051f7834a2 100644 (file)
@@ -43,12 +43,31 @@ const char* get_cached_trace_turbo_filename(OptimizedCompilationInfo* info) {
   return info->trace_turbo_filename();
 }
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+const char* get_cached_stats_turbo_filename(OptimizedCompilationInfo* info) {
+  if (!info->stats_turbo_filename()) {
+    info->set_stats_turbo_filename(
+        GetVisualizerLogFileName(info, v8_flags.trace_turbo_path, nullptr,
+                                 "txt"));
+  }
+  return info->stats_turbo_filename();
+}
+#endif
+
 TurboJsonFile::TurboJsonFile(OptimizedCompilationInfo* info,
                              std::ios_base::openmode mode)
     : std::ofstream(get_cached_trace_turbo_filename(info), mode) {}
 
 TurboJsonFile::~TurboJsonFile() { flush(); }
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+TurboStatsFile::TurboStatsFile(OptimizedCompilationInfo* info,
+                             std::ios_base::openmode mode)
+    : std::ofstream(get_cached_stats_turbo_filename(info), mode) {}
+
+TurboStatsFile::~TurboStatsFile() { flush(); }
+#endif
+
 TurboCfgFile::TurboCfgFile(Isolate* isolate)
     : std::ofstream(Isolate::GetTurboCfgFileName(isolate).c_str(),
                     std::ios_base::app) {}
index 38d0b26269a08b43c5e6e661545e0965994ade8b..dbe51fe62f896c77e61f808ca2bf0f1bc6ae4f1c 100644 (file)
@@ -82,6 +82,13 @@ struct TurboJsonFile : public std::ofstream {
   ~TurboJsonFile() override;
 };
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+struct TurboStatsFile : public std::ofstream {
+  TurboStatsFile(OptimizedCompilationInfo* info, std::ios_base::openmode mode);
+  ~TurboStatsFile() override;
+};
+#endif
+
 struct TurboCfgFile : public std::ofstream {
   explicit TurboCfgFile(Isolate* isolate = nullptr);
   ~TurboCfgFile() override;
index 82d50a377fd2d19bb389ecf81756d7000bd0756b..a369c6847f0edb8300c31af5ca51874e62583878 100644 (file)
@@ -294,10 +294,47 @@ class V8_NODISCARD PipelineRunScope {
       : phase_scope_(data->pipeline_statistics(), phase_name),
         zone_scope_(data->zone_stats(), phase_name),
         origin_scope_(data->node_origins(), phase_name) {
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+    data_ = data;
+    phase_name_ = phase_name;
+    if (phase_name_ && data_->info()->trace_turbo_stats()) {
+      TurboStatsFile stats_of(data_->info(), std::ios_base::app);
+      stats_of << "start phase " << phase_name_;
+      stats_of << ": current: "
+          << data_->zone_stats()->GetCurrentAllocatedBytes();
+      stats_of << ", max: "
+          << data_->zone_stats()->GetMaxAllocatedBytes();
+      stats_of << ", total: "
+          << data_->zone_stats()->GetTotalAllocatedBytes();
+      stats_of << "\n";
+    }
+#else
     DCHECK_NOT_NULL(phase_name);
+#endif  // defined(BUILDING_V8_FOR_TIZEN_TV)
   }
 #endif  // V8_RUNTIME_CALL_STATS
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  PipelineRunScope(const PipelineRunScope&) = delete;
+  PipelineRunScope& operator=(const PipelineRunScope&) = delete;
+  PipelineRunScope(const PipelineRunScope&&) = delete;
+  PipelineRunScope& operator=(const PipelineRunScope&&) = delete;
+
+  ~PipelineRunScope() {
+    if (phase_name_ && data_->info()->trace_turbo_stats()) {
+      TurboStatsFile stats_of(data_->info(), std::ios_base::app);
+      stats_of << "end phase " << phase_name_;
+      stats_of << ": current: "
+          << data_->zone_stats()->GetCurrentAllocatedBytes();
+      stats_of << ", max: "
+          << data_->zone_stats()->GetMaxAllocatedBytes();
+      stats_of << ", total: "
+          << data_->zone_stats()->GetTotalAllocatedBytes();
+      stats_of << "\n";
+    }
+  }
+#endif  // defined(BUILDING_V8_FOR_TIZEN_TV)
+
   Zone* zone() { return zone_scope_.zone(); }
 
  private:
@@ -307,6 +344,11 @@ class V8_NODISCARD PipelineRunScope {
 #ifdef V8_RUNTIME_CALL_STATS
   RuntimeCallTimerScope runtime_call_timer_scope;
 #endif  // V8_RUNTIME_CALL_STATS
+
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  TFPipelineData* data_;
+  const char* phase_name_;
+#endif  // defined(BUILDING_V8_FOR_TIZEN_TV)
 };
 
 // LocalIsolateScope encapsulates the phase where persistent handles are
@@ -429,6 +471,16 @@ void AddReducer(TFPipelineData* data, GraphReducer* graph_reducer,
   graph_reducer->AddReducer(reducer);
 }
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+void InitTurboStatsFile(OptimizedCompilationInfo* info) {
+  if (info && info->trace_turbo_stats()) {
+    TurboStatsFile stats_of(info, std::ios_base::trunc);
+    stats_of << "function: " << info->GetDebugName().get() << '\n';
+    stats_of << "code size: " << info->source_size() << '\n';
+  }
+}
+#endif  // defined(BUILDING_V8_FOR_TIZEN_TV)
+
 TurbofanPipelineStatistics* CreatePipelineStatistics(
     Handle<Script> script, OptimizedCompilationInfo* info, Isolate* isolate,
     ZoneStats* zone_stats) {
@@ -443,6 +495,10 @@ TurbofanPipelineStatistics* CreatePipelineStatistics(
     pipeline_statistics->BeginPhaseKind("V8.TFInitializing");
   }
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(info);
+#endif
+
   if (info->trace_turbo_json()) {
     TurboJsonFile json_of(info, std::ios_base::trunc);
     json_of << "{\"function\" : ";
@@ -469,6 +525,10 @@ TurbofanPipelineStatistics* CreatePipelineStatistics(
     pipeline_statistics->BeginPhaseKind("V8.WasmInitializing");
   }
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(info);
+#endif
+
   if (info->trace_turbo_json()) {
     TurboJsonFile json_of(info, std::ios_base::trunc);
     std::unique_ptr<char[]> function_name = info->GetDebugName();
@@ -2848,6 +2908,10 @@ MaybeHandle<Code> Pipeline::GenerateCodeForCodeStub(
 
   PipelineImpl pipeline(&data);
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(&info);
+#endif
+
   // Trace initial graph (if requested).
   if (info.trace_turbo_json() || info.trace_turbo_graph()) {
     CodeTracer::StreamScope tracing_scope(data.GetCodeTracer());
@@ -3112,6 +3176,11 @@ wasm::WasmCompilationResult Pipeline::GenerateCodeForWasmNativeStub(
   TraceWrapperCompilation("TurboFan", &info, &data);
 
   PipelineImpl pipeline(&data);
+
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(&info);
+#endif
+
   pipeline.RunPrintAndVerify("V8.WasmNativeStubMachineCode", true);
 
   pipeline.Run<MemoryOptimizationPhase>();
@@ -3169,6 +3238,10 @@ Pipeline::GenerateCodeForWasmNativeStubFromTurboshaft(
 
   PipelineImpl pipeline(&data);
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(&info);
+#endif
+
   {
     turboshaft::PipelineData turboshaft_data(
         &zone_stats, turboshaft::TurboshaftPipelineKind::kWasm, nullptr, &info,
@@ -3325,6 +3398,10 @@ void Pipeline::GenerateCodeForWasmFunction(
 
   PipelineImpl pipeline(&data);
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(info);
+#endif
+
   if (data.info()->trace_turbo_json() || data.info()->trace_turbo_graph()) {
     CodeTracer::StreamScope tracing_scope(data.GetCodeTracer());
     tracing_scope.stream()
@@ -3820,6 +3897,10 @@ MaybeHandle<Code> Pipeline::GenerateCodeForTesting(
 
   PipelineImpl pipeline(&data);
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  InitTurboStatsFile(info);
+#endif
+
   if (info->trace_turbo_json()) {
     TurboJsonFile json_of(info, std::ios_base::trunc);
     json_of << "{\"function\":\"" << info->GetDebugName().get()
index 86db49e1ea5d3e48a5a7f254681475efb06c5705..7a8a913bf376d500da5c64857e8ee5e1b2e345f9 100644 (file)
@@ -9127,6 +9127,10 @@ wasm::WasmCompilationResult ExecuteTurbofanWasmCompilation(
       &zone, CodeKind::WASM_FUNCTION);
   info.set_allocation_folding();
 
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+  info.set_source_size(data.body_size());
+#endif
+
   if (info.trace_turbo_json()) {
     TurboCfgFile tcf;
     tcf << AsC1VCompilation(&info);
index 97fa14b1014278ae3c9d9ac425549dc9d36672cb..5d44a494e87915cf6ddacfcb8a38fea18513047b 100644 (file)
@@ -18,7 +18,11 @@ void CompilationStatistics::RecordPhaseStats(const char* phase_kind_name,
                                              const BasicStats& stats) {
   base::MutexGuard guard(&record_mutex_);
 
+#if !defined(BUILDING_V8_FOR_TIZEN_TV)
   std::string phase_name_str(phase_name);
+#else
+  std::string phase_name_str(phase_name ? phase_name : "unknown");
+#endif
   auto it = phase_map_.find(phase_name_str);
   if (it == phase_map_.end()) {
     PhaseStats phase_stats(phase_map_.size(), phase_kind_name);
index 2a273fb0ea697e353910b20e648cf7f3f253f090..eb8b39889bddf71a82311d9aebb3683dbc64a399 100644 (file)
@@ -1119,6 +1119,9 @@ DEFINE_BOOL(turbo_wasm_address_reassociation, true,
 
 DEFINE_STRING(turbo_filter, "*", "optimization filter for TurboFan compiler")
 DEFINE_BOOL(trace_turbo, false, "trace generated TurboFan IR")
+#if defined(BUILDING_V8_FOR_TIZEN_TV)
+DEFINE_BOOL(trace_turbo_stats, false, "trace TurboFan stats")
+#endif
 DEFINE_STRING(trace_turbo_path, nullptr,
               "directory to dump generated TurboFan IR to")
 DEFINE_STRING(trace_turbo_filter, "*",