[libc-commits] [libc] c8e69fa - [libc] Fix GPU 'printf' on strings with padding

Joseph Huber via libc-commits libc-commits at lists.llvm.org
Sat Jul 20 20:36:23 PDT 2024


Author: Joseph Huber
Date: 2024-07-20T22:36:12-05:00
New Revision: c8e69fa4a0bd158fbd63cdb4794dd1232184f155

URL: https://github.com/llvm/llvm-project/commit/c8e69fa4a0bd158fbd63cdb4794dd1232184f155
DIFF: https://github.com/llvm/llvm-project/commit/c8e69fa4a0bd158fbd63cdb4794dd1232184f155.diff

LOG: [libc] Fix GPU 'printf' on strings with padding

Summary:
We get the `strlen` to know how much memory to allocate here, but it
wasn't taking into account if the padding was larger than the string
itself. This patch sets it to an empty string so we always add the
minimum size. This implementation is slightly wasteful with memory, but
I am not concerned with a few extra bytes here and there for some memory
that gets immediately free'd.

Added: 
    

Modified: 
    libc/utils/gpu/server/rpc_server.cpp

Removed: 
    


################################################################################
diff  --git a/libc/utils/gpu/server/rpc_server.cpp b/libc/utils/gpu/server/rpc_server.cpp
index 119539e3cad44..ed23d22f0bc36 100644
--- a/libc/utils/gpu/server/rpc_server.cpp
+++ b/libc/utils/gpu/server/rpc_server.cpp
@@ -108,6 +108,10 @@ void handle_printf(rpc::Server::Port &port) {
       if (cur_section.has_conv && cur_section.conv_name == 's' &&
           cur_section.conv_val_ptr) {
         strs_to_copy[lane].emplace_back(cur_section.conv_val_ptr);
+        // Get the minimum size of the string in the case of padding.
+        char c = '\0';
+        cur_section.conv_val_ptr = &c;
+        convert(&writer, cur_section);
       } else if (cur_section.has_conv) {
         // Ignore conversion errors for the first pass.
         convert(&writer, cur_section);


        


More information about the libc-commits mailing list