[Mlir-commits] [mlir] Fix crash when using when using --finalize-memref-to-llvm (PR #112433)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Oct 15 13:22:29 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Siddhesh Deodhar (siddhesh195)

<details>
<summary>Changes</summary>

This patch fixes crash when attempting to convert uint to int address space during finalize-memref-to-llvm by doing the following:

 1. Add a check to verify that  IntegerAttr is signed int before calling IntegerAttr::getInt()
 2. Emit error when getMemRefAddressSpace returns a failure()

Closes  #<!-- -->111242 

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


3 Files Affected:

- (modified) mlir/lib/Conversion/LLVMCommon/Pattern.cpp (+7-1) 
- (modified) mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp (+4-2) 
- (added) mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir (+10) 


``````````diff
diff --git a/mlir/lib/Conversion/LLVMCommon/Pattern.cpp b/mlir/lib/Conversion/LLVMCommon/Pattern.cpp
index d551506485a454..d502b4f49b00b9 100644
--- a/mlir/lib/Conversion/LLVMCommon/Pattern.cpp
+++ b/mlir/lib/Conversion/LLVMCommon/Pattern.cpp
@@ -106,8 +106,14 @@ bool ConvertToLLVMPattern::isConvertibleAndHasIdentityMaps(
 
 Type ConvertToLLVMPattern::getElementPtrType(MemRefType type) const {
   auto addressSpace = getTypeConverter()->getMemRefAddressSpace(type);
-  if (failed(addressSpace))
+  if (failed(addressSpace)) {
+    emitError(UnknownLoc::get(type.getContext()),
+              "conversion of memref memory space ")
+        << type.getMemorySpace()
+        << " to integer address space "
+           "failed. Consider adding memory space conversions.";
     return {};
+  }
   return LLVM::LLVMPointerType::get(type.getContext(), *addressSpace);
 }
 
diff --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
index 5a92fa839e9847..8693a60ad6e1b0 100644
--- a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
+++ b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
@@ -527,8 +527,10 @@ LLVMTypeConverter::getMemRefAddressSpace(BaseMemRefType type) const {
     return failure();
   if (!(*converted)) // Conversion to default is 0.
     return 0;
-  if (auto explicitSpace = llvm::dyn_cast_if_present<IntegerAttr>(*converted))
-    return explicitSpace.getInt();
+  if (auto explicitSpace = llvm::dyn_cast_if_present<IntegerAttr>(*converted)) {
+    if (explicitSpace.getType().isSignedInteger())
+      return explicitSpace.getInt();
+  }
   return failure();
 }
 
diff --git a/mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir b/mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir
new file mode 100644
index 00000000000000..adad1cb0922337
--- /dev/null
+++ b/mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir
@@ -0,0 +1,10 @@
+// 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
+// -veri-y-diagnostics here
+
+// CHECK: conversion of memref memory space 1 : ui64 to integer address space failed. Consider adding memory space conversions.
+// CHECK-LABEL: @invalid_int_conversion
+func.func @invalid_int_conversion() {
+     %alloc_0 = memref.alloc() {alignment = 64 : i64} : memref<10xf32, 1 : ui64>
+    return
+}

``````````

</details>


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


More information about the Mlir-commits mailing list