[mlir][linalg] Runtime verification checks nothing when the iteration domain is empty
[!WARNING]
Discovered while working on tensor shape checking via interprocedural dataflow analysis. The following issue is AI-generated. Unfortunately, I'm currently unable to look into the matter any further. I apologize.
Tested with LLVM 22.1.2 (Homebrew build of the release).
Problem
With static shapes the verifier rejects a linalg.add whose operands disagree
on a dimension, even when one of them is zero:
func.func @add(%a: tensor<0xf32>, %b: tensor<3xf32>, %o: tensor<0xf32>) -> tensor<0xf32> {
%r = linalg.add ins(%a, %b : tensor<0xf32>, tensor<3xf32>) outs(%o : tensor<0xf32>) -> tensor<0xf32>
return %r : tensor<0xf32>
}
// error: 'linalg.add' op unexpected result less than 0 at expression #0 in (d0) -> (d0)
With dynamic shapes the same program is accepted by the runtime verification
when the sizes are 0 and 3, and rejected when they are 2 and 3. So the
check depends on whether the iteration domain is empty, and a size mismatch
goes unreported in that case.
Reproducer
func.func @add(%a: tensor<?xf32>, %b: tensor<?xf32>, %o: tensor<?xf32>) -> tensor<?xf32> {
%r = linalg.add ins(%a, %b : tensor<?xf32>, tensor<?xf32>) outs(%o : tensor<?xf32>) -> tensor<?xf32>
return %r : tensor<?xf32>
}
func.func @run(%na: index, %nb: index) {
%a = tensor.empty(%na) : tensor<?xf32>
%b = tensor.empty(%nb) : tensor<?xf32>
%o = tensor.empty(%na) : tensor<?xf32>
%r = func.call @add(%a, %b, %o) : (tensor<?xf32>, tensor<?xf32>, tensor<?xf32>) -> tensor<?xf32>
return
}
func.func @main() {
%c0 = arith.constant 0 : index
%c2 = arith.constant 2 : index
%c3 = arith.constant 3 : index
func.call @run(%c0, %c3) : (index, index) -> ()
vector.print %c0 : index
func.call @run(%c2, %c3) : (index, index) -> ()
vector.print %c2 : index
return
}
Pipeline:
mlir-opt --generate-runtime-verification repro.mlir \
--convert-elementwise-to-linalg \
--one-shot-bufferize=bufferize-function-boundaries \
--buffer-deallocation-pipeline --convert-bufferization-to-memref \
--canonicalize --cse --convert-linalg-to-loops --convert-scf-to-cf \
--expand-strided-metadata --lower-affine --convert-vector-to-llvm \
--convert-arith-to-llvm --convert-index-to-llvm --finalize-memref-to-llvm \
--convert-func-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts \
| mlir-runner -e main -entry-point-result=void \
--shared-libs=libmlir_runner_utils.dylib,libmlir_c_runner_utils.dylib
Output:
0
ERROR: Runtime op verification failed
%0 = linalg.add ins(%arg0, %arg1 : tensor<?xf32>, tensor<?xf32>) outs(%arg2 : tensor<?xf32>) -> tensor<?xf32>
^ dimension #0 of input/output operand #1 is incompatible with inferred dimension size
The first call (0 and 3) prints 0 and passes. The second (2 and 3)
asserts.
Question
Is it intended that the shape compatibility is only checked when the domain is
not empty? The static verifier treats 0 against 3 as an error, so this makes
the dynamic case weaker. If it is not intended, the check can be done
unconditionally for every operand dimension.
@llvm/issue-subscribers-mlir
Author: Federico Bruzzone (FedericoBruzzone)
Tested with LLVM 22.1.2 (Homebrew build of the release).
Problem
With static shapes the verifier rejects a linalg.add whose operands disagree
on a dimension, even when one of them is zero:
func.func @<!-- -->add(%a: tensor<0xf32>, %b: tensor<3xf32>, %o: tensor<0xf32>) -> tensor<0xf32> {
%r = linalg.add ins(%a, %b : tensor<0xf32>, tensor<3xf32>) outs(%o : tensor<0xf32>) -> tensor<0xf32>
return %r : tensor<0xf32>
}
// error: 'linalg.add' op unexpected result less than 0 at expression #<!-- -->0 in (d0) -> (d0)
With dynamic shapes the same program is accepted by the runtime verification
when the sizes are 0 and 3, and rejected when they are 2 and 3. So the
check depends on whether the iteration domain is empty, and a size mismatch
goes unreported in that case.
Reproducer
func.func @<!-- -->add(%a: tensor<?xf32>, %b: tensor<?xf32>, %o: tensor<?xf32>) -> tensor<?xf32> {
%r = linalg.add ins(%a, %b : tensor<?xf32>, tensor<?xf32>) outs(%o : tensor<?xf32>) -> tensor<?xf32>
return %r : tensor<?xf32>
}
func.func @<!-- -->run(%na: index, %nb: index) {
%a = tensor.empty(%na) : tensor<?xf32>
%b = tensor.empty(%nb) : tensor<?xf32>
%o = tensor.empty(%na) : tensor<?xf32>
%r = func.call @<!-- -->add(%a, %b, %o) : (tensor<?xf32>, tensor<?xf32>, tensor<?xf32>) -> tensor<?xf32>
return
}
func.func @<!-- -->main() {
%c0 = arith.constant 0 : index
%c2 = arith.constant 2 : index
%c3 = arith.constant 3 : index
func.call @<!-- -->run(%c0, %c3) : (index, index) -> ()
vector.print %c0 : index
func.call @<!-- -->run(%c2, %c3) : (index, index) -> ()
vector.print %c2 : index
return
}
Pipeline:
mlir-opt --generate-runtime-verification repro.mlir \
--convert-elementwise-to-linalg \
--one-shot-bufferize=bufferize-function-boundaries \
--buffer-deallocation-pipeline --convert-bufferization-to-memref \
--canonicalize --cse --convert-linalg-to-loops --convert-scf-to-cf \
--expand-strided-metadata --lower-affine --convert-vector-to-llvm \
--convert-arith-to-llvm --convert-index-to-llvm --finalize-memref-to-llvm \
--convert-func-to-llvm --convert-cf-to-llvm --reconcile-unrealized-casts \
| mlir-runner -e main -entry-point-result=void \
--shared-libs=libmlir_runner_utils.dylib,libmlir_c_runner_utils.dylib
Output:
0
ERROR: Runtime op verification failed
%0 = linalg.add ins(%arg0, %arg1 : tensor<?xf32>, tensor<?xf32>) outs(%arg2 : tensor<?xf32>) -> tensor<?xf32>
^ dimension #<!-- -->0 of input/output operand #<!-- -->1 is incompatible with inferred dimension size
The first call (0 and 3) prints 0 and passes. The second (2 and 3)
asserts.
Question
Is it intended that the shape compatibility is only checked when the domain is
not empty? The static verifier treats 0 against 3 as an error, so this makes
the dynamic case weaker. If it is not intended, the check can be done
unconditionally for every operand dimension.
Heads up, the reproducer uses linalg.add, which was removed #220912, so the repro fails in recent tree.
use linalg.elementwise instead:
%r = linalg.elementwise <add> ins(%a, %b : tensor<?xf32>, tensor<?xf32>) outs(%o : tensor<?xf32>) -> tensor<?xf32>