Skip to content

[MLIR][IR] add -mlir-print-unique-ssa-ids to AsmPrinter #91241

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
May 7, 2024

Conversation

rscottmanley
Copy link
Contributor

Add an option to unique the numbers of values, block arguments and naming conflicts when requested and/or printing generic op form. This is helpful when debugging. For example, if you have:

scf.for
  %0 =
  %1 = opA %0

scf.for
  %0 =
  %1 = opB %0

And you get a verifier error which says opB's "operand #0 does not dominate this use", it looks like %0 does dominate the use. This is not intuitive. If these were numbered uniquely, it would look like:

scf.for
  %0 =
  %1 = opA %0

scf.for
  %2 =
  %3 = opB %0

And thus, much clearer as to why you are getting the error since %0 is out of scope. Since generic op form should aim to give you the most possible information, it seems like a good idea to use unique numbers in this situation. Adding an option also gives those an option to use it outside of generic op form.

Copy link

github-actions bot commented May 6, 2024

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 May 6, 2024
@llvmbot
Copy link
Member

llvmbot commented May 6, 2024

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Scott Manley (rscottmanley)

Changes

Add an option to unique the numbers of values, block arguments and naming conflicts when requested and/or printing generic op form. This is helpful when debugging. For example, if you have:

scf.for
  %0 =
  %1 = opA %0

scf.for
  %0 =
  %1 = opB %0

And you get a verifier error which says opB's "operand #0 does not dominate this use", it looks like %0 does dominate the use. This is not intuitive. If these were numbered uniquely, it would look like:

scf.for
  %0 =
  %1 = opA %0

scf.for
  %2 =
  %3 = opB %0

And thus, much clearer as to why you are getting the error since %0 is out of scope. Since generic op form should aim to give you the most possible information, it seems like a good idea to use unique numbers in this situation. Adding an option also gives those an option to use it outside of generic op form.


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

3 Files Affected:

  • (modified) mlir/include/mlir/IR/OperationSupport.h (+6)
  • (modified) mlir/lib/IR/AsmPrinter.cpp (+20-3)
  • (added) mlir/test/IR/print-unique-ids.mlir (+40)
diff --git a/mlir/include/mlir/IR/OperationSupport.h b/mlir/include/mlir/IR/OperationSupport.h
index e661bb87a27ed0..e0b88d3b1bb4f3 100644
--- a/mlir/include/mlir/IR/OperationSupport.h
+++ b/mlir/include/mlir/IR/OperationSupport.h
@@ -1219,6 +1219,9 @@ class OpPrintingFlags {
   /// Return if the printer should print users of values.
   bool shouldPrintValueUsers() const;
 
+  /// Return if printer should use unique IDs.
+  bool shouldPrintUniqueIDs() const;
+
 private:
   /// Elide large elements attributes if the number of elements is larger than
   /// the upper limit.
@@ -1249,6 +1252,9 @@ class OpPrintingFlags {
 
   /// Print users of values.
   bool printValueUsersFlag : 1;
+
+  /// Print unique ids for values, block arguments and naming conflicts
+  bool printUniqueIDsFlag : 1;
 };
 
 //===----------------------------------------------------------------------===//
diff --git a/mlir/lib/IR/AsmPrinter.cpp b/mlir/lib/IR/AsmPrinter.cpp
index e915b97d9ff17b..72c84b57222092 100644
--- a/mlir/lib/IR/AsmPrinter.cpp
+++ b/mlir/lib/IR/AsmPrinter.cpp
@@ -189,6 +189,11 @@ struct AsmPrinterOptions {
       "mlir-print-value-users", llvm::cl::init(false),
       llvm::cl::desc(
           "Print users of operation results and block arguments as a comment")};
+
+  llvm::cl::opt<bool> printUniqueIDs{
+      "mlir-print-unique-ids", llvm::cl::init(false),
+      llvm::cl::desc("Print unique id numbers for values, block arguments and "
+                     "naming conflicts across all regions")};
 };
 } // namespace
 
