[Mlir-commits] [mlir] 2ce4caf - Moved getStaticLoopRanges and getStaticShape methods to LinalgInterfaces.td to add static shape verification
Hanhan Wang
llvmlistbot at llvm.org
Wed Mar 10 04:06:37 PST 2021
Author: Inho Seo
Date: 2021-03-10T04:06:22-08:00
New Revision: 2ce4caf4143bc7f6fe084da03d2ce268a7a06a6d
URL: https://github.com/llvm/llvm-project/commit/2ce4caf4143bc7f6fe084da03d2ce268a7a06a6d
DIFF: https://github.com/llvm/llvm-project/commit/2ce4caf4143bc7f6fe084da03d2ce268a7a06a6d.diff
LOG: Moved getStaticLoopRanges and getStaticShape methods to LinalgInterfaces.td to add static shape verification
It is to use the methods in LinalgInterfaces.cpp for additional static shape verification to match the shaped operands and loop on linalgOps. If I used the existing methods, I would face circular dependency linking issue. Now we can use them as methods of LinalgOp.
Reviewed By: hanchung
Differential Revision: https://reviews.llvm.org/D98163
Added:
Modified:
mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
mlir/include/mlir/Dialect/Linalg/Utils/Utils.h
mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp
mlir/lib/Dialect/Linalg/Utils/Utils.cpp
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
index d84d52d858ad..2a797ec9fa30 100644
--- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
+++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgInterfaces.td
@@ -1101,6 +1101,44 @@ def LinalgStructuredInterface : OpInterface<"LinalgOp"> {
getNumInputs() + resultIdx, dim);
}]
>,
+ InterfaceMethod<
+ /*desc=*/[{
+ Like `getShape`, but only returns statically-known information, without
+ generating any new IR. For each shape dimension, returns >=0 if that
+ dimension is statically known, or ShapeType::kDynamicSize otherwise.
+ }],
+ /*retTy=*/"SmallVector<int64_t, 8>",
+ /*methodName=*/"getStaticShape",
+ /*args=*/(ins),
+ /*methodBody=*/"",
+ /*defaultImplementation=*/[{
+ SmallVector<int64_t, 8> res;
+ for (Value v : getShapedOperands()) {
+ auto shape = v.getType().cast<ShapedType>().getShape();
+ res.append(shape.begin(), shape.end());
+ }
+ return res;
+ }]
+ >,
+ InterfaceMethod<
+ /*desc=*/[{
+ Returns the statically-known loop ranges. Composes
+ `getShapesToLoopsMap()` with the result of `getStaticShape`.
+ Returns None if `getShapesToLoopsMap()` fails. Returns
+ ShapeType::kDynamicSize for non-statically-known loop ranges.
+ }],
+ /*retTy=*/"Optional<SmallVector<int64_t, 4>>",
+ /*methodName=*/"getStaticLoopRanges",
+ /*args=*/(ins),
+ /*methodBody=*/"",
+ /*defaultImplementation=*/[{
+ SmallVector<int64_t, 8> viewSizes = getStaticShape();
+ AffineMap invertedMap = getShapesToLoopsMap();
+ if (!invertedMap)
+ return {};
+ return invertedMap.compose(viewSizes);
+ }]
+ >,
//===------------------------------------------------------------------===//
// Other static interface methods.
diff --git a/mlir/include/mlir/Dialect/Linalg/Utils/Utils.h b/mlir/include/mlir/Dialect/Linalg/Utils/Utils.h
index 1a7dc939435e..21b7dcfe4319 100644
--- a/mlir/include/mlir/Dialect/Linalg/Utils/Utils.h
+++ b/mlir/include/mlir/Dialect/Linalg/Utils/Utils.h
@@ -118,17 +118,6 @@ Optional<FusionInfo> fuseProducerOfTensor(OpBuilder &b,
Optional<SmallVector<Value, 1>> fuseTensorOps(PatternRewriter &rewriter,
OpOperand &consumerOpOperand);
-/// Like `getShape`, but only returns statically-known information, without
-/// generating any new IR. For each shape dimension, returns >=0 if that
-/// dimension is statically known, or -1 otherwise.
-SmallVector<int64_t, 8> getStaticShape(LinalgOp linalgOp);
-
-/// Returns the statically-known loop ranges of the `linalgOp`. Composes
-/// `linalgOp.getShapesToLoopsMap()` with the result of `getStaticShape`.
-/// Returns None if `linalgOp.getShapesToLoopsMap()` fails. Returns -1
-/// for non-statically-known loop ranges.
-Optional<SmallVector<int64_t, 4>> getStaticLoopRanges(LinalgOp linalgOp);
-
/// Apply the permutation defined by `permutation` to `inVec`.
/// Element `i` in `inVec` is mapped to location `j = permutation[i]`.
/// E.g.: for an input vector `inVec = ['a', 'b', 'c']` and a permutation vector
diff --git a/mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp b/mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp
index 4dc22c2219d4..ad7ad116ce66 100644
--- a/mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp
+++ b/mlir/lib/Dialect/Linalg/Transforms/FusionOnTensors.cpp
@@ -499,7 +499,7 @@ LogicalResult ExpansionInfo::compute(LinalgOp linalgOp,
AffineMap fusedIndexMap = linalgOp.getIndexingMap(fusedTensorIndex);
Optional<SmallVector<int64_t, 4>> originalLoopRange =
- getStaticLoopRanges(linalgOp);
+ linalgOp.getStaticLoopRanges();
if (!originalLoopRange)
return linalgOp.emitError("unable to find loop range for operation");
diff --git a/mlir/lib/Dialect/Linalg/Utils/Utils.cpp b/mlir/lib/Dialect/Linalg/Utils/Utils.cpp
index 32b32be066cb..8d91bd74712b 100644
--- a/mlir/lib/Dialect/Linalg/Utils/Utils.cpp
+++ b/mlir/lib/Dialect/Linalg/Utils/Utils.cpp
@@ -98,23 +98,6 @@ static void unpackRanges(ArrayRef<Range> ranges, SmallVectorImpl<Value> &lbs,
namespace mlir {
namespace linalg {
-SmallVector<int64_t, 8> getStaticShape(LinalgOp linalgOp) {
- SmallVector<int64_t, 8> res;
- for (Value v : linalgOp.getShapedOperands()) {
- auto shape = v.getType().cast<ShapedType>().getShape();
- res.append(shape.begin(), shape.end());
- }
- return res;
-}
-
-Optional<SmallVector<int64_t, 4>> getStaticLoopRanges(LinalgOp linalgOp) {
- SmallVector<int64_t, 8> viewSizes = getStaticShape(linalgOp);
- AffineMap invertedMap = linalgOp.getShapesToLoopsMap();
- if (!invertedMap)
- return {};
- return invertedMap.compose(viewSizes);
-}
-
/// If `size` comes from an AffineMinOp and one of the values of AffineMinOp
/// is a constant then return a new value set to the smallest such constant.
/// Otherwise returngetSmallestBoundingIndex nullptr.
More information about the Mlir-commits
mailing list