Skip to content

Commit 2e3ab52

Browse files
committed
[mlir][Ptr] Init the Ptr dialect with the !ptr.ptr type.
This patch initializes the `ptr` dialect directories and some base files. It also add the `!ptr.ptr` type, together with the `DataLayoutTypeInterface` interface. The implementation of the `DataLayoutTypeInterface` interface clones the implementation from `LLVM::LLVMPointerType`.
1 parent 31480b0 commit 2e3ab52

15 files changed

+450
-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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
add_mlir_dialect(PtrOps ptr)
2+
add_mlir_doc(PtrOps PtrOps Dialects/ -gen-op-doc)
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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 = 0;
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
46+
a reference to an object in memory. Pointers are optionally parameterized
47+
by a memory space.
48+
Syntax:
49+
50+
```mlir
51+
pointer ::= `ptr` (`<` memory-space `>`)?
52+
memory-space ::= attribute-value
53+
```
54+
}];
55+
let parameters = (ins OptionalParameter<"Attribute">:$memorySpace);
56+
let assemblyFormat = "(`<` $memorySpace^ `>`)?";
57+
let builders = [
58+
TypeBuilder<(ins CArg<"Attribute", "nullptr">:$addressSpace), [{
59+
return $_get($_ctxt, addressSpace);
60+
}]>,
61+
TypeBuilder<(ins CArg<"unsigned">:$addressSpace), [{
62+
return $_get($_ctxt, IntegerAttr::get(IntegerType::get($_ctxt, 32),
63+
addressSpace));
64+
}]>
65+
];
66+
let skipDefaultBuilders = 1;
67+
let extraClassDeclaration = [{
68+
/// Returns the default memory space.
69+
Attribute getDefaultMemorySpace() const;
70+
71+
/// Returns the memory space as an unsigned number.
72+
int64_t getAddressSpace() const;
73+
}];
74+
}
75+
76+
//===----------------------------------------------------------------------===//
77+
// Base address operation definition.
78+
//===----------------------------------------------------------------------===//
79+
80+
class Pointer_Op<string mnemonic, list<Trait> traits = []> :
81+
Op<Ptr_Dialect, mnemonic, traits>;
82+
83+
#endif // PTR_DIALECT
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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/PtrDialect.h"
18+
#include "mlir/Dialect/Ptr/IR/PtrTypes.h"
19+
#include "mlir/IR/OpDefinition.h"
20+
21+
#define GET_OP_CLASSES
22+
#include "mlir/Dialect/Ptr/IR/PtrOps.h.inc"
23+
24+
#endif // MLIR_DIALECT_PTR_IR_PTROPS_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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/IR/OpAsmInterface.td"
14+
15+
#endif // PTR_OPS
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
namespace mlir {
20+
namespace ptr {
21+
/// The positions of different values in the data layout entry for pointers.
22+
enum class PtrDLEntryPos { Size = 0, Abi = 1, Preferred = 2, Index = 3 };
23+
24+
/// Returns the value that corresponds to named position `pos` from the
25+
/// data layout entry `attr` assuming it's a dense integer elements attribute.
26+
/// Returns `std::nullopt` if `pos` is not present in the entry.
27+
/// Currently only `PtrDLEntryPos::Index` is optional, and all other positions
28+
/// may be assumed to be present.
29+
std::optional<uint64_t> extractPointerSpecValue(Attribute attr,
30+
PtrDLEntryPos pos);
31+
} // namespace ptr
32+
} // namespace mlir
33+
34+
#define GET_TYPEDEF_CLASSES
35+
#include "mlir/Dialect/Ptr/IR/PtrOpsTypes.h.inc"
36+
37+
#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
@@ -62,6 +62,7 @@
6262
#include "mlir/Dialect/PDL/IR/PDL.h"
6363
#include "mlir/Dialect/PDLInterp/IR/PDLInterp.h"
6464
#include "mlir/Dialect/Polynomial/IR/PolynomialDialect.h"
65+
#include "mlir/Dialect/Ptr/IR/PtrDialect.h"
6566
#include "mlir/Dialect/Quant/QuantOps.h"
6667
#include "mlir/Dialect/SCF/IR/SCF.h"
6768
#include "mlir/Dialect/SCF/IR/ValueBoundsOpInterfaceImpl.h"
@@ -133,6 +134,7 @@ inline void registerAllDialects(DialectRegistry &registry) {
133134
pdl::PDLDialect,
134135
pdl_interp::PDLInterpDialect,
135136
polynomial::PolynomialDialect,
137+
ptr::PtrDialect,
136138
quant::QuantizationDialect,
137139
ROCDL::ROCDLDialect,
138140
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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_mlir_dialect_library(
2+
MLIRPtrDialect
3+
PtrTypes.cpp
4+
PtrDialect.cpp
5+
ADDITIONAL_HEADER_DIRS
6+
${PROJECT_SOURCE_DIR}/mlir/Dialect/Pointer
7+
DEPENDS
8+
MLIRPtrOpsIncGen
9+
LINK_LIBS
10+
PUBLIC
11+
MLIRIR
12+
MLIRDataLayoutInterfaces
13+
MLIRMemorySlotInterfaces
14+
)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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/PatternMatch.h"
16+
#include "mlir/Transforms/InliningUtils.h"
17+
#include "llvm/ADT/SmallString.h"
18+
#include "llvm/ADT/TypeSwitch.h"
19+
20+
using namespace mlir;
21+
using namespace mlir::ptr;
22+
23+
//===----------------------------------------------------------------------===//
24+
// Pointer dialect
25+
//===----------------------------------------------------------------------===//
26+
27+
void PtrDialect::initialize() {
28+
addOperations<
29+
#define GET_OP_LIST
30+
#include "mlir/Dialect/Ptr/IR/PtrOps.cpp.inc"
31+
>();
32+
addTypes<
33+
#define GET_TYPEDEF_LIST
34+
#include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc"
35+
>();
36+
}
37+
38+
//===----------------------------------------------------------------------===//
39+
// Pointer API.
40+
//===----------------------------------------------------------------------===//
41+
42+
#include "mlir/Dialect/Ptr/IR/PtrOpsDialect.cpp.inc"
43+
44+
#define GET_TYPEDEF_CLASSES
45+
#include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc"
46+
47+
#define GET_OP_CLASSES
48+
#include "mlir/Dialect/Ptr/IR/PtrOps.cpp.inc"

0 commit comments

Comments
 (0)