Skip to content

Conversation

@siddhesh195
Copy link
Contributor

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

Fix crash when attempting to convert uint to int address space during finalize-memref-to-llvm
@github-actions
Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2024

@llvm/pr-subscribers-mlir-llvm

Author: Siddhesh Deodhar (siddhesh195)

Changes

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

@llvmbot
Copy link
Member

llvmbot commented Oct 15, 2024

@llvm/pr-subscribers-mlir

Author: Siddhesh Deodhar (siddhesh195)

Changes

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

Resolve test failure by checking if an IntegerAttr is int
Copy link
Contributor

@Dinistro Dinistro left a comment

Choose a reason for hiding this comment

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

Some drive-by comments, but this should be reviewed by people who rely on this conversion as they have more context.

if (auto explicitSpace = llvm::dyn_cast_if_present<IntegerAttr>(*converted))
return explicitSpace.getInt();
if (auto explicitSpace = dyn_cast_if_present<IntegerAttr>(*converted)) {
if (explicitSpace.getType().isIndex() ||
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we instead change this to get the integer out of uiN attributes instead?

If not, this is reasonably approved

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, there currently is no separate attributes defined for signless and signed integer in BuiltinAttributes and IntegerAttr represents both signless and signed. BuiltinAttributes

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the reviews. If the PR looks good, can someone help merge because I don't have write access

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll run CI and land this, thanks for helping!

@github-actions
Copy link

github-actions bot commented Oct 25, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@krzysz00 krzysz00 merged commit 40740c4 into llvm:main Nov 8, 2024
8 checks passed
@github-actions
Copy link

github-actions bot commented Nov 8, 2024

@siddhesh195 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
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  llvm#111242

---------

Co-authored-by: Christian Ulmann <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[mlir] Crash when using --finalize-memref-to-llvm

4 participants