[MLIR][Tensor] Avoid crash on negative dimensions
authorRik Huijzer <github@huijzer.xyz>
Thu, 20 Jul 2023 08:09:34 +0000 (10:09 +0200)
committerRik Huijzer <github@huijzer.xyz>
Thu, 20 Jul 2023 08:09:34 +0000 (10:09 +0200)
commit8b61ae4e931842cf909c0365664eb79b65253598
treecb6d8a8962b03f75a250b2145eb134552a35d41e
parent91ccbc6c1c4c121935ee4fbfa0db13ad86590a59
[MLIR][Tensor] Avoid crash on negative dimensions

In https://reviews.llvm.org/D151611, a check was added to the tensor verifier to
emit an error on negative tensor dimensions. This check allowed for dynamic
dimensions, hence negative dimensions were still able to get through the verifier.
This is a problem in situations such as #60558, where the dynamic dimension is
converted to a static (and possibly negative) dimension by another pass in the
compiler. This patch fixes that by doing another check during the
`StaticTensorGenerate` conversion, and return a failure if the dimension is
negative.

As a side-note, I have to admit that I do not know why returning a failure in
`StaticTensorGenerate` gives a nice "tensor dimensions must be non-negative"
error. I suspect that the verifier runs again when `return failure()` is called,
but I am not sure.

Fixes #60558.

Reviewed By: mehdi_amini

Differential Revision: https://reviews.llvm.org/D155728
mlir/lib/Dialect/Tensor/IR/TensorOps.cpp
mlir/test/Dialect/Tensor/invalid-canonicalize.mlir [new file with mode: 0644]