diff --git a/mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td b/mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td --- a/mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td +++ b/mlir/include/mlir/Dialect/Linalg/IR/LinalgOps.td @@ -256,6 +256,14 @@ SmallVector getMixedHighPad() { return getMixedPadImpl(static_high(), high()); } + SmallVector getLowPadAsValues(OpBuilder &b, Location loc) { + return getOperandsOrIntegersSizesListAsValues(b, loc, low(), + static_low()); + } + SmallVector getHighPadAsValues(OpBuilder &b, Location loc) { + return getOperandsOrIntegersSizesListAsValues(b, loc, high(), + static_high()); + } }]; let builders = [ diff --git a/mlir/include/mlir/Interfaces/ViewLikeInterface.h b/mlir/include/mlir/Interfaces/ViewLikeInterface.h --- a/mlir/include/mlir/Interfaces/ViewLikeInterface.h +++ b/mlir/include/mlir/Interfaces/ViewLikeInterface.h @@ -30,6 +30,21 @@ class OffsetSizeAndStrideOpInterface; LogicalResult verify(OffsetSizeAndStrideOpInterface op); + +/// Utility method to take a list of mixed static and dynamic values that +/// represent offsets or strides and return a vector of Values. The `values` +/// correspond to the dynamic values for the position in `integers` (which is a +/// ArrayAttr of IntegerAttrs) which contain ShapedType::kDynamicStrideOrOffset. +SmallVector getOperandsOrIntegersOffsetsOrStridesListAsValues( + OpBuilder &b, Location loc, ValueRange values, ArrayAttr integers); + +/// Utility method to take a list of mixed static and dynamic values that +/// represent sizes and return a vector of Values. The `values` correspond to +/// the dynamic values for the position in `integers` (which is a ArrayAttr of +/// IntegerAttrs) which contain ShapedType::kDynamicSize. +SmallVector +getOperandsOrIntegersSizesListAsValues(OpBuilder &b, Location loc, + ValueRange values, ArrayAttr integers); } // namespace mlir /// Include the generated interface declarations. diff --git a/mlir/include/mlir/Interfaces/ViewLikeInterface.td b/mlir/include/mlir/Interfaces/ViewLikeInterface.td --- a/mlir/include/mlir/Interfaces/ViewLikeInterface.td +++ b/mlir/include/mlir/Interfaces/ViewLikeInterface.td @@ -177,6 +177,20 @@ return res; }] >, + InterfaceMethod< + /*desc=*/[{ + Return a vector of all the static or dynamic offsets of the op + as Values. + }], + /*retTy=*/"::mlir::SmallVector<::mlir::Value, 4>", + /*methodName=*/"getOffsetsAsValues", + /*args=*/(ins "OpBuilder &":$b, "Location":$loc), + /*methodBody=*/"", + /*defaultImplementation=*/[{ + return getOperandsOrIntegersOffsetsOrStridesListAsValues( + b, loc, $_op.offsets(), $_op.static_offsets()); + }] + >, InterfaceMethod< /*desc=*/[{ Return a vector of all the static or dynamic sizes of the op. @@ -198,6 +212,20 @@ return res; }] >, + InterfaceMethod< + /*desc=*/[{ + Return a vector of all the static or dynamic sizes of the op + as Values. + }], + /*retTy=*/"::mlir::SmallVector<::mlir::Value, 4>", + /*methodName=*/"getSizesAsValues", + /*args=*/(ins "OpBuilder &":$b, "Location":$loc), + /*methodBody=*/"", + /*defaultImplementation=*/[{ + return getOperandsOrIntegersSizesListAsValues( + b, loc, $_op.sizes(), $_op.static_sizes()); + }] + >, InterfaceMethod< /*desc=*/[{ Return a vector of all the static or dynamic strides of the op. @@ -219,6 +247,20 @@ return res; }] >, + InterfaceMethod< + /*desc=*/[{ + Return a vector of all the static or dynamic strides of the op + as Values. + }], + /*retTy=*/"::mlir::SmallVector<::mlir::Value, 4>", + /*methodName=*/"getStridesAsValues", + /*args=*/(ins "OpBuilder &":$b, "Location":$loc), + /*methodBody=*/"", + /*defaultImplementation=*/[{ + return getOperandsOrIntegersOffsetsOrStridesListAsValues( + b, loc, $_op.strides(), $_op.static_strides()); + }] + >, InterfaceMethod< /*desc=*/[{ diff --git a/mlir/lib/Interfaces/ViewLikeInterface.cpp b/mlir/lib/Interfaces/ViewLikeInterface.cpp --- a/mlir/lib/Interfaces/ViewLikeInterface.cpp +++ b/mlir/lib/Interfaces/ViewLikeInterface.cpp @@ -8,6 +8,8 @@ #include "mlir/Interfaces/ViewLikeInterface.h" +#include "mlir/Dialect/StandardOps/IR/Ops.h" + using namespace mlir; //===----------------------------------------------------------------------===// @@ -17,6 +19,36 @@ /// Include the definitions of the loop-like interfaces. #include "mlir/Interfaces/ViewLikeInterface.cpp.inc" +template +static SmallVector +getOperandsOrIntegersListAsValues(OpBuilder &b, Location loc, ValueRange values, + ArrayAttr integers) { + SmallVector allValues; + unsigned dynamicOperandNum = 0; + for (auto integerValue : llvm::map_range(integers, [](Attribute attr) { + return attr.cast().getInt(); + })) { + if (integerValue != dynVal) { + allValues.push_back(b.create(loc, integerValue)); + } else { + allValues.push_back(values[dynamicOperandNum++]); + } + } + return allValues; +} + +SmallVector mlir::getOperandsOrIntegersOffsetsOrStridesListAsValues( + OpBuilder &b, Location loc, ValueRange values, ArrayAttr integers) { + return getOperandsOrIntegersListAsValues( + b, loc, values, integers); +} + +SmallVector mlir::getOperandsOrIntegersSizesListAsValues( + OpBuilder &b, Location loc, ValueRange values, ArrayAttr integers) { + return getOperandsOrIntegersListAsValues( + b, loc, values, integers); +} + LogicalResult mlir::verifyListOfOperandsOrIntegers( Operation *op, StringRef name, unsigned maxNumElements, ArrayAttr attr, ValueRange values, llvm::function_ref isDynamic) {