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