MLIR  18.0.0git
Utils.cpp
Go to the documentation of this file.
1 //===- Utils.cpp - Utilities to support the Tensor dialect ----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements utilities for the Tensor dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 
20 
21 using namespace mlir;
22 using namespace mlir::tensor;
23 
24 PadOp mlir::tensor::createPadHighOp(RankedTensorType type, Value source,
25  Value pad, bool nofold, Location loc,
26  OpBuilder &b) {
27  SmallVector<OpFoldResult> low(type.getRank(), b.getIndexAttr(0));
28  SmallVector<OpFoldResult> high(type.getRank(), b.getIndexAttr(0));
29  for (const auto &en : enumerate(type.getShape())) {
30  // Pad only the static dimensions of the result tensor type.
31  if (ShapedType::isDynamic(en.value()))
32  continue;
33  // Compute the padding width.
34  AffineExpr d0;
35  bindDims(b.getContext(), d0);
36  OpFoldResult sz = tensor::getMixedSize(b, loc, source, en.index());
37  high[en.index()] =
38  affine::makeComposedFoldedAffineApply(b, loc, en.value() - d0, {sz});
39  }
40  return b.create<PadOp>(loc, type, source, low, high, pad, nofold);
41 }
42 
44  Location loc,
45  Value rankedTensor) {
46  auto tensorTy = cast<RankedTensorType>(rankedTensor.getType());
47  SmallVector<Value> dynamicDims;
48  for (const auto &en : llvm::enumerate(tensorTy.getShape())) {
49  if (en.value() == ShapedType::kDynamic)
50  dynamicDims.push_back(
51  b.create<tensor::DimOp>(loc, rankedTensor, en.index()));
52  }
53  return dynamicDims;
54 }
55 
57 mlir::tensor::computeTransposedType(RankedTensorType rankedTensorType,
58  ArrayRef<int64_t> transposeVector) {
59  if (transposeVector.empty())
60  return rankedTensorType;
61 
62  if (!isPermutationVector(transposeVector) ||
63  transposeVector.size() != static_cast<size_t>(rankedTensorType.getRank()))
64  return failure();
65 
66  SmallVector<int64_t> transposedShape(rankedTensorType.getShape().begin(),
67  rankedTensorType.getShape().end());
68  applyPermutationToVector(transposedShape, transposeVector);
69 
70  using RTTBuilder = RankedTensorType::Builder;
71  RankedTensorType transposedTensorType =
72  RTTBuilder(rankedTensorType).setShape(transposedShape);
73  return transposedTensorType;
74 }
75 
76 bool mlir::tensor::isCastLikeInsertSliceOp(InsertSliceOp op) {
77  llvm::SmallBitVector droppedDims = op.getDroppedDims();
78  int64_t srcDim = 0;
79  // Source dims and destination dims (apart from dropped dims) must have the
80  // same size.
81  for (int64_t resultDim = 0; resultDim < op.getDestType().getRank();
82  ++resultDim) {
83  if (droppedDims.test(resultDim)) {
84  continue;
85  }
87  op.getSource(), op.getResult(), srcDim, resultDim);
88  if (failed(equalDimSize) || !*equalDimSize)
89  return false;
90  ++srcDim;
91  }
92 
93  return true;
94 }
95 
96 bool mlir::tensor::isCastLikeExtractSliceOp(ExtractSliceOp op) {
97  llvm::SmallBitVector droppedDims = op.getDroppedDims();
98  int64_t resultDim = 0;
99  // Source dims and result dims (apart from dropped dims) must have the same
100  // size.
101  RankedTensorType sourceType = op.getSourceType();
102  for (int64_t dim = 0, e = sourceType.getRank(); dim < e; ++dim) {
103  if (droppedDims.test(dim)) {
104  // ExtractSlice may drop unit dimensions that result from taking a size-1
105  // slice from a non-size-1 source dimension.
106  if (sourceType.getDimSize(dim) != 1)
107  return false;
108  continue;
109  }
111  op.getSource(), op.getResult(), dim, resultDim);
112  if (failed(equalDimSize) || !*equalDimSize)
113  return false;
114  ++resultDim;
115  }
116 
117  return true;
118 }
Base type for affine expression.
Definition: AffineExpr.h:68
IntegerAttr getIndexAttr(int64_t value)
Definition: Builders.cpp:124
MLIRContext * getContext() const
Definition: Builders.h:55
This class provides support for representing a failure result, or a valid value of type T.
Definition: LogicalResult.h:78
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
This class helps build Operations.
Definition: Builders.h:206
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:446
This class represents a single result from folding an operation.
Definition: OpDefinition.h:266
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition: Operation.h:402
This is a builder type that keeps local references to arguments.
Definition: BuiltinTypes.h:248
Builder & setShape(ArrayRef< int64_t > newShape)
Definition: BuiltinTypes.h:259
static FailureOr< bool > areEqual(Value value1, Value value2, std::optional< int64_t > dim1=std::nullopt, std::optional< int64_t > dim2=std::nullopt)
Compute whether the given values/dimensions are equal.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:93
Type getType() const
Return the type of this value.
Definition: Value.h:122
OpFoldResult makeComposedFoldedAffineApply(OpBuilder &b, Location loc, AffineMap map, ArrayRef< OpFoldResult > operands)
Constructs an AffineApplyOp that applies map to operands after composing the map with the maps of any...
Definition: AffineOps.cpp:1276
constexpr void enumerate(std::tuple< Tys... > &tuple, CallbackT &&callback)
Definition: Matchers.h:285
SmallVector< Value > createDynamicDimValues(OpBuilder &b, Location loc, Value rankedTensor)
Definition: Utils.cpp:43
bool isCastLikeInsertSliceOp(InsertSliceOp op)
A tensor.insert_slice is a cast-like operation if it merely rank-extends the source tensor or inserts...
Definition: Utils.cpp:76
bool isCastLikeExtractSliceOp(ExtractSliceOp op)
A tensor.extract_slice is a cast-like operation if it merely rank-reduces unit dimensions of the sour...
Definition: Utils.cpp:96
OpFoldResult getMixedSize(OpBuilder &builder, Location loc, Value value, int64_t dim)
Return the dimension of the given tensor value.
Definition: TensorOps.cpp:50
PadOp createPadHighOp(RankedTensorType type, Value source, Value pad, bool nofold, Location loc, OpBuilder &builder)
Definition: Utils.cpp:24
FailureOr< RankedTensorType > computeTransposedType(RankedTensorType rankedTensorType, ArrayRef< int64_t > transposeVector)
Returns the transposed rankedTensorType if transposeVector is non-empty.
Definition: Utils.cpp:57
This header declares functions that assist transformations in the MemRef dialect.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
void bindDims(MLIRContext *ctx, AffineExprTy &...exprs)
Bind a list of AffineExpr references to DimExpr at positions: [0 .
Definition: AffineExpr.h:331
void applyPermutationToVector(SmallVector< T, N > &inVec, ArrayRef< int64_t > permutation)
Apply the permutation defined by permutation to inVec.
bool isPermutationVector(ArrayRef< int64_t > interchange)
Method to check if an interchange vector is a permutation.
bool failed(LogicalResult result)
Utility function that returns true if the provided LogicalResult corresponds to a failure value.
Definition: LogicalResult.h:72