llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[mlir][vector] Negative transfer starts are treated as in-bounds: canonicalization sets in_bounds=true and VectorToSCF guards only the upper bound

Open snarang181 opened this issue 2 months ago • 4 comments

Summary

vector.transfer_read/transfer_write document that with in_bounds = false the accesses, including the starting point, may run out of bounds. Two independent paths assume only the upper end can overrun, so a negative starting index is treated as in-bounds.

1. The canonicalizer asserts a safety attribute that is false. isInBounds (mlir/lib/Dialect/Vector/IR/VectorOps.cpp:5391) ends with:

int64_t sourceSize = op.getShapedType().getDimSize(indicesIdx);
int64_t vectorSize = op.getVectorType().getDimSize(resultIdx);

return cstOp.value() + vectorSize <= sourceSize;

There is no cstOp.value() >= 0 conjunct. For a start of -1 into a memref<4xf32> with vector<3xf32>, this computes -1 + 3 = 2 <= 4 and returns true, so foldTransferInBoundsAttribute rewrites in_bounds = [false] to [true] under plain -canonicalize.

2. VectorToSCF guards only the upper bound. generateInBoundsCheck (mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp) emits

%3 = arith.cmpi slt, %2, %c4 : index
scf.if %3 { ... } else { ... }

with no %2 >= 0 conjunct, so a negative index enters the then branch and performs a scalar access below the allocation instead of padding the read or suppressing the write.

Both reproduce at 31a1c03fe67c.

Reproducers

Canonicalization flips the attribute

func.func @negative_start(%a: memref<4xf32>) -> vector<3xf32> {
  %cm1 = arith.constant -1 : index
  %pad = arith.constant -42.0 : f32
  %v = vector.transfer_read %a[%cm1], %pad {in_bounds = [false]}
    : memref<4xf32>, vector<3xf32>
  return %v : vector<3xf32>
}
mlir-opt -canonicalize repro.mlir
%0 = vector.transfer_read %arg0[%c-1], %cst {in_bounds = [true]} : memref<4xf32>, vector<3xf32>

The transfer starts one element below the buffer, and is now marked as guaranteed in-bounds. Any consumer that trusts the attribute will skip the bounds handling this transfer requires.

The lowered guard misses the lower bound

