MLIR 23.0.0git
Arith.h
Go to the documentation of this file.
1//===- Arith.h - Arith 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_ARITH_IR_ARITH_H_
10#define MLIR_DIALECT_ARITH_IR_ARITH_H_
11
13#include "mlir/IR/Dialect.h"
23#include "llvm/ADT/StringExtras.h"
24
25//===----------------------------------------------------------------------===//
26// ArithDialect
27//===----------------------------------------------------------------------===//
28
29#include "mlir/Dialect/Arith/IR/ArithOpsDialect.h.inc"
30
31//===----------------------------------------------------------------------===//
32// Arith Dialect Enum Attributes
33//===----------------------------------------------------------------------===//
34
35#include "mlir/Dialect/Arith/IR/ArithOpsEnums.h.inc"
36#define GET_ATTRDEF_CLASSES
37#include "mlir/Dialect/Arith/IR/ArithOpsAttributes.h.inc"
38
39//===----------------------------------------------------------------------===//
40// Arith Interfaces
41//===----------------------------------------------------------------------===//
42#include "mlir/Dialect/Arith/IR/ArithOpsInterfaces.h.inc"
43
44//===----------------------------------------------------------------------===//
45// Arith Dialect Operations
46//===----------------------------------------------------------------------===//
47
48#define GET_OP_CLASSES
49#include "mlir/Dialect/Arith/IR/ArithOps.h.inc"
50
51namespace mlir {
52namespace arith {
53
54/// Specialization of `arith.constant` op that returns an integer value.
55class ConstantIntOp : public arith::ConstantOp {
56public:
57 using arith::ConstantOp::ConstantOp;
58 static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
59
60 /// Build a constant int op that produces an integer of the specified width.
61 static void build(OpBuilder &builder, OperationState &result, int64_t value,
62 unsigned width);
63 static ConstantIntOp create(OpBuilder &builder, Location location,
64 int64_t value, unsigned width);
66 unsigned width);
67
68 /// Build a constant int op that produces an integer of the specified type,
69 /// which must be an integer type.
70 static void build(OpBuilder &builder, OperationState &result, Type type,
72 static ConstantIntOp create(OpBuilder &builder, Location location, Type type,
74 static ConstantIntOp create(ImplicitLocOpBuilder &builder, Type type,
76
77 /// Build a constant int op that produces an integer from an APInt
78 static void build(OpBuilder &builder, OperationState &result, Type type,
79 const APInt &value);
80 static ConstantIntOp create(OpBuilder &builder, Location location, Type type,
81 const APInt &value);
82 static ConstantIntOp create(ImplicitLocOpBuilder &builder, Type type,
83 const APInt &value);
84
85 inline int64_t value() {
86 return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
87 }
88
89 static bool classof(Operation *op);
90};
91
92/// Specialization of `arith.constant` op that returns a floating point value.
93class ConstantFloatOp : public arith::ConstantOp {
94public:
95 using arith::ConstantOp::ConstantOp;
96 static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
97
98 /// Build a constant float op that produces a float of the specified type.
99 static void build(OpBuilder &builder, OperationState &result, FloatType type,
100 const APFloat &value);
101 static ConstantFloatOp create(OpBuilder &builder, Location location,
102 FloatType type, const APFloat &value);
103 static ConstantFloatOp create(ImplicitLocOpBuilder &builder, FloatType type,
104 const APFloat &value);
105
106 inline APFloat value() {
107 return cast<FloatAttr>(arith::ConstantOp::getValue()).getValue();
108 }
109
110 static bool classof(Operation *op);
111};
112
113/// Specialization of `arith.constant` op that returns an integer of index type.
114class ConstantIndexOp : public arith::ConstantOp {
115public:
116 using arith::ConstantOp::ConstantOp;
117 static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
118 /// Build a constant int op that produces an index.
119 static void build(OpBuilder &builder, OperationState &result, int64_t value);
120 static ConstantIndexOp create(OpBuilder &builder, Location location,
121 int64_t value);
123
124 inline int64_t value() {
125 return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
126 }
127
128 static bool classof(Operation *op);
129};
130
131} // namespace arith
132} // namespace mlir
133
134//===----------------------------------------------------------------------===//
135// Utility Functions
136//===----------------------------------------------------------------------===//
137
138namespace mlir {
139namespace arith {
140
141/// Compute `lhs` `pred` `rhs`, where `pred` is one of the known integer
142/// comparison predicates.
143bool applyCmpPredicate(arith::CmpIPredicate predicate, const APInt &lhs,
144 const APInt &rhs);
145
146/// Compute `lhs` `pred` `rhs`, where `pred` is one of the known floating point
147/// comparison predicates.
148bool applyCmpPredicate(arith::CmpFPredicate predicate, const APFloat &lhs,
149 const APFloat &rhs);
150
151/// Returns the identity value attribute associated with an AtomicRMWKind op.
152/// `useOnlyFiniteValue` defines whether the identity value should steer away
153/// from infinity representations or anything that is not a proper finite
154/// number.
155/// E.g., The identity value for maxf is in theory `-Inf`, but if we want to
156/// stay in the finite range, it would be `BiggestRepresentableNegativeFloat`.
157/// The purpose of this boolean is to offer constants that will play nice
158/// with fast math related optimizations.
159TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType,
160 OpBuilder &builder, Location loc,
161 bool useOnlyFiniteValue = false);
162
163/// Return the identity numeric value associated to the give op. Return
164/// std::nullopt if there is no known neutral element.
165/// If `op` has `FastMathFlags::ninf`, only finite values will be used
166/// as neutral element.
167std::optional<TypedAttr> getNeutralElement(Operation *op);
168
169/// Returns the identity value associated with an AtomicRMWKind op.
170/// \see getIdentityValueAttr for a description of what `useOnlyFiniteValue`
171/// does.
172Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder,
173 Location loc, bool useOnlyFiniteValue = false);
174
175/// Returns the value obtained by applying the reduction operation kind
176/// associated with a binary AtomicRMWKind op to `lhs` and `rhs`.
177Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc,
178 Value lhs, Value rhs);
179
180arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred);
181
182/// Creates an `arith.constant` operation with a zero value of type `type`. This
183/// method asserts if `type` is invalid for representing zero with
184/// `arith.constant`.
185Value getZeroConstant(OpBuilder &builder, Location loc, Type type);
186} // namespace arith
187} // namespace mlir
188
189#endif // MLIR_DIALECT_ARITH_IR_ARITH_H_
lhs
ImplicitLocOpBuilder maintains a 'current location', allowing use of the create<> method without spec...
Definition Builders.h:632
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:209
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
static TypeID get()
Construct a type info object for the given type T.
Definition TypeID.h:245
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Specialization of arith.constant op that returns a floating point value.
Definition Arith.h:93
static ConstantFloatOp create(OpBuilder &builder, Location location, FloatType type, const APFloat &value)
Definition ArithOps.cpp:355
static bool classof(Operation *op)
Definition ArithOps.cpp:372
static void build(OpBuilder &builder, OperationState &result, FloatType type, const APFloat &value)
Build a constant float op that produces a float of the specified type.
Definition ArithOps.cpp:349
::mlir::TypeID resolveTypeID()
Definition Arith.h:96
Specialization of arith.constant op that returns an integer of index type.
Definition Arith.h:114
static void build(OpBuilder &builder, OperationState &result, int64_t value)
Build a constant int op that produces an index.
Definition ArithOps.cpp:378
::mlir::TypeID resolveTypeID()
Definition Arith.h:117
static bool classof(Operation *op)
Definition ArithOps.cpp:399
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:384
Specialization of arith.constant op that returns an integer value.
Definition Arith.h:55
static ConstantIntOp create(OpBuilder &builder, Location location, int64_t value, unsigned width)
Definition ArithOps.cpp:283
::mlir::TypeID resolveTypeID()
Definition Arith.h:58
static void build(OpBuilder &builder, OperationState &result, int64_t value, unsigned width)
Build a constant int op that produces an integer of the specified width.
Definition ArithOps.cpp:276
static bool classof(Operation *op)
Definition ArithOps.cpp:343
std::optional< TypedAttr > getNeutralElement(Operation *op)
Return the identity numeric value associated to the give op.
bool applyCmpPredicate(arith::CmpIPredicate predicate, const APInt &lhs, const APInt &rhs)
Compute lhs pred rhs, where pred is one of the known integer comparison predicates.
TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType, OpBuilder &builder, Location loc, bool useOnlyFiniteValue=false)
Returns the identity value attribute associated with an AtomicRMWKind op.
Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc, Value lhs, Value rhs)
Returns the value obtained by applying the reduction operation kind associated with a binary AtomicRM...
Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder, Location loc, bool useOnlyFiniteValue=false)
Returns the identity value associated with an AtomicRMWKind op.
arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred)
Invert an integer comparison predicate.
Definition ArithOps.cpp:94
Value getZeroConstant(OpBuilder &builder, Location loc, Type type)
Creates an arith.constant operation with a zero value of type type.
Definition ArithOps.cpp:405
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.