[Mlir-commits] [mlir] [mlir][linalg] Restrict linalg.pack to not have artificial padding. (PR #149624)

Han-Chung Wang llvmlistbot at llvm.org
Tue Jul 22 11:48:37 PDT 2025


================
@@ -263,6 +309,24 @@ struct FoldUnpackWithExtractSliceOp
           sliceOp, "expects offsets to be 0s and strides to be 1s");
     }
 
+    // Folding is not allowed if any tile is dropped.
+    RankedTensorType unpackedType = sliceOp.getResultType();
+    SmallVector<int64_t> outerShapeWithoutTranspose =
+        getPackedOuterShapeWithoutTransposition(unpackOp);
+    for (auto [pos, tileSize] : llvm::zip_equal(
+             unpackOp.getInnerDimsPos(), unpackOp.getStaticInnerTiles())) {
+      if (unpackedType.isDynamicDim(pos))
+        return failure();
+      if (ShapedType::isDynamic(outerShapeWithoutTranspose[pos]))
+        return failure();
+      if (ShapedType::isDynamic(tileSize))
+        return failure();
+      int64_t paddingSize = outerShapeWithoutTranspose[pos] * tileSize -
+                            unpackedType.getDimSize(pos);
+      if (paddingSize >= tileSize)
+        return failure();
+    }
----------------
hanhanW wrote:

After the refactoring and update, I found that they are basically the same pattern except:

- The pattern has control function while the canonicalization pattern requires a single user which is the extract_slice op.
- There is a small bug in the folding pattern because it always creates a new tensor.empty op. The correct version should be the canonicalization one that gets the slice from the dest.

Anyway, I'd like to scope the PR to the semantic check, and I can fix the pattern one in a follow-up.

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


More information about the Mlir-commits mailing list