1 | //===- UBOps.cpp - UB Dialect Operations ----------------------------------===// |
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/Dialect/UB/IR/UBOps.h" |
10 | #include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h" |
11 | #include "mlir/Transforms/InliningUtils.h" |
12 | |
13 | #include "mlir/IR/Builders.h" |
14 | #include "mlir/IR/DialectImplementation.h" |
15 | #include "llvm/ADT/TypeSwitch.h" |
16 | |
17 | #include "mlir/Dialect/UB/IR/UBOpsDialect.cpp.inc" |
18 | |
19 | using namespace mlir; |
20 | using namespace mlir::ub; |
21 | |
22 | namespace { |
23 | /// This class defines the interface for handling inlining with UB |
24 | /// operations. |
25 | struct UBInlinerInterface : public DialectInlinerInterface { |
26 | using DialectInlinerInterface::DialectInlinerInterface; |
27 | |
28 | /// All UB ops can be inlined. |
29 | bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final { |
30 | return true; |
31 | } |
32 | }; |
33 | } // namespace |
34 | |
35 | //===----------------------------------------------------------------------===// |
36 | // UBDialect |
37 | //===----------------------------------------------------------------------===// |
38 | |
39 | void UBDialect::initialize() { |
40 | addOperations< |
41 | #define GET_OP_LIST |
42 | #include "mlir/Dialect/UB/IR/UBOps.cpp.inc" |
43 | >(); |
44 | addAttributes< |
45 | #define GET_ATTRDEF_LIST |
46 | #include "mlir/Dialect/UB/IR/UBOpsAttributes.cpp.inc" |
47 | >(); |
48 | addInterfaces<UBInlinerInterface>(); |
49 | declarePromisedInterface<ConvertToLLVMPatternInterface, UBDialect>(); |
50 | } |
51 | |
52 | Operation *UBDialect::materializeConstant(OpBuilder &builder, Attribute value, |
53 | Type type, Location loc) { |
54 | if (auto attr = dyn_cast<PoisonAttr>(value)) |
55 | return builder.create<PoisonOp>(loc, type, attr); |
56 | |
57 | return nullptr; |
58 | } |
59 | |
60 | OpFoldResult PoisonOp::fold(FoldAdaptor /*adaptor*/) { return getValue(); } |
61 | |
62 | #include "mlir/Dialect/UB/IR/UBOpsInterfaces.cpp.inc" |
63 | |
64 | #define GET_ATTRDEF_CLASSES |
65 | #include "mlir/Dialect/UB/IR/UBOpsAttributes.cpp.inc" |
66 | |
67 | #define GET_OP_CLASSES |
68 | #include "mlir/Dialect/UB/IR/UBOps.cpp.inc" |
69 | |