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