MLIR  18.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"
14 #include "mlir/IR/OpDefinition.h"
21 #include "llvm/ADT/StringExtras.h"
22 
23 //===----------------------------------------------------------------------===//
24 // ArithDialect
25 //===----------------------------------------------------------------------===//
26 
27 #include "mlir/Dialect/Arith/IR/ArithOpsDialect.h.inc"
28 
29 //===----------------------------------------------------------------------===//
30 // Arith Dialect Enum Attributes
31 //===----------------------------------------------------------------------===//
32 
33 #include "mlir/Dialect/Arith/IR/ArithOpsEnums.h.inc"
34 #define GET_ATTRDEF_CLASSES
35 #include "mlir/Dialect/Arith/IR/ArithOpsAttributes.h.inc"
36 
37 //===----------------------------------------------------------------------===//
38 // Arith Interfaces
39 //===----------------------------------------------------------------------===//
40 #include "mlir/Dialect/Arith/IR/ArithOpsInterfaces.h.inc"
41 
42 //===----------------------------------------------------------------------===//
43 // Arith Dialect Operations
44 //===----------------------------------------------------------------------===//
45 
46 #define GET_OP_CLASSES
47 #include "mlir/Dialect/Arith/IR/ArithOps.h.inc"
48 
49 namespace mlir {
50 namespace arith {
51 
52 /// Specialization of `arith.constant` op that returns an integer value.
53 class ConstantIntOp : public arith::ConstantOp {
54 public:
55  using arith::ConstantOp::ConstantOp;
56 
57  /// Build a constant int op that produces an integer of the specified width.
58  static void build(OpBuilder &builder, OperationState &result, int64_t value,
59  unsigned width);
60 
61  /// Build a constant int op that produces an integer of the specified type,
62  /// which must be an integer type.
63  static void build(OpBuilder &builder, OperationState &result, int64_t value,
64  Type type);
65 
66  inline int64_t value() {
67  return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
68  }
69 
70  static bool classof(Operation *op);
71 };
72 
73 /// Specialization of `arith.constant` op that returns a floating point value.
74 class ConstantFloatOp : public arith::ConstantOp {
75 public:
76  using arith::ConstantOp::ConstantOp;
77 
78  /// Build a constant float op that produces a float of the specified type.
79  static void build(OpBuilder &builder, OperationState &result,
80  const APFloat &value, FloatType type);
81 
82  inline APFloat value() {
83  return cast<FloatAttr>(arith::ConstantOp::getValue()).getValue();
84  }
85 
86  static bool classof(Operation *op);
87 };
88 
89 /// Specialization of `arith.constant` op that returns an integer of index type.
90 class ConstantIndexOp : public arith::ConstantOp {
91 public:
92  using arith::ConstantOp::ConstantOp;
93 
94  /// Build a constant int op that produces an index.
95  static void build(OpBuilder &builder, OperationState &result, int64_t value);
96 
97  inline int64_t value() {
98  return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
99  }
100 
101  static bool classof(Operation *op);
102 };
103 
104 } // namespace arith
105 } // namespace mlir
106 
107 //===----------------------------------------------------------------------===//
108 // Utility Functions
109 //===----------------------------------------------------------------------===//
110 
111 namespace mlir {
112 namespace arith {
113 
114 /// Compute `lhs` `pred` `rhs`, where `pred` is one of the known integer
115 /// comparison predicates.
116 bool applyCmpPredicate(arith::CmpIPredicate predicate, const APInt &lhs,
117  const APInt &rhs);
118 
119 /// Compute `lhs` `pred` `rhs`, where `pred` is one of the known floating point
120 /// comparison predicates.
121 bool applyCmpPredicate(arith::CmpFPredicate predicate, const APFloat &lhs,
122  const APFloat &rhs);
123 
124 /// Returns the identity value attribute associated with an AtomicRMWKind op.
125 /// `useOnlyFiniteValue` defines whether the identity value should steer away
126 /// from infinity representations or anything that is not a proper finite
127 /// number.
128 /// E.g., The identity value for maxf is in theory `-Inf`, but if we want to
129 /// stay in the finite range, it would be `BiggestRepresentableNegativeFloat`.
130 /// The purpose of this boolean is to offer constants that will play nice
131 /// with fast math related optimizations.
132 TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType,
133  OpBuilder &builder, Location loc,
134  bool useOnlyFiniteValue = false);
135 
136 /// Return the identity numeric value associated to the give op. Return
137 /// std::nullopt if there is no known neutral element.
138 /// If `op` has `FastMathFlags::ninf`, only finite values will be used
139 /// as neutral element.
140 std::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.
145 Value 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`.
150 Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc,
151  Value lhs, Value rhs);
152 
153 arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred);
154 } // namespace arith
155 } // namespace mlir
156 
157 #endif // MLIR_DIALECT_ARITH_IR_ARITH_H_
This class helps build Operations.
Definition: Builders.h:206
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
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:74
static void build(OpBuilder &builder, OperationState &result, const APFloat &value, FloatType type)
Build a constant float op that produces a float of the specified type.
Definition: ArithOps.cpp:236
static bool classof(Operation *op)
Definition: ArithOps.cpp:242
Specialization of arith.constant op that returns an integer of index type.
Definition: Arith.h:90
static void build(OpBuilder &builder, OperationState &result, int64_t value)
Build a constant int op that produces an index.
Definition: ArithOps.cpp:248
static bool classof(Operation *op)
Definition: ArithOps.cpp:254
Specialization of arith.constant op that returns an integer value.
Definition: Arith.h:53
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:215
static bool classof(Operation *op)
Definition: ArithOps.cpp:230
std::optional< TypedAttr > getNeutralElement(Operation *op)
Return the identity numeric value associated to the give op.
Definition: ArithOps.cpp:2462
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.
Definition: ArithOps.cpp:1661
TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType, OpBuilder &builder, Location loc, bool useOnlyFiniteValue=false)
Returns the identity value attribute associated with an AtomicRMWKind op.
Definition: ArithOps.cpp:2407
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...
Definition: ArithOps.cpp:2513
Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder, Location loc, bool useOnlyFiniteValue=false)
Returns the identity value associated with an AtomicRMWKind op.
Definition: ArithOps.cpp:2503
arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred)
Invert an integer comparison predicate.
Definition: ArithOps.cpp:65
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.