Skip to content

[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

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions mlir/lib/IR/Operation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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()) {
op->emitOpError() << "folder produced a value of incorrect type: "
<< opResult.getType()
<< ", expected: " << value.getType();
assert(false && "incorrect fold result type");
Copy link
Member

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 ...");

Copy link
Member Author

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

Copy link
Collaborator

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).

}
}
}
}
#endif // NDEBUG

Expand Down