[Mlir-commits] [mlir] Fix crash when using when using --finalize-memref-to-llvm (PR #112433)
Siddhesh Deodhar
llvmlistbot at llvm.org
Tue Oct 15 13:21:27 PDT 2024
https://github.com/siddhesh195 created https://github.com/llvm/llvm-project/pull/112433
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
>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] 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
+}
More information about the Mlir-commits
mailing list