Skip to content

[mlir] Add op printing flag to skip regions #77726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Conversation

kuhar
Copy link
Member

@kuhar kuhar commented Jan 11, 2024

The new flag, --mlir-print-skip-regions, sets the op printing option that disables region printing. This results in the usual --mlir-print-ir-* debug options printing only the names of the executed passes and the signatures of the ops.

Example:

// -----// IR Dump Before CSE (cse) //----- //
func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}

// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}

The main use-case is to be triage compilation issues (crashes, slowness) on very deep pass pipelines and with very large IR files, where printing IR is prohibitively slow otherwise.

@llvmbot
Copy link
Member

llvmbot commented Jan 11, 2024

@llvm/pr-subscribers-mlir-core

Author: Jakub Kuderski (kuhar)

Changes

The new flag, --mlir-print-ir-skip-regions, sets the op printing option that disables region printing. This results in the usual --mlir-print-ir-* debug options printing only the names of the executed passes and the signatures of the ops.

Example:

// -----// IR Dump Before CSE (cse) //----- //
func.func @<!-- -->bar(%arg0: f32, %arg1: f32) -&gt; f32 {...}

// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
func.func @<!-- -->bar(%arg0: f32, %arg1: f32) -&gt; f32 {...}

The main use-case is to be triage compilation issues (crashes, slowness) on very deep pass pipelines and with very large IR files, where printing IR is prohibitively slow otherwise.

Unlike the pass statistics mechanism, the new flag can be used as an 'interactive' progress indicator.


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

3 Files Affected:

  • (modified) mlir/docs/PassManagement.md (+21)
  • (modified) mlir/lib/Pass/PassManagerOptions.cpp (+11-1)
  • (modified) mlir/test/Pass/ir-printing.mlir (+11)
