MLIR  19.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"
21 #include "mlir/IR/PatternMatch.h"
22 #include "mlir/IR/Value.h"
23 #include "llvm/ADT/ArrayRef.h"
24 
25 namespace mlir {
26 
27 /// Matches a ConstantIndexOp.
28 detail::op_matcher<arith::ConstantIndexOp> matchConstantIndex();
29 
30 llvm::SmallBitVector getPositionsOfShapeOne(unsigned rank,
31  ArrayRef<int64_t> shape);
32 
33 /// Converts an OpFoldResult to a Value. Returns the fold result if it casts to
34 /// a Value or creates a ConstantIndexOp if it casts to an IntegerAttribute.
35 /// Other attribute types are not supported.
36 Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc,
37  OpFoldResult ofr);
38 
39 /// Similar to the other overload, but converts multiple OpFoldResults into
40 /// Values.
41 SmallVector<Value>
42 getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc,
43  ArrayRef<OpFoldResult> valueOrAttrVec);
44 
45 /// Create a cast from an index-like value (index or integer) to another
46 /// index-like value. If the value type and the target type are the same, it
47 /// returns the original value.
48 Value getValueOrCreateCastToIndexLike(OpBuilder &b, Location loc,
49  Type targetType, Value value);
50 
51 /// Converts a scalar value `operand` to type `toType`. If the value doesn't
52 /// convert, a warning will be issued and the operand is returned as is (which
53 /// will presumably yield a verification issue downstream).
54 Value convertScalarToDtype(OpBuilder &b, Location loc, Value operand,
55  Type toType, bool isUnsignedCast);
56 
57 /// Create a constant of type `type` at location `loc` whose value is `value`
58 /// (an APInt or APFloat whose type must match the element type of `type`).
59 /// If `type` is a shaped type, create a splat constant of the given value.
60 /// Constants are folded if possible.
61 Value createScalarOrSplatConstant(OpBuilder &builder, Location loc, Type type,
62  const APInt &value);
63 Value createScalarOrSplatConstant(OpBuilder &builder, Location loc, Type type,
64  int64_t value);
65 Value createScalarOrSplatConstant(OpBuilder &builder, Location loc, Type type,
66  const APFloat &value);
67 
68 /// Helper struct to build simple arithmetic quantities with minimal type
69 /// inference support.
70 struct ArithBuilder {
71  ArithBuilder(OpBuilder &b, Location loc) : b(b), loc(loc) {}
72 
73  Value _and(Value lhs, Value rhs);
74  Value add(Value lhs, Value rhs);
75  Value sub(Value lhs, Value rhs);
76  Value mul(Value lhs, Value rhs);
77  Value select(Value cmp, Value lhs, Value rhs);
78  Value sgt(Value lhs, Value rhs);
79  Value slt(Value lhs, Value rhs);
80 
81 private:
82  OpBuilder &b;
83  Location loc;
84 };
85 
86 namespace arith {
87 
88 // Build the product of a sequence.
89 // If values = (v0, v1, ..., vn) than the returned
90 // value is v0 * v1 * ... * vn.
91 // All values must have the same type.
92 //
93 // The version without `resultType` must contain at least one element in values.
94 // Then the result will have the same type as the elements in `values`.
95 // If `values` is empty in the version with `resultType` returns 1 with type
96 // `resultType`.
97 Value createProduct(OpBuilder &builder, Location loc, ArrayRef<Value> values);
98 Value createProduct(OpBuilder &builder, Location loc, ArrayRef<Value> values,
99  Type resultType);
100 } // namespace arith
101 } // namespace mlir
102 
103 #endif // MLIR_DIALECT_ARITH_UTILS_UTILS_H
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:209
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:96
Value createProduct(OpBuilder &builder, Location loc, ArrayRef< Value > values)
Definition: Utils.cpp:269
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:169
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:201
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:50
Value getValueOrCreateConstantIndexOp(OpBuilder &b, Location loc, OpFoldResult ofr)
Converts an OpFoldResult to a Value.
Definition: Utils.cpp:41
llvm::SmallBitVector getPositionsOfShapeOne(unsigned rank, ArrayRef< int64_t > shape)
Definition: Utils.cpp:29
detail::op_matcher< arith::ConstantIndexOp > matchConstantIndex()
Matches a ConstantIndexOp.
Definition: Utils.cpp:25
Helper struct to build simple arithmetic quantities with minimal type inference support.
Definition: Utils.h:70
Value mul(Value lhs, Value rhs)
Definition: Utils.cpp:248
Value _and(Value lhs, Value rhs)
Definition: Utils.cpp:235
Value slt(Value lhs, Value rhs)
Definition: Utils.cpp:258
ArithBuilder(OpBuilder &b, Location loc)
Definition: Utils.h:71
Value select(Value cmp, Value lhs, Value rhs)
Definition: Utils.cpp:263
Value add(Value lhs, Value rhs)
Definition: Utils.cpp:238
Value sgt(Value lhs, Value rhs)
Definition: Utils.cpp:253
Value sub(Value lhs, Value rhs)
Definition: Utils.cpp:243