MLIR  17.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 TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType,
126  OpBuilder &builder, Location loc);
127 
128 /// Returns the identity value associated with an AtomicRMWKind op.
129 Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder,
130  Location loc);
131 
132 /// Returns the value obtained by applying the reduction operation kind
133 /// associated with a binary AtomicRMWKind op to `lhs` and `rhs`.
134 Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc,
135  Value lhs, Value rhs);
136 
137 arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred);
138 } // namespace arith
139 } // namespace mlir
140 
141 #endif // MLIR_DIALECT_ARITH_IR_ARITH_H_
This class helps build Operations.
Definition: Builders.h:202
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:220
static bool classof(Operation *op)
Definition: ArithOps.cpp:226
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:232
static bool classof(Operation *op)
Definition: ArithOps.cpp:238
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:199
static bool classof(Operation *op)
Definition: ArithOps.cpp:214
Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder, Location loc)
Returns the identity value associated with an AtomicRMWKind op.
Definition: ArithOps.cpp:2381
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:1589
TypedAttr getIdentityValueAttr(AtomicRMWKind kind, Type resultType, OpBuilder &builder, Location loc)
Returns the identity value attribute associated with an AtomicRMWKind op.
Definition: ArithOps.cpp:2334
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:2389
arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred)
Invert an integer comparison predicate.
Definition: ArithOps.cpp:51
This header declares functions that assit transformations in the MemRef dialect.
This represents an operation in an abstracted form, suitable for use with the builder APIs.