[Mlir-commits] [mlir] [mlir][memref] Allow out-of-bounds semantics for memref.subview (PR #152164)

Kunwar Grover llvmlistbot at llvm.org
Wed Aug 6 02:47:35 PDT 2025


Groverkss wrote:

> > My question is what do we mean by "compile-time"? Is it at any point in the pipeline (and therefore the behaviour recently added is "ok")? Or at egress (and then terms like canonicalization make more sense)?
> 
> I think I wrote that sentence. By "compile-time information" I meant "looking at the attributes + type of the memref.subview operation".
> 
> > What would be the case we'd want to have an out-of-bounds all constant indexing into a static shape to be valid?
> 
> I have asked myself the same question.
> 
> In the current design, whether an op is considered valid is different depending on whether it can be detected based on (a) looking at attributes and types or (b) looking at attributes, types, values and other operations. That situation being a bit odd is [one argument](https://discourse.llvm.org/t/out-of-bounds-semantics-of-memref-subview/85293/10). I didn't buy it at first because I was used to it (we have many MLIR ops that work like that), but I'm starting to warm up to the idea... Canonicalizers + folders will require fewer special cases.
> 
> Instead, we could have an additional folder that folds such `memref.subview` (out-of-bounds and all constant indexing) to `ub.poison`. (The dynamic -> static folder/pattern would not have to care about out-of-bounds.)

I don't know if it's correct to fold it to poison. It is only correct to fold it to poison if the entire subview is out of bounds. I don't think there is a notion of a slice being poison. For example:

```
%subview = memref.subview %sub[3][2][1] : memref<4xf32> -> memref<2xf32>
memref.load %subview[0] : f32
```

this is valid, we never access any out of bounds memory and there is no undefined behavior here.

```
%subview = memref.subview %sub[3][2][1] : memref<4xf32> -> memref<2xf32>
memref.load %subview[1] : f32
```

this is invalid, and there is undefined behavior here.

We can fold a subview to poison, if the entire subview is out-of-bounds.

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


More information about the Mlir-commits mailing list