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