Skip to content

[mlir][MemRefToLLVM] Fix crash with unconvertable memory space #132323

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 26, 2025

Conversation

CoTinker
Copy link
Contributor

This PR adds handling when the memref.alloca with unconvertable memory space to prevent a crash. Fixes #131439.

@CoTinker CoTinker requested a review from krzysz00 March 21, 2025 03:32
@llvmbot llvmbot added the mlir label Mar 21, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 21, 2025

@llvm/pr-subscribers-mlir

Author: Longsheng Mou (CoTinker)

Changes

This PR adds handling when the memref.alloca with unconvertable memory space to prevent a crash. Fixes #131439.


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

2 Files Affected:

  • (modified) mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp (+5-2)
  • (added) mlir/test/Conversion/MemRefToLLVM/issue-131439.mlir (+15)
diff --git a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
index fe0ee11d84adb..cb4317ef1bcec 100644
--- a/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
+++ b/mlir/lib/Conversion/MemRefToLLVM/MemRefToLLVM.cpp
@@ -105,8 +105,11 @@ struct AllocaOpLowering : public AllocLikeOpLLVMLowering {
     auto allocaOp = cast<memref::AllocaOp>(op);
     auto elementType =
         typeConverter->convertType(allocaOp.getType().getElementType());
-    unsigned addrSpace =
-        *getTypeConverter()->getMemRefAddressSpace(allocaOp.getType());
+    FailureOr<unsigned> maybeAddressSpace =
+        getTypeConverter()->getMemRefAddressSpace(allocaOp.getType());
+    if (failed(maybeAddressSpace))
+      return std::make_tuple(Value(), Value());
+    unsigned addrSpace = *maybeAddressSpace;
     auto elementPtrType =
         LLVM::LLVMPointerType::get(rewriter.getContext(), addrSpace);
 
diff --git a/mlir/test/Conversion/MemRefToLLVM/issue-131439.mlir b/mlir/test/Conversion/MemRefToLLVM/issue-131439.mlir
new file mode 100644
index 0000000000000..d5aa75fb45f52
--- /dev/null
+++ b/mlir/test/Conversion/MemRefToLLVM/issue-131439.mlir
@@ -0,0 +1,15 @@
+// RUN: mlir-opt %s -finalize-memref-to-llvm 2>&1 | FileCheck %s
+// Since the error is at an unknown location, we use FileCheck instead of
+// -verify-diagnostics here
+
+// CHECK: conversion of memref memory space #spirv.storage_class<StorageBuffer> to integer address space failed. Consider adding memory space conversions.
+// CHECK-LABEL: @issue_131439
+func.func @issue_131439(%arg0 : memref<i32>) {
+  // CHECK: memref.alloca
+  %alloca = memref.alloca() : memref<1x32x33xi32, #spirv.storage_class<StorageBuffer>>
+  %c0 = arith.constant 0 : index
+  // CHECK: memref.load
+  %0 = memref.load %alloca[%c0, %c0, %c0] : memref<1x32x33xi32, #spirv.storage_class<StorageBuffer>>
+  memref.store %0, %arg0[] : memref<i32>
+  func.return
+}

This PR adds handling when the `memref.alloca` with unconvertable memory space to prevent a crash.
Copy link
Contributor

@krzysz00 krzysz00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, thanks

@CoTinker
Copy link
Contributor Author

Thanks for your review.

@CoTinker CoTinker merged commit 894b27a into llvm:main Mar 26, 2025
11 checks passed
@CoTinker CoTinker deleted the address_space branch March 26, 2025 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MLIR] crashed on allocateBuffer when using -finalize-memref-to-llvm : Assertion Failure 'this->_M_is_engaged()'
4 participants