Skip to content

Commit e7e93ca

Browse files
committed
[mlir][Ptr] Add the MemorySpaceAttrInterface interface and dependencies.
This patch introduces the `MemorySpaceAttrInterface` interface. This interface is responsible for handling the semantics of `ptr` operations. For example, this interface can be used to create read-only memory spaces, making any other operation other than a load a verification error, see `TestConstMemorySpaceAttr` for a possible implementation of this concept. This patch also introduces Enum depedencies `AtomicOrdering`, and `AtomicBinOp`, both enumerations are clones of the Enums with same name in the LLVM Dialect.
1 parent ee572ed commit e7e93ca

File tree

17 files changed

+379
-25
lines changed

17 files changed

+379
-25
lines changed

mlir/include/mlir/Dialect/Ptr/IR/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,15 @@ set(LLVM_TARGET_DEFINITIONS PtrOps.td)
55
mlir_tablegen(PtrOpsAttrs.h.inc -gen-attrdef-decls -attrdefs-dialect=ptr)
66
mlir_tablegen(PtrOpsAttrs.cpp.inc -gen-attrdef-defs -attrdefs-dialect=ptr)
77
add_public_tablegen_target(MLIRPtrOpsAttributesIncGen)
8+
9+
set(LLVM_TARGET_DEFINITIONS MemorySpaceInterfaces.td)
10+
mlir_tablegen(MemorySpaceInterfaces.h.inc -gen-op-interface-decls)
11+
mlir_tablegen(MemorySpaceInterfaces.cpp.inc -gen-op-interface-defs)
12+
mlir_tablegen(MemorySpaceAttrInterfaces.h.inc -gen-attr-interface-decls)
13+
mlir_tablegen(MemorySpaceAttrInterfaces.cpp.inc -gen-attr-interface-defs)
14+
add_public_tablegen_target(MLIRPtrMemorySpaceInterfacesIncGen)
15+
16+
set(LLVM_TARGET_DEFINITIONS PtrOps.td)
17+
mlir_tablegen(PtrOpsEnums.h.inc -gen-enum-decls)
18+
mlir_tablegen(PtrOpsEnums.cpp.inc -gen-enum-defs)
19+
add_public_tablegen_target(MLIRPtrOpsEnumsGen)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//===-- MemorySpaceInterfaces.h - ptr memory space interfaces ---*- C++ -*-===//
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+
// This file defines the ptr dialect memory space interfaces.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_PTR_IR_MEMORYSPACEINTERFACES_H
14+
#define MLIR_DIALECT_PTR_IR_MEMORYSPACEINTERFACES_H
15+
16+
#include "mlir/IR/Attributes.h"
17+
#include "mlir/IR/BuiltinAttributes.h"
18+
#include "mlir/IR/OpDefinition.h"
19+
20+
namespace mlir {
21+
class Operation;
22+
namespace ptr {
23+
enum class AtomicBinOp : uint64_t;
24+
enum class AtomicOrdering : uint64_t;
25+
} // namespace ptr
26+
} // namespace mlir
27+
28+
#include "mlir/Dialect/Ptr/IR/MemorySpaceAttrInterfaces.h.inc"
29+
30+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h.inc"
31+
32+
#endif // MLIR_DIALECT_PTR_IR_MEMORYSPACEINTERFACES_H
Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
//===-- MemorySpaceInterfaces.td - Memory space interfaces ----------------===//
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+
// This file defines memory space attribute interfaces.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef PTR_MEMORYSPACEINTERFACES
14+
#define PTR_MEMORYSPACEINTERFACES
15+
16+
include "mlir/IR/AttrTypeBase.td"
17+
include "mlir/IR/OpBase.td"
18+
19+
//===----------------------------------------------------------------------===//
20+
// Memory space attribute interface.
21+
//===----------------------------------------------------------------------===//
22+
23+
def MemorySpaceAttrInterface : AttrInterface<"MemorySpaceAttrInterface"> {
24+
let description = [{
25+
This interface defines a common API for interacting with the memory model of
26+
a memory space and the operations in the pointer dialect, giving proper
27+
semantical meaning to the ops.
28+
29+
Furthermore, this interface allows concepts such as read-only memory to be
30+
adequately modeled and enforced.
31+
}];
32+
let cppNamespace = "::mlir::ptr";
33+
let methods = [
34+
InterfaceMethod<
35+
/*desc=*/ [{
36+
Returns the default memory space as an attribute.
37+
}],
38+
/*returnType=*/ "::mlir::ptr::MemorySpaceAttrInterface",
39+
/*methodName=*/ "getDefaultMemorySpace",
40+
/*args=*/ (ins),
41+
/*methodBody=*/ [{}],
42+
/*defaultImpl=*/ [{}]
43+
>,
44+
InterfaceMethod<
45+
/*desc=*/ [{
46+
This method checks if it's valid to load a value from the memory space
47+
with a specific type, alignment, and atomic ordering.
48+
If `emitError` is non-null then the method is allowed to emit errors.
49+
}],
50+
/*returnType=*/ "::mlir::LogicalResult",
51+
/*methodName=*/ "isValidLoad",
52+
/*args=*/ (ins "::mlir::Type":$type,
53+
"::mlir::ptr::AtomicOrdering":$ordering,
54+
"::mlir::IntegerAttr":$alignment,
55+
"::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError),
56+
/*methodBody=*/ [{}],
57+
/*defaultImpl=*/ [{}]
58+
>,
59+
InterfaceMethod<
60+
/*desc=*/ [{
61+
This method checks if it's valid to store a value in the memory space
62+
with a specific type, alignment, and atomic ordering.
63+
If `emitError` is non-null then the method is allowed to emit errors.
64+
}],
65+
/*returnType=*/ "::mlir::LogicalResult",
66+
/*methodName=*/ "isValidStore",
67+
/*args=*/ (ins "::mlir::Type":$type,
68+
"::mlir::ptr::AtomicOrdering":$ordering,
69+
"::mlir::IntegerAttr":$alignment,
70+
"::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError),
71+
/*methodBody=*/ [{}],
72+
/*defaultImpl=*/ [{}]
73+
>,
74+
InterfaceMethod<
75+
/*desc=*/ [{
76+
This method checks if it's valid to perform an atomic operation in the
77+
memory space with a specific type, alignment, and atomic ordering.
78+
If `emitError` is non-null then the method is allowed to emit errors.
79+
}],
80+
/*returnType=*/ "::mlir::LogicalResult",
81+
/*methodName=*/ "isValidAtomicOp",
82+
/*args=*/ (ins "::mlir::ptr::AtomicBinOp":$op,
83+
"::mlir::Type":$type,
84+
"::mlir::ptr::AtomicOrdering":$ordering,
85+
"::mlir::IntegerAttr":$alignment,
86+
"::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError),
87+
/*methodBody=*/ [{}],
88+
/*defaultImpl=*/ [{}]
89+
>,
90+
InterfaceMethod<
91+
/*desc=*/ [{
92+
This method checks if it's valid to perform an atomic exchange operation
93+
in the memory space with a specific type, alignment, and atomic
94+
orderings.
95+
If `emitError` is non-null then the method is allowed to emit errors.
96+
}],
97+
/*returnType=*/ "::mlir::LogicalResult",
98+
/*methodName=*/ "isValidAtomicXchg",
99+
/*args=*/ (ins "::mlir::Type":$type,
100+
"::mlir::ptr::AtomicOrdering":$successOrdering,
101+
"::mlir::ptr::AtomicOrdering":$failureOrdering,
102+
"::mlir::IntegerAttr":$alignment,
103+
"::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError),
104+
/*methodBody=*/ [{}],
105+
/*defaultImpl=*/ [{}]
106+
>,
107+
InterfaceMethod<
108+
/*desc=*/ [{
109+
This method checks if it's valid to perform an `addrspacecast` op
110+
in the memory space.
111+
Both types are expected to be vectors of rank 1, or scalars of `ptr`
112+
type.
113+
If `emitError` is non-null then the method is allowed to emit errors.
114+
}],
115+
/*returnType=*/ "::mlir::LogicalResult",
116+
/*methodName=*/ "isValidAddrSpaceCast",
117+
/*args=*/ (ins "::mlir::Type":$tgt,
118+
"::mlir::Type":$src,
119+
"::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError),
120+
/*methodBody=*/ [{}],
121+
/*defaultImpl=*/ [{}]
122+
>,
123+
InterfaceMethod<
124+
/*desc=*/ [{
125+
This method checks if it's valid to perform a `ptrtoint` or `inttoptr`
126+
op in the memory space. `CastValidity::InvalidSourceType` always refers
127+
to the 'ptr-like' type and `CastValidity::InvalidTargetType` always
128+
refers to the `int-like` type.
129+
The first type is expected to be integer-like, while the second must be a
130+
ptr-like type.
131+
If `emitError` is non-null then the method is allowed to emit errors.
132+
}],
133+
/*returnType=*/ "::mlir::LogicalResult",
134+
/*methodName=*/ "isValidPtrIntCast",
135+
/*args=*/ (ins "::mlir::Type":$intLikeTy,
136+
"::mlir::Type":$ptrLikeTy,
137+
"::llvm::function_ref<::mlir::InFlightDiagnostic()>":$emitError),
138+
/*methodBody=*/ [{}],
139+
/*defaultImpl=*/ [{}]
140+
>,
141+
];
142+
}
143+
144+
def MemorySpaceOpInterface : OpInterface<"MemorySpaceOpInterface"> {
145+
let description = [{
146+
An interface for operations with a memory space.
147+
}];
148+
149+
let cppNamespace = "::mlir::ptr";
150+
151+
let methods = [
152+
InterfaceMethod<
153+
/*desc=*/ "Returns the memory space of the op.",
154+
/*returnType=*/ "::mlir::ptr::MemorySpaceAttrInterface",
155+
/*methodName=*/ "getMemorySpace",
156+
/*args=*/ (ins),
157+
/*methodBody=*/ [{}],
158+
/*defaultImpl=*/ [{}]
159+
>,
160+
];
161+
}
162+
#endif // PTR_MEMORYSPACEINTERFACES

mlir/include/mlir/Dialect/Ptr/IR/PtrAttrs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,6 @@
1818
#define GET_ATTRDEF_CLASSES
1919
#include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.h.inc"
2020

21+
#include "mlir/Dialect/Ptr/IR/PtrOpsEnums.h.inc"
22+
2123
#endif // MLIR_DIALECT_PTR_IR_PTRATTRS_H

mlir/include/mlir/Dialect/Ptr/IR/PtrDialect.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def Ptr_PtrType : Ptr_Type<"Ptr", "ptr", [
4444
let description = [{
4545
The `ptr` type is an opaque pointer type. This type typically represents a
4646
handle to an object in memory or target-dependent values like `nullptr`.
47-
Pointers are optionally parameterized by a memory space.
47+
Pointers are parameterized by a memory space.
4848

4949
Syntax:
5050

@@ -53,10 +53,10 @@ def Ptr_PtrType : Ptr_Type<"Ptr", "ptr", [
5353
memory-space ::= attribute-value
5454
```
5555
}];
56-
let parameters = (ins OptionalParameter<"Attribute">:$memorySpace);
57-
let assemblyFormat = "(`<` $memorySpace^ `>`)?";
56+
let parameters = (ins "MemorySpaceAttrInterface":$memorySpace);
57+
let assemblyFormat = "`<` $memorySpace `>`";
5858
let builders = [
59-
TypeBuilder<(ins CArg<"Attribute", "nullptr">:$memorySpace), [{
59+
TypeBuilder<(ins CArg<"MemorySpaceAttrInterface", "nullptr">:$memorySpace), [{
6060
return $_get($_ctxt, memorySpace);
6161
}]>
6262
];
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//===-- PtrEnums.td - Ptr dialect enumerations -------------*- tablegen -*-===//
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+
#ifndef PTR_ENUMS
10+
#define PTR_ENUMS
11+
12+
include "mlir/IR/EnumAttr.td"
13+
14+
//===----------------------------------------------------------------------===//
15+
// Atomic binary op enum attribute
16+
//===----------------------------------------------------------------------===//
17+
18+
def AtomicBinOpXchg : I64EnumAttrCase<"xchg", 0, "xchg">;
19+
def AtomicBinOpAdd : I64EnumAttrCase<"add", 1, "add">;
20+
def AtomicBinOpSub : I64EnumAttrCase<"sub", 2, "sub">;
21+
def AtomicBinOpAnd : I64EnumAttrCase<"_and", 3, "_and">;
22+
def AtomicBinOpNand : I64EnumAttrCase<"nand", 4, "nand">;
23+
def AtomicBinOpOr : I64EnumAttrCase<"_or", 5, "_or">;
24+
def AtomicBinOpXor : I64EnumAttrCase<"_xor", 6, "_xor">;
25+
def AtomicBinOpMax : I64EnumAttrCase<"max", 7, "max">;
26+
def AtomicBinOpMin : I64EnumAttrCase<"min", 8, "min">;
27+
def AtomicBinOpUMax : I64EnumAttrCase<"umax", 9, "umax">;
28+
def AtomicBinOpUMin : I64EnumAttrCase<"umin", 10, "umin">;
29+
def AtomicBinOpFAdd : I64EnumAttrCase<"fadd", 11, "fadd">;
30+
def AtomicBinOpFSub : I64EnumAttrCase<"fsub", 12, "fsub">;
31+
def AtomicBinOpFMax : I64EnumAttrCase<"fmax", 13, "fmax">;
32+
def AtomicBinOpFMin : I64EnumAttrCase<"fmin", 14, "fmin">;
33+
def AtomicBinOpUIncWrap : I64EnumAttrCase<"uinc_wrap", 15, "uinc_wrap">;
34+
def AtomicBinOpUDecWrap : I64EnumAttrCase<"udec_wrap", 16, "udec_wrap">;
35+
36+
def AtomicBinOp : I64EnumAttr<
37+
"AtomicBinOp",
38+
"ptr.atomicrmw binary operations",
39+
[AtomicBinOpXchg, AtomicBinOpAdd, AtomicBinOpSub, AtomicBinOpAnd,
40+
AtomicBinOpNand, AtomicBinOpOr, AtomicBinOpXor, AtomicBinOpMax,
41+
AtomicBinOpMin, AtomicBinOpUMax, AtomicBinOpUMin, AtomicBinOpFAdd,
42+
AtomicBinOpFSub, AtomicBinOpFMax, AtomicBinOpFMin, AtomicBinOpUIncWrap,
43+
AtomicBinOpUDecWrap]> {
44+
let cppNamespace = "::mlir::ptr";
45+
}
46+
47+
//===----------------------------------------------------------------------===//
48+
// Atomic ordering enum attribute
49+
//===----------------------------------------------------------------------===//
50+
51+
def AtomicOrderingNotAtomic : I64EnumAttrCase<"not_atomic", 0, "not_atomic">;
52+
def AtomicOrderingUnordered : I64EnumAttrCase<"unordered", 1, "unordered">;
53+
def AtomicOrderingMonotonic : I64EnumAttrCase<"monotonic", 2, "monotonic">;
54+
def AtomicOrderingAcquire : I64EnumAttrCase<"acquire", 3, "acquire">;
55+
def AtomicOrderingRelease : I64EnumAttrCase<"release", 4, "release">;
56+
def AtomicOrderingAcqRel : I64EnumAttrCase<"acq_rel", 5, "acq_rel">;
57+
def AtomicOrderingSeqCst : I64EnumAttrCase<"seq_cst", 6, "seq_cst">;
58+
59+
def AtomicOrdering : I64EnumAttr<
60+
"AtomicOrdering",
61+
"Atomic ordering for LLVM's memory model",
62+
[AtomicOrderingNotAtomic, AtomicOrderingUnordered, AtomicOrderingMonotonic,
63+
AtomicOrderingAcquire, AtomicOrderingRelease, AtomicOrderingAcqRel,
64+
AtomicOrderingSeqCst
65+
]> {
66+
let cppNamespace = "::mlir::ptr";
67+
}
68+
69+
#endif // PTR_ENUMS

mlir/include/mlir/Dialect/Ptr/IR/PtrOps.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
include "mlir/Dialect/Ptr/IR/PtrDialect.td"
1313
include "mlir/Dialect/Ptr/IR/PtrAttrDefs.td"
14+
include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.td"
1415
include "mlir/IR/OpAsmInterface.td"
1516

1617
#endif // PTR_OPS

mlir/include/mlir/Dialect/Ptr/IR/PtrTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef MLIR_DIALECT_PTR_IR_PTRTYPES_H
1414
#define MLIR_DIALECT_PTR_IR_PTRTYPES_H
1515

16+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
1617
#include "mlir/IR/Types.h"
1718
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1819

mlir/lib/Dialect/Ptr/IR/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ add_mlir_dialect_library(
77
DEPENDS
88
MLIRPtrOpsAttributesIncGen
99
MLIRPtrOpsIncGen
10-
10+
MLIRPtrOpsEnumsGen
11+
MLIRPtrMemorySpaceInterfacesIncGen
1112
LINK_LIBS
1213
PUBLIC
1314
MLIRIR

mlir/lib/Dialect/Ptr/IR/PtrDialect.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ void PtrDialect::initialize() {
4848
#define GET_ATTRDEF_CLASSES
4949
#include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.cpp.inc"
5050

51+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.cpp.inc"
52+
53+
#include "mlir/Dialect/Ptr/IR/MemorySpaceAttrInterfaces.cpp.inc"
54+
55+
#include "mlir/Dialect/Ptr/IR/PtrOpsEnums.cpp.inc"
56+
5157
#define GET_TYPEDEF_CLASSES
5258
#include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc"
5359

mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ constexpr const static unsigned kDefaultPointerSizeBits = 64;
2525
constexpr const static unsigned kBitsInByte = 8;
2626
constexpr const static unsigned kDefaultPointerAlignment = 8;
2727

28-
static Attribute getDefaultMemorySpace(PtrType ptr) { return nullptr; }
28+
static MemorySpaceAttrInterface getDefaultMemorySpace(PtrType ptr) {
29+
return ptr.getMemorySpace().getDefaultMemorySpace();
30+
}
2931

3032
/// Searches the data layout for the pointer spec, returns nullptr if it is not
3133
/// found.

0 commit comments

Comments
 (0)