[Mlir-commits] [mlir] [MLIR] Expose API and types for setting output format in DefaultTimingManager (PR #104557)

Shaurya Sharma llvmlistbot at llvm.org
Fri Aug 16 00:23:19 PDT 2024


https://github.com/shaurya0 created https://github.com/llvm/llvm-project/pull/104557

The purpose of this change is to allow for selecting the output format in situations when we would like to add timing to the pass manager without CL options.

>From 82c0e75e88a6bc57bb0f713a6a1680eb618190cf Mon Sep 17 00:00:00 2001
From: Shaurya Sharma <shaurya at modular.com>
Date: Fri, 16 Aug 2024 09:19:29 +0200
Subject: [PATCH] [MLIR] Expose API and types for setting output format in
 DefaultTimingManager

---
 mlir/include/mlir/Support/Timing.h |  32 ++++++
 mlir/lib/Support/Timing.cpp        | 170 ++++++++++++++---------------
 2 files changed, 116 insertions(+), 86 deletions(-)

diff --git a/mlir/include/mlir/Support/Timing.h b/mlir/include/mlir/Support/Timing.h
index a8a4bfd1c6cf16..1c5bff654fa65c 100644
--- a/mlir/include/mlir/Support/Timing.h
+++ b/mlir/include/mlir/Support/Timing.h
@@ -367,6 +367,35 @@ class OutputStrategy {
   raw_ostream &os;
 };
 
+
+/// Text output format of timing reports
+class OutputTextStrategy : public OutputStrategy {
+public:
+  OutputTextStrategy(llvm::raw_ostream &os);
+  void printHeader(const TimeRecord &total) override;
+  void printFooter() override;
+  void printTime(const TimeRecord &time, const TimeRecord &total) override;
+  void printListEntry(llvm::StringRef name, const TimeRecord &time,
+                      const TimeRecord &total, bool lastEntry) override;
+  void printTreeEntry(unsigned indent, llvm::StringRef name,
+                      const TimeRecord &time, const TimeRecord &total) override;
+  void printTreeEntryEnd(unsigned indent, bool lastEntry) override;
+};
+
+/// JSON output format of timing reports
+class OutputJsonStrategy : public OutputStrategy {
+public:
+  OutputJsonStrategy(llvm::raw_ostream &os);
+  void printHeader(const TimeRecord &total) override;
+  void printFooter() override;
+  void printTime(const TimeRecord &time, const TimeRecord &total) override;
+  void printListEntry(llvm::StringRef name, const TimeRecord &time,
+                      const TimeRecord &total, bool lastEntry) override;
+  void printTreeEntry(unsigned indent, llvm::StringRef name,
+                      const TimeRecord &time, const TimeRecord &total) override;
+  void printTreeEntryEnd(unsigned indent, bool lastEntry) override;
+};
+
 //===----------------------------------------------------------------------===//
 // DefaultTimingManager
 //===----------------------------------------------------------------------===//
@@ -430,6 +459,9 @@ class DefaultTimingManager : public TimingManager {
   /// Change the stream where the output will be printed to.
   void setOutput(std::unique_ptr<OutputStrategy> output);
 
+  /// Change the output format.
+  void setOutputFormat(OutputFormat outputFormat);
+
   /// 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
   /// the timers.
diff --git a/mlir/lib/Support/Timing.cpp b/mlir/lib/Support/Timing.cpp
index ac16eb7d224c9a..89345aea5dff19 100644
--- a/mlir/lib/Support/Timing.cpp
+++ b/mlir/lib/Support/Timing.cpp
@@ -108,107 +108,97 @@ TimingIdentifier TimingIdentifier::get(StringRef str, TimingManager &tm) {
 // Helpers for time record printing
 //===----------------------------------------------------------------------===//
 
-namespace {
-
-class OutputTextStrategy : public OutputStrategy {
-public:
-  OutputTextStrategy(raw_ostream &os) : OutputStrategy(os) {}
-
-  void printHeader(const TimeRecord &total) override {
-    // Figure out how many spaces to description name.
-    unsigned padding = (80 - kTimingDescription.size()) / 2;
-    os << "===" << std::string(73, '-') << "===\n";
-    os.indent(padding) << kTimingDescription << '\n';
-    os << "===" << std::string(73, '-') << "===\n";
-
-    // Print the total time followed by the section headers.
-    os << llvm::format("  Total Execution Time: %.4f seconds\n\n", total.wall);
-    if (total.user != total.wall)
-      os << "  ----User Time----";
-    os << "  ----Wall Time----  ----Name----\n";
-  }
+OutputTextStrategy::OutputTextStrategy(raw_ostream &os) : OutputStrategy(os) {}
+
+void OutputTextStrategy::printHeader(const TimeRecord &total)  {
+  // Figure out how many spaces to description name.
+  unsigned padding = (80 - kTimingDescription.size()) / 2;
+  os << "===" << std::string(73, '-') << "===\n";
+  os.indent(padding) << kTimingDescription << '\n';
+  os << "===" << std::string(73, '-') << "===\n";
+
+  // Print the total time followed by the section headers.
+  os << llvm::format("  Total Execution Time: %.4f seconds\n\n", total.wall);
+  if (total.user != total.wall)
+    os << "  ----User Time----";
+  os << "  ----Wall Time----  ----Name----\n";
+}
 
-  void printFooter() override { os.flush(); }
+void OutputTextStrategy::printFooter() { os.flush(); }
 
-  void printTime(const TimeRecord &time, const TimeRecord &total) override {
-    if (total.user != total.wall) {
-      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 OutputTextStrategy::printTime(const TimeRecord &time, const TimeRecord &total) {
+  if (total.user != total.wall) {
+    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(StringRef name, const TimeRecord &time,
-                      const TimeRecord &total, bool lastEntry) override {
-    printTime(time, total);
-    os << name << "\n";
-  }
+void OutputTextStrategy::printListEntry(StringRef name, const TimeRecord &time,
+                    const TimeRecord &total, bool lastEntry) {
+  printTime(time, total);
+  os << name << "\n";
+}
 
-  void printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
-                      const TimeRecord &total) override {
-    printTime(time, total);
-    os.indent(indent) << name << "\n";
-  }
+void OutputTextStrategy::printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
+                    const TimeRecord &total) {
+  printTime(time, total);
+  os.indent(indent) << name << "\n";
+}
 
-  void printTreeEntryEnd(unsigned indent, bool lastEntry) override {}
-};
+void OutputTextStrategy::printTreeEntryEnd(unsigned indent, bool lastEntry) {}
 
-class OutputJsonStrategy : public OutputStrategy {
-public:
-  OutputJsonStrategy(raw_ostream &os) : OutputStrategy(os) {}
+OutputJsonStrategy::OutputJsonStrategy(raw_ostream &os) : OutputStrategy(os) {}
 
-  void printHeader(const TimeRecord &total) override { os << "[" << "\n"; }
+void OutputJsonStrategy::printHeader(const TimeRecord &total) { os << "[" << "\n"; }
 
-  void printFooter() override {
-    os << "]" << "\n";
-    os.flush();
-  }
+void OutputJsonStrategy::printFooter() {
+  os << "]" << "\n";
+  os.flush();
+}
 
-  void printTime(const TimeRecord &time, const TimeRecord &total) override {
-    if (total.user != total.wall) {
-      os << "\"user\": {";
-      os << "\"duration\": " << llvm::format("%8.4f", time.user) << ", ";
-      os << "\"percentage\": "
-         << llvm::format("%5.1f", 100.0 * time.user / total.user);
-      os << "}, ";
-    }
-    os << "\"wall\": {";
-    os << "\"duration\": " << llvm::format("%8.4f", time.wall) << ", ";
+void OutputJsonStrategy::printTime(const TimeRecord &time, const TimeRecord &total) {
+  if (total.user != total.wall) {
+    os << "\"user\": {";
+    os << "\"duration\": " << llvm::format("%8.4f", time.user) << ", ";
     os << "\"percentage\": "
-       << llvm::format("%5.1f", 100.0 * time.wall / total.wall);
-    os << "}";
-  }
-
-  void printListEntry(StringRef name, const TimeRecord &time,
-                      const TimeRecord &total, bool lastEntry) override {
-    os << "{";
-    printTime(time, total);
-    os << ", \"name\": " << "\"" << name << "\"";
-    os << "}";
-    if (!lastEntry)
-      os << ",";
-    os << "\n";
+       << llvm::format("%5.1f", 100.0 * time.user / total.user);
+    os << "}, ";
   }
+  os << "\"wall\": {";
+  os << "\"duration\": " << llvm::format("%8.4f", time.wall) << ", ";
+  os << "\"percentage\": "
+     << llvm::format("%5.1f", 100.0 * time.wall / total.wall);
+  os << "}";
+}
 
-  void printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
-                      const TimeRecord &total) override {
-    os.indent(indent) << "{";
-    printTime(time, total);
-    os << ", \"name\": " << "\"" << name << "\"";
-    os << ", \"passes\": [" << "\n";
-  }
+void OutputJsonStrategy::printListEntry(StringRef name, const TimeRecord &time,
+                    const TimeRecord &total, bool lastEntry) {
+  os << "{";
+  printTime(time, total);
+  os << ", \"name\": " << "\"" << name << "\"";
+  os << "}";
+  if (!lastEntry)
+    os << ",";
+  os << "\n";
+}
 
-  void printTreeEntryEnd(unsigned indent, bool lastEntry) override {
-    os.indent(indent) << "{}]";
-    os << "}";
-    if (!lastEntry)
-      os << ",";
-    os << "\n";
-  }
-};
+void OutputJsonStrategy::printTreeEntry(unsigned indent, StringRef name, const TimeRecord &time,
+                    const TimeRecord &total) {
+  os.indent(indent) << "{";
+  printTime(time, total);
+  os << ", \"name\": " << "\"" << name << "\"";
+  os << ", \"passes\": [" << "\n";
+}
 
-} // namespace
+void OutputJsonStrategy::printTreeEntryEnd(unsigned indent, bool lastEntry) {
+  os.indent(indent) << "{}]";
+  os << "}";
+  if (!lastEntry)
+    os << ",";
+  os << "\n";
+}
 
 //===----------------------------------------------------------------------===//
 // Timer Implementation for DefaultTimingManager
@@ -527,6 +517,14 @@ void DefaultTimingManager::setOutput(std::unique_ptr<OutputStrategy> output) {
   out = std::move(output);
 }
 
+/// Change the output format.
+void DefaultTimingManager::setOutputFormat(OutputFormat outputFormat) {
+  if (outputFormat == OutputFormat::Text)
+    setOutput(std::make_unique<OutputTextStrategy>(llvm::errs()));
+  else if (outputFormat == OutputFormat::Json)
+    setOutput(std::make_unique<OutputJsonStrategy>(llvm::errs()));
+}
+
 /// Print and clear the timing results.
 void DefaultTimingManager::print() {
   if (impl->enabled) {



More information about the Mlir-commits mailing list