[Mlir-commits] [mlir] [mlir] Make fold result type check more verbose (PR #76867)

Jakub Kuderski llvmlistbot at llvm.org
Wed Jan 3 13:59:06 PST 2024


https://github.com/kuhar updated https://github.com/llvm/llvm-project/pull/76867

>From 66d2b91799c6063991fb77ee06c67be7ee94d357 Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Wed, 3 Jan 2024 16:19:30 -0500
Subject: [PATCH 1/2] [mlir] Make fold result type check more verbose

Print the op and its types when the fold type check fails. This is to
speed up debuging as it should be trivial to map the offending op to its
folder based on the op name.
---
 mlir/lib/IR/Operation.cpp | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index a726790391a0c5..a7d5904faa5980 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -20,6 +20,7 @@
 #include "mlir/Interfaces/FoldInterfaces.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
+#include "llvm/Support/ErrorHandling.h"
 #include <numeric>
 #include <optional>
 
@@ -611,11 +612,19 @@ void Operation::setSuccessor(Block *block, unsigned index) {
 /// the results of the given op.
 static void checkFoldResultTypes(Operation *op,
                                  SmallVectorImpl<OpFoldResult> &results) {
-  if (!results.empty())
-    for (auto [ofr, opResult] : llvm::zip_equal(results, op->getResults()))
-      if (auto value = ofr.dyn_cast<Value>())
-        assert(value.getType() == opResult.getType() &&
-               "folder produced value of incorrect type");
+  if (results.empty())
+    return;
+
+  for (auto [ofr, opResult] : llvm::zip_equal(results, op->getResults())) {
+    if (auto value = dyn_cast<Value>(ofr)) {
+      if (value.getType() != opResult.getType()) {
+        llvm::errs() << "Folder produced a value of incorrect type for: '"
+                     << *op << "'\nOriginal type: '" << value.getType()
+                     << "'\nNew type: '" << opResult.getType() << "'\n";
+        assert(false && "incorrect fold result type");
+      }
+    }
+  }
 }
 #endif // NDEBUG
 

>From 16718a9f5b228bab8a1305e6af2b3672225bb21b Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Wed, 3 Jan 2024 16:58:55 -0500
Subject: [PATCH 2/2] Remove redundant if condition

---
 mlir/lib/IR/Operation.cpp | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index a7d5904faa5980..b99887f06cf141 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -612,14 +612,11 @@ void Operation::setSuccessor(Block *block, unsigned index) {
 /// the results of the given op.
 static void checkFoldResultTypes(Operation *op,
                                  SmallVectorImpl<OpFoldResult> &results) {
-  if (results.empty())
-    return;
-
   for (auto [ofr, opResult] : llvm::zip_equal(results, op->getResults())) {
     if (auto value = dyn_cast<Value>(ofr)) {
       if (value.getType() != opResult.getType()) {
-        llvm::errs() << "Folder produced a value of incorrect type for: '"
-                     << *op << "'\nOriginal type: '" << value.getType()
+        llvm::errs() << "Folder produced a value of incorrect type for: " << *op
+                     << "\nOriginal type: '" << value.getType()
                      << "'\nNew type: '" << opResult.getType() << "'\n";
         assert(false && "incorrect fold result type");
       }



More information about the Mlir-commits mailing list