Skip to content

Commit 553c327

Browse files
committed
[mlir] Ptr dialect
This patch introduces the Ptr dialect, a dialect to model pointer operations motivated by the goal of modularizing the LLVM dialect. More specifically, this patch introduces: - The pointer dialect and type. - The `MemorySpaceAttrInterface` interface, an interface to conceptualize memory models, giving proper semantical meaning to the Ptr dialect ops. - The `ptr::LoadOp` operation, an operation to load data from memory, with the semantics defined by `MemorySpaceAttrInterface` and translatable to LLVM IR. - The `SharedDialectTypeInterface` interface, an interface to delegate printing and parsing to a different dialect. - The introduction of `LLVM::AddressSpaceAttr`, an attribute to model LLVM memory semantics. - The replacement of `LLVMPointerType` with `ptr::PtrType`.
1 parent f4c6947 commit 553c327

36 files changed

+985
-159
lines changed

mlir/include/mlir/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ add_subdirectory(OpenACCMPCommon)
2727
add_subdirectory(OpenMP)
2828
add_subdirectory(PDL)
2929
add_subdirectory(PDLInterp)
30+
add_subdirectory(Ptr)
3031
add_subdirectory(Quant)
3132
add_subdirectory(SCF)
3233
add_subdirectory(Shape)

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

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

1212
include "mlir/Dialect/LLVMIR/LLVMDialect.td"
1313
include "mlir/IR/AttrTypeBase.td"
14+
include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.td"
1415

1516
// All of the attributes will extend this class.
1617
class LLVM_Attr<string name, string attrMnemonic,
@@ -47,6 +48,20 @@ def LinkageAttr : LLVM_Attr<"Linkage", "linkage"> {
4748
let assemblyFormat = "`<` $linkage `>`";
4849
}
4950

51+
//===----------------------------------------------------------------------===//
52+
// AddressSpaceAttr
53+
//===----------------------------------------------------------------------===//
54+
55+
def AddressSpaceAttr : LLVM_Attr<"AddressSpace", "address_space", [
56+
DeclareAttrInterfaceMethods<MemorySpaceAttrInterface, [
57+
"getModelOwner",
58+
"getDefaultMemorySpace",
59+
"isValidLoad"]>
60+
]> {
61+
let parameters = (ins DefaultValuedParameter<"unsigned", "0">:$addressSpace);
62+
let assemblyFormat = "(`<` $addressSpace^ `>`)?";
63+
}
64+
5065
//===----------------------------------------------------------------------===//
5166
// Loop Attributes
5267
//===----------------------------------------------------------------------===//

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define MLIR_DIALECT_LLVMIR_LLVMATTRS_H_
1616

1717
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
18+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h"
1819
#include "mlir/IR/OpImplementation.h"
1920
#include <optional>
2021

mlir/include/mlir/Dialect/LLVMIR/LLVMDialect.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ def LLVM_Dialect : Dialect {
9696
/// Register the attributes of this dialect.
9797
void registerAttributes();
9898
}];
99+
let dependentDialects = [
100+
"ptr::PtrDialect"
101+
];
99102
}
100103

101104
#endif // LLVMIR_DIALECT

mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
1515
#define MLIR_DIALECT_LLVMIR_LLVMTYPES_H_
1616

17+
#include "mlir/Dialect/Ptr/IR/PtrTypes.h"
1718
#include "mlir/IR/Types.h"
1819
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1920
#include "mlir/Interfaces/MemorySlotInterfaces.h"
@@ -291,7 +292,37 @@ enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
291292
std::optional<unsigned> extractPointerSpecValue(Attribute attr,
292293
PtrDLEntryPos pos);
293294

295+
/// Returns whether a pointer type has an LLVM address space.
296+
bool isLLVMPointerType(Type type);
297+
298+
/// Utility class for creating pointer types of the form
299+
/// `ptr.ptr<#llvm.address_space<#int_attr>>`
300+
class LLVMPointerType : public ptr::PtrType {
301+
public:
302+
LLVMPointerType() : ptr::PtrType() {}
303+
LLVMPointerType(const ptr::PtrType &ty) : ptr::PtrType(ty) {}
304+
template <typename T>
305+
static bool classof(T val) {
306+
static_assert(std::is_convertible<Type, T>::value,
307+
"casting from a non-convertible type");
308+
return isLLVMPointerType(val);
309+
}
310+
static PtrType get(::mlir::MLIRContext *context, unsigned addressSpace = 0);
311+
static ::mlir::Type parse(::mlir::AsmParser &odsParser);
312+
void print(::mlir::AsmPrinter &odsPrinter) const;
313+
};
314+
294315
} // namespace LLVM
295316
} // namespace mlir
296317

