[llvm-branch-commits] [mlir] [mlir][MemRef] Use specialized index ops to fold expand/collapse_shape (PR #138930)

2025-05-10 Thread via llvm-branch-commits

https://github.com/MaheshRavishankar approved this pull request.

The changes look good to me. It isn't strictly required,  by given that book h 
of us work on the same downstream project, does this pass with the said 
downstream project. But this looks good to me

https://github.com/llvm/llvm-project/pull/138930
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][MemRef] Use specialized index ops to fold expand/collapse_shape (PR #138930)

2025-05-09 Thread Krzysztof Drewniak via llvm-branch-commits

krzysz00 wrote:

Ping

https://github.com/llvm/llvm-project/pull/138930
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][MemRef] Use specialized index ops to fold expand/collapse_shape (PR #138930)

2025-05-07 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir-memref

Author: Krzysztof Drewniak (krzysz00)


Changes

This PR updates the FoldMemRefAliasOps to use `affine.linearize_index` and 
`affine.delinearize_index` to perform the index computations needed to fold a 
`memref.expand_shape` or `memref.collapse_shape` into its consumers, 
respectively.

This also loosens some limitations of the pass:
1. The existing `output_shape` argument to `memref.expand_shape` is now used, 
eliminating the need to re-infer this shape or call `memref.dim`.
2. Because we're using `affine.delinearize_index`, the restriction that each 
group in a `memref.collapse_shape` can only have one dynamic dimension is 
removed.

---

Patch is 31.32 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/138930.diff


3 Files Affected:

- (modified) mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td (+10-4) 
- (modified) mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp 
(+49-120) 
- (modified) mlir/test/Dialect/MemRef/fold-memref-alias-ops.mlir (+64-65) 


``diff
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td 
b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index d6d8161d3117b..f34b5b46cab50 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -1342,14 +1342,14 @@ def MemRef_ReinterpretCastOp
 according to specified offsets, sizes, and strides.
 
 ```mlir
-%result1 = memref.reinterpret_cast %arg0 to 
+%result1 = memref.reinterpret_cast %arg0 to
   offset: [9],
   sizes: [4, 4],
   strides: [16, 2]
 : memref<8x8xf32, strided<[8, 1], offset: 0>> to
   memref<4x4xf32, strided<[16, 2], offset: 9>>
 
-%result2 = memref.reinterpret_cast %result1 to 
+%result2 = memref.reinterpret_cast %result1 to
   offset: [0],
   sizes: [2, 2],
   strides: [4, 2]
@@ -1755,6 +1755,12 @@ def MemRef_ExpandShapeOp : 
MemRef_ReassociativeReshapeOp<"expand_shape", [
 OpBuilder &b, Location loc, MemRefType expandedType,
 ArrayRef reassociation,
 ArrayRef inputShape);
+
+// Return a vector with all the static and dynamic values in the output 
shape.
+SmallVector getMixedOutputShape() {
+  OpBuilder builder(getContext());
+  return ::mlir::getMixedValues(getStaticOutputShape(), getOutputShape(), 
builder);
+}
   }];
 
   let hasVerifier = 1;
@@ -1873,7 +1879,7 @@ def MemRef_StoreOp : MemRef_Op<"store",
   let summary = "store operation";
   let description = [{
 The `store` op stores an element into a memref at the specified indices.
-
+
 The number of indices must match the rank of the memref. The indices must
 be in-bounds: `0 <= idx < dim_size`
 
@@ -2025,7 +2031,7 @@ def SubViewOp : 
MemRef_OpWithOffsetSizesAndStrides<"subview", [
 Unlike the `reinterpret_cast`, the values are relative to the strided
 memref of the input (`%result1` in this case) and not its
 underlying memory.
-
+
 Example 2:
 
 ```mlir
diff --git a/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp 
b/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp
index e4fb3f9bb87ed..2acb90613e5d1 100644
--- a/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp
+++ b/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp
@@ -59,92 +59,28 @@ using namespace mlir;
 ///
 /// %2 = load %0[6 * i1 + i2, %i3] :
 ///  memref<12x42xf32>
-static LogicalResult
-resolveSourceIndicesExpandShape(Location loc, PatternRewriter &rewriter,
-memref::ExpandShapeOp expandShapeOp,
-ValueRange indices,
-SmallVectorImpl &sourceIndices) {
-  // Record the rewriter context for constructing ops later.
-  MLIRContext *ctx = rewriter.getContext();
-
-  // Capture expand_shape's input dimensions as `SmallVector`.
-  // This is done for the purpose of inferring the output shape via
-  // `inferExpandOutputShape` which will in turn be used for suffix product
-  // calculation later.
-  SmallVector srcShape;
-  MemRefType srcType = expandShapeOp.getSrcType();
-
-  for (int64_t i = 0, e = srcType.getRank(); i < e; ++i) {
-if (srcType.isDynamicDim(i)) {
-  srcShape.push_back(
-  rewriter.create(loc, expandShapeOp.getSrc(), i)
-  .getResult());
-} else {
-  srcShape.push_back(rewriter.getIndexAttr(srcType.getShape()[i]));
-}
-  }
-
-  auto outputShape = inferExpandShapeOutputShape(
-  rewriter, loc, expandShapeOp.getResultType(),
-  expandShapeOp.getReassociationIndices(), srcShape);
-  if (!outputShape.has_value())
-return failure();
+static LogicalResult resolveSourceIndicesExpandShape(
+Location loc, PatternRewriter &rewriter,
+memref::ExpandShapeOp expandShapeOp, ValueRange indices,
+SmallVectorImpl &sourceIndices, bool startsInbounds) {
+  SmallVector destShape = expandShapeOp.getMixedOu

[llvm-branch-commits] [mlir] [mlir][MemRef] Use specialized index ops to fold expand/collapse_shape (PR #138930)

2025-05-07 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-mlir

Author: Krzysztof Drewniak (krzysz00)


Changes

This PR updates the FoldMemRefAliasOps to use `affine.linearize_index` and 
`affine.delinearize_index` to perform the index computations needed to fold a 
`memref.expand_shape` or `memref.collapse_shape` into its consumers, 
respectively.

This also loosens some limitations of the pass:
1. The existing `output_shape` argument to `memref.expand_shape` is now used, 
eliminating the need to re-infer this shape or call `memref.dim`.
2. Because we're using `affine.delinearize_index`, the restriction that each 
group in a `memref.collapse_shape` can only have one dynamic dimension is 
removed.

---

Patch is 31.32 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/138930.diff


3 Files Affected:

- (modified) mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td (+10-4) 
- (modified) mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp 
(+49-120) 
- (modified) mlir/test/Dialect/MemRef/fold-memref-alias-ops.mlir (+64-65) 


``diff
diff --git a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td 
b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
index d6d8161d3117b..f34b5b46cab50 100644
--- a/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
+++ b/mlir/include/mlir/Dialect/MemRef/IR/MemRefOps.td
@@ -1342,14 +1342,14 @@ def MemRef_ReinterpretCastOp
 according to specified offsets, sizes, and strides.
 
 ```mlir
-%result1 = memref.reinterpret_cast %arg0 to 
+%result1 = memref.reinterpret_cast %arg0 to
   offset: [9],
   sizes: [4, 4],
   strides: [16, 2]
 : memref<8x8xf32, strided<[8, 1], offset: 0>> to
   memref<4x4xf32, strided<[16, 2], offset: 9>>
 
-%result2 = memref.reinterpret_cast %result1 to 
+%result2 = memref.reinterpret_cast %result1 to
   offset: [0],
   sizes: [2, 2],
   strides: [4, 2]
@@ -1755,6 +1755,12 @@ def MemRef_ExpandShapeOp : 
MemRef_ReassociativeReshapeOp<"expand_shape", [
 OpBuilder &b, Location loc, MemRefType expandedType,
 ArrayRef reassociation,
 ArrayRef inputShape);
+
+// Return a vector with all the static and dynamic values in the output 
shape.
+SmallVector getMixedOutputShape() {
+  OpBuilder builder(getContext());
+  return ::mlir::getMixedValues(getStaticOutputShape(), getOutputShape(), 
builder);
+}
   }];
 
   let hasVerifier = 1;
@@ -1873,7 +1879,7 @@ def MemRef_StoreOp : MemRef_Op<"store",
   let summary = "store operation";
   let description = [{
 The `store` op stores an element into a memref at the specified indices.
-
+
 The number of indices must match the rank of the memref. The indices must
 be in-bounds: `0 <= idx < dim_size`
 
@@ -2025,7 +2031,7 @@ def SubViewOp : 
MemRef_OpWithOffsetSizesAndStrides<"subview", [
 Unlike the `reinterpret_cast`, the values are relative to the strided
 memref of the input (`%result1` in this case) and not its
 underlying memory.
-
+
 Example 2:
 
 ```mlir
diff --git a/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp 
b/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp
index e4fb3f9bb87ed..2acb90613e5d1 100644
--- a/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp
+++ b/mlir/lib/Dialect/MemRef/Transforms/FoldMemRefAliasOps.cpp
@@ -59,92 +59,28 @@ using namespace mlir;
 ///
 /// %2 = load %0[6 * i1 + i2, %i3] :
 ///  memref<12x42xf32>
-static LogicalResult
-resolveSourceIndicesExpandShape(Location loc, PatternRewriter &rewriter,
-memref::ExpandShapeOp expandShapeOp,
-ValueRange indices,
-SmallVectorImpl &sourceIndices) {
-  // Record the rewriter context for constructing ops later.
-  MLIRContext *ctx = rewriter.getContext();
-
-  // Capture expand_shape's input dimensions as `SmallVector`.
-  // This is done for the purpose of inferring the output shape via
-  // `inferExpandOutputShape` which will in turn be used for suffix product
-  // calculation later.
-  SmallVector srcShape;
-  MemRefType srcType = expandShapeOp.getSrcType();
-
-  for (int64_t i = 0, e = srcType.getRank(); i < e; ++i) {
-if (srcType.isDynamicDim(i)) {
-  srcShape.push_back(
-  rewriter.create(loc, expandShapeOp.getSrc(), i)
-  .getResult());
-} else {
-  srcShape.push_back(rewriter.getIndexAttr(srcType.getShape()[i]));
-}
-  }
-
-  auto outputShape = inferExpandShapeOutputShape(
-  rewriter, loc, expandShapeOp.getResultType(),
-  expandShapeOp.getReassociationIndices(), srcShape);
-  if (!outputShape.has_value())
-return failure();
+static LogicalResult resolveSourceIndicesExpandShape(
+Location loc, PatternRewriter &rewriter,
+memref::ExpandShapeOp expandShapeOp, ValueRange indices,
+SmallVectorImpl &sourceIndices, bool startsInbounds) {
+  SmallVector destShape = expandShapeOp.getMixedOutputSha

[llvm-branch-commits] [mlir] [mlir][MemRef] Use specialized index ops to fold expand/collapse_shape (PR #138930)

2025-05-07 Thread Krzysztof Drewniak via llvm-branch-commits

krzysz00 wrote:

PRs this depends on:
* #138929

https://github.com/llvm/llvm-project/pull/138930
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [mlir] [mlir][MemRef] Use specialized index ops to fold expand/collapse_shape (PR #138930)

2025-05-07 Thread Krzysztof Drewniak via llvm-branch-commits

https://github.com/krzysz00 created 
https://github.com/llvm/llvm-project/pull/138930

This PR updates the FoldMemRefAliasOps to use `affine.linearize_index` and 
`affine.delinearize_index` to perform the index computations needed to fold a 
`memref.expand_shape` or `memref.collapse_shape` into its consumers, 
respectively.

This also loosens some limitations of the pass:
1. The existing `output_shape` argument to `memref.expand_shape` is now used, 
eliminating the need to re-infer this shape or call `memref.dim`.
2. Because we're using `affine.delinearize_index`, the restriction that each 
group in a `memref.collapse_shape` can only have one dynamic dimension is 
removed.



  



Rate limit · GitHub


  body {
background-color: #f6f8fa;
color: #24292e;
font-family: -apple-system,BlinkMacSystemFont,Segoe 
UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji,Segoe UI Symbol;
font-size: 14px;
line-height: 1.5;
margin: 0;
  }

  .container { margin: 50px auto; max-width: 600px; text-align: center; 
padding: 0 24px; }

  a { color: #0366d6; text-decoration: none; }
  a:hover { text-decoration: underline; }

  h1 { line-height: 60px; font-size: 48px; font-weight: 300; margin: 0px; 
text-shadow: 0 1px 0 #fff; }
  p { color: rgba(0, 0, 0, 0.5); margin: 20px 0 40px; }

  ul { list-style: none; margin: 25px 0; padding: 0; }
  li { display: table-cell; font-weight: bold; width: 1%; }

  .logo { display: inline-block; margin-top: 35px; }
  .logo-img-2x { display: none; }
  @media
  only screen and (-webkit-min-device-pixel-ratio: 2),
  only screen and (   min--moz-device-pixel-ratio: 2),
  only screen and ( -o-min-device-pixel-ratio: 2/1),
  only screen and (min-device-pixel-ratio: 2),
  only screen and (min-resolution: 192dpi),
  only screen and (min-resolution: 2dppx) {
.logo-img-1x { display: none; }
.logo-img-2x { display: inline-block; }
  }

  #suggestions {
margin-top: 35px;
color: #ccc;
  }
  #suggestions a {
color: #66;
font-weight: 200;
font-size: 14px;
margin: 0 10px;
  }


  
  



  Whoa there!
  You have exceeded a secondary rate limit.
Please wait a few minutes before you try again;
in some cases this may take up to an hour.
  
  
https://support.github.com/contact";>Contact Support —
https://githubstatus.com";>GitHub Status —
https://twitter.com/githubstatus";>@githubstatus
  

  

  

  

  

  


___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits