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

Jianbang Yang llvmlistbot at llvm.org
Wed Mar 20 03:50:23 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());
----------------
dynamicheart wrote:

I read the code comment again and think you are right.  It seems `tensor.dim` ops with out-of-bound indices are treated as valid IR and may have following handling solutions:
- Compiled to runnable binary and raise exception on runtime
- Handled in other passes to raise warning or errors on compile time depends on the compiler design.

So should I just simply fix the out-of-bound access bug in the PatternRewriter `FoldDimOfAllocTensorOp` and keep this folding behavior as before. What's your opinion?

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


More information about the Mlir-commits mailing list