| 1 | //===- ArithDialect.cpp - MLIR Arith dialect implementation -----===// |
| 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 | #include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h" |
| 10 | #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" |
| 11 | #include "mlir/Dialect/Arith/IR/Arith.h" |
| 12 | #include "mlir/Dialect/Bufferization/IR/BufferDeallocationOpInterface.h" |
| 13 | #include "mlir/Dialect/Bufferization/IR/BufferizableOpInterface.h" |
| 14 | #include "mlir/Dialect/UB/IR/UBOps.h" |
| 15 | #include "mlir/IR/Builders.h" |
| 16 | #include "mlir/IR/DialectImplementation.h" |
| 17 | #include "mlir/Interfaces/ValueBoundsOpInterface.h" |
| 18 | #include "mlir/Transforms/InliningUtils.h" |
| 19 | #include "llvm/ADT/TypeSwitch.h" |
| 20 | |
| 21 | using namespace mlir; |
| 22 | using namespace mlir::arith; |
| 23 | |
| 24 | #include "mlir/Dialect/Arith/IR/ArithOpsDialect.cpp.inc" |
| 25 | #include "mlir/Dialect/Arith/IR/ArithOpsInterfaces.cpp.inc" |
| 26 | #define GET_ATTRDEF_CLASSES |
| 27 | #include "mlir/Dialect/Arith/IR/ArithOpsAttributes.cpp.inc" |
| 28 | |
| 29 | namespace { |
| 30 | /// This class defines the interface for handling inlining for arithmetic |
| 31 | /// dialect operations. |
| 32 | struct ArithInlinerInterface : public DialectInlinerInterface { |
| 33 | using DialectInlinerInterface::DialectInlinerInterface; |
| 34 | |
| 35 | /// All arithmetic dialect ops can be inlined. |
| 36 | bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { |
| 37 | return true; |
| 38 | } |
| 39 | }; |
| 40 | } // namespace |
| 41 | |
| 42 | void arith::ArithDialect::initialize() { |
| 43 | addOperations< |
| 44 | #define GET_OP_LIST |
| 45 | #include "mlir/Dialect/Arith/IR/ArithOps.cpp.inc" |
| 46 | >(); |
| 47 | addAttributes< |
| 48 | #define GET_ATTRDEF_LIST |
| 49 | #include "mlir/Dialect/Arith/IR/ArithOpsAttributes.cpp.inc" |
| 50 | >(); |
| 51 | addInterfaces<ArithInlinerInterface>(); |
| 52 | declarePromisedInterface<ConvertToEmitCPatternInterface, ArithDialect>(); |
| 53 | declarePromisedInterface<ConvertToLLVMPatternInterface, ArithDialect>(); |
| 54 | declarePromisedInterface<bufferization::BufferDeallocationOpInterface, |
| 55 | SelectOp>(); |
| 56 | declarePromisedInterfaces<bufferization::BufferizableOpInterface, ConstantOp, |
| 57 | IndexCastOp, SelectOp>(); |
| 58 | declarePromisedInterfaces<ValueBoundsOpInterface, AddIOp, ConstantOp, SubIOp, |
| 59 | MulIOp>(); |
| 60 | } |
| 61 | |
| 62 | /// Materialize an integer or floating point constant. |
| 63 | Operation *arith::ArithDialect::materializeConstant(OpBuilder &builder, |
| 64 | Attribute value, Type type, |
| 65 | Location loc) { |
| 66 | if (auto poison = dyn_cast<ub::PoisonAttr>(value)) |
| 67 | return builder.create<ub::PoisonOp>(loc, type, poison); |
| 68 | |
| 69 | return ConstantOp::materialize(builder, value, type, loc); |
| 70 | } |
| 71 | |