[Mlir-commits] [flang] [mlir] [mlir][NFC] Wrap TimingManager components into namespace (PR #167298)

Andrei Golubev llvmlistbot at llvm.org
Mon Nov 10 05:27:48 PST 2025


https://github.com/andrey-golubev updated https://github.com/llvm/llvm-project/pull/167298

>From 7e0c231126adf3f496eb32105cf6960ceddc110a Mon Sep 17 00:00:00 2001
From: "Golubev, Andrey" <andrey.golubev at intel.com>
Date: Mon, 10 Nov 2025 11:09:19 +0000
Subject: [PATCH 1/2] [mlir][NFC] Wrap TimingManager components into namespace

TimingManager and its components (e.g. helper classes and utility
functions) reside in mlir namespace. Certain components are generic
enough that it makes sense to enclose them into an additional namespace.
---
 mlir/include/mlir/Support/Timing.h |  8 ++-
 mlir/lib/Support/Timing.cpp        | 83 +++++++++++++++++-------------
 2 files changed, 54 insertions(+), 37 deletions(-)

diff --git a/mlir/include/mlir/Support/Timing.h b/mlir/include/mlir/Support/Timing.h
index 50ae84735dbf0..19ec3c41e36a1 100644
--- a/mlir/include/mlir/Support/Timing.h
+++ b/mlir/include/mlir/Support/Timing.h
@@ -320,6 +320,7 @@ class TimingScope {
   Timer timer;
 };
 
+namespace timing {
 //===----------------------------------------------------------------------===//
 // OutputStrategy
 //===----------------------------------------------------------------------===//
@@ -366,6 +367,7 @@ class OutputStrategy {
 
   raw_ostream &os;
 };
+} // namespace timing
 
 //===----------------------------------------------------------------------===//
 // DefaultTimingManager
@@ -428,7 +430,7 @@ class DefaultTimingManager : public TimingManager {
   DisplayMode getDisplayMode() const;
 
   /// Change the stream where the output will be printed to.
-  void setOutput(std::unique_ptr<OutputStrategy> output);
+  void setOutput(std::unique_ptr<timing::OutputStrategy> output);
 
   /// Print and clear the timing results. Only call this when there are no more
   /// references to nested timers around, as printing post-processes and clears
@@ -461,7 +463,7 @@ class DefaultTimingManager : public TimingManager {
 
 private:
   const std::unique_ptr<detail::DefaultTimingManagerImpl> impl;
-  std::unique_ptr<OutputStrategy> out;
+  std::unique_ptr<timing::OutputStrategy> out;
 };
 
 /// Register a set of useful command-line options that can be used to configure
@@ -473,10 +475,12 @@ void registerDefaultTimingManagerCLOptions();
 /// 'registerDefaultTimingManagerOptions' to a `DefaultTimingManager`.
 void applyDefaultTimingManagerCLOptions(DefaultTimingManager &tm);
 
+namespace timing {
 /// Create an output strategy for the specified format, to be passed to
 /// DefaultTimingManager::setOutput().
 std::unique_ptr<OutputStrategy>
 createOutputStrategy(DefaultTimingManager::OutputFormat fmt, raw_ostream &os);
+} // namespace timing
 
 } // namespace mlir
 
diff --git a/mlir/lib/Support/Timing.cpp b/mlir/lib/Support/Timing.cpp
index b0ac3798bc984..67d7bd73cfd18 100644
--- a/mlir/lib/Support/Timing.cpp
+++ b/mlir/lib/Support/Timing.cpp
@@ -109,11 +109,11 @@ TimingIdentifier TimingIdentifier::get(StringRef str, TimingManager &tm) {
 
 namespace {
 
-class OutputTextStrategy : public OutputStrategy {
+class OutputTextStrategy : public mlir::timing::OutputStrategy {
 public:
-  OutputTextStrategy(raw_ostream &os) : OutputStrategy(os) {}
+  OutputTextStrategy(raw_ostream &os) : mlir::timing::OutputStrategy(os) {}
 
-  void printHeader(const TimeRecord &total) override {
+  void printHeader(const mlir::timing::TimeRecord &total) override {
     // Figure out how many spaces to description name.
     unsigned padding = (80 - kTimingDescription.size()) / 2;
     os << "===" << std::string(73, '-') << "===\n";
@@ -129,7 +129,8 @@ class OutputTextStrategy : public OutputStrategy {
 
   void printFooter() override { os.flush(); }
 
-  void printTime(const TimeRecord &time, const TimeRecord &total) override {
+  void printTime(const mlir::timing::TimeRecord &time,
+                 const mlir::timing::TimeRecord &total) override {
     if (total.user != total.wall) {
       os << llvm::format("  %8.4f (%5.1f%%)", time.user,
                          100.0 * time.user / total.user);
@@ -138,14 +139,16 @@ class OutputTextStrategy : public OutputStrategy {
                        100.0 * time.wall / total.wall);
   }
 
-  void printListEntry(StringRef name, const TimeRecord &time,
-                      const TimeRecord &total, bool lastEntry) override {
+  void printListEntry(StringRef name, const mlir::timing::TimeRecord &time,
+                      const mlir::timing::TimeRecord &total,
+                      bool lastEntry) override {
     printTime(time, total);
     os << name << "\n";
   }
 
-  void printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
-                      const TimeRecord &total) override {
+  void printTreeEntry(unsigned indent, StringRef name,
+                      const mlir::timing::TimeRecord &time,
+                      const mlir::timing::TimeRecord &total) override {
     printTime(time, total);
     os.indent(indent) << name << "\n";
   }
@@ -153,18 +156,21 @@ class OutputTextStrategy : public OutputStrategy {
   void printTreeEntryEnd(unsigned indent, bool lastEntry) override {}
 };
 
-class OutputJsonStrategy : public OutputStrategy {
+class OutputJsonStrategy : public mlir::timing::OutputStrategy {
 public:
-  OutputJsonStrategy(raw_ostream &os) : OutputStrategy(os) {}
+  OutputJsonStrategy(raw_ostream &os) : mlir::timing::OutputStrategy(os) {}
 
-  void printHeader(const TimeRecord &total) override { os << "[" << "\n"; }
+  void printHeader(const mlir::timing::TimeRecord &total) override {
+    os << "[" << "\n";
+  }
 
   void printFooter() override {
     os << "]" << "\n";
     os.flush();
   }
 
-  void printTime(const TimeRecord &time, const TimeRecord &total) override {
+  void printTime(const mlir::timing::TimeRecord &time,
+                 const mlir::timing::TimeRecord &total) override {
     if (total.user != total.wall) {
       os << "\"user\": {";
       os << "\"duration\": " << llvm::format("%8.4f", time.user) << ", ";
@@ -179,8 +185,9 @@ class OutputJsonStrategy : public OutputStrategy {
     os << "}";
   }
 
-  void printListEntry(StringRef name, const TimeRecord &time,
-                      const TimeRecord &total, bool lastEntry) override {
+  void printListEntry(StringRef name, const mlir::timing::TimeRecord &time,
+                      const mlir::timing::TimeRecord &total,
+                      bool lastEntry) override {
     os << "{";
     printTime(time, total);
     os << ", \"name\": " << "\"" << name << "\"";
@@ -190,8 +197,9 @@ class OutputJsonStrategy : public OutputStrategy {
     os << "\n";
   }
 
-  void printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
-                      const TimeRecord &total) override {
+  void printTreeEntry(unsigned indent, StringRef name,
+                      const mlir::timing::TimeRecord &time,
+                      const mlir::timing::TimeRecord &total) override {
     os.indent(indent) << "{";
     printTime(time, total);
     os << ", \"name\": " << "\"" << name << "\"";
@@ -225,7 +233,8 @@ class TimerImpl {
   using ChildrenMap = llvm::MapVector<const void *, std::unique_ptr<TimerImpl>>;
   using AsyncChildrenMap = llvm::DenseMap<uint64_t, ChildrenMap>;
 
-  TimerImpl(std::string &&name, std::unique_ptr<OutputStrategy> &output)
+  TimerImpl(std::string &&name,
+            std::unique_ptr<mlir::timing::OutputStrategy> &output)
       : threadId(llvm::get_threadid()), name(name), output(output) {}
 
   /// Start the timer.
@@ -360,8 +369,8 @@ class TimerImpl {
   }
 
   /// Returns the time for this timer in seconds.
-  TimeRecord getTimeRecord() {
-    return TimeRecord(
+  mlir::timing::TimeRecord getTimeRecord() {
+    return mlir::timing::TimeRecord(
         std::chrono::duration_cast<std::chrono::duration<double>>(wallTime)
             .count(),
         std::chrono::duration_cast<std::chrono::duration<double>>(userTime)
@@ -369,9 +378,9 @@ class TimerImpl {
   }
 
   /// Print the timing result in list mode.
-  void printAsList(TimeRecord total) {
+  void printAsList(mlir::timing::TimeRecord total) {
     // Flatten the leaf timers in the tree and merge them by name.
-    llvm::StringMap<TimeRecord> mergedTimers;
+    llvm::StringMap<mlir::timing::TimeRecord> mergedTimers;
     std::function<void(TimerImpl *)> addTimer = [&](TimerImpl *timer) {
       mergedTimers[timer->name] += timer->getTimeRecord();
       for (auto &children : timer->children)
@@ -380,15 +389,17 @@ class TimerImpl {
     addTimer(this);
 
     // Sort the timing information by wall time.
-    std::vector<std::pair<StringRef, TimeRecord>> timerNameAndTime;
+    std::vector<std::pair<StringRef, mlir::timing::TimeRecord>>
+        timerNameAndTime;
     for (auto &it : mergedTimers)
       timerNameAndTime.emplace_back(it.first(), it.second);
-    llvm::array_pod_sort(timerNameAndTime.begin(), timerNameAndTime.end(),
-                         [](const std::pair<StringRef, TimeRecord> *lhs,
-                            const std::pair<StringRef, TimeRecord> *rhs) {
-                           return llvm::array_pod_sort_comparator<double>(
-                               &rhs->second.wall, &lhs->second.wall);
-                         });
+    llvm::array_pod_sort(
+        timerNameAndTime.begin(), timerNameAndTime.end(),
+        [](const std::pair<StringRef, mlir::timing::TimeRecord> *lhs,
+           const std::pair<StringRef, mlir::timing::TimeRecord> *rhs) {
+          return llvm::array_pod_sort_comparator<double>(&rhs->second.wall,
+                                                         &lhs->second.wall);
+        });
 
     // Print the timing information sequentially.
     for (auto &timeData : timerNameAndTime)
@@ -396,7 +407,7 @@ class TimerImpl {
   }
 
   /// Print the timing result in tree mode.
-  void printAsTree(TimeRecord total, unsigned indent = 0) {
+  void printAsTree(mlir::timing::TimeRecord total, unsigned indent = 0) {
     unsigned childIndent = indent;
     if (!hidden) {
       output->printTreeEntry(indent, name, getTimeRecord(), total);
@@ -468,7 +479,7 @@ class TimerImpl {
   /// Mutex for the async children.
   std::mutex asyncMutex;
 
-  std::unique_ptr<OutputStrategy> &output;
+  std::unique_ptr<mlir::timing::OutputStrategy> &output;
 };
 
 } // namespace
@@ -521,7 +532,8 @@ DefaultTimingManager::DisplayMode DefaultTimingManager::getDisplayMode() const {
 }
 
 /// Change the stream where the output will be printed to.
-void DefaultTimingManager::setOutput(std::unique_ptr<OutputStrategy> output) {
+void DefaultTimingManager::setOutput(
+    std::unique_ptr<mlir::timing::OutputStrategy> output) {
   out = std::move(output);
 }
 
@@ -619,12 +631,13 @@ void mlir::applyDefaultTimingManagerCLOptions(DefaultTimingManager &tm) {
     return;
   tm.setEnabled(options->timing);
   tm.setDisplayMode(options->displayMode);
-  tm.setOutput(createOutputStrategy(options->outputFormat, llvm::errs()));
+  tm.setOutput(
+      mlir::timing::createOutputStrategy(options->outputFormat, llvm::errs()));
 }
 
-std::unique_ptr<OutputStrategy>
-mlir::createOutputStrategy(DefaultTimingManager::OutputFormat fmt,
-                           raw_ostream &os) {
+std::unique_ptr<mlir::timing::OutputStrategy>
+mlir::timing::createOutputStrategy(DefaultTimingManager::OutputFormat fmt,
+                                   raw_ostream &os) {
   switch (fmt) {
   case OutputFormat::Text:
     return std::make_unique<OutputTextStrategy>(os);

>From bb98bf81f0d965bda2bfb5693bc3e2dbbf1937fa Mon Sep 17 00:00:00 2001
From: "Golubev, Andrey" <andrey.golubev at intel.com>
Date: Mon, 10 Nov 2025 13:23:43 +0000
Subject: [PATCH 2/2] Fix namespaces in Flang

---
 flang/include/flang/Support/Timing.h |  2 +-
 flang/lib/Support/Timing.cpp         | 21 ++++++++++++---------
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/flang/include/flang/Support/Timing.h b/flang/include/flang/Support/Timing.h
index 75ba2a8d85f39..5dc612524dde7 100644
--- a/flang/include/flang/Support/Timing.h
+++ b/flang/include/flang/Support/Timing.h
@@ -19,7 +19,7 @@ namespace Fortran::support {
 
 /// Create a strategy to render the captured times in plain text. This is
 /// intended to be passed to a TimingManager.
-std::unique_ptr<mlir::OutputStrategy> createTimingFormatterText(
+std::unique_ptr<mlir::timing::OutputStrategy> createTimingFormatterText(
     llvm::raw_ostream &os);
 
 } // namespace Fortran::support
diff --git a/flang/lib/Support/Timing.cpp b/flang/lib/Support/Timing.cpp
index ee8309a950eec..cf3c368b3db4e 100644
--- a/flang/lib/Support/Timing.cpp
+++ b/flang/lib/Support/Timing.cpp
@@ -13,14 +13,15 @@
 #include "flang/Support/Timing.h"
 #include "llvm/Support/Format.h"
 
-class OutputStrategyText : public mlir::OutputStrategy {
+class OutputStrategyText : public mlir::timing::OutputStrategy {
 protected:
   static constexpr llvm::StringLiteral header = "Flang execution timing report";
 
 public:
-  OutputStrategyText(llvm::raw_ostream &os) : mlir::OutputStrategy(os) {}
+  OutputStrategyText(llvm::raw_ostream &os)
+      : mlir::timing::OutputStrategy(os) {}
 
-  void printHeader(const mlir::TimeRecord &total) override {
+  void printHeader(const mlir::timing::TimeRecord &total) override {
     // Figure out how many spaces to description name.
     unsigned padding = (80 - header.size()) / 2;
     os << "===" << std::string(73, '-') << "===\n";
@@ -34,22 +35,24 @@ class OutputStrategyText : public mlir::OutputStrategy {
 
   void printFooter() override { os.flush(); }
 
-  void printTime(
-      const mlir::TimeRecord &time, const mlir::TimeRecord &total) override {
+  void printTime(const mlir::timing::TimeRecord &time,
+      const mlir::timing::TimeRecord &total) override {
     os << llvm::format(
         "  %8.4f (%5.1f%%)", time.user, 100.0 * time.user / total.user);
     os << llvm::format(
         "  %8.4f (%5.1f%%)  ", time.wall, 100.0 * time.wall / total.wall);
   }
 
-  void printListEntry(llvm::StringRef name, const mlir::TimeRecord &time,
-      const mlir::TimeRecord &total, bool lastEntry) override {
+  void printListEntry(llvm::StringRef name,
+      const mlir::timing::TimeRecord &time,
+      const mlir::timing::TimeRecord &total, bool lastEntry) override {
     printTime(time, total);
     os << name << "\n";
   }
 
   void printTreeEntry(unsigned indent, llvm::StringRef name,
-      const mlir::TimeRecord &time, const mlir::TimeRecord &total) override {
+      const mlir::timing::TimeRecord &time,
+      const mlir::timing::TimeRecord &total) override {
     printTime(time, total);
     os.indent(indent) << name << "\n";
   }
@@ -59,7 +62,7 @@ class OutputStrategyText : public mlir::OutputStrategy {
 
 namespace Fortran::support {
 
-std::unique_ptr<mlir::OutputStrategy> createTimingFormatterText(
+std::unique_ptr<mlir::timing::OutputStrategy> createTimingFormatterText(
     llvm::raw_ostream &os) {
   return std::make_unique<OutputStrategyText>(os);
 }



More information about the Mlir-commits mailing list