1 //===- Arithmetic.h - Arithmetic 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 #ifndef MLIR_DIALECT_ARITHMETIC_IR_ARITHMETIC_H_ 9 #define MLIR_DIALECT_ARITHMETIC_IR_ARITHMETIC_H_ 10 11 #include "mlir/IR/Dialect.h" 12 #include "mlir/IR/OpDefinition.h" 13 #include "mlir/IR/OpImplementation.h" 14 #include "mlir/Interfaces/CastInterfaces.h" 15 #include "mlir/Interfaces/InferIntRangeInterface.h" 16 #include "mlir/Interfaces/InferTypeOpInterface.h" 17 #include "mlir/Interfaces/SideEffectInterfaces.h" 18 #include "mlir/Interfaces/VectorInterfaces.h" 19 20 //===----------------------------------------------------------------------===// 21 // ArithmeticDialect 22 //===----------------------------------------------------------------------===// 23 24 #include "mlir/Dialect/Arithmetic/IR/ArithmeticOpsDialect.h.inc" 25 26 //===----------------------------------------------------------------------===// 27 // Arithmetic Dialect Enum Attributes 28 //===----------------------------------------------------------------------===// 29 30 #include "mlir/Dialect/Arithmetic/IR/ArithmeticOpsEnums.h.inc" 31 32 //===----------------------------------------------------------------------===// 33 // Arithmetic Dialect Operations 34 //===----------------------------------------------------------------------===// 35 36 #define GET_OP_CLASSES 37 #include "mlir/Dialect/Arithmetic/IR/ArithmeticOps.h.inc" 38 39 namespace mlir { 40 namespace arith { 41 42 /// Specialization of `arith.constant` op that returns an integer value. 43 class ConstantIntOp : public arith::ConstantOp { 44 public: 45 using arith::ConstantOp::ConstantOp; 46 47 /// Build a constant int op that produces an integer of the specified width. 48 static void build(OpBuilder &builder, OperationState &result, int64_t value, 49 unsigned width); 50 51 /// Build a constant int op that produces an integer of the specified type, 52 /// which must be an integer type. 53 static void build(OpBuilder &builder, OperationState &result, int64_t value, 54 Type type); 55 value()56 inline int64_t value() { 57 return arith::ConstantOp::getValue().cast<IntegerAttr>().getInt(); 58 } 59 60 static bool classof(Operation *op); 61 }; 62 63 /// Specialization of `arith.constant` op that returns a floating point value. 64 class ConstantFloatOp : public arith::ConstantOp { 65 public: 66 using arith::ConstantOp::ConstantOp; 67 68 /// Build a constant float op that produces a float of the specified type. 69 static void build(OpBuilder &builder, OperationState &result, 70 const APFloat &value, FloatType type); 71 value()72 inline APFloat value() { 73 return arith::ConstantOp::getValue().cast<FloatAttr>().getValue(); 74 } 75 76 static bool classof(Operation *op); 77 }; 78 79 /// Specialization of `arith.constant` op that returns an integer of index type. 80 class ConstantIndexOp : public arith::ConstantOp { 81 public: 82 using arith::ConstantOp::ConstantOp; 83 84 /// Build a constant int op that produces an index. 85 static void build(OpBuilder &builder, OperationState &result, int64_t value); 86 value()87 inline int64_t value() { 88 return arith::ConstantOp::getValue().cast<IntegerAttr>().getInt(); 89 } 90 91 static bool classof(Operation *op); 92 }; 93 94 } // namespace arith 95 } // namespace mlir 96 97 //===----------------------------------------------------------------------===// 98 // Utility Functions 99 //===----------------------------------------------------------------------===// 100 101 namespace mlir { 102 namespace arith { 103 104 /// Compute `lhs` `pred` `rhs`, where `pred` is one of the known integer 105 /// comparison predicates. 106 bool applyCmpPredicate(arith::CmpIPredicate predicate, const APInt &lhs, 107 const APInt &rhs); 108 109 /// Compute `lhs` `pred` `rhs`, where `pred` is one of the known floating point 110 /// comparison predicates. 111 bool applyCmpPredicate(arith::CmpFPredicate predicate, const APFloat &lhs, 112 const APFloat &rhs); 113 114 /// Returns the identity value attribute associated with an AtomicRMWKind op. 115 Attribute getIdentityValueAttr(AtomicRMWKind kind, Type resultType, 116 OpBuilder &builder, Location loc); 117 118 /// Returns the identity value associated with an AtomicRMWKind op. 119 Value getIdentityValue(AtomicRMWKind op, Type resultType, OpBuilder &builder, 120 Location loc); 121 122 /// Returns the value obtained by applying the reduction operation kind 123 /// associated with a binary AtomicRMWKind op to `lhs` and `rhs`. 124 Value getReductionOp(AtomicRMWKind op, OpBuilder &builder, Location loc, 125 Value lhs, Value rhs); 126 127 arith::CmpIPredicate invertPredicate(arith::CmpIPredicate pred); 128 } // namespace arith 129 } // namespace mlir 130 131 #endif // MLIR_DIALECT_ARITHMETIC_IR_ARITHMETIC_H_ 132