diff --git a/mlir/docs/PassManagement.md b/mlir/docs/PassManagement.md
index 95a38207b7f854..a303e350d28b6b 100644
--- a/mlir/docs/PassManagement.md
+++ b/mlir/docs/PassManagement.md
@@ -1302,6 +1302,27 @@ func.func @simple_constant() -> (i32, i32) {
 }
 ```
 
+*   `mlir-print-ir-skip-regions`
+    *   Do not print op regions.
+    *   This option is useful to when we are interested in the names of the
+        executed passes but do not want to inspect the IR, e.g., when the
+        module is very large and printing is prohibitively slow.
+
+```shell
+$ mlir-opt foo.mlir -mlir-disable-threading -pass-pipeline='func.func(canonicalize,cse)' -mlir-print-ir-after-all -mlir-print-ir-skip-regions
+// -----// IR Dump Before CSE (cse) //----- //
+func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}
+
+// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
+func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}
+
+// -----// IR Dump Before CSE (cse) //----- //
+func.func @simple_constant() -> (i32, i32) {...}
+
+// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
+func.func @simple_constant() -> (i32, i32) {...}
+```
+
 ## Crash and Failure Reproduction
 
 The [pass manager](#pass-manager) in MLIR contains a builtin mechanism to
diff --git a/mlir/lib/Pass/PassManagerOptions.cpp b/mlir/lib/Pass/PassManagerOptions.cpp
index ffc53b7e3ed023..da7f27fb75a29c 100644
--- a/mlir/lib/Pass/PassManagerOptions.cpp
+++ b/mlir/lib/Pass/PassManagerOptions.cpp
@@ -58,6 +58,12 @@ struct PassManagerOptions {
       llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} "
                      "always print the top-level operation"),
       llvm::cl::init(false)};
+  llvm::cl::opt<bool> printSkipRegions{
+      "mlir-print-ir-skip-regions",
+      llvm::cl::desc(
+          "When printing IR for print-ir-[beforef|after]{-all} do not output "
+          "regions. This is useful to indicate pass pipeline progress."),
+      llvm::cl::init(false)};
 
   /// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
   void addPrinterInstrumentation(PassManager &pm);
@@ -119,10 +125,14 @@ void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) {
   if (!shouldPrintBeforePass && !shouldPrintAfterPass)
     return;
 
+  OpPrintingFlags opPrintingFlags;
+  if (printSkipRegions)
+    opPrintingFlags.skipRegions();
+
   // Otherwise, add the IR printing instrumentation.
   pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
                       printModuleScope, printAfterChange, printAfterFailure,
-                      llvm::errs());
+                      llvm::errs(), opPrintingFlags);
 }
 
 void mlir::registerPassManagerCLOptions() {
diff --git a/mlir/test/Pass/ir-printing.mlir b/mlir/test/Pass/ir-printing.mlir
index 048b721ba6d530..cc2f277d76ee52 100644
--- a/mlir/test/Pass/ir-printing.mlir
+++ b/mlir/test/Pass/ir-printing.mlir
@@ -3,6 +3,7 @@
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-after=cse -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-after-all -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_ALL %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-before=cse -mlir-print-ir-module-scope -o /dev/null 2>&1 | FileCheck -check-prefix=BEFORE_MODULE %s
+// RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-before-all -mlir-print-ir-skip-regions -o /dev/null 2>&1 | FileCheck -check-prefix=BEFORE_ALL_SKIP_REGIONS %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,cse))' -mlir-print-ir-after-all -mlir-print-ir-after-change -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_ALL_CHANGE %s
 // RUN: not mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,test-pass-failure))' -mlir-print-ir-after-failure -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_FAILURE %s
 
@@ -56,6 +57,16 @@ func.func @bar() {
 // BEFORE_MODULE: func @foo()
 // BEFORE_MODULE: func @bar()
 
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}CSE (cse) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @foo() {...}
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}Canonicalizer (canonicalize) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @foo() {...}
+
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}CSE (cse) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @bar() {...}
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}Canonicalizer (canonicalize) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @bar() {...}
+
 // AFTER_ALL_CHANGE: // -----// IR Dump After{{.*}}CSE (cse) //----- //
 // AFTER_ALL_CHANGE-NEXT: func @foo()
 // AFTER_ALL_CHANGE-NOT: // -----// IR Dump After{{.*}}CSE (cse) //----- //

@llvmbot
Copy link
Member

llvmbot commented Jan 11, 2024

@llvm/pr-subscribers-mlir

Author: Jakub Kuderski (kuhar)

Changes

The new flag, --mlir-print-ir-skip-regions, sets the op printing option that disables region printing. This results in the usual --mlir-print-ir-* debug options printing only the names of the executed passes and the signatures of the ops.

Example:

// -----// IR Dump Before CSE (cse) //----- //
func.func @<!-- -->bar(%arg0: f32, %arg1: f32) -&gt; f32 {...}

// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
func.func @<!-- -->bar(%arg0: f32, %arg1: f32) -&gt; f32 {...}

The main use-case is to be triage compilation issues (crashes, slowness) on very deep pass pipelines and with very large IR files, where printing IR is prohibitively slow otherwise.

Unlike the pass statistics mechanism, the new flag can be used as an 'interactive' progress indicator.


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

3 Files Affected:

  • (modified) mlir/docs/PassManagement.md (+21)
  • (modified) mlir/lib/Pass/PassManagerOptions.cpp (+11-1)
  • (modified) mlir/test/Pass/ir-printing.mlir (+11)
diff --git a/mlir/docs/PassManagement.md b/mlir/docs/PassManagement.md
index 95a38207b7f854..a303e350d28b6b 100644
--- a/mlir/docs/PassManagement.md
+++ b/mlir/docs/PassManagement.md
@@ -1302,6 +1302,27 @@ func.func @simple_constant() -> (i32, i32) {
 }
 ```
 
