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

Siddhesh Deodhar llvmlistbot at llvm.org
Fri Oct 18 06:46:09 PDT 2024


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

>From b8780fbccd3b39a0018e8c17330175730c0fc11d Mon Sep 17 00:00:00 2001
From: Siddhesh Deodhar <siddheshdeodhar95 at gmail.com>
Date: Tue, 15 Oct 2024 16:03:47 -0400
Subject: [PATCH 1/3] Fix crash when using when using --finalize-memref-to-llvm

Fix crash when attempting to convert uint to int address space during finalize-memref-to-llvm
---
 mlir/lib/Conversion/LLVMCommon/Pattern.cpp          |  8 +++++++-
 mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp    |  6 ++++--
 mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir | 10 ++++++++++
 3 files changed, 21 insertions(+), 3 deletions(-)
 create mode 100644 mlir/test/Conversion/MemRefToLLVM/invalid-uint.mlir

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
+}

>From b3eba807493df21497cd574e5af7d1eff36ce09f Mon Sep 17 00:00:00 2001
From: Siddhesh Deodhar <siddheshdeodhar95 at gmail.com>
Date: Tue, 15 Oct 2024 20:06:14 -0400
Subject: [PATCH 2/3] Resolve test failure due to incorrect usage of getInt()

Resolve test failure by checking if an IntegerAttr is int
---
 mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
index 8693a60ad6e1b0..b81497dbbd5298 100644
--- a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
+++ b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
@@ -528,7 +528,8 @@ LLVMTypeConverter::getMemRefAddressSpace(BaseMemRefType type) const {
   if (!(*converted)) // Conversion to default is 0.
     return 0;
   if (auto explicitSpace = llvm::dyn_cast_if_present<IntegerAttr>(*converted)) {
-    if (explicitSpace.getType().isSignedInteger())
+    if (explicitSpace.getType().isIndex() ||
+        explicitSpace.getType().isSignlessInteger())
       return explicitSpace.getInt();
   }
   return failure();

>From 709f0ce43633f07c951123942f10eed935e6617d Mon Sep 17 00:00:00 2001
From: Siddhesh Deodhar <153800103+siddhesh195 at users.noreply.github.com>
Date: Fri, 18 Oct 2024 09:46:01 -0400
Subject: [PATCH 3/3] Update mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp

Co-authored-by: Christian Ulmann <christianulmann at gmail.com>
---
 mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
index b81497dbbd5298..78f1960fc8569b 100644
--- a/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
+++ b/mlir/lib/Conversion/LLVMCommon/TypeConverter.cpp
@@ -527,7 +527,7 @@ 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)) {
+  if (auto explicitSpace = dyn_cast_if_present<IntegerAttr>(*converted)) {
     if (explicitSpace.getType().isIndex() ||
         explicitSpace.getType().isSignlessInteger())
       return explicitSpace.getInt();



More information about the Mlir-commits mailing list