Skip to content

Commit 8ed2bd1

Browse files
committed
[mlir][LLVM] Fix DataLayoutTypeInterface for opqaue pointers with non-default address space
As a fallback mechanism, if no entry was supplied for a given address space, the size or alignment for a pointer type with the default address space is returned instead. This code currently crashes with opaque pointers, as it tries to construct a typed pointer type from the opaque pointer type, leading to a null pointer dereference when fetching the element type. This patch fixes the issue by handling the opaque pointer cases explicitly. Differential Revision: https://reviews.llvm.org/D124290
1 parent bab3d37 commit 8ed2bd1

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ LLVMPointerType::getTypeSizeInBits(const DataLayout &dataLayout,
261261

262262
// For other memory spaces, use the size of the pointer to the default memory
263263
// space.
264+
if (isOpaque())
265+
return dataLayout.getTypeSizeInBits(get(getContext()));
264266
return dataLayout.getTypeSizeInBits(get(getElementType()));
265267
}
266268

@@ -270,6 +272,8 @@ unsigned LLVMPointerType::getABIAlignment(const DataLayout &dataLayout,
270272
getPointerDataLayoutEntry(params, *this, DLEntryPos::Abi))
271273
return *alignment;
272274

275+
if (isOpaque())
276+
return dataLayout.getTypeABIAlignment(get(getContext()));
273277
return dataLayout.getTypeABIAlignment(get(getElementType()));
274278
}
275279

@@ -280,6 +284,8 @@ LLVMPointerType::getPreferredAlignment(const DataLayout &dataLayout,
280284
getPointerDataLayoutEntry(params, *this, DLEntryPos::Preferred))
281285
return *alignment;
282286

287+
if (isOpaque())
288+
return dataLayout.getTypePreferredAlignment(get(getContext()));
283289
return dataLayout.getTypePreferredAlignment(get(getElementType()));
284290
}
285291

mlir/test/Dialect/LLVMIR/layout.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ module {
3333
// CHECK: preferred = 8
3434
// CHECK: size = 8
3535
"test.data_layout_query"() : () -> !llvm.ptr<i8, 5>
36+
// CHECK: alignment = 8
37+
// CHECK: bitsize = 64
38+
// CHECK: preferred = 8
39+
// CHECK: size = 8
40+
"test.data_layout_query"() : () -> !llvm.ptr<5>
3641
return
3742
}
3843
}
@@ -75,6 +80,11 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
7580
// CHECK: preferred = 8
7681
// CHECK: size = 8
7782
"test.data_layout_query"() : () -> !llvm.ptr<i8, 5>
83+
// CHECK: alignment = 4
84+
// CHECK: bitsize = 32
85+
// CHECK: preferred = 8
86+
// CHECK: size = 4
87+
"test.data_layout_query"() : () -> !llvm.ptr<3>
7888
return
7989
}
8090
}

0 commit comments

Comments
 (0)