1 | //===- PtrDialect.cpp - Pointer dialect ---------------------*- C++ -*-===// |
2 | // |
3 | // This file is licensed 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 | // This file implements the Pointer dialect. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "mlir/Dialect/Ptr/IR/PtrOps.h" |
14 | #include "mlir/IR/DialectImplementation.h" |
15 | #include "mlir/IR/Matchers.h" |
16 | #include "mlir/IR/PatternMatch.h" |
17 | #include "mlir/Interfaces/DataLayoutInterfaces.h" |
18 | #include "mlir/Transforms/InliningUtils.h" |
19 | #include "llvm/ADT/SmallString.h" |
20 | #include "llvm/ADT/TypeSwitch.h" |
21 | |
22 | using namespace mlir; |
23 | using namespace mlir::ptr; |
24 | |
25 | //===----------------------------------------------------------------------===// |
26 | // Pointer dialect |
27 | //===----------------------------------------------------------------------===// |
28 | |
29 | void PtrDialect::initialize() { |
30 | addOperations< |
31 | #define GET_OP_LIST |
32 | #include "mlir/Dialect/Ptr/IR/PtrOps.cpp.inc" |
33 | >(); |
34 | addAttributes< |
35 | #define GET_ATTRDEF_LIST |
36 | #include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.cpp.inc" |
37 | >(); |
38 | addTypes< |
39 | #define GET_TYPEDEF_LIST |
40 | #include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc" |
41 | >(); |
42 | } |
43 | |
44 | //===----------------------------------------------------------------------===// |
45 | // PtrAddOp |
46 | //===----------------------------------------------------------------------===// |
47 | |
48 | /// Fold: ptradd ptr + 0 -> ptr |
49 | OpFoldResult PtrAddOp::fold(FoldAdaptor adaptor) { |
50 | Attribute attr = adaptor.getOffset(); |
51 | if (!attr) |
52 | return nullptr; |
53 | if (llvm::APInt value; m_ConstantInt(&value).match(attr) && value.isZero()) |
54 | return getBase(); |
55 | return nullptr; |
56 | } |
57 | |
58 | //===----------------------------------------------------------------------===// |
59 | // TypeOffsetOp |
60 | //===----------------------------------------------------------------------===// |
61 | |
62 | llvm::TypeSize TypeOffsetOp::getTypeSize(std::optional<DataLayout> layout) { |
63 | if (layout) |
64 | return layout->getTypeSize(getElementType()); |
65 | DataLayout dl = DataLayout::closest(*this); |
66 | return dl.getTypeSize(getElementType()); |
67 | } |
68 | |
69 | //===----------------------------------------------------------------------===// |
70 | // Pointer API. |
71 | //===----------------------------------------------------------------------===// |
72 | |
73 | #include "mlir/Dialect/Ptr/IR/PtrOpsDialect.cpp.inc" |
74 | |
75 | #define GET_ATTRDEF_CLASSES |
76 | #include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.cpp.inc" |
77 | |
78 | #include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.cpp.inc" |
79 | |
80 | #include "mlir/Dialect/Ptr/IR/MemorySpaceAttrInterfaces.cpp.inc" |
81 | |
82 | #include "mlir/Dialect/Ptr/IR/PtrOpsEnums.cpp.inc" |
83 | |
84 | #define GET_TYPEDEF_CLASSES |
85 | #include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc" |
86 | |
87 | #define GET_OP_CLASSES |
88 | #include "mlir/Dialect/Ptr/IR/PtrOps.cpp.inc" |
89 | |