[Mlir-commits] [mlir] [mlir][linalg] Fix UBSan division-by-zero in PackOp folding (PR #186271)

Andrzej WarzyƄski llvmlistbot at llvm.org
Tue Mar 24 06:57:44 PDT 2026


================
@@ -134,6 +134,24 @@ func.func @pack_32x1_to_16x1x1x2(%arg0 : tensor<32x1xf32>) -> tensor<16x1x1x2xf3
 
 // -----
 
+// Zero tile size: pack must not be folded into expand_shape.
+// CHECK-LABEL: func.func @pack_zero_tile_not_folded
+// CHECK-NOT:     tensor.expand_shape
+// CHECK:         linalg.pack
+func.func @pack_zero_tile_not_folded(%A: tensor<7x16xi32>) -> tensor<1x16x?x1xi32> {
+  %pad_val = arith.constant 123 : i32
+  %tile_size = arith.constant 0 : index
+  %empty = tensor.empty(%tile_size) : tensor<1x16x?x1xi32>
+  %pack = linalg.pack %A
+    padding_value(%pad_val : i32)
+    inner_dims_pos = [0, 1]
+    inner_tiles = [%tile_size, 1]
+    into %empty : tensor<7x16xi32> -> tensor<1x16x?x1xi32>
+  return %pack : tensor<1x16x?x1xi32>
+}
----------------
banach-space wrote:

ATM, this example fails verification (due to `0` tile size). This PR removes that verification, but then `SimplifyPackToExpandShape` bails out due to:
```
** Match Failure : expects no padding value
```

Out of two fixes in this PR:
> Two fixes:
>
>Guard FoldTensorCastPackOp: bail out early if any of the resolved tile sizes is zero, preventing the invalid fold entirely.
>
> Restrict the hasZeros check in commonVerifierPackAndUnPackOp to only inspect Attribute operands (statically-known zeros), not dynamic Value operands. The verifier can only meaningfully reject zero tiles that are statically visible; dynamic zeros are an inherently runtime condition.

this seems to only "verify" the 2nd one, no?

https://github.com/llvm/llvm-project/pull/186271


More information about the Mlir-commits mailing list