func.func @neg2d(%a: memref<4x8xf32>, %i: index) -> vector<2x8xf32> {
  %pad = arith.constant -42.0 : f32
  %v = vector.transfer_read %a[%i, %i], %pad {in_bounds = [false, true]}
    : memref<4x8xf32>, vector<2x8xf32>
  return %v : vector<2x8xf32>
}
mlir-opt -convert-vector-to-scf repro2.mlir
scf.for %arg2 = %c0 to %c2 step %c1 {
  %2 = affine.apply #map(%arg2)[%arg1]
  %3 = arith.cmpi slt, %2, %c4 : index      // upper bound only
  scf.if %3 {
    %5 = vector.transfer_read %arg0[%4, %arg1], %cst_0 {in_bounds = [true]} ...

For a negative %i the condition holds and the guarded access runs below the source.

Why this is a bug and not intended behaviour

The op documentation is explicit that the starting point is covered:

For every vector dimension, the boolean array attribute in_bounds specifies if the transfer is guaranteed to be within the source bounds. If set to "false", accesses (including the starting point) may run out-of-bounds along the respective vector dimension as the index increases.

So a negative start is a legal input for in_bounds = false, and both the in_bounds inference and the lowered guard must establish start >= 0 before treating the access as safe.

Environment

  • llvm-project at 31a1c03fe67c
  • Release build with assertions, Linux x86-64

snarang181 avatar Sep 13 '26 17:09 snarang181

@llvm/issue-subscribers-mlir

Author: Samarth Narang (snarang181)

## Summary

vector.transfer_read/transfer_write document that with in_bounds = false the accesses, including the starting point, may run out of bounds. Two independent paths assume only the upper end can overrun, so a negative starting index is treated as in-bounds.

1. The canonicalizer asserts a safety attribute that is false. isInBounds (mlir/lib/Dialect/Vector/IR/VectorOps.cpp:5391) ends with:

int64_t sourceSize = op.getShapedType().getDimSize(indicesIdx);
int64_t vectorSize = op.getVectorType().getDimSize(resultIdx);

return cstOp.value() + vectorSize &lt;= sourceSize;

There is no cstOp.value() &gt;= 0 conjunct. For a start of -1 into a memref&lt;4xf32&gt; with vector&lt;3xf32&gt;, this computes -1 + 3 = 2 &lt;= 4 and returns true, so foldTransferInBoundsAttribute rewrites in_bounds = [false] to [true] under plain -canonicalize.

2. VectorToSCF guards only the upper bound. generateInBoundsCheck (mlir/lib/Conversion/VectorToSCF/VectorToSCF.cpp) emits

%3 = arith.cmpi slt, %2, %c4 : index
scf.if %3 { ... } else { ... }

with no %2 &gt;= 0 conjunct, so a negative index enters the then branch and performs a scalar access below the allocation instead of padding the read or suppressing the write.

Both reproduce at 31a1c03fe67c.

Reproducers

Canonicalization flips the attribute

func.func @<!-- -->negative_start(%a: memref&lt;4xf32&gt;) -&gt; vector&lt;3xf32&gt; {
  %cm1 = arith.constant -1 : index
  %pad = arith.constant -42.0 : f32
  %v = vector.transfer_read %a[%cm1], %pad {in_bounds = [false]}
    : memref&lt;4xf32&gt;, vector&lt;3xf32&gt;
  return %v : vector&lt;3xf32&gt;
}
mlir-opt -canonicalize repro.mlir
%0 = vector.transfer_read %arg0[%c-1], %cst {in_bounds = [true]} : memref&lt;4xf32&gt;, vector&lt;3xf32&gt;

The transfer starts one element below the buffer, and is now marked as guaranteed in-bounds. Any consumer that trusts the attribute will skip the bounds handling this transfer requires.

The lowered guard misses the lower bound

func.func @<!-- -->neg2d(%a: memref&lt;4x8xf32&gt;, %i: index) -&gt; vector&lt;2x8xf32&gt; {
  %pad = arith.constant -42.0 : f32
  %v = vector.transfer_read %a[%i, %i], %pad {in_bounds = [false, true]}
    : memref&lt;4x8xf32&gt;, vector&lt;2x8xf32&gt;
  return %v : vector&lt;2x8xf32&gt;
}
mlir-opt -convert-vector-to-scf repro2.mlir
scf.for %arg2 = %c0 to %c2 step %c1 {
  %2 = affine.apply #map(%arg2)[%arg1]
  %3 = arith.cmpi slt, %2, %c4 : index      // upper bound only
  scf.if %3 {
    %5 = vector.transfer_read %arg0[%4, %arg1], %cst_0 {in_bounds = [true]} ...

For a negative %i the condition holds and the guarded access runs below the source.

Why this is a bug and not intended behaviour

The op documentation is explicit that the starting point is covered:

> For every vector dimension, the boolean array attribute in_bounds specifies > if the transfer is guaranteed to be within the source bounds. If set to > "false", accesses (including the starting point) may run out-of-bounds > along the respective vector dimension as the index increases.

So a negative start is a legal input for in_bounds = false, and both the in_bounds inference and the lowered guard must establish start &gt;= 0 before treating the access as safe.

Suggested fix

Require a non-negative start in isInBounds:

return cstOp.value() &gt;= 0 &amp;&amp; cstOp.value() + vectorSize &lt;= sourceSize;

and conjoin a lower-bound check in generateInBoundsCheck, so the guard is 0 &lt;= idx &amp;&amp; idx &lt; dim rather than idx &lt; dim.

A third site has the same assumption and is worth checking in the same change: MaterializeTransferMask (mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp) builds vector.create_mask(dim - start). For a negative start that extent is larger, not smaller, so clamping enables the low lanes that sit below the source while the pattern also sets in_bounds to true.

Regression test

A transfer with a statically negative start and in_bounds = [false] should keep that attribute under -canonicalize, and its VectorToSCF lowering should guard both ends of the range.

Environment

  • llvm-project at 31a1c03fe67c
  • Release build with assertions, Linux x86-64

I'd like to take this on, will have a PR up shortly covering both the canonicalizer fold and the VectorToSCF guard.

cc @dcaballe @Groverkss

StarOne01 avatar Sep 14 '26 14:09 StarOne01

Status update: the canonicalizer side of this (isInBounds in VectorOps.cpp) has already been fixed and merged in #219681 before 2 weeks.

The VectorToSCF side (generateInBoundsCheck) is open no fix in main yet. There's a draft in progress at @ammardab3an fork using an unsigned comparison instead of signed, which looks like the right approach.

StarOne01 avatar Sep 15 '26 14:09 StarOne01

LLVM Discourse Discussion: https://discourse.llvm.org/t/rfc-vector-should-vector-transfer-read-transfer-write-indices-be-treated-as-unsigned-non-negative/91869

FedericoBruzzone avatar Sep 20 '26 09:09 FedericoBruzzone