Skip to content

Commit c57fb25

Browse files
committed
add layout test, address reviewer comments and fix a bug in layout methods
1 parent fb9267a commit c57fb25

File tree

3 files changed

+99
-14
lines changed

3 files changed

+99
-14
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ def Ptr_PtrType : Ptr_Type<"Ptr", "ptr", [
5555
let parameters = (ins OptionalParameter<"Attribute">:$memorySpace);
5656
let assemblyFormat = "(`<` $memorySpace^ `>`)?";
5757
let builders = [
58-
TypeBuilder<(ins CArg<"Attribute", "nullptr">:$addressSpace), [{
59-
return $_get($_ctxt, addressSpace);
58+
TypeBuilder<(ins CArg<"Attribute", "nullptr">:$memorySpace), [{
59+
return $_get($_ctxt, memorySpace);
6060
}]>,
6161
TypeBuilder<(ins CArg<"unsigned">:$addressSpace), [{
6262
return $_get($_ctxt, IntegerAttr::get(IntegerType::get($_ctxt, 32),
@@ -69,7 +69,7 @@ def Ptr_PtrType : Ptr_Type<"Ptr", "ptr", [
6969
Attribute getDefaultMemorySpace() const;
7070

7171
/// Returns the memory space as an unsigned number.
72-
int64_t getAddressSpace() const;
72+
uint64_t getAddressSpace() const;
7373
}];
7474
}
7575

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,32 @@ getPointerDataLayoutEntry(DataLayoutEntryListRef params, PtrType type,
3636
for (DataLayoutEntryInterface entry : params) {
3737
if (!entry.isTypeEntry())
3838
continue;
39-
if (cast<PtrType>(entry.getKey().get<Type>()).getAddressSpace() ==
40-
type.getAddressSpace()) {
39+
if (cast<PtrType>(entry.getKey().get<Type>()).getMemorySpace() ==
40+
type.getMemorySpace()) {
4141
currentEntry = entry.getValue();
4242
break;
4343
}
4444
}
45+
bool isSizeOrIndex =
46+
pos == PtrDLEntryPos::Size || pos == PtrDLEntryPos::Index;
4547
if (currentEntry) {
4648
std::optional<uint64_t> value = extractPointerSpecValue(currentEntry, pos);
4749
// If the optional `PtrDLEntryPos::Index` entry is not available, use the
4850
// pointer size as the index bitwidth.
4951
if (!value && pos == PtrDLEntryPos::Index)
5052
value = extractPointerSpecValue(currentEntry, PtrDLEntryPos::Size);
51-
bool isSizeOrIndex =
52-
pos == PtrDLEntryPos::Size || pos == PtrDLEntryPos::Index;
5353
return *value / (isSizeOrIndex ? 1 : kBitsInByte);
5454
}
5555

5656
// If not found, and this is the pointer to the default memory space, assume
5757
// 64-bit pointers.
58-
if (type.getAddressSpace() == 0) {
59-
bool isSizeOrIndex =
60-
pos == PtrDLEntryPos::Size || pos == PtrDLEntryPos::Index;
58+
if (type.getMemorySpace() == type.getDefaultMemorySpace())
6159
return isSizeOrIndex ? kDefaultPointerSizeBits : kDefaultPointerAlignment;
62-
}
6360

6461
return std::nullopt;
6562
}
6663

67-
int64_t PtrType::getAddressSpace() const { return 0; }
64+
uint64_t PtrType::getAddressSpace() const { return 0; }
6865

6966
Attribute PtrType::getDefaultMemorySpace() const { return nullptr; }
7067

@@ -85,9 +82,10 @@ bool PtrType::areCompatible(DataLayoutEntryListRef oldLayout,
8582
return false;
8683
});
8784
if (it == oldLayout.end()) {
88-
llvm::find_if(oldLayout, [&](DataLayoutEntryInterface entry) {
85+
it = llvm::find_if(oldLayout, [&](DataLayoutEntryInterface entry) {
8986
if (auto type = llvm::dyn_cast_if_present<Type>(entry.getKey())) {
90-
return llvm::cast<PtrType>(type).getAddressSpace() == 0;
87+
auto ptrTy = llvm::cast<PtrType>(type);
88+
return ptrTy.getMemorySpace() == ptrTy.getDefaultMemorySpace();
9189
}
9290
return false;
9391
});

mlir/test/Dialect/Ptr/layout.mlir

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// RUN: mlir-opt --test-data-layout-query --split-input-file --verify-diagnostics %s | FileCheck %s
2+
3+
module attributes { dlti.dl_spec = #dlti.dl_spec<
4+
#dlti.dl_entry<!ptr.ptr, dense<[32, 32, 64]> : vector<3xi64>>,
5+
#dlti.dl_entry<!ptr.ptr<5>, dense<[64, 64, 64]> : vector<3xi64>>,
6+
#dlti.dl_entry<!ptr.ptr<4>, dense<[32, 64, 64, 24]> : vector<4xi64>>,
7+
#dlti.dl_entry<"dlti.alloca_memory_space", 5 : ui64>,
8+
#dlti.dl_entry<"dlti.global_memory_space", 2 : ui64>,
9+
#dlti.dl_entry<"dlti.program_memory_space", 3 : ui64>,
10+
#dlti.dl_entry<"dlti.stack_alignment", 128 : i64>
11+
>} {
12+
// CHECK: @spec
13+
func.func @spec() {
14+
// CHECK: alignment = 4
15+
// CHECK: alloca_memory_space = 5
16+
// CHECK: bitsize = 32
17+
// CHECK: global_memory_space = 2
18+
// CHECK: index = 32
19+
// CHECK: preferred = 8
20+
// CHECK: program_memory_space = 3
21+
// CHECK: size = 4
22+
// CHECK: stack_alignment = 128
23+
"test.data_layout_query"() : () -> !ptr.ptr
24+
// CHECK: alignment = 4
25+
// CHECK: alloca_memory_space = 5
26+
// CHECK: bitsize = 32
27+
// CHECK: global_memory_space = 2
28+
// CHECK: index = 32
29+
// CHECK: preferred = 8
30+
// CHECK: program_memory_space = 3
31+
// CHECK: size = 4
32+
// CHECK: stack_alignment = 128
33+
"test.data_layout_query"() : () -> !ptr.ptr<3>
34+
// CHECK: alignment = 8
35+
// CHECK: alloca_memory_space = 5
36+
// CHECK: bitsize = 64
37+
// CHECK: global_memory_space = 2
38+
// CHECK: index = 64
39+
// CHECK: preferred = 8
40+
// CHECK: program_memory_space = 3
41+
// CHECK: size = 8
42+
// CHECK: stack_alignment = 128
43+
"test.data_layout_query"() : () -> !ptr.ptr<5>
44+
// CHECK: alignment = 8
45+
// CHECK: alloca_memory_space = 5
46+
// CHECK: bitsize = 32
47+
// CHECK: global_memory_space = 2
48+
// CHECK: index = 24
49+
// CHECK: preferred = 8
50+
// CHECK: program_memory_space = 3
51+
// CHECK: size = 4
52+
// CHECK: stack_alignment = 128
53+
"test.data_layout_query"() : () -> !ptr.ptr<4>
54+
return
55+
}
56+
}
57+
58+
// -----
59+
60+
// expected-error@below {{expected layout attribute for '!ptr.ptr' to be a dense integer elements attribute with 3 or 4 elements}}
61+
module attributes { dlti.dl_spec = #dlti.dl_spec<
62+
#dlti.dl_entry<!ptr.ptr, dense<[64.0, 64.0, 64.0]> : vector<3xf32>>
63+
>} {
64+
func.func @pointer() {
65+
return
66+
}
67+
}
68+
69+
// -----
70+
71+
// expected-error@below {{preferred alignment is expected to be at least as large as ABI alignment}}
72+
module attributes { dlti.dl_spec = #dlti.dl_spec<
73+
#dlti.dl_entry<!ptr.ptr, dense<[64, 64, 32]> : vector<3xi64>>
74+
>} {
75+
func.func @pointer() {
76+
return
77+
}
78+
}
79+
80+
// -----
81+
82+
// expected-error @below {{expected i64 parameters for '!ptr.ptr'}}
83+
module attributes { dlti.dl_spec = #dlti.dl_spec<
84+
#dlti.dl_entry<!ptr.ptr, dense<[32, 32, 64]> : vector<3xi32>>
85+
>} {
86+
}
87+

0 commit comments

Comments
 (0)