MLIR  21.0.0git
PtrDialect.cpp
Go to the documentation of this file.
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 
15 #include "mlir/IR/Matchers.h"
16 #include "mlir/IR/PatternMatch.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"
static Value getBase(Value v)
Looks through known "view-like" ops to find the base memref.
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity,...
Definition: SPIRVOps.cpp:188
Attributes are known-constant values of operations.
Definition: Attributes.h:25
The main mechanism for performing data layout queries.
static DataLayout closest(Operation *op)
Returns the layout of the closest parent operation carrying layout info.
llvm::TypeSize getTypeSize(Type t) const
Returns the size of the given type in the current scope.
This class represents a single result from folding an operation.
Definition: OpDefinition.h:271
Include the generated interface declarations.
detail::constant_int_value_binder m_ConstantInt(IntegerAttr::ValueType *bind_value)
Matches a constant holding a scalar/vector/tensor integer (splat) and writes the integer value to bin...
Definition: Matchers.h:527