[Mlir-commits] [mlir] [MLIR][MemRefToLLVM] Fix crash in MemorySpaceCastOpLowering when type conversion fails (PR #186404)
Mehdi Amini
llvmlistbot at llvm.org
Fri Mar 13 07:20:42 PDT 2026
https://github.com/joker-eph created https://github.com/llvm/llvm-project/pull/186404
`MemorySpaceCastOpLowering::matchAndRewrite` called `cast<LLVMStructType>` on the result of `typeConverter->convertType(resultType)` without checking whether the conversion succeeded. When the destination memref has a non-integer address space (e.g. `#gpu.address_space<workgroup>`), `convertType` returns a null `Type`, and the subsequent `cast<>` dereferences a null pointer, causing a crash.
Fix by checking the result of `convertType` and returning `notifyMatchFailure` on failure, allowing the conversion framework to produce a proper diagnostic instead of crashing.
Fixes #186041
Assisted-by: Claude Code
>From 52f59965435b6cba2e8d4a67e6ca18d48d5d255a Mon Sep 17 00:00:00 2001
From: Mehdi Amini <joker.eph at gmail.com>
Date: Thu, 12 Mar 2026 16:24:27 -0700
Subject: [PATCH] [MLIR][MemRefToLLVM] Fix crash in MemorySpaceCastOpLowering
when type conversion fails
`MemorySpaceCastOpLowering::matchAndRewrite` called `cast<LLVMStructType>` on
the result of `typeConverter->convertType(resultType)` without checking whether
the conversion succeeded. When the destination memref has a non-integer address
space (e.g. `#gpu.address_space<workgroup>`), `convertType` returns a null
`Type`, and the subsequent `cast<>` dereferences a null pointer, causing a
crash.
Fix by checking the result of `convertType` and returning `notifyMatchFailure`
on failure, allowing the conversion framework to produce a proper diagnostic
instead of crashing.
Fixes #186041
Assisted-by: Claude Code
---
mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp | 6 ++++--
.../memory-space-cast-non-integer-addrspace.mlir | 12 ++++++++++++
2 files changed, 16 insertions(+), 2 deletions(-)
create mode 100644 mlir/test/Conversion/MemRefToLLVM/memory-space-cast-non-integer-addrspace.mlir
diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index 91a0c4b55fa84..b7645b96816a6 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -1255,8 +1255,10 @@ struct MemorySpaceCastOpLowering
Type resultType = op.getDest().getType();
if (auto resultTypeR = dyn_cast<MemRefType>(resultType)) {
- auto resultDescType =
- cast<LLVM::LLVMStructType>(typeConverter->convertType(resultTypeR));
+ Type convertedType = typeConverter->convertType(resultTypeR);
+ if (!convertedType)
+ return rewriter.notifyMatchFailure(op, "memref type conversion failed");
+ auto resultDescType = cast<LLVM::LLVMStructType>(convertedType);
Type newPtrType = resultDescType.getBody()[0];
SmallVector<Value> descVals;
diff --git a/mlir/test/Conversion/MemRefToLLVM/memory-space-cast-non-integer-addrspace.mlir b/mlir/test/Conversion/MemRefToLLVM/memory-space-cast-non-integer-addrspace.mlir
new file mode 100644
index 0000000000000..c01ac4e53e530
--- /dev/null
+++ b/mlir/test/Conversion/MemRefToLLVM/memory-space-cast-non-integer-addrspace.mlir
@@ -0,0 +1,12 @@
+// RUN: mlir-opt %s -finalize-memref-to-llvm | FileCheck %s
+// Regression test for https://github.com/llvm/llvm-project/issues/186041.
+// memref.memory_space_cast to a non-integer address space should not crash when
+// the MemRefToLLVM type conversion fails for the result type. The op is left
+// unconverted (partial conversion succeeds, exit 0).
+
+// CHECK-LABEL: @memory_space_cast_non_integer_addrspace
+// CHECK: memref.memory_space_cast
+func.func @memory_space_cast_non_integer_addrspace(%arg0: memref<128xi32, 3>) {
+ %cast = memref.memory_space_cast %arg0 : memref<128xi32, 3> to memref<128xi32, #gpu.address_space<workgroup>>
+ return
+}
More information about the Mlir-commits
mailing list