Skip to content

Commit f474676

Browse files
fabianmcgAlexisPerry
authored andcommitted
[mlir][Ptr] Init the Ptr dialect with the !ptr.ptr type. (llvm#86860)
This patch initializes the `ptr` dialect directories and base files, adding the `!ptr.ptr` type and the `#ptr.spec<...>` data layout spec attribute. The `!ptr.ptr` type is an opaque pointer type optionally parameterized by a memory space. This type typically represents a handle to an object in memory or target-dependent values like `nullptr`. The implementation of the `DataLayoutTypeInterface` interface for `!ptr.ptr` was adapted from `!llvm.ptr`'s implementation. This implementation uses the `#ptr.spec<...>` attribute for defining the data layout specification. See [[RFC] `ptr` dialect & modularizing ptr ops in the LLVM dialect](https://discourse.llvm.org/t/rfc-ptr-dialect-modularizing-ptr-ops-in-the-llvm-dialect/75142) for rationale and roadmap.
1 parent 3e8703d commit f474676

19 files changed

+646
-0
lines changed

mlir/include/mlir/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_subdirectory(OpenMP)
2929
add_subdirectory(PDL)
3030
add_subdirectory(PDLInterp)
3131
add_subdirectory(Polynomial)
32+
add_subdirectory(Ptr)
3233
add_subdirectory(Quant)
3334
add_subdirectory(SCF)
3435
add_subdirectory(Shape)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(IR)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
add_mlir_dialect(PtrOps ptr)
2+
add_mlir_doc(PtrOps PtrOps Dialects/ -gen-op-doc)
3+
4+
set(LLVM_TARGET_DEFINITIONS PtrOps.td)
5+
mlir_tablegen(PtrOpsAttrs.h.inc -gen-attrdef-decls -attrdefs-dialect=ptr)
6+
mlir_tablegen(PtrOpsAttrs.cpp.inc -gen-attrdef-defs -attrdefs-dialect=ptr)
7+
add_public_tablegen_target(MLIRPtrOpsAttributesIncGen)
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===-- PtrAttrDefs.td - Ptr Attributes definition file ----*- 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_ATTRDEFS
10+
#define PTR_ATTRDEFS
11+
12+
include "mlir/Dialect/Ptr/IR/PtrDialect.td"
13+
include "mlir/IR/AttrTypeBase.td"
14+
15+
// All of the attributes will extend this class.
16+
class Ptr_Attr<string name, string attrMnemonic,
17+
list<Trait> traits = [],
18+
string baseCppClass = "::mlir::Attribute">
19+
: AttrDef<Ptr_Dialect, name, traits, baseCppClass> {
20+
let mnemonic = attrMnemonic;
21+
}
22+
23+
//===----------------------------------------------------------------------===//
24+
// SpecAttr
25+
//===----------------------------------------------------------------------===//
26+
27+
def Ptr_SpecAttr : Ptr_Attr<"Spec", "spec"> {
28+
let summary = "ptr data layout spec";
29+
let description = [{
30+
Defines the data layout spec for a pointer type. This attribute has 4
31+
fields:
32+
- [Required] size: size of the pointer in bits.
33+
- [Required] abi: ABI-required alignment for the pointer in bits.
34+
- [Required] preferred: preferred alignment for the pointer in bits.
35+
- [Optional] index: bitwidth that should be used when performing index
36+
computations for the type. Setting the field to `kOptionalSpecValue`, means
37+
the field is optional.
38+
39+
Furthermore, the attribute will verify that all present values are divisible
40+
by 8 (number of bits in a byte), and that `preferred` > `abi`.
41+
42+
Example:
43+
```mlir
44+
// Spec for a 64 bit ptr, with a required alignment of 64 bits, but with
45+
// a preferred alignment of 128 bits and an index bitwidth of 64 bits.
46+
#ptr.spec<size = 64, abi = 64, preferred = 128, index = 64>
47+
```
48+
}];
49+
let parameters = (ins
50+
"uint32_t":$size,
51+
"uint32_t":$abi,
52+
"uint32_t":$preferred,
53+
DefaultValuedParameter<"uint32_t", "kOptionalSpecValue">:$index
54+
);
55+
let skipDefaultBuilders = 1;
56+
let builders = [
57+
AttrBuilder<(ins "uint32_t":$size, "uint32_t":$abi, "uint32_t":$preferred,
58+
CArg<"uint32_t", "kOptionalSpecValue">:$index), [{
59+
return $_get($_ctxt, size, abi, preferred, index);
60+
}]>
61+
];
62+
let assemblyFormat = "`<` struct(params) `>`";
63+
let extraClassDeclaration = [{
64+
/// Constant for specifying a spec entry is optional.
65+
static constexpr uint32_t kOptionalSpecValue = std::numeric_limits<uint32_t>::max();
66+
}];
67+
let genVerifyDecl = 1;
68+
}
69+
70+
#endif // PTR_ATTRDEFS
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//===- PtrAttrs.h - Pointer dialect attributes ------------------*- 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 declares the Ptr dialect attributes.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_PTR_IR_PTRATTRS_H
14+
#define MLIR_DIALECT_PTR_IR_PTRATTRS_H
15+
16+
#include "mlir/IR/OpImplementation.h"
17+
18+
#define GET_ATTRDEF_CLASSES
19+
#include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.h.inc"
20+
21+
#endif // MLIR_DIALECT_PTR_IR_PTRATTRS_H
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===- PtrDialect.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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===- PtrDialect.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+
let useDefaultAttributePrinterParser = 1;
27+
}
28+
29+
//===----------------------------------------------------------------------===//
30+
// Pointer type definitions
31+
//===----------------------------------------------------------------------===//
32+
33+
class Ptr_Type<string name, string typeMnemonic, list<Trait> traits = []>
34+
: TypeDef<Ptr_Dialect, name, traits> {
35+
let mnemonic = typeMnemonic;
36+
}
37+
38+
def Ptr_PtrType : Ptr_Type<"Ptr", "ptr", [
39+
MemRefElementTypeInterface,
40+
DeclareTypeInterfaceMethods<DataLayoutTypeInterface, [
41+
"areCompatible", "getIndexBitwidth", "verifyEntries"]>
42+
]> {
43+
let summary = "pointer type";
44+
let description = [{
45+
The `ptr` type is an opaque pointer type. This type typically represents a
46+
handle to an object in memory or target-dependent values like `nullptr`.
47+
Pointers are optionally parameterized by a memory space.
48+
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 builders = [
59+
TypeBuilder<(ins CArg<"Attribute", "nullptr">:$memorySpace), [{
60+
return $_get($_ctxt, memorySpace);
61+
}]>
62+
];
63+
let skipDefaultBuilders = 1;
64+
}
65+
66+
//===----------------------------------------------------------------------===//
67+
// Base address operation definition.
68+
//===----------------------------------------------------------------------===//
69+
70+
class Pointer_Op<string mnemonic, list<Trait> traits = []> :
71+
Op<Ptr_Dialect, mnemonic, traits>;
72+
73+
#endif // PTR_DIALECT
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===- PtrDialect.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_PTROPS_H
14+
#define MLIR_DIALECT_PTR_IR_PTROPS_H
15+
16+
#include "mlir/Bytecode/BytecodeOpInterface.h"
17+
#include "mlir/Dialect/Ptr/IR/PtrAttrs.h"
18+
#include "mlir/Dialect/Ptr/IR/PtrDialect.h"
19+
#include "mlir/Dialect/Ptr/IR/PtrTypes.h"
20+
#include "mlir/IR/OpDefinition.h"
21+
22+
#define GET_OP_CLASSES
23+
#include "mlir/Dialect/Ptr/IR/PtrOps.h.inc"
24+
25+
#endif // MLIR_DIALECT_PTR_IR_PTROPS_H
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//===- PtrOps.td - Pointer dialect ops ---------------------*- tablegen -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://ptr.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef PTR_OPS
10+
#define PTR_OPS
11+
12+
include "mlir/Dialect/Ptr/IR/PtrDialect.td"
13+
include "mlir/Dialect/Ptr/IR/PtrAttrDefs.td"
14+
include "mlir/IR/OpAsmInterface.td"
15+
16+
#endif // PTR_OPS
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//===- PtrTypes.h - Pointer types -------------------------------*- 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 Pointer dialect types.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_PTR_IR_PTRTYPES_H
14+
#define MLIR_DIALECT_PTR_IR_PTRTYPES_H
15+
16+
#include "mlir/IR/Types.h"
17+
#include "mlir/Interfaces/DataLayoutInterfaces.h"
18+
19+
#define GET_TYPEDEF_CLASSES
20+
#include "mlir/Dialect/Ptr/IR/PtrOpsTypes.h.inc"
21+
22+
#endif // MLIR_DIALECT_PTR_IR_PTRTYPES_H

mlir/include/mlir/InitAllDialects.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "mlir/Dialect/PDL/IR/PDL.h"
6464
#include "mlir/Dialect/PDLInterp/IR/PDLInterp.h"
6565
#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h"
66+
#include "mlir/Dialect/Ptr/IR/PtrDialect.h"
6667
#include "mlir/Dialect/Quant/QuantOps.h"
6768
#include "mlir/Dialect/SCF/IR/SCF.h"
6869
#include "mlir/Dialect/SCF/IR/ValueBoundsOpInterfaceImpl.h"
@@ -134,6 +135,7 @@ inline void registerAllDialects(DialectRegistry &registry) {
134135
pdl::PDLDialect,
135136
pdl_interp::PDLInterpDialect,
136137
polynomial::PolynomialDialect,
138+
ptr::PtrDialect,
137139
quant::QuantizationDialect,
138140
ROCDL::ROCDLDialect,
139141
scf::SCFDialect,

mlir/lib/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_subdirectory(OpenMP)
2929
add_subdirectory(PDL)
3030
add_subdirectory(PDLInterp)
3131
add_subdirectory(Polynomial)
32+
add_subdirectory(Ptr)
3233
add_subdirectory(Quant)
3334
add_subdirectory(SCF)
3435
add_subdirectory(Shape)

mlir/lib/Dialect/Ptr/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(IR)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
add_mlir_dialect_library(
2+
MLIRPtrDialect
3+
PtrAttrs.cpp
4+
PtrTypes.cpp
5+
PtrDialect.cpp
6+
7+
DEPENDS
8+
MLIRPtrOpsAttributesIncGen
9+
MLIRPtrOpsIncGen
10+
11+
LINK_LIBS
12+
PUBLIC
13+
MLIRIR
14+
MLIRDataLayoutInterfaces
15+
MLIRMemorySlotInterfaces
16+
)

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===- PtrAttrs.cpp - Pointer dialect attributes ----------------*- 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 attributes.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "mlir/Dialect/Ptr/IR/PtrAttrs.h"
14+
#include "llvm/ADT/TypeSwitch.h"
15+
16+
using namespace mlir;
17+
using namespace mlir::ptr;
18+
19+
constexpr const static unsigned kBitsInByte = 8;
20+
21+
//===----------------------------------------------------------------------===//
22+
// SpecAttr
23+
//===----------------------------------------------------------------------===//
24+
25+
LogicalResult SpecAttr::verify(function_ref<InFlightDiagnostic()> emitError,
26+
uint32_t size, uint32_t abi, uint32_t preferred,
27+
uint32_t index) {
28+
if (size % kBitsInByte != 0)
29+
return emitError() << "size entry must be divisible by 8";
30+
if (abi % kBitsInByte != 0)
31+
return emitError() << "abi entry must be divisible by 8";
32+
if (preferred % kBitsInByte != 0)
33+
return emitError() << "preferred entry must be divisible by 8";
34+
if (index != kOptionalSpecValue && index % kBitsInByte != 0)
35+
return emitError() << "index entry must be divisible by 8";
36+
if (abi > preferred)
37+
return emitError() << "preferred alignment is expected to be at least "
38+
"as large as ABI alignment";
39+
return success();
40+
}

0 commit comments

Comments
 (0)