[Mlir-commits] [mlir] 87b1a2f - [mlir][Ptr] Don't assert when the default memory space isn't a MemorySpaceAttrInterface (#217512)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Aug 29 06:44:45 PDT 2026


Author: Aman Singh
Date: 2026-08-29T09:44:41-04:00
New Revision: 87b1a2f7246bc0a4ed5335c45635bddb75847890

URL: https://github.com/llvm/llvm-project/commit/87b1a2f7246bc0a4ed5335c45635bddb75847890
DIFF: https://github.com/llvm/llvm-project/commit/87b1a2f7246bc0a4ed5335c45635bddb75847890.diff

LOG: [mlir][Ptr] Don't assert when the default memory space isn't a MemorySpaceAttrInterface (#217512)

Added: 
    

Modified: 
    mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp
    mlir/test/Dialect/Ptr/layout.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp b/mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp
index 689de15a0415e..5d09c0149e77b 100644
--- a/mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp
+++ b/mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp
@@ -94,7 +94,7 @@ bool PtrType::areCompatible(DataLayoutEntryListRef oldLayout,
 
 uint64_t PtrType::getABIAlignment(const DataLayout &dataLayout,
                                   DataLayoutEntryListRef params) const {
-  auto defaultMemorySpace = llvm::cast_if_present<MemorySpaceAttrInterface>(
+  auto defaultMemorySpace = llvm::dyn_cast_if_present<MemorySpaceAttrInterface>(
       dataLayout.getDefaultMemorySpace());
   if (SpecAttr spec = getPointerSpec(params, *this, defaultMemorySpace))
     return spec.getAbi() / kBitsInByte;
@@ -105,7 +105,7 @@ uint64_t PtrType::getABIAlignment(const DataLayout &dataLayout,
 std::optional<uint64_t>
 PtrType::getIndexBitwidth(const DataLayout &dataLayout,
                           DataLayoutEntryListRef params) const {
-  auto defaultMemorySpace = llvm::cast_if_present<MemorySpaceAttrInterface>(
+  auto defaultMemorySpace = llvm::dyn_cast_if_present<MemorySpaceAttrInterface>(
       dataLayout.getDefaultMemorySpace());
   if (SpecAttr spec = getPointerSpec(params, *this, defaultMemorySpace)) {
     return spec.getIndex() == SpecAttr::kOptionalSpecValue ? spec.getSize()
@@ -117,7 +117,7 @@ PtrType::getIndexBitwidth(const DataLayout &dataLayout,
 
 llvm::TypeSize PtrType::getTypeSizeInBits(const DataLayout &dataLayout,
                                           DataLayoutEntryListRef params) const {
-  auto defaultMemorySpace = llvm::cast_if_present<MemorySpaceAttrInterface>(
+  auto defaultMemorySpace = llvm::dyn_cast_if_present<MemorySpaceAttrInterface>(
       dataLayout.getDefaultMemorySpace());
   if (SpecAttr spec = getPointerSpec(params, *this, defaultMemorySpace))
     return llvm::TypeSize::getFixed(spec.getSize());
@@ -129,7 +129,7 @@ llvm::TypeSize PtrType::getTypeSizeInBits(const DataLayout &dataLayout,
 
 uint64_t PtrType::getPreferredAlignment(const DataLayout &dataLayout,
                                         DataLayoutEntryListRef params) const {
-  auto defaultMemorySpace = llvm::cast_if_present<MemorySpaceAttrInterface>(
+  auto defaultMemorySpace = llvm::dyn_cast_if_present<MemorySpaceAttrInterface>(
       dataLayout.getDefaultMemorySpace());
   if (SpecAttr spec = getPointerSpec(params, *this, defaultMemorySpace))
     return spec.getPreferred() / kBitsInByte;

diff  --git a/mlir/test/Dialect/Ptr/layout.mlir b/mlir/test/Dialect/Ptr/layout.mlir
index f904e729fcbe3..c58b7704c0bf8 100644
--- a/mlir/test/Dialect/Ptr/layout.mlir
+++ b/mlir/test/Dialect/Ptr/layout.mlir
@@ -147,3 +147,22 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
     return
   }
 }
+
+// -----
+
+// Regression test for https://github.com/llvm/llvm-project/issues/217170:
+// querying the ptr layout caused a crash when the default memory space was not
+// a `MemorySpaceAttrInterface` (e.g. a plain `i32` attr).
+module attributes { dlti.dl_spec = #dlti.dl_spec<"dlti.default_memory_space" = 7 : ui64>} {
+  // CHECK-LABEL: @non_memory_space_default
+  func.func @non_memory_space_default() {
+    // CHECK: alignment = 1
+    // CHECK: bitsize = 64
+    // CHECK: default_memory_space = 7 : ui64
+    // CHECK: index = 64
+    // CHECK: preferred = 1
+    // CHECK: size = 8
+    "test.data_layout_query"() : () -> !ptr.ptr<#test.const_memory_space<4>>
+    return
+  }
+}


        


More information about the Mlir-commits mailing list