MLIR 24.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
22
23//===----------------------------------------------------------------------===//
24// Arith Dialect Operations
25//===----------------------------------------------------------------------===//
26
27#define GET_OP_CLASSES
28#include "mlir/Dialect/Arith/IR/ArithOps.h.inc"
29
30namespace mlir {
31namespace arith {
32
33/// Specialization of `arith.constant` op that returns an integer value.
34class ConstantIntOp : public arith::ConstantOp {
35public:
36 using arith::ConstantOp::ConstantOp;
37 static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
38
39 /// Build a constant int op that produces an integer of the specified width.
40 static void build(OpBuilder &builder, OperationState &result, int64_t value,
41 unsigned width);
42 static ConstantIntOp create(OpBuilder &builder, Location location,
43 int64_t value, unsigned width);
45 unsigned width);
46
47 /// Build a constant int op that produces an integer of the specified type,
48 /// which must be an integer type.
49 static void build(OpBuilder &builder, OperationState &result, Type type,
51 static ConstantIntOp create(OpBuilder &builder, Location location, Type type,
53 static ConstantIntOp create(ImplicitLocOpBuilder &builder, Type type,
55
56 /// Build a constant int op that produces an integer from an APInt
57 static void build(OpBuilder &builder, OperationState &result, Type type,
58 const APInt &value);
59 static ConstantIntOp create(OpBuilder &builder, Location location, Type type,
60 const APInt &value);
61 static ConstantIntOp create(ImplicitLocOpBuilder &builder, Type type,
62 const APInt &value);
63
64 inline int64_t value() {
65 return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
66 }
67
68 static bool classof(Operation *op);
69};
70
71/// Specialization of `arith.constant` op that returns a floating point value.
72class ConstantFloatOp : public arith::ConstantOp {
73public:
74 using arith::ConstantOp::ConstantOp;
75 static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
76
77 /// Build a constant float op that produces a float of the specified type.
78 static void build(OpBuilder &builder, OperationState &result, FloatType type,
79 const APFloat &value);
80 static ConstantFloatOp create(OpBuilder &builder, Location location,
81 FloatType type, const APFloat &value);
82 static ConstantFloatOp create(ImplicitLocOpBuilder &builder, FloatType type,
83 const APFloat &value);
84
85 inline APFloat value() {
86 return cast<FloatAttr>(arith::ConstantOp::getValue()).getValue();
87 }
88
89 static bool classof(Operation *op);
90};
91
92/// Specialization of `arith.constant` op that returns an integer of index type.
93class ConstantIndexOp : public arith::ConstantOp {
94public:
95 using arith::ConstantOp::ConstantOp;
96 static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
97 /// Build a constant int op that produces an index.
98 static void build(OpBuilder &builder, OperationState &result, int64_t value);
99 static ConstantIndexOp create(OpBuilder &builder, Location location,
100 int64_t value);
102
103 inline int64_t value() {
104 return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
105 }
106
107 static bool classof(Operation *op);
108};
109
110} // namespace arith
111} // namespace mlir
112
113//===----------------------------------------------------------------------===//
114// Utility Functions
115//===----------------------------------------------------------------------===//
116
117namespace mlir {
118namespace arith {
119
120/// Compute `lhs` `pred` `rhs`, where `pred` is one of the known integer
121/// comparison predicates.
122bool applyCmpPredicate(arith::CmpIPredicate predicate, const APInt &lhs,
123 const APInt &rhs);
124
125/// Compute `lhs` `pred` `rhs`, where `pred` is one of the known floating point
126/// comparison predicates.
127bool applyCmpPredicate(arith::CmpFPredicate predicate, const APFloat &lhs,
128 const APFloat &rhs);
129
130/// Returns the identity value attribute associated with an AtomicRMWKind op.
131/// `useOnlyFiniteValue` defines whether the identity value should steer away
132/// from infinity representations or anything that is not a proper finite
133/// number.
134/// E.g., The identity value for maxf is in theory `-Inf`, but if we want to
135/// stay in the finite range, it would be `BiggestRepresentableNegativeFloat`.
136/// The purpose of this boolean is to offer constants that will play nice
137/// with fast math related optimizations.
138TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType,
139 OpBuilder &builder, Location loc,
140 bool useOnlyFiniteValue = false);
141
142/// Return the identity numeric value associated to the give op. Return
143/// std::nullopt if there is no known neutral element.
144/// If `op` has `FastMathFlags::ninf`, only finite values will be used
145/// as neutral element.
146std::optional<TypedAttr> getNeutralElement(Operation *op);
147
148/// Returns the identity value associated with an AtomicRMWKind op.
149/// \see getIdentityValueAttr for a description of what `useOnlyFiniteValue`
150/// does.
151Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder,
152 Location loc, bool useOnlyFiniteValue = false);
153
154/// Returns the value obtained by applying the reduction operation kind
155/// associated with a binary AtomicRMWKind op to `lhs` and `rhs`.
156Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc,
157 Value lhs, Value rhs);
158
159arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred);
160
161/// Creates an `arith.constant` operation with a zero value of type `type`. This
162/// method asserts if `type` is invalid for representing zero with
163/// `arith.constant`.
164Value getZeroConstant(OpBuilder &builder, Location loc, Type type);
165} // namespace arith
166} // namespace mlir
167
168#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:210
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:72
static ConstantFloatOp create(OpBuilder &builder, Location location, FloatType type, const APFloat &value)
Definition ArithOps.cpp:369
static bool classof(Operation *op)
Definition ArithOps.cpp:386
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:363
::mlir::TypeID resolveTypeID()
Definition Arith.h:75
Specialization of arith.constant op that returns an integer of index type.
Definition Arith.h:93
static void build(OpBuilder &builder, OperationState &result, int64_t value)
Build a constant int op that produces an index.
Definition ArithOps.cpp:392
::mlir::TypeID resolveTypeID()
Definition Arith.h:96
static bool classof(Operation *op)
Definition ArithOps.cpp:413
static ConstantIndexOp create(OpBuilder &builder, Location location, int64_t value)
Definition ArithOps.cpp:398
Specialization of arith.constant op that returns an integer value.
Definition Arith.h:34
static ConstantIntOp create(OpBuilder &builder, Location location, int64_t value, unsigned width)
Definition ArithOps.cpp:297
::mlir::TypeID resolveTypeID()
Definition Arith.h:37
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:290
static bool classof(Operation *op)
Definition ArithOps.cpp:357
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:95
Value getZeroConstant(OpBuilder &builder, Location loc, Type type)
Creates an arith.constant operation with a zero value of type type.
Definition ArithOps.cpp:419
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.