+*   `mlir-print-ir-skip-regions`
+    *   Do not print op regions.
+    *   This option is useful to when we are interested in the names of the
+        executed passes but do not want to inspect the IR, e.g., when the
+        module is very large and printing is prohibitively slow.
+
+```shell
+$ mlir-opt foo.mlir -mlir-disable-threading -pass-pipeline='func.func(canonicalize,cse)' -mlir-print-ir-after-all -mlir-print-ir-skip-regions
+// -----// IR Dump Before CSE (cse) //----- //
+func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}
+
+// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
+func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}
+
+// -----// IR Dump Before CSE (cse) //----- //
+func.func @simple_constant() -> (i32, i32) {...}
+
+// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
+func.func @simple_constant() -> (i32, i32) {...}
+```
+
 ## Crash and Failure Reproduction
 
 The [pass manager](#pass-manager) in MLIR contains a builtin mechanism to
diff --git a/mlir/lib/Pass/PassManagerOptions.cpp b/mlir/lib/Pass/PassManagerOptions.cpp
index ffc53b7e3ed023..da7f27fb75a29c 100644
--- a/mlir/lib/Pass/PassManagerOptions.cpp
+++ b/mlir/lib/Pass/PassManagerOptions.cpp
@@ -58,6 +58,12 @@ struct PassManagerOptions {
       llvm::cl::desc("When printing IR for print-ir-[before|after]{-all} "
                      "always print the top-level operation"),
       llvm::cl::init(false)};
+  llvm::cl::opt<bool> printSkipRegions{
+      "mlir-print-ir-skip-regions",
+      llvm::cl::desc(
+          "When printing IR for print-ir-[beforef|after]{-all} do not output "
+          "regions. This is useful to indicate pass pipeline progress."),
+      llvm::cl::init(false)};
 
   /// Add an IR printing instrumentation if enabled by any 'print-ir' flags.
   void addPrinterInstrumentation(PassManager &pm);
@@ -119,10 +125,14 @@ void PassManagerOptions::addPrinterInstrumentation(PassManager &pm) {
   if (!shouldPrintBeforePass && !shouldPrintAfterPass)
     return;
 
+  OpPrintingFlags opPrintingFlags;
+  if (printSkipRegions)
+    opPrintingFlags.skipRegions();
+
   // Otherwise, add the IR printing instrumentation.
   pm.enableIRPrinting(shouldPrintBeforePass, shouldPrintAfterPass,
                       printModuleScope, printAfterChange, printAfterFailure,
-                      llvm::errs());
+                      llvm::errs(), opPrintingFlags);
 }
 
 void mlir::registerPassManagerCLOptions() {
diff --git a/mlir/test/Pass/ir-printing.mlir b/mlir/test/Pass/ir-printing.mlir
index 048b721ba6d530..cc2f277d76ee52 100644
--- a/mlir/test/Pass/ir-printing.mlir
+++ b/mlir/test/Pass/ir-printing.mlir
@@ -3,6 +3,7 @@
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-after=cse -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-after-all -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_ALL %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-before=cse -mlir-print-ir-module-scope -o /dev/null 2>&1 | FileCheck -check-prefix=BEFORE_MODULE %s
+// RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,canonicalize))' -mlir-print-ir-before-all -mlir-print-ir-skip-regions -o /dev/null 2>&1 | FileCheck -check-prefix=BEFORE_ALL_SKIP_REGIONS %s
 // RUN: mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,cse))' -mlir-print-ir-after-all -mlir-print-ir-after-change -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_ALL_CHANGE %s
 // RUN: not mlir-opt %s -mlir-disable-threading=true -pass-pipeline='builtin.module(func.func(cse,test-pass-failure))' -mlir-print-ir-after-failure -o /dev/null 2>&1 | FileCheck -check-prefix=AFTER_FAILURE %s
 
@@ -56,6 +57,16 @@ func.func @bar() {
 // BEFORE_MODULE: func @foo()
 // BEFORE_MODULE: func @bar()
 
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}CSE (cse) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @foo() {...}
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}Canonicalizer (canonicalize) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @foo() {...}
+
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}CSE (cse) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @bar() {...}
+// BEFORE_ALL_SKIP_REGIONS: // -----// IR Dump Before{{.*}}Canonicalizer (canonicalize) //----- //
+// BEFORE_ALL_SKIP_REGIONS-NEXT: func @bar() {...}
+
 // AFTER_ALL_CHANGE: // -----// IR Dump After{{.*}}CSE (cse) //----- //
 // AFTER_ALL_CHANGE-NEXT: func @foo()
 // AFTER_ALL_CHANGE-NOT: // -----// IR Dump After{{.*}}CSE (cse) //----- //

@kuhar kuhar requested a review from Groverkss January 11, 2024 05:30
Copy link
Member

@Groverkss Groverkss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This is really useful.

@kuhar kuhar changed the title [mlir] Add IR printing flag to skip regions [mlir] Add op printing flag to skip regions Jan 11, 2024
@kuhar kuhar requested a review from joker-eph January 11, 2024 22:44
@kuhar kuhar merged commit 3513267 into llvm:main Jan 11, 2024
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
The new flag, `--mlir-print-skip-regions`, sets the op printing option
that disables region printing. This results in the usual
`--mlir-print-ir-*` debug options printing only the names of the
executed passes and the signatures of the ops.

Example:
```mlir
// -----// IR Dump Before CSE (cse) //----- //
func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}

// -----// IR Dump Before Canonicalizer (canonicalize) //----- //
func.func @bar(%arg0: f32, %arg1: f32) -> f32 {...}
```

The main use-case is to be triage compilation issues (crashes, slowness)
on very deep pass pipelines and with very large IR files, where printing
IR is prohibitively slow otherwise.
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.

4 participants