MLIR  21.0.0git
Utils.h
Go to the documentation of this file.
1 //===- Utils.h - Utilities to support the Tensor dialect -------*- C++ -*-===//
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 #ifndef MLIR_DIALECT_TENSOR_UTILS_UTILS_H_
10 #define MLIR_DIALECT_TENSOR_UTILS_UTILS_H_
11 
13 
14 namespace mlir {
15 namespace tensor {
16 
17 // Return a PadOp that pads `source` to `resType` size. The op performs "high"
18 // padding, i.e. it adds trailing padding values until the desired size is met.
19 // Output sizes are assumed to be greater than the input sizes. The padding
20 // width is calculated as: resDim - sourceDim.
21 //
22 // Handling static sizes is trivial. Dynamic dimensions are trickier (*):
23 // 1. Dynamic input sizes are extracted from `source` (e.g. via `tensor.dim`).
24 // 2. For dynamic output dims, there are two options:
25 // 2.1 All output dynamic dim sizes are specified in `dynOutDims`, or
26 // 2.2 `dynOutDims is empty - the padding width for all the output dynamic
27 // dims is set to 0.
28 //
29 // (*) Note that `resType` is just a shape and it only encodes the actual sizes
30 // for _static_ dimensions.
31 PadOp createPadHighOp(RankedTensorType resType, Value source, Value pad,
32  bool nofold, Location loc, OpBuilder &builder,
33  SmallVector<Value> dynOutDims = {});
34 
35 // Creates dim ops for each dynamic dimension of the ranked tensor argument and
36 // returns these as values.
37 SmallVector<Value> createDynamicDimValues(OpBuilder &b, Location loc,
38  Value rankedTensor);
39 
40 /// Returns the transposed `rankedTensorType` if `transposeVector` is non-empty.
41 /// Fail if `transposeVector` is not a permutation matching the tensor rank.
42 FailureOr<RankedTensorType>
43 computeTransposedType(RankedTensorType rankedTensorType,
44  ArrayRef<int64_t> transposeVector);
45 
46 /// Create tensor.collapse_shape to drop unit dimensions in `dropDims` in tensor
47 /// `src`.
48 CollapseShapeOp dropGivenUnitDims(OpBuilder &b, Location loc, Value src,
49  const llvm::SmallBitVector &dropDims);
50 
51 /// A tensor.insert_slice is a cast-like operation if it merely rank-extends the
52 /// source tensor or inserts the source tensor into a destination tensor with
53 /// the same shape.
54 bool isCastLikeInsertSliceOp(InsertSliceOp op);
55 
56 /// A tensor.extract_slice is a cast-like operation if it merely rank-reduces
57 /// unit dimensions of the source tensor or extracts the entire source tensor.
58 bool isCastLikeExtractSliceOp(ExtractSliceOp op);
59 
60 } // namespace tensor
61 } // namespace mlir
62 
63 #endif // MLIR_DIALECT_TENSOR_UTILS_UTILS_H_
PadOp createPadHighOp(RankedTensorType resType, Value source, Value pad, bool nofold, Location loc, OpBuilder &builder, SmallVector< Value > dynOutDims={})
Definition: Utils.cpp:25
SmallVector< Value > createDynamicDimValues(OpBuilder &b, Location loc, Value rankedTensor)
Definition: Utils.cpp:65
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:128
CollapseShapeOp dropGivenUnitDims(OpBuilder &b, Location loc, Value src, const llvm::SmallBitVector &dropDims)
Create tensor.collapse_shape to drop unit dimensions in dropDims in tensor src.
Definition: Utils.cpp:98
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:152
FailureOr< RankedTensorType > computeTransposedType(RankedTensorType rankedTensorType, ArrayRef< int64_t > transposeVector)
Returns the transposed rankedTensorType if transposeVector is non-empty.
Definition: Utils.cpp:79
Include the generated interface declarations.
SmallVector< int64_t > dropDims(ArrayRef< int64_t > inputPerm, ArrayRef< int64_t > dropPositions)
Returns a permutation vector that drop the input dims in dropPositions from inputPerm.