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

Jakub Kuderski llvmlistbot at llvm.org
Thu Jan 4 07:53:09 PST 2024


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

>From 288c80165b50ad0edf54b398e6293e0234a10ddc 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/3] [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..92182c5d24b51c 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 813993e21fdfbfd3502fee8c30a9269700bc3292 Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Wed, 3 Jan 2024 17:09:42 -0500
Subject: [PATCH 2/3] Format

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

diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index 92182c5d24b51c..b399e9f0e3f192 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -618,8 +618,8 @@ static void checkFoldResultTypes(Operation *op,
   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");
       }

>From 503527cc8306152b8efd513342f13cd39976cb85 Mon Sep 17 00:00:00 2001
From: Jakub Kuderski <jakub at nod-labs.com>
Date: Thu, 4 Jan 2024 10:52:56 -0500
Subject: [PATCH 3/3] Use emitOpError

---
 mlir/lib/IR/Operation.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mlir/lib/IR/Operation.cpp b/mlir/lib/IR/Operation.cpp
index b399e9f0e3f192..311f5bb5ef77c0 100644
--- a/mlir/lib/IR/Operation.cpp
+++ b/mlir/lib/IR/Operation.cpp
@@ -618,9 +618,9 @@ static void checkFoldResultTypes(Operation *op,
   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";
+        op->emitOpError() << "folder produced a value of incorrect type: "
+                          << opResult.getType()
+                          << ", expected: " << value.getType();
         assert(false && "incorrect fold result type");
       }
     }



More information about the Mlir-commits mailing list