[Mlir-commits] [mlir] [mlir][tensor] fix out-of-bound index in tensor.dim (PR #85901)

Mehdi Amini llvmlistbot at llvm.org
Sun Mar 24 22:17:30 PDT 2024


================
@@ -738,11 +741,11 @@ OpFoldResult DimOp::fold(FoldAdaptor adaptor) {
   if (!tensorType)
     return {};
 
-  // Out of bound indices produce undefined behavior but are still valid IR.
-  // Don't choke on them.
+  // Fold dim to posion if the index is out of bound. Poison represents
+  // undefined behavior.
   int64_t indexVal = index.getInt();
   if (indexVal < 0 || indexVal >= tensorType.getRank())
-    return {};
+    return ub::PoisonAttr::get(getContext());
----------------
joker-eph wrote:

>  It seems tensor.dim ops with out-of-bound indices are treated as valid IR 

It is documented as undefined behavior, so turning it into poison is clearly allowed by the spec.
However at the moment we haven't deployed the UB dialect as a really "central" component, and "fold" is, even more than canonicalization, very anchored in the core of MLIR (it's not optional, can't be disabled, etc.). 
So folding to poison makes me a bit nervous here, if there were already other folding of the tensor dialect to poison that wouldn't be a problem.

> Can we simply skip folding and do nothing 

This is the most conservative solution IMO.


https://github.com/llvm/llvm-project/pull/85901


More information about the Mlir-commits mailing list