MLIR  20.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"
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 
50 namespace mlir {
51 namespace arith {
52 
53 /// Specialization of `arith.constant` op that returns an integer value.
54 class ConstantIntOp : public arith::ConstantOp {
55 public:
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 
63  /// Build a constant int op that produces an integer of the specified type,
64  /// which must be an integer type.
65  static void build(OpBuilder &builder, OperationState &result, int64_t value,
66  Type type);
67 
68  inline int64_t value() {
69  return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
70  }
71 
72  static bool classof(Operation *op);
73 };
74 
75 /// Specialization of `arith.constant` op that returns a floating point value.
76 class ConstantFloatOp : public arith::ConstantOp {
77 public:
78  using arith::ConstantOp::ConstantOp;
79  static ::mlir::TypeID resolveTypeID() { return TypeID::get<ConstantOp>(); }
80 
81  /// Build a constant float op that produces a float of the specified type.
82  static void build(OpBuilder &builder, OperationState &result,
83  const APFloat &value, FloatType type);
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.
93 class ConstantIndexOp : public arith::ConstantOp {
94 public:
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 
100  inline int64_t value() {
101  return cast<IntegerAttr>(arith::ConstantOp::getValue()).getInt();
102  }
103 
104  static bool classof(Operation *op);
105 };
106 
107 } // namespace arith
108 } // namespace mlir
109 
110 //===----------------------------------------------------------------------===//
111 // Utility Functions
112 //===----------------------------------------------------------------------===//
113 
114 namespace mlir {
115 namespace arith {
116 
117 /// Compute `lhs` `pred` `rhs`, where `pred` is one of the known integer
118 /// comparison predicates.
119 bool applyCmpPredicate(arith::CmpIPredicate predicate, const APInt &lhs,
120  const APInt &rhs);
121 
122 /// Compute `lhs` `pred` `rhs`, where `pred` is one of the known floating point
123 /// comparison predicates.
124 bool applyCmpPredicate(arith::CmpFPredicate predicate, const APFloat &lhs,
125  const APFloat &rhs);
126 
127 /// Returns the identity value attribute associated with an AtomicRMWKind op.
128 /// `useOnlyFiniteValue` defines whether the identity value should steer away
129 /// from infinity representations or anything that is not a proper finite
130 /// number.
131 /// E.g., The identity value for maxf is in theory `-Inf`, but if we want to
132 /// stay in the finite range, it would be `BiggestRepresentableNegativeFloat`.
133 /// The purpose of this boolean is to offer constants that will play nice
134 /// with fast math related optimizations.
135 TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType,
136  OpBuilder &builder, Location loc,
137  bool useOnlyFiniteValue = false);
138 
139 /// Return the identity numeric value associated to the give op. Return
140 /// std::nullopt if there is no known neutral element.
141 /// If `op` has `FastMathFlags::ninf`, only finite values will be used
142 /// as neutral element.
143 std::optional<TypedAttr> getNeutralElement(Operation *op);
144 
145 /// Returns the identity value associated with an AtomicRMWKind op.
146 /// \see getIdentityValueAttr for a description of what `useOnlyFiniteValue`
147 /// does.
148 Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder,
149  Location loc, bool useOnlyFiniteValue = false);
150 
151 /// Returns the value obtained by applying the reduction operation kind
152 /// associated with a binary AtomicRMWKind op to `lhs` and `rhs`.
153 Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc,
154  Value lhs, Value rhs);
155 
156 arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred);
157 } // namespace arith
158 } // namespace mlir
159 
160 #endif // MLIR_DIALECT_ARITH_IR_ARITH_H_
This class helps build Operations.
Definition: Builders.h:215
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:76
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:278
static bool classof(Operation *op)
Definition: ArithOps.cpp:284
::mlir::TypeID resolveTypeID()
Definition: Arith.h:79
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:290
::mlir::TypeID resolveTypeID()
Definition: Arith.h:96
static bool classof(Operation *op)
Definition: ArithOps.cpp:296
Specialization of arith.constant op that returns an integer value.
Definition: Arith.h:54
::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:257
static bool classof(Operation *op)
Definition: ArithOps.cpp:272
std::optional< TypedAttr > getNeutralElement(Operation *op)
Return the identity numeric value associated to the give op.
Definition: ArithOps.cpp:2547
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:1764
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:2480
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:2599
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:2589
arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred)
Invert an integer comparison predicate.
Definition: ArithOps.cpp:75
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.