[Mlir-commits] [mlir] [mlir][Ptr] Don't assert when the default memory space isn't a MemorySpaceAttrInterface (PR #217512)
Aman Singh
llvmlistbot at llvm.org
Wed Aug 19 19:57:35 PDT 2026
https://github.com/amanyagami created https://github.com/llvm/llvm-project/pull/217512
`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)
>From 0867ed04baf6e6bd12e4097ce7b2788d457c475b Mon Sep 17 00:00:00 2001
From: amanyagami <2amansingh2 at gmail.com>
Date: Wed, 19 Aug 2026 19:45:29 -0700
Subject: [PATCH] [mlir][Ptr] Don't assert when the default memory space isn't
a MemorySpaceAttrInterface
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 switch to
dyn_cast_if_present to take that path instead of asserting.
Fixes https://github.com/llvm/llvm-project/issues/217170
---
mlir/lib/Dialect/Ptr/IR/PtrTypes.cpp | 8 ++++----
mlir/test/Dialect/Ptr/layout.mlir | 22 ++++++++++++++++++++++
2 files changed, 26 insertions(+), 4 deletions(-)
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
+ }
+}
More information about the Mlir-commits
mailing list