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