[llvm] [OpenMP][Offload] Add a buffer layer to debug messaging (PR #172446)

Alex Duran via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 13 08:56:58 PST 2026


https://github.com/adurang updated https://github.com/llvm/llvm-project/pull/172446

>From 493e7aa1e8d27407ba8ee0970e4618dede1b19d1 Mon Sep 17 00:00:00 2001
From: Alex Duran <alejandro.duran at intel.com>
Date: Tue, 16 Dec 2025 10:25:18 +0100
Subject: [PATCH 1/2] [OpenMP][Offload] Add a buffer layer to debug to avoid
 thread interference

---
 offload/include/Shared/Debug.h | 57 +++++++++++++++++++++++++---------
 1 file changed, 42 insertions(+), 15 deletions(-)

diff --git a/offload/include/Shared/Debug.h b/offload/include/Shared/Debug.h
index d5cf719f1ebf3..1af264ba5f08d 100644
--- a/offload/include/Shared/Debug.h
+++ b/offload/include/Shared/Debug.h
@@ -44,7 +44,7 @@
 #include <string>
 
 #include "llvm/ADT/StringExtras.h"
-#include "llvm/Support/circular_raw_ostream.h"
+#include "llvm/Support/raw_ostream.h"
 
 /// 32-Bit field data attributes controlling information presented to the user.
 enum OpenMPInfoType : uint32_t {
@@ -176,6 +176,41 @@ class LLVM_ABI odbg_ostream final : public raw_ostream {
   /// If the stream is muted, writes to it are ignored
   bool Muted = false;
 
+  /// Small buffer to reduce interferance between different threads
+  /// writing at the same time to the underlying stream.
+  static constexpr size_t BufferSize = 256;
+  std::array<char, BufferSize> Buffer;
+  size_t BufferPos = 0;
+
+  void flush() {
+    if (BufferPos > 0) {
+      Os.write(Buffer.data(), BufferPos);
+      BufferPos = 0;
+    }
+    Os.flush();
+  }
+
+  void writeToBuffer(const char *Ptr, size_t Size) {
+    size_t RemainingBufferSize = BufferSize - BufferPos;
+    if (Size <= RemainingBufferSize) {
+      std::memcpy(&Buffer[BufferPos], Ptr, Size);
+      BufferPos += Size;
+      return;
+    }
+    while (Size > RemainingBufferSize) {
+      std::memcpy(&Buffer[BufferPos], Ptr, RemainingBufferSize);
+      BufferPos = BufferSize;
+      Ptr += RemainingBufferSize;
+      Size -= RemainingBufferSize;
+      flush();
+      RemainingBufferSize = BufferSize;
+    }
+    if (Size > 0) {
+      std::memcpy(&Buffer[BufferPos], Ptr, Size);
+      BufferPos += Size;
+    }
+  }
+
   /// Split the line on newlines and insert the prefix before each
   /// newline. Forward everything to the underlying stream.
   void write_impl(const char *Ptr, size_t Size) final {
@@ -202,13 +237,13 @@ class LLVM_ABI odbg_ostream final : public raw_ostream {
       NeedEndNewLine = true;
     }
   }
-  void emitPrefix() { Os.write(Prefix.c_str(), Prefix.size()); }
+  void emitPrefix() { writeToBuffer(Prefix.c_str(), Prefix.size()); }
   void writeWithPrefix(StringRef Str) {
     if (ShouldPrefixNextString) {
       emitPrefix();
       ShouldPrefixNextString = false;
     }
-    Os.write(Str.data(), Str.size());
+    writeToBuffer(Str.data(), Str.size());
   }
 
 public:
@@ -222,7 +257,8 @@ class LLVM_ABI odbg_ostream final : public raw_ostream {
   }
   ~odbg_ostream() final {
     if (ShouldEmitNewLineOnDestruction && NeedEndNewLine)
-      Os << '\n';
+      writeToBuffer("\n", 1);
+    flush();
   }
   odbg_ostream(const odbg_ostream &) = delete;
   odbg_ostream &operator=(const odbg_ostream &) = delete;
@@ -246,17 +282,8 @@ class LLVM_ABI odbg_ostream final : public raw_ostream {
   void shouldMute(const OnlyLevel Filter) { Muted = BaseLevel != Filter; }
 };
 
-/// dbgs - Return a circular-buffered debug stream.
-[[maybe_unused]] static llvm::raw_ostream &dbgs() {
-  // Do one-time initialization in a thread-safe way.
-  static struct dbgstream {
-    llvm::circular_raw_ostream strm;
-
-    dbgstream() : strm(llvm::errs(), "*** Debug Log Output ***\n", 0) {}
-  } thestrm;
-
-  return thestrm.strm;
-}
+/// dbgs - Return the debug stream for offload debugging (just llvm::errs()).
+[[maybe_unused]] static llvm::raw_ostream &dbgs() { return llvm::errs(); }
 
 #ifdef OMPTARGET_DEBUG
 

>From 1d82dd4f0d22cafa531b9bff8262b4d190678407 Mon Sep 17 00:00:00 2001
From: Alex Duran <alejandro.duran at intel.com>
Date: Tue, 16 Dec 2025 11:02:09 +0100
Subject: [PATCH 2/2] Update offload/include/Shared/Debug.h

Co-authored-by: Copilot <175728472+Copilot at users.noreply.github.com>
---
 offload/include/Shared/Debug.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/offload/include/Shared/Debug.h b/offload/include/Shared/Debug.h
index 1af264ba5f08d..b1b68284dba78 100644
--- a/offload/include/Shared/Debug.h
+++ b/offload/include/Shared/Debug.h
@@ -176,7 +176,7 @@ class LLVM_ABI odbg_ostream final : public raw_ostream {
   /// If the stream is muted, writes to it are ignored
   bool Muted = false;
 
-  /// Small buffer to reduce interferance between different threads
+  /// Small buffer to reduce interference between different threads
   /// writing at the same time to the underlying stream.
   static constexpr size_t BufferSize = 256;
   std::array<char, BufferSize> Buffer;



More information about the llvm-commits mailing list