[Mlir-commits] [mlir] [MLIR][MemRefToLLVM] Fix crash in MemorySpaceCastOpLowering when type conversion fails (PR #186404)

Mehdi Amini llvmlistbot at llvm.org
Fri Mar 13 08:34:08 PDT 2026


https://github.com/joker-eph updated https://github.com/llvm/llvm-project/pull/186404

>From 080e69aa1c12a907dd59cd0f7bd56b490c481f7c 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
---
 .../lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp | 16 +++++++++-------
 .../memory-space-cast-non-integer-addrspace.mlir | 12 ++++++++++++
 2 files changed, 21 insertions(+), 7 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..e17ad62a8d645 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -1255,9 +1255,11 @@ struct MemorySpaceCastOpLowering
 
     Type resultType = op.getDest().getType();
     if (auto resultTypeR = dyn_cast<MemRefType>(resultType)) {
-      auto resultDescType =
-          cast<LLVM::LLVMStructType>(typeConverter->convertType(resultTypeR));
-      Type newPtrType = resultDescType.getBody()[0];
+      auto convertedType =
+          typeConverter->convertType<LLVM::LLVMStructType>(resultTypeR);
+      if (!convertedType)
+        return rewriter.notifyMatchFailure(op, "memref type conversion failed");
+      Type newPtrType = convertedType.getBody()[0];
 
       SmallVector<Value> descVals;
       MemRefDescriptor::unpack(rewriter, loc, adaptor.getSource(), resultTypeR,
@@ -1416,8 +1418,8 @@ struct MemRefReinterpretCastOpLowering
       memref::ReinterpretCastOp::Adaptor adaptor, Value *descriptor) const {
     MemRefType targetMemRefType =
         cast<MemRefType>(castOp.getResult().getType());
-    auto llvmTargetDescriptorTy = dyn_cast_or_null<LLVM::LLVMStructType>(
-        typeConverter->convertType(targetMemRefType));
+    auto llvmTargetDescriptorTy =
+        typeConverter->convertType<LLVM::LLVMStructType>(targetMemRefType);
     if (!llvmTargetDescriptorTy)
       return failure();
 
@@ -1485,8 +1487,8 @@ struct MemRefReshapeOpLowering
     if (shapeMemRefType.hasStaticShape()) {
       MemRefType targetMemRefType =
           cast<MemRefType>(reshapeOp.getResult().getType());
-      auto llvmTargetDescriptorTy = dyn_cast_or_null<LLVM::LLVMStructType>(
-          typeConverter->convertType(targetMemRefType));
+      auto llvmTargetDescriptorTy =
+          typeConverter->convertType<LLVM::LLVMStructType>(targetMemRefType);
       if (!llvmTargetDescriptorTy)
         return failure();
 
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