[Mlir-commits] [mlir] 8ed2bd1 - [mlir][LLVM] Fix `DataLayoutTypeInterface` for opqaue pointers with non-default address space

Markus Böck llvmlistbot at llvm.org
Fri Apr 22 15:12:37 PDT 2022


Author: Markus Böck
Date: 2022-04-23T00:10:31+02:00
New Revision: 8ed2bd1e746567ab82e138896db340a6a6781511

URL: https://github.com/llvm/llvm-project/commit/8ed2bd1e746567ab82e138896db340a6a6781511
DIFF: https://github.com/llvm/llvm-project/commit/8ed2bd1e746567ab82e138896db340a6a6781511.diff

LOG: [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

Added: 
    

Modified: 
    mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
    mlir/test/Dialect/LLVMIR/layout.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
index 101e255f99efd..3cdcb3285e9ed 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMTypes.cpp
@@ -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()));
 }
 

diff  --git a/mlir/test/Dialect/LLVMIR/layout.mlir b/mlir/test/Dialect/LLVMIR/layout.mlir
index df1e0ea7940ff..12b6c61fc0664 100644
--- a/mlir/test/Dialect/LLVMIR/layout.mlir
+++ b/mlir/test/Dialect/LLVMIR/layout.mlir
@@ -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
   }
 }


        


More information about the Mlir-commits mailing list