This is an archive of the discontinued LLVM Phabricator instance.

[MLIR] Fail instead of asserting while computing memref strides
Needs RevisionPublic

Authored by bondhugula on Nov 30 2022, 7:56 PM.

Details

Summary

Fail getStridesAndOffset utility instead of asserting during an integer
overflow while computing memref lowering strides. The utility is already
designed to fail in two other scenarios. Update the underlying
makeCanonicalStridedLayoutExpr to fail instead of assert. Asserting
leads to a worse user experience in the conversions layered on top.

Diff Detail

Event Timeline

bondhugula created this revision.Nov 30 2022, 7:56 PM
bondhugula requested review of this revision.Nov 30 2022, 7:56 PM

Remove stray change.

bondhugula added a comment.EditedNov 30 2022, 8:10 PM

Despite this change, LLVM type conversion's convertMemRefType would still assert because it asserts on finding a valid strided type; but this change still makes the situation better.

ftynse requested changes to this revision.Dec 1 2022, 12:42 AM
ftynse added inline comments.
mlir/lib/IR/BuiltinTypes.cpp
992–993

This is checking for the result of undefined behavior (singled integer overflow). I see it was checking that before, but let's not perpetuate this. If we actually want to detect overflow, let's use APInt with quad bitwidth for multiplication and see if the result fits.

This revision now requires changes to proceed.Dec 1 2022, 12:42 AM
bondhugula added inline comments.Dec 1 2022, 6:08 AM
mlir/lib/IR/BuiltinTypes.cpp
992–993

Yes, but that's not the purpose of this patch. That can be addressed in a separate patch. This is strictly better on the handling part.

ftynse added inline comments.Dec 1 2022, 6:12 AM
mlir/lib/IR/BuiltinTypes.cpp
992–993

Given that an undefined behavior happens in this flow, there is no guarantee that the error message will be emitted. The compiler may optimize away the branch. This may result in a segfault elsewhere, all depending on the compiler version and optimization flags. I don't see how that is better. We cannot even test reliably.

bondhugula added inline comments.Dec 1 2022, 6:51 AM
mlir/lib/IR/BuiltinTypes.cpp
992–993

Wouldn't it have been worse with the assert? (since you won't have it on the release no- assert-build.) This patch is just exposing the issue and keeping the condition ditto. In fact, not exposing this would have more likely perpetuated it. I won't be changing the condition itself and it should be changed in a separate commit. I can rebase on top if someone else wants to fix that. (From the history, the safety check was added by @aartbik.)

ftynse added a comment.Dec 1 2022, 7:15 AM

I see asserts as messages for developers that they didn't handle preconditions properly and that the program would eventually crash. A debugging aid that may or may not be there. On the other hand, diagnostics are user-visible messages that should remain consistent across platforms. That's why we have an implicit convention of adding a test for every diagnostics, which would be problematic here. Diagnostics also typically indicate a problem with the IR the user sent to the compiler that the user can act on. While here it is likely that the overflow may be caused by some input IR, there is no indication whether it is indeed the case and how the user can remedy the problem.

I see asserts as messages for developers that they didn't handle preconditions properly and that the program would eventually crash. A debugging aid that may or may not be there. On the other hand, diagnostics are user-visible messages that should remain consistent across platforms. That's why we have an implicit convention of adding a test for every diagnostics, which would be problematic here. Diagnostics also typically indicate a problem with the IR the user sent to the compiler that the user can act on. While here it is likely that the overflow may be caused by some input IR, there is no indication whether it is indeed the case and how the user can remedy the problem.

I can wait until the condition is properly fixed, after which this can be rebased.