[Mlir-commits] [mlir] [mlir] fix overflow warning when generating embedded libdevice (PR #125801)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Feb 4 20:55:18 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Zichen Lu (MikaOvO)

<details>
<summary>Changes</summary>

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
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))));

``````````

</details>


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


More information about the Mlir-commits mailing list