-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[mlir] Make fold result type check more verbose #76867
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
Conversation
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-core Author: Jakub Kuderski (kuhar) ChangesPrint 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. Full diff: https://github.com/llvm/llvm-project/pull/76867.diff 1 Files Affected:
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
|
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.
✅ With the latest revision this PR passed the C/C++ code formatter. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. This change helped a lot in debugging downstream folders.
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"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: You can use llvm_unreachable("incorrect ...");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do want the error message to be printed though and that code to be reachable. Probably doesn't matter as long as this code if wrapped in NDEBUG, but I remember there was a long discussion on this at some point that made me hesitant to use unreachable in cases like this one: https://discourse.llvm.org/t/llvm-unreachable-is-widely-misused/60587
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I remember there was a long discussion on this at some point that made me hesitant to use unreachable in cases like this one: https://discourse.llvm.org/t/llvm-unreachable-is-widely-misused/60587
In LLVM, I don't see any case for assert(false
.
I believe we should always use llvm_unreachable
instead. We already have means to control the behavior:
6316129e066e
(this patch is at the end of the thread you link to).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! I am a big proponent of making this kind of error as verbose as needed.
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.