MLIR 22.0.0git
Utils.h
Go to the documentation of this file.
1//===- Utils.h - General Arith transformation utilities ----*- 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// This header file defines prototypes for various transformation utilities for
10// the Arith dialect. These are not passes by themselves but are used
11// either by passes, optimization sequences, or in turn by other transformation
12// utilities.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef MLIR_DIALECT_ARITH_UTILS_UTILS_H
17#define MLIR_DIALECT_ARITH_UTILS_UTILS_H
18
20#include "mlir/IR/Matchers.h"
22#include "mlir/IR/Value.h"
23#include "llvm/ADT/ArrayRef.h"
24
25namespace mlir {
26
28
29/// Infer the output shape for a {memref|tensor}.expand_shape when it is
30/// possible to do so.
31///
32/// Note: This should *only* be used to implement
33/// `ExpandShapeOp::inferOutputShape` in both the memref and tensor namespaces.
34/// If you need to infer the output shape you should use the static method of
35/// `ExpandShapeOp` instead of calling this.
36///
37/// `inputShape` is the shape of the tensor or memref being expanded as a
38/// sequence of SSA values or constants. `expandedType` is the output shape of
39/// the expand_shape operation. `reassociation` is the reassociation denoting
40/// the output dims each input dim is mapped to.
41///
42/// Returns the output shape in `outputShape` and `staticOutputShape`, following
43/// the conventions for the output_shape and static_output_shape inputs to the
44/// expand_shape ops.
45std::optional<SmallVector<OpFoldResult>>
46inferExpandShapeOutputShape(OpBuilder &b, Location loc, ShapedType expandedType,
48 ArrayRef<OpFoldResult> inputShape);
49
50/// Matches a ConstantIndexOp.
52
53llvm::SmallBitVector getPositionsOfShapeOne(unsigned rank,
55
56/// Converts an OpFoldResult to a Value. Returns the fold result if it casts to
57/// a Value or creates a ConstantOp if it casts to an Integer Attribute.
58/// Other attribute types are not supported.
60 OpFoldResult ofr);
61
62/// Converts an OpFoldResult to a Value. Returns the fold result if it casts to
63/// a Value or creates a ConstantIndexOp if it casts to an Integer Attribute.
64/// Other attribute types are not supported.
66 OpFoldResult ofr);
67
68/// Similar to the other overload, but converts multiple OpFoldResults into
69/// Values.
72 ArrayRef<OpFoldResult> valueOrAttrVec);
73
74/// Create a cast from an index-like value (index or integer) to another
75/// index-like value. If the value type and the target type are the same, it
76/// returns the original value.
78 Type targetType, Value value);
79
80/// Converts a scalar value `operand` to type `toType`. If the value doesn't
81/// convert, a warning will be issued and the operand is returned as is (which
82/// will presumably yield a verification issue downstream).
84 Type toType, bool isUnsignedCast);
85
86/// Create a constant of type `type` at location `loc` whose value is `value`
87/// (an APInt or APFloat whose type must match the element type of `type`).
88/// If `type` is a shaped type, create a splat constant of the given value.
89/// Constants are folded if possible.
91 const APInt &value);
93 int64_t value);
95 const APFloat &value);
96
97/// Returns the int type of the integer in ofr.
98/// Other attribute types are not supported.
100
101/// Helper struct to build simple arithmetic quantities with minimal type
102/// inference support.
105 OpBuilder &b, Location loc,
106 arith::IntegerOverflowFlags ovf = arith::IntegerOverflowFlags::none)
107 : b(b), loc(loc), ovf(ovf) {}
108
116
117private:
118 OpBuilder &b;
119 Location loc;
120 arith::IntegerOverflowFlags ovf;
121};
122
123/// ArithBuilder specialized specifically for tensor/memref indexing
124/// calculations. Those calculations generally should never signed overflow and
125/// always use signed integers, so we can set oveflow flags accordingly.
128 : ArithBuilder(b, loc, arith::IntegerOverflowFlags::nsw) {}
129};
130
131namespace arith {
132
133// Build the product of a sequence.
134// If values = (v0, v1, ..., vn) than the returned
135// value is v0 * v1 * ... * vn.
136// All values must have the same type.
137//
138// The version without `resultType` must contain at least one element in values.
139// Then the result will have the same type as the elements in `values`.
140// If `values` is empty in the version with `resultType` returns 1 with type
141// `resultType`.
142Value createProduct(OpBuilder &builder, Location loc, ArrayRef<Value> values);
143Value createProduct(OpBuilder &builder, Location loc, ArrayRef<Value> values,
144 Type resultType);
145
146// Map strings to float types.
147std::optional<FloatType> parseFloatType(MLIRContext *ctx, StringRef name);
148
149} // namespace arith
150} // namespace mlir
151
152#endif // MLIR_DIALECT_ARITH_UTILS_UTILS_H
lhs
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
#define mul(a, b)
#define add(a, b)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
This class represents a single result from folding an operation.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
std::optional< FloatType > parseFloatType(MLIRContext *ctx, StringRef name)
Map strings to float types.
Definition Utils.cpp:360
Value createProduct(OpBuilder &builder, Location loc, ArrayRef< Value > values)
Definition Utils.cpp:345
Include the generated interface declarations.
Value convertScalarToDtype(OpBuilder &b, Location loc, Value operand, Type toType, bool isUnsignedCast)
Converts a scalar value operand to type toType.
Definition Utils.cpp:238
Value createScalarOrSplatConstant(OpBuilder &builder, Location loc, Type type, const APInt &value)
Create a constant of type type at location loc whose value is value (an APInt or APFloat whose type m...
Definition Utils.cpp:270
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:304
Value getValueOrCreateConstantIntOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Definition Utils.cpp:102
Value getValueOrCreateCastToIndexLike(OpBuilder &b, Location loc, Type targetType, Value value)
Create a cast from an index-like value (index or integer) to another index-like value.
Definition Utils.cpp:119
Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Definition Utils.cpp:111
std::optional< SmallVector< OpFoldResult > > inferExpandShapeOutputShape(OpBuilder &b, Location loc, ShapedType expandedType, ArrayRef< ReassociationIndices > reassociation, ArrayRef< OpFoldResult > inputShape)
Infer the output shape for a {memref|tensor}.expand_shape when it is possible to do so.
Definition Utils.cpp:23
llvm::SmallBitVector getPositionsOfShapeOne(unsigned rank, ArrayRef< int64_t > shape)
Definition Utils.cpp:90
detail::op_matcher< arith::ConstantIndexOp > matchConstantIndex()
Matches a ConstantIndexOp.
Definition Utils.cpp:86
SmallVector< int64_t, 2 > ReassociationIndices
Definition Utils.h:27
Value _and(Value lhs, Value rhs)
Definition Utils.cpp:311
Value slt(Value lhs, Value rhs)
Definition Utils.cpp:334
Value select(Value cmp, Value lhs, Value rhs)
Definition Utils.cpp:339
Value sgt(Value lhs, Value rhs)
Definition Utils.cpp:329
ArithBuilder(OpBuilder &b, Location loc, arith::IntegerOverflowFlags ovf=arith::IntegerOverflowFlags::none)
Definition Utils.h:104
Value sub(Value lhs, Value rhs)
Definition Utils.cpp:319
ArithIndexingBuilder(OpBuilder &b, Location loc)
Definition Utils.h:127
The matcher that matches a certain kind of op.
Definition Matchers.h:283