[libc-commits] [PATCH] D149493: [libc] Fix printing on the GPU when given a `cpp::string_ref`

Joseph Huber via Phabricator via libc-commits libc-commits at lists.llvm.org
Fri Apr 28 15:46:34 PDT 2023


jhuber6 created this revision.
jhuber6 added reviewers: sivachandra, lntue, michaelrj.
Herald added subscribers: libc-commits, ecnelises, tschuett.
Herald added projects: libc-project, All.
jhuber6 requested review of this revision.

The implementation of the test printing currently expects a null
terminated C-string. However, the `write_to_stderr` interface uses a
string view, which doesn't need to be null terminated. This patch
changes the printing interface to directly use `fwrite` instead rather
than relying on a null terminator.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D149493

Files:
  libc/src/__support/OSUtil/gpu/io.cpp
  libc/utils/gpu/loader/Server.h


Index: libc/utils/gpu/loader/Server.h
===================================================================
--- libc/utils/gpu/loader/Server.h
+++ libc/utils/gpu/loader/Server.h
@@ -30,9 +30,14 @@
 
   switch (port->get_opcode()) {
   case __llvm_libc::rpc::Opcode::PRINT_TO_STDERR: {
+    uint64_t str_size;
     void *str = nullptr;
-    port->recv_n([&](uint64_t size) { return str = malloc(size); });
-    fputs(reinterpret_cast<char *>(str), stderr);
+    port->recv_n([&](uint64_t size) {
+      str_size = size;
+      str = malloc(size);
+      return str;
+    });
+    fwrite(str, str_size, 1, stderr);
     free(str);
     break;
   }
Index: libc/src/__support/OSUtil/gpu/io.cpp
===================================================================
--- libc/src/__support/OSUtil/gpu/io.cpp
+++ libc/src/__support/OSUtil/gpu/io.cpp
@@ -16,7 +16,7 @@
 
 void write_to_stderr(cpp::string_view msg) {
   rpc::Port port = rpc::client.open(rpc::PRINT_TO_STDERR);
-  port.send_n(msg.data(), msg.size() + 1);
+  port.send_n(msg.data(), msg.size());
   port.close();
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D149493.518074.patch
Type: text/x-patch
Size: 1070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20230428/aae7fea7/attachment.bin>


More information about the libc-commits mailing list