[mlir][affine] Affine-super-vectorize does not set in_bounds on transfer ops for statically divisible shapes

Hi, I am new to MLIR and while working on a pipeline lowering Torch-MLIR to LLVM, where I wanted to vectorize a vector_add function for NEON on Apple M5, I noticed that --affine-super-vectorize emits vector.transfer_read and vector.transfer_write without in_bounds = [true] even when the memref size is static and divisible by the vector width. This causes --convert-vector-to-llvm to emit llvm.masked.load instead of plain loads, with a measurable ~3x performance penalty.

Environment:

- LLVM version: 23.0.0git (trunk)

- Target: Apple M5 Pro (AArch64), macOS

Reproducer:

Input MLIR (torch_output.mlir)

module {
  func.func @vec_add(%arg0: tensor<131072xf32>, %arg1: tensor<131072xf32>) 
      -> tensor<131072xf32> {
    %0 = tensor.empty() : tensor<131072xf32>
    %1 = linalg.generic {
      indexing_maps = [affine_map<(d0) -> (d0)>, affine_map<(d0) -> (d0)>, 
                       affine_map<(d0) -> (d0)>],
      iterator_types = ["parallel"]
    } ins(%arg0, %arg1 : tensor<131072xf32>, tensor<131072xf32>)
      outs(%0 : tensor<131072xf32>) {
    ^bb0(%in: f32, %in_0: f32, %out: f32):
      %2 = arith.addf %in, %in_0 : f32
      linalg.yield %2 : f32
    } -> tensor<131072xf32>
    return %1 : tensor<131072xf32>
  }
}

Pipeline:

mlir-opt torch_output.mlir
–one-shot-bufferize=“allow-unknown-ops=0 bufferize-function-boundaries=1 function-boundary-type-conversion=identity-layout-map”
–convert-linalg-to-affine-loops
–affine-super-vectorize=“virtual-vector-size=4”
–canonicalize

Result: transfer ops missing in_bounds:

affine.for %arg2 = 0 to 131072 step 4 {
  %1 = vector.transfer_read %arg0[%arg2], %0 : memref<131072xf32>, vector<4xf32>
  %2 = vector.transfer_read %arg1[%arg2], %0 : memref<131072xf32>, vector<4xf32>
  %4 = arith.addf %1, %2 : vector<4xf32>
  vector.transfer_write %4, %alloc[%arg2] : vector<4xf32>, memref<131072xf32>
}

This causes --convert-vector-to-llvm to emit masked loads:

%19 = call <4 x float> @llvm.masked.load.v4f32.p0(ptr align 4 %18, 
                                                    <4 x i1> %17, 
                                                    <4 x float> poison)
%22 = fadd <4 x float> %19, %21

Expected: with in_bounds = [true], the lowering would produce plain loads:

%14 = load <4 x float>, ptr %13, align 4 
%17 = fadd <4 x float> %14, %16 

Performance impact: (measured on Apple M5 Pro, 131072xf32, vector width 4):

- Without in_bounds (masked loads), same as scalar execution : ~76 µs

- With in_bounds = [true] (plain loads): ~22 µs

- ~3x overhead from masking

Proposed fix:

The Linalg vectorizer already handles this via useInBoundsInsteadOfMasking in Vectorization.cpp. The same logic could be applied in SuperVectorize.cpp at the point where vector.transfer_read` is emitted: if the memref dimension size is static and divisible by the vector width, set in_bounds = [true] for that dimension. Is this the right place to fix it, or is there a design reason for the behavior in the affine super-vectorizer?

I would like to implement this if the approach looks reasonable.

Thanks

Christian

PS: Sorry for the bad editing :smiley:

Hello :waving_hand:

You mentioned NEON and that got me interested (I work at Arm) :slight_smile:

I haven’t worked on SuperVectorize myself, but the issue you are facing sounds quite familiar. From vector.transfer_read docs :

This attribute has a default value: false (i.e. “out-of-bounds”).

I have a strong suspicion that whatever is creating those xfer Ops just doesn’t attempt to specify that attribute. It should be a relatively easy fix (though keep in mind - I have not worked in that specific area).

It’s not clear whether useInBoundsInsteadOfMasking is relevant here - it’s just a bool option for createReadOrMaskedRead to specify whether to use in_bounds instead of the mask operand.

There are patterns to remove masks so I’d imagine that it’s actually the masked version that you want instead (i.e. useInBoundsInsteadOfMasking = false) . There’s far fewer (if any) patterns to update the in_bounds attribute :wink:

Yes. I would start by trying to re-use createReadOrMaskedRead - that might be able to actually avoid the masks “auto-magically”. And even if you do end-up with masks, you should be able to leverage various patterns to remove them for you.

Good luck and let me know how it goes!

Thank you so much for the input! I will have a look at createReadOrMaskedRead and see if I can reuse it in SuperVectorize.cpp. Will report back once I have had a chance to dig into the code.

Hi :waving_hand:

I think this PR [1] can be a starting point for this problem.

[1] [mlir][affine] emit `in_bounds` on `transfer_read`/`write` when statically provable in `affine-super-vectorize` by FedericoBruzzone · Pull Request #201180 · llvm/llvm-project · GitHub

Hi @KuratorX : D

Thank you very much for reporting this issue.

I am writing to inform you that, with great suggestions from @banach-space , the problem has been resolved. The specific graph data is not important, the only thing that matters is seeing how the bar (super-vec / current) closed the gap (which, according to my analysis, is greater than ~3x :'D) relative to (super-vec/pre-fix).

If you would like more details, please don’t hesitate to ask anything.