318+
namespace mlir {
319+
namespace detail {
320+
template <>
321+
class TypeIDResolver<LLVM::LLVMPointerType> {
322+
public:
323+
static TypeID resolveTypeID() { return TypeID::get<ptr::PtrType>(); }
324+
};
325+
} /* namespace detail */
326+
} // namespace mlir
327+
297328
#endif // MLIR_DIALECT_LLVMIR_LLVMTYPES_H_

mlir/include/mlir/Dialect/LLVMIR/LLVMTypes.td

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,39 +117,6 @@ def LLVMFunctionType : LLVMType<"LLVMFunction", "func"> {
117117
}];
118118
}
119119

120-
//===----------------------------------------------------------------------===//
121-
// LLVMPointerType
122-
//===----------------------------------------------------------------------===//
123-
124-
def LLVMPointerType : LLVMType<"LLVMPointer", "ptr", [
125-
DeclareTypeInterfaceMethods<DataLayoutTypeInterface, [
126-
"areCompatible", "verifyEntries"]>]> {
127-
let summary = "LLVM pointer type";
128-
let description = [{
129-
The `!llvm.ptr` type is an LLVM pointer type. This type typically represents
130-
a reference to an object in memory. Pointers are optionally parameterized
131-
by the address space.
132-
133-
Example:
134-
135-
```mlir
136-
!llvm.ptr
137-
```
138-
}];
139-
140-
let parameters = (ins DefaultValuedParameter<"unsigned", "0">:$addressSpace);
141-
let assemblyFormat = [{
142-
(`<` $addressSpace^ `>`)?
143-
}];
144-
145-
let skipDefaultBuilders = 1;
146-
let builders = [
147-
TypeBuilder<(ins CArg<"unsigned", "0">:$addressSpace), [{
148-
return $_get($_ctxt, addressSpace);
149-
}]>
150-
];
151-
}
152-
153120
//===----------------------------------------------------------------------===//
154121
// LLVMFixedVectorType
155122
//===----------------------------------------------------------------------===//
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(IR)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_mlir_dialect(PtrOps ptr)
2+
add_mlir_doc(PtrOps PtrOps Dialects/ -gen-op-doc)
3+
4+
set(LLVM_TARGET_DEFINITIONS MemorySpaceInterfaces.td)
5+
mlir_tablegen(MemorySpaceInterfaces.h.inc -gen-attr-interface-decls)
6+
mlir_tablegen(MemorySpaceInterfaces.cpp.inc -gen-attr-interface-defs)
7+
add_public_tablegen_target(MLIRPtrMemorySpaceInterfacesIncGen)
8+
9+
set(LLVM_TARGET_DEFINITIONS PtrOps.td)
10+
mlir_tablegen(PtrOpsEnums.h.inc -gen-enum-decls)
11+
mlir_tablegen(PtrOpsEnums.cpp.inc -gen-enum-defs)
12+
add_public_tablegen_target(MLIRPtrOpsEnumsGen)
13+
14+
set(LLVM_TARGET_DEFINITIONS PtrOps.td)
15+
mlir_tablegen(PtrOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=ptr)
16+
mlir_tablegen(PtrOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=ptr)
17+
add_public_tablegen_target(MLIRPtrOpsAttributesIncGen)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===-- MemorySpaceInterfaces.h - GPU compilation 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 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+
18+
#include "mlir/Dialect/Ptr/IR/MemorySpaceInterfaces.h.inc"
19+
20+
#endif // MLIR_DIALECT_PTR_IR_MEMORYSPACEINTERFACES_H
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
Returns which dialect owns the memory model.
36+
}],
37+
"Dialect*", "getModelOwner", (ins),
38+
[{}],
39+
[{ return nullptr; }]
40+
>,
41+
InterfaceMethod<[{
42+
Returns the address space as an unsigned int.
43+
}],
44+
"unsigned", "getAddressSpace", (ins),
45+
[{}],
46+
[{ return 0; }]
47+
>,
48+
InterfaceMethod<[{
49+
Returns the address space as an unsigned int.
50+
}],
51+
"Attribute", "getDefaultMemorySpace", (ins)
52+
>,
53+
InterfaceMethod<[{
54+
Returns whether a type can be loaded from memory.
55+
}],
56+
"bool", "isValidLoad", (ins "Type":$type),
57+
[{}],
58+
[{ return true; }]
59+
>,
60+
];
61+
}
62+
63+
#endif // PTR_MEMORYSPACEINTERFACES
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===- PointerDialect.h - 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 defines the Ptr dialect.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_PTR_IR_PTRDIALECT_H
14+
#define MLIR_DIALECT_PTR_IR_PTRDIALECT_H
15+
16+
#include "mlir/IR/Dialect.h"
17+
18+
#include "mlir/Dialect/Ptr/IR/PtrOpsDialect.h.inc"
19+
20+
#endif // MLIR_DIALECT_PTR_IR_PTRDIALECT_H
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//===- PointerDialect.td - Pointer dialect -----------------*- tablegen -*-===//
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+
#ifndef PTR_DIALECT
10+
#define PTR_DIALECT
11+
12+
include "mlir/Interfaces/DataLayoutInterfaces.td"
13+
include "mlir/IR/AttrTypeBase.td"
14+
include "mlir/IR/BuiltinTypeInterfaces.td"
15+
include "mlir/IR/OpBase.td"
16+
17+
//===----------------------------------------------------------------------===//
18+
// Pointer dialect definition.
19+
//===----------------------------------------------------------------------===//
20+
21+
def Ptr_Dialect : Dialect {
22+
let name = "ptr";
23+
let summary = "Pointer dialect";
24+
let cppNamespace = "::mlir::ptr";
25+
let useDefaultTypePrinterParser = 1;
26+
}
27+
28+
//===----------------------------------------------------------------------===//
29+
// Pointer type definitions
30+
//===----------------------------------------------------------------------===//
31+
32+
class Pointer_Type<string name, string typeMnemonic, list<Trait> traits = []>
33+
: TypeDef<Ptr_Dialect, name, traits> {
34+
let mnemonic = typeMnemonic;
35+
}
36+
37+
def PtrType : Pointer_Type<"Ptr", "ptr", [
38+
MemRefElementTypeInterface,
39+
DeclareTypeInterfaceMethods<DataLayoutTypeInterface, [
40+
"areCompatible", "verifyEntries"]>,
41+
DeclareTypeInterfaceMethods<SharedDialectTypeInterface, [
42+
"getSharedDialect"]>
43+
]> {
44+
let summary = "Pointer type";
45+
let description = [{
46+
The `ptr` type is an opaque pointer type. This type typically represents
47+
a reference to an object in memory. Pointers are optionally parameterized
48+
by a memory space.
49+
Syntax:
50+
51+
```mlir
52+
pointer ::= `ptr` (`<` memory-space `>`)?
53+
memory-space ::= attribute-value
54+
```
55+
}];
56+
let parameters = (ins OptionalParameter<"Attribute">:$memorySpace);
57+
let assemblyFormat = "(`<` $memorySpace^ `>`)?";
58+
let skipDefaultBuilders = 1;
59+
let builders = [
60+
TypeBuilder<(ins CArg<"Attribute", "nullptr">:$addressSpace), [{
61+
return $_get($_ctxt, addressSpace);
62+
}]>,
63+
TypeBuilder<(ins CArg<"unsigned">:$addressSpace), [{
64+
return $_get($_ctxt, IntegerAttr::get(IntegerType::get($_ctxt, 32),
65+
addressSpace));
66+
}]>
67+
];
68+
let extraClassDeclaration = [{
69+
/// Returns the default memory space.
70+
Attribute getDefaultMemorySpace() const;
71+
72+
/// Returns the memory space as an unsigned number.
73+
int64_t getAddressSpace() const;
74+
}];
75+
}
76+
77+
//===----------------------------------------------------------------------===//
78+
// Base address operation definition.
79+
//===----------------------------------------------------------------------===//
80+
81+
class Pointer_Op<string mnemonic, list<Trait> traits = []> :
82+
Op<Ptr_Dialect, mnemonic, traits>;
83+
84+
#endif // PTR_DIALECT

0 commit comments

Comments
 (0)