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 ![]()
