[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
This commit is contained in:
Markus Böck 2022-04-23 00:10:02 +02:00
parent bab3d3778d
commit 8ed2bd1e74
2 changed files with 16 additions and 0 deletions

View File

@ -261,6 +261,8 @@ LLVMPointerType::getTypeSizeInBits(const DataLayout &dataLayout,
// For other memory spaces, use the size of the pointer to the default memory
// space.
if (isOpaque())
return dataLayout.getTypeSizeInBits(get(getContext()));
return dataLayout.getTypeSizeInBits(get(getElementType()));
}
@ -270,6 +272,8 @@ unsigned LLVMPointerType::getABIAlignment(const DataLayout &dataLayout,
getPointerDataLayoutEntry(params, *this, DLEntryPos::Abi))
return *alignment;
if (isOpaque())
return dataLayout.getTypeABIAlignment(get(getContext()));
return dataLayout.getTypeABIAlignment(get(getElementType()));
}
@ -280,6 +284,8 @@ LLVMPointerType::getPreferredAlignment(const DataLayout &dataLayout,
getPointerDataLayoutEntry(params, *this, DLEntryPos::Preferred))
return *alignment;
if (isOpaque())
return dataLayout.getTypePreferredAlignment(get(getContext()));
return dataLayout.getTypePreferredAlignment(get(getElementType()));
}

View File

@ -33,6 +33,11 @@ module {
// CHECK: preferred = 8
// CHECK: size = 8
"test.data_layout_query"() : () -> !llvm.ptr<i8, 5>
// CHECK: alignment = 8
// CHECK: bitsize = 64
// CHECK: preferred = 8
// CHECK: size = 8
"test.data_layout_query"() : () -> !llvm.ptr<5>
return
}
}
@ -75,6 +80,11 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
// CHECK: preferred = 8
// CHECK: size = 8
"test.data_layout_query"() : () -> !llvm.ptr<i8, 5>
// CHECK: alignment = 4
// CHECK: bitsize = 32
// CHECK: preferred = 8
// CHECK: size = 4
"test.data_layout_query"() : () -> !llvm.ptr<3>
return
}
}