Skip to content

Commit

Permalink
[mlir][LLVM] Fix DataLayoutTypeInterface for opqaue pointers with n…
Browse files Browse the repository at this point in the history
…on-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
  • Loading branch information
zero9178 committed Apr 22, 2022
1 parent bab3d37 commit 8ed2bd1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
Expand Up @@ -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()));
}

Expand All @@ -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()));
}

Expand All @@ -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()));
}

Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Dialect/LLVMIR/layout.mlir
Expand Up @@ -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
}
}
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 8ed2bd1

Please sign in to comment.