-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[mlir][memref] Fix computeCollapsedLayoutMap for contiguous dynamic dim #136485
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
amrami
wants to merge
1
commit into
llvm:main
Choose a base branch
from
amrami:amrami/collapse_stride_fix
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-mlir Author: Maya Amrami (amrami) ChangesFull diff: https://github.com/llvm/llvm-project/pull/136485.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index 6f10a31c15626..45ac4c9d5117e 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -23,6 +23,7 @@
#include "mlir/Interfaces/ViewLikeInterface.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
+#include <algorithm>
using namespace mlir;
using namespace mlir::memref;
@@ -2401,11 +2402,19 @@ computeCollapsedLayoutMap(MemRefType srcType,
if (!ShapedType::isDynamic(srcShape[ref.back()]) || ref.size() == 1) {
resultStrides.push_back(srcStrides[ref.back()]);
} else {
- // Dynamically-sized dims may turn out to be dims of size 1 at runtime, so
- // the corresponding stride may have to be skipped. (See above comment.)
- // Therefore, the result stride cannot be statically determined and must
- // be dynamic.
- resultStrides.push_back(ShapedType::kDynamic);
+ bool contiguousSrcDim = srcStrides[ref.back()] == 1;
+ bool dynamicSizeIsPreserved =
+ std::all_of(ref.begin(), ref.end() - 1,
+ [srcShape](int64_t dim) { return srcShape[dim] == 1; });
+ if (contiguousSrcDim && dynamicSizeIsPreserved)
+ resultStrides.push_back(1);
+ else {
+ // Dynamically-sized dims may turn out to be dims of size 1 at runtime,
+ // so the corresponding stride may have to be skipped. (See above
+ // comment.) Therefore, the result stride cannot be statically
+ // determined and must be dynamic.
+ resultStrides.push_back(ShapedType::kDynamic);
+ }
}
}
diff --git a/mlir/test/Dialect/MemRef/invalid.mlir b/mlir/test/Dialect/MemRef/invalid.mlir
index 34fc4775924e7..a8fcd91fba097 100644
--- a/mlir/test/Dialect/MemRef/invalid.mlir
+++ b/mlir/test/Dialect/MemRef/invalid.mlir
@@ -502,6 +502,14 @@ func.func @collapse_shape_wrong_collapsed_type(%arg0: memref<?x?x?xf32>) {
// -----
+func.func @collapse_shape_infer_stride_of_dynamic_dim(%arg0: memref<1x1x?x1xsi32, strided<[36960, 4620, 1, 330]>, 1>, %dim : index) -> (memref<?xsi32, strided<[?]>, 1>) {
+ // expected-error @+1 {{expected collapsed type to be 'memref<?xsi32, strided<[1]>, 1>' but found 'memref<?xsi32, strided<[?]>, 1>'}}
+ %collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3]] : memref<1x1x?x1xsi32, strided<[36960, 4620, 1, 330]>, 1> into memref<?xsi32, strided<[?]>, 1>
+ return %collapse_shape : memref<?xsi32, strided<[?]>, 1>
+}
+
+// -----
+
func.func @expand_shape_illegal_static_memref
(%arg0: memref<2x3x20xf32>) -> memref<2x3x2x4x5xf32> {
// expected-error @+1 {{collapsed dim size (20) must equal reassociation group size (40)}}
|
@llvm/pr-subscribers-mlir-memref Author: Maya Amrami (amrami) ChangesFull diff: https://github.com/llvm/llvm-project/pull/136485.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
index 6f10a31c15626..45ac4c9d5117e 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp
@@ -23,6 +23,7 @@
#include "mlir/Interfaces/ViewLikeInterface.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallBitVector.h"
+#include <algorithm>
using namespace mlir;
using namespace mlir::memref;
@@ -2401,11 +2402,19 @@ computeCollapsedLayoutMap(MemRefType srcType,
if (!ShapedType::isDynamic(srcShape[ref.back()]) || ref.size() == 1) {
resultStrides.push_back(srcStrides[ref.back()]);
} else {
- // Dynamically-sized dims may turn out to be dims of size 1 at runtime, so
- // the corresponding stride may have to be skipped. (See above comment.)
- // Therefore, the result stride cannot be statically determined and must
- // be dynamic.
- resultStrides.push_back(ShapedType::kDynamic);
+ bool contiguousSrcDim = srcStrides[ref.back()] == 1;
+ bool dynamicSizeIsPreserved =
+ std::all_of(ref.begin(), ref.end() - 1,
+ [srcShape](int64_t dim) { return srcShape[dim] == 1; });
+ if (contiguousSrcDim && dynamicSizeIsPreserved)
+ resultStrides.push_back(1);
+ else {
+ // Dynamically-sized dims may turn out to be dims of size 1 at runtime,
+ // so the corresponding stride may have to be skipped. (See above
+ // comment.) Therefore, the result stride cannot be statically
+ // determined and must be dynamic.
+ resultStrides.push_back(ShapedType::kDynamic);
+ }
}
}
diff --git a/mlir/test/Dialect/MemRef/invalid.mlir b/mlir/test/Dialect/MemRef/invalid.mlir
index 34fc4775924e7..a8fcd91fba097 100644
--- a/mlir/test/Dialect/MemRef/invalid.mlir
+++ b/mlir/test/Dialect/MemRef/invalid.mlir
@@ -502,6 +502,14 @@ func.func @collapse_shape_wrong_collapsed_type(%arg0: memref<?x?x?xf32>) {
// -----
+func.func @collapse_shape_infer_stride_of_dynamic_dim(%arg0: memref<1x1x?x1xsi32, strided<[36960, 4620, 1, 330]>, 1>, %dim : index) -> (memref<?xsi32, strided<[?]>, 1>) {
+ // expected-error @+1 {{expected collapsed type to be 'memref<?xsi32, strided<[1]>, 1>' but found 'memref<?xsi32, strided<[?]>, 1>'}}
+ %collapse_shape = memref.collapse_shape %arg0 [[0, 1, 2, 3]] : memref<1x1x?x1xsi32, strided<[36960, 4620, 1, 330]>, 1> into memref<?xsi32, strided<[?]>, 1>
+ return %collapse_shape : memref<?xsi32, strided<[?]>, 1>
+}
+
+// -----
+
func.func @expand_shape_illegal_static_memref
(%arg0: memref<2x3x20xf32>) -> memref<2x3x2x4x5xf32> {
// expected-error @+1 {{collapsed dim size (20) must equal reassociation group size (40)}}
|
ping |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.