[Mlir-commits] [mlir] [mlir][affine] Add folders for delinearize_index and linearize_index (PR #115766)

Krzysztof Drewniak llvmlistbot at llvm.org
Wed Nov 13 09:37:34 PST 2024


================
@@ -4820,11 +4860,39 @@ struct CancelLinearizeOfDelinearizeExact final
     return success();
   }
 };
+
+/// Strip leading zero from affine.linearize_index.
+///
+/// `affine.linearize_index [%c0, ...a] by (%x, ...b)` can be rewritten
+/// to `affine.linearize_index [...a] by (...b)` in all cases.
+struct DropLinearizeLeadingZero final
+    : OpRewritePattern<affine::AffineLinearizeIndexOp> {
+  using OpRewritePattern::OpRewritePattern;
+
+  LogicalResult matchAndRewrite(affine::AffineLinearizeIndexOp op,
+                                PatternRewriter &rewriter) const override {
+    Value leadingIdx = op.getMultiIndex().front();
----------------
krzysz00 wrote:

This is
```
%0 = affine.linearize_index [%c0, %x, %y] by (A, B, C)
```
going to
```
%0 = affine.linearize_index [%x, %y] by (B, C)
```

We can't do
```
%0 = affine.linearize_index [%x, %c0, %y] by (A, B, C)
```
to
```
%0 = affine.linearize_index [%x, %y] by (A, C)
```
since that has an `%x * C` term and not a `%x * BC` term.

Whether or not canonicalizing that to
```
%0 = affine.linearize_index [%x, %y] (A, BC)
```
is profitable is something I'm not sure about yet.

(We definitely can't drop *trailing* zeroes, since they'd be used to express `%y * C`-type operations)

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


More information about the Mlir-commits mailing list