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 /// A tensor.insert_slice is a cast-like operation if it merely rank-extends the
47 /// source tensor or inserts the source tensor into a destination tensor with
48 /// the same shape.
49 bool isCastLikeInsertSliceOp(InsertSliceOp op);
50 
51 /// A tensor.extract_slice is a cast-like operation if it merely rank-reduces
52 /// unit dimensions of the source tensor or extracts the entire source tensor.
53 bool isCastLikeExtractSliceOp(ExtractSliceOp op);
54 
55 } // namespace tensor
56 } // namespace mlir
57 
58 #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:97
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:121
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.