Skip to content

Conversation

shaurya0
Copy link

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.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Aug 16, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 16, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Shaurya Sharma (shaurya0)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/104557.diff

2 Files Affected:

  • (modified) mlir/include/mlir/Support/Timing.h (+32)
  • (modified) mlir/lib/Support/Timing.cpp (+84-86)
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) {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants