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

[MLIR] Missed Optimization: Sparse vectorization rejects loop-invariant block arguments

Open dev-816 opened this issue 2 months ago • 3 comments

isInvariantValue requires a defining operation, so it does not recognize function arguments as loop-invariant values. Consequently, the supported contiguous access pattern a[base + i] is rejected when base is a function argument. The same omission affects invariant shift amounts passed as function arguments.

Code Example

input.mlir

func.func @invariant_base(%a: memref<?xi32>, %base: index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c64 = arith.constant 64 : index
  %two = arith.constant 2 : i32
  scf.for %i = %c0 to %c64 step %c1 {
    %p = arith.addi %base, %i : index
    %x = memref.load %a[%p] : memref<?xi32>
    %y = arith.muli %x, %two : i32
    memref.store %y, %a[%p] : memref<?xi32>
  } {"Emitted from" = "linalg.generic"}
  return
}

Command

mlir-opt input.mlir --sparse-vectorization='vl=4'

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

dev-816 avatar Oct 01 '26 16:10 dev-816

@llvm/issue-subscribers-mlir

Author: CC_Fans (dev-816)

`isInvariantValue` requires a defining operation, so it does not recognize function arguments as loop-invariant values. Consequently, the supported contiguous access pattern `a[base + i]` is rejected when `base` is a function argument. The same omission affects invariant shift amounts passed as function arguments.

Code Example

input.mlir

func.func @<!-- -->invariant_base(%a: memref&lt;?xi32&gt;, %base: index) {
  %c0 = arith.constant 0 : index
  %c1 = arith.constant 1 : index
  %c64 = arith.constant 64 : index
  %two = arith.constant 2 : i32
  scf.for %i = %c0 to %c64 step %c1 {
    %p = arith.addi %base, %i : index
    %x = memref.load %a[%p] : memref&lt;?xi32&gt;
    %y = arith.muli %x, %two : i32
    memref.store %y, %a[%p] : memref&lt;?xi32&gt;
  } {"Emitted from" = "linalg.generic"}
  return
}

Command

mlir-opt input.mlir --sparse-vectorization='vl=4'

Version: 6f54fe6b6fb22f8a335fd19b94049fc769dfea25

hey, can this issue be assigned? I was interested

Vara715 avatar Oct 01 '26 17:10 Vara715

@Vara715 I don't have permission to assign issues. If you are interested, please submit a PR to fix this bug and @ relevant developers.

dev-816 avatar Oct 02 '26 03:10 dev-816