Skip to content

[mlir] fix overflow warning when generating embedded libdevice #125801

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

Conversation

MikaOvO
Copy link
Contributor

@MikaOvO MikaOvO commented Feb 5, 2025

When building mlir with -DMLIR_NVVM_EMBED_LIBDEVICE=ON, there will be a warning

build/tools/mlir/lib/Target/LLVM/libdevice_embedded.c:1: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘143’ to ‘-113’ [-Woverflow]

which is followed by a large number of characters in stdout.

Fix this to avoid stdout outputting a large number of characters (3e5).

@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-mlir-llvm

@llvm/pr-subscribers-mlir

Author: Zichen Lu (MikaOvO)

Changes

When building mlir with -DMLIR_NVVM_EMBED_LIBDEVICE=ON, there will be a warning

build/tools/mlir/lib/Target/LLVM/libdevice_embedded.c:1: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘143’ to ‘-113’ [-Woverflow]

which is followed by a large number of characters in stdout.

Fix this to avoid stdout outputting a large number of characters (3e5).


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

2 Files Affected:

  • (modified) mlir/lib/Target/LLVM/CMakeLists.txt (+1-1)
  • (modified) mlir/lib/Target/LLVM/NVVM/Target.cpp (+4-3)
diff --git a/mlir/lib/Target/LLVM/CMakeLists.txt b/mlir/lib/Target/LLVM/CMakeLists.txt
index 4be147d02d579af..83fbf7a5fe5f32c 100644
--- a/mlir/lib/Target/LLVM/CMakeLists.txt
+++ b/mlir/lib/Target/LLVM/CMakeLists.txt
@@ -125,7 +125,7 @@ function(embed_binary_to_src file output_file symbol)
     # Convert hex data for C compatibility
     string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata})
     # Write data to output file
-    file(WRITE ${output_file} "const char ${symbol}[] = {${filedata}};\nconst int ${symbol}_size = sizeof(${symbol});\n")
+    file(WRITE ${output_file} "const unsigned char ${symbol}[] = {${filedata}};\nconst int ${symbol}_size = sizeof(${symbol});\n")
 endfunction()
 
 set(MLIR_NVVM_EMBED_LIBDEVICE 0 CACHE BOOL "Embed CUDA libdevice.bc in the binary at build time instead of looking it up at runtime")
diff --git a/mlir/lib/Target/LLVM/NVVM/Target.cpp b/mlir/lib/Target/LLVM/NVVM/Target.cpp
index 86ff848d6c6c2d3..58051db7bd066ea 100644
--- a/mlir/lib/Target/LLVM/NVVM/Target.cpp
+++ b/mlir/lib/Target/LLVM/NVVM/Target.cpp
@@ -47,7 +47,7 @@ using namespace mlir::NVVM;
 #define __DEFAULT_CUDATOOLKIT_PATH__ ""
 #endif
 
-extern "C" const char _mlir_embedded_libdevice[];
+extern "C" const unsigned char _mlir_embedded_libdevice[];
 extern "C" const unsigned _mlir_embedded_libdevice_size;
 
 namespace {
@@ -159,8 +159,9 @@ LogicalResult SerializeGPUModuleBase::appendStandardLibs() {
 
   // Allocate a resource using one of the UnManagedResourceBlob method to wrap
   // the embedded data.
-  auto unmanagedBlob = UnmanagedAsmResourceBlob::allocateInferAlign(
-      ArrayRef<char>{_mlir_embedded_libdevice, _mlir_embedded_libdevice_size});
+  auto unmanagedBlob =
+      UnmanagedAsmResourceBlob::allocateInferAlign(ArrayRef<char>{
+          (char *)_mlir_embedded_libdevice, _mlir_embedded_libdevice_size});
   librariesToLink.push_back(DenseResourceElementsAttr::get(
       type, resourceManager.insert("_mlir_embedded_libdevice",
                                    std::move(unmanagedBlob))));

ArrayRef<char>{_mlir_embedded_libdevice, _mlir_embedded_libdevice_size});
auto unmanagedBlob =
UnmanagedAsmResourceBlob::allocateInferAlign(ArrayRef<char>{
(char *)_mlir_embedded_libdevice, _mlir_embedded_libdevice_size});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
(char *)_mlir_embedded_libdevice, _mlir_embedded_libdevice_size});
(const char *)_mlir_embedded_libdevice, _mlir_embedded_libdevice_size});

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed, could you help merge this PR?

@MikaOvO MikaOvO force-pushed the mikaovo/remove_overflow_warning_in_embedded_libdevice branch from 0f7dd06 to 4ce95fe Compare February 5, 2025 09:11
Copy link

github-actions bot commented Feb 5, 2025

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

@MikaOvO MikaOvO force-pushed the mikaovo/remove_overflow_warning_in_embedded_libdevice branch from 4ce95fe to e0eca73 Compare February 5, 2025 09:17
@grypp grypp merged commit a61ca99 into llvm:main Feb 5, 2025
8 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
…125801)

When building mlir with `-DMLIR_NVVM_EMBED_LIBDEVICE=ON`, there will be
a warning
```
build/tools/mlir/lib/Target/LLVM/libdevice_embedded.c:1: warning: overflow in conversion from ‘int’ to ‘char’ changes value from ‘143’ to ‘-113’ [-Woverflow]
```
which is followed by a large number of characters in stdout.

Fix this to avoid stdout outputting a large number of characters (3e5).
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.

4 participants