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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Aug 19 19:58:20 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Aman Singh (amanyagami)

<details>
<summary>Changes</summary>

`PtrType`'s `DataLayoutTypeInterface`/`DataLayoutInterface` methods
used `cast_if_present<MemorySpaceAttrInterface>` on the data layout's
default memory space attribute, which asserts if that attribute is
non-null but does not implement `MemorySpaceAttrInterface` (e.g. a
plain `IntegerAttr` set via `dlti.default_memory_space`). The callers
already handle a null `MemorySpaceAttrInterface` gracefully by
falling back to the generic default pointer layout, so this switches
to `dyn_cast_if_present` to take that path instead of asserting.

Verified: reverting this fix reproduces the reported crash; with the
fix, `mlir-opt --test-data-layout-query` on the reported reproducer
succeeds, and `mlir/test/Dialect/Ptr/layout.mlir` passes.

Fixes #<!-- -->217170

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---
Full diff: https://github.com/llvm/llvm-project/pull/217512.diff


2 Files Affected:

- (modified) mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp (+4-4) 
- (modified) mlir/test/Dialect/Ptr/layout.mlir (+22) 


``````````diff
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..ca8897f15dc02 100644
--- a/mlir/test/Dialect/Ptr/layout.mlir
+++ b/mlir/test/Dialect/Ptr/layout.mlir
@@ -147,3 +147,25 @@ module attributes { dlti.dl_spec = #dlti.dl_spec<
     return
   }
 }
+
+// -----
+
+// Regression test for https://github.com/llvm/llvm-project/issues/217170:
+// the default memory space in the data layout does not have to implement
+// `MemorySpaceAttrInterface` (e.g. it may be a plain integer attribute).
+// Querying a pointer type whose memory space differs from that default
+// memory space used to crash instead of falling back to the default
+// pointer spec.
+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
+  }
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/217512


More information about the Mlir-commits mailing list