@@ -206,7 +211,7 @@ OpPrintingFlags::OpPrintingFlags()
     : printDebugInfoFlag(false), printDebugInfoPrettyFormFlag(false),
       printGenericOpFormFlag(false), skipRegionsFlag(false),
       assumeVerifiedFlag(false), printLocalScope(false),
-      printValueUsersFlag(false) {
+      printValueUsersFlag(false), printUniqueIDsFlag(false) {
   // Initialize based upon command line options, if they are available.
   if (!clOptions.isConstructed())
     return;
@@ -224,6 +229,7 @@ OpPrintingFlags::OpPrintingFlags()
   printLocalScope = clOptions->printLocalScopeOpt;
   skipRegionsFlag = clOptions->skipRegionsOpt;
   printValueUsersFlag = clOptions->printValueUsers;
+  printUniqueIDsFlag = clOptions->printUniqueIDs;
 }
 
 /// Enable the elision of large elements attributes, by printing a '...'
@@ -350,6 +356,11 @@ bool OpPrintingFlags::shouldPrintValueUsers() const {
   return printValueUsersFlag;
 }
 
+/// Return if the printer should use unique IDs.
+bool OpPrintingFlags::shouldPrintUniqueIDs() const {
+  return printUniqueIDsFlag || shouldPrintGenericOpForm();
+}
+
 //===----------------------------------------------------------------------===//
 // NewLineCounter
 //===----------------------------------------------------------------------===//
@@ -1369,8 +1380,14 @@ SSANameState::SSANameState(Operation *op, const OpPrintingFlags &printerFlags)
   while (!nameContext.empty()) {
     Region *region;
     UsedNamesScopeTy *parentScope;
-    std::tie(region, nextValueID, nextArgumentID, nextConflictID, parentScope) =
-        nameContext.pop_back_val();
+
+    if (printerFlags.shouldPrintUniqueIDs())
+      // When printing unique IDs, ignore saved ID counts from parent regions
+      std::tie(region, std::ignore, std::ignore, std::ignore, parentScope) =
+          nameContext.pop_back_val();
+    else
+      std::tie(region, nextValueID, nextArgumentID, nextConflictID,
+               parentScope) = nameContext.pop_back_val();
 
     // When we switch from one subtree to another, pop the scopes(needless)
     // until the parent scope.
diff --git a/mlir/test/IR/print-unique-ids.mlir b/mlir/test/IR/print-unique-ids.mlir
new file mode 100644
index 00000000000000..8be57e0259de19
--- /dev/null
+++ b/mlir/test/IR/print-unique-ids.mlir
@@ -0,0 +1,40 @@
+// RUN: mlir-opt -mlir-print-unique-ids %s | FileCheck %s
+
+// CHECK: %arg5
+// CHECK: %15
+module {
+  func.func @uniqueConflicts(%arg0 : memref<i32>, %arg1 : memref<i32>) {
+    %c0 = arith.constant 0 : index
+    %c1 = arith.constant 1 : index
+    %c8 = arith.constant 8 : index
+    scf.for %arg2 = %c0 to %c8 step %c1 {
+      %a = memref.load %arg0[] : memref<i32>
+      %b = memref.load %arg1[] : memref<i32>
+      %0 = arith.addi %a, %b : i32
+      %1 = arith.subi %a, %b : i32
+      scf.for %arg3 = %c0 to %c8 step %c1 {
+        %a2 = memref.load %arg0[] : memref<i32>
+        %b2 = memref.load %arg1[] : memref<i32>
+        %2 = arith.addi %a2, %b2 : i32
+        %3 = arith.subi %a2, %b2 : i32
+        scf.yield
+      }
+      scf.for %arg3 = %c0 to %c8 step %c1 {
+        %a2 = memref.load %arg0[] : memref<i32>
+        %b2 = memref.load %arg1[] : memref<i32>
+        %2 = arith.addi %a2, %b2 : i32
+        %3 = arith.subi %a2, %b2 : i32
+        scf.yield
+      }
+      scf.yield
+    }
+    scf.for %arg2 = %c0 to %c8 step %c1 {
+      %a = memref.load %arg0[] : memref<i32>
+      %b = memref.load %arg1[] : memref<i32>
+      %0 = arith.addi %a, %b : i32
+      %1 = arith.subi %a, %b : i32
+      scf.yield
+    }
+    return
+  }
+}

@joker-eph joker-eph requested review from joker-eph and Mogball May 6, 2024 17:18
Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

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

LG, but let's see what @Mogball thinks!

@@ -189,6 +189,11 @@ struct AsmPrinterOptions {
"mlir-print-value-users", llvm::cl::init(false),
llvm::cl::desc(
"Print users of operation results and block arguments as a comment")};

llvm::cl::opt<bool> printUniqueIDs{
"mlir-print-unique-ids", llvm::cl::init(false),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be -mlir-print-unique-ssa-ids to be more explicit? "unique ids" seems a bit generic.

Or an option with a named value: -mlir-print-ssa-ids-scope={local, global}

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to being explicit about ssa

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unless you feel strongly it should be a named option, I like -mlir-print-unique-ssa-ids.

@@ -0,0 +1,40 @@
// RUN: mlir-opt -mlir-print-unique-ids %s | FileCheck %s
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would also add the negative test:

// RUN: mlir-opt %s | FileCheck %s --check-prefix=LOCAL_SCOPE

And:

// LOCAL_SCOPE-NOT: %arg5
// LOCAL_SCOPE-NOT: %15

Copy link
Contributor

@Mogball Mogball left a comment

Choose a reason for hiding this comment

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

This flag makes sense to me

@@ -189,6 +189,11 @@ struct AsmPrinterOptions {
"mlir-print-value-users", llvm::cl::init(false),
llvm::cl::desc(
"Print users of operation results and block arguments as a comment")};

llvm::cl::opt<bool> printUniqueIDs{
"mlir-print-unique-ids", llvm::cl::init(false),
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to being explicit about ssa

// CHECK: %arg5
// CHECK: %15
module {
func.func @uniqueConflicts(%arg0 : memref<i32>, %arg1 : memref<i32>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

This test seems unnecessarily large. Don't you only need to test a function with two scf.for ops?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I suppose I wanted to make sure it worked for nesting depths > 1, but I can reduce.

@rscottmanley rscottmanley force-pushed the rscottmanley/mlir-asm-printer branch from 04c08ab to 1cde917 Compare May 6, 2024 18:21
@rscottmanley rscottmanley changed the title [MLIR][IR] add -mlir-print-unique-ids to AsmPrinter [MLIR][IR] add -mlir-print-unique-ssa-ids to AsmPrinter May 6, 2024
@rscottmanley rscottmanley force-pushed the rscottmanley/mlir-asm-printer branch from 1cde917 to ce517a2 Compare May 6, 2024 18:28
Add an option to print unique SSA IDs of values, block arguments and naming
conflicts when requested and/or printing generic op form. This is helpful
when debugging. For example, if you have:

    scf.for
      %0 =
      %1 = opA %0

    scf.for
      %0 =
      %1 = opB %0

And you get a verifier error which says opB's "operand #0 does not dominate
this use", it looks like %0 does dominate the use. This is not intuitive. If
these were numbered uniquely, it would look like:

    scf.for
      %0 =
      %1 = opA %0

    scf.for
      %2 =
      %3 = opB %0

And thus, much clearer as to why you are getting the error since %0 is out
of scope. Since generic op form should aim to give you the most possible
information, it seems like a good idea to use unique numbers in this
situation. Adding an option also gives those an option to use it outside
of generic op form.
@rscottmanley rscottmanley force-pushed the rscottmanley/mlir-asm-printer branch from ce517a2 to 11fe987 Compare May 6, 2024 18:30
Copy link
Collaborator

@joker-eph joker-eph left a comment

Choose a reason for hiding this comment

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

Thanks!

@vzakhari vzakhari merged commit 5717553 into llvm:main May 7, 2024
Copy link

github-actions bot commented May 7, 2024

@rscottmanley Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

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.

5 participants