[llvm] [offload] Add opt-in `std::ostream` support to OffloadPrint.hpp (PR #222269)

Jan Trusiłło via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 01:48:41 PDT 2026


https://github.com/311Volt updated https://github.com/llvm/llvm-project/pull/222269

>From 2830cdc2dfb56eb49c801fe8ecc39eab6d3278fe Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Trusi=C5=82=C5=82o?= <jan.trusillo at intel.com>
Date: Wed, 9 Sep 2026 08:12:19 +0000
Subject: [PATCH 1/2] support std::ostream in OffloadPrint.hpp

---
 .../offload-tblgen/functions_ranged_param.td  |  2 +-
 .../test/tools/offload-tblgen/print_enum.td   | 11 +++++-
 .../tools/offload-tblgen/print_function.td    |  2 +-
 .../tools/offload-tblgen/type_tagged_enum.td  |  4 +--
 offload/tools/offload-tblgen/PrintGen.cpp     | 34 ++++++++++++-------
 5 files changed, 35 insertions(+), 18 deletions(-)

diff --git a/offload/test/tools/offload-tblgen/functions_ranged_param.td b/offload/test/tools/offload-tblgen/functions_ranged_param.td
index 1ce8b394b1575..d80fc80de2ab0 100644
--- a/offload/test/tools/offload-tblgen/functions_ranged_param.td
+++ b/offload/test/tools/offload-tblgen/functions_ranged_param.td
@@ -23,7 +23,7 @@ def FunctionA : Function {
   let returns = [];
 }
 
-// CHECK: inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const struct function_a_params_t *params) {
+// CHECK: inline offload_ostream &operator<<(offload_ostream &os, const struct function_a_params_t *params) {
 // CHECK:   os << ".OutPtr = ";
 // CHECK:   for (size_t i = 0; i < *params->pOutCount; i++) {
 // CHECK:     if (i > 0) {
diff --git a/offload/test/tools/offload-tblgen/print_enum.td b/offload/test/tools/offload-tblgen/print_enum.td
index c7573a9a415cb..6662196fb827f 100644
--- a/offload/test/tools/offload-tblgen/print_enum.td
+++ b/offload/test/tools/offload-tblgen/print_enum.td
@@ -4,6 +4,15 @@
 
 include "APIDefs.td"
 
+// CHECK: #include <type_traits>
+// CHECK: #ifdef OFFLOAD_PRINT_USE_STD_OSTREAM
+// CHECK-NEXT: #include <ostream>
+// CHECK-NEXT: using offload_ostream = std::ostream;
+// CHECK-NEXT: #else
+// CHECK-NEXT: #include <llvm/Support/raw_ostream.h>
+// CHECK-NEXT: using offload_ostream = llvm::raw_ostream;
+// CHECK-NEXT: #endif
+
 def my_enum_t : Enum {
   let desc = "An example enum";
   let etors =[
@@ -14,7 +23,7 @@ def my_enum_t : Enum {
   ];
 }
 
-// CHECK: inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, enum my_enum_t value)
+// CHECK: inline offload_ostream &operator<<(offload_ostream &os, enum my_enum_t value)
 // CHECK: switch (value) {
 // CHECK: case MY_ENUM_VALUE_ONE:
 // CHECK:   os << "MY_ENUM_VALUE_ONE";
diff --git a/offload/test/tools/offload-tblgen/print_function.td b/offload/test/tools/offload-tblgen/print_function.td
index 74b39f145a400..22783b98b44ee 100644
--- a/offload/test/tools/offload-tblgen/print_function.td
+++ b/offload/test/tools/offload-tblgen/print_function.td
@@ -25,7 +25,7 @@ def FunctionA : Function {
 // CHECK-API-NEXT: ol_foo_handle_t* pParamHandle;
 // CHECK-API-NEXT: uint32_t** pParamPointer;
 
-// CHECK-PRINT: inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const struct function_a_params_t *params)
+// CHECK-PRINT: inline offload_ostream &operator<<(offload_ostream &os, const struct function_a_params_t *params)
 // CHECK-PRINT: os << ".ParamValue = ";
 // CHECK-PRINT: os << *params->pParamValue;
 // CHECK-PRINT: os << ", ";
diff --git a/offload/test/tools/offload-tblgen/type_tagged_enum.td b/offload/test/tools/offload-tblgen/type_tagged_enum.td
index b32531aac9c81..5d4068740c9fe 100644
--- a/offload/test/tools/offload-tblgen/type_tagged_enum.td
+++ b/offload/test/tools/offload-tblgen/type_tagged_enum.td
@@ -47,7 +47,7 @@ def FunctionA : Function {
 }
 
 // Check that a tagged enum print function definition is generated
-// CHECK-PRINT: void printTagged(llvm::raw_ostream &os, const void *ptr, my_type_tagged_enum_t value, size_t size) {
+// CHECK-PRINT: void printTagged(offload_ostream &os, const void *ptr, my_type_tagged_enum_t value, size_t size) {
 // CHECK-PRINT: case MY_TYPE_TAGGED_ENUM_VALUE_ONE: {
 // CHECK-PRINT:   const uint32_t * const tptr = (const uint32_t * const)ptr;
 // CHECK-PRINT:   os << (const void *)tptr << " (";
@@ -68,6 +68,6 @@ def FunctionA : Function {
 // CHECK-PRINT: }
 
 // Check that the tagged type information is used when printing function parameters
-// CHECK-PRINT: llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const struct function_a_params_t *params) {
+// CHECK-PRINT: offload_ostream &operator<<(offload_ostream &os, const struct function_a_params_t *params) {
 // CHECK-PRINT: os << ".PropValue = " 
 // CHECK-PRINT-NEXT: printTagged(os, *params->pPropValue, *params->pPropName, *params->pPropSize);
diff --git a/offload/tools/offload-tblgen/PrintGen.cpp b/offload/tools/offload-tblgen/PrintGen.cpp
index d2475d0d43a01..3bdf24d64b765 100644
--- a/offload/tools/offload-tblgen/PrintGen.cpp
+++ b/offload/tools/offload-tblgen/PrintGen.cpp
@@ -23,18 +23,18 @@ using namespace offload::tblgen;
 constexpr auto PrintTypeHeader =
     R"(///////////////////////////////////////////////////////////////////////////////
 /// @brief Print operator for the {0} type
-/// @returns llvm::raw_ostream &
+/// @returns offload_ostream &
 )";
 
 constexpr auto PrintTaggedEnumHeader =
     R"(///////////////////////////////////////////////////////////////////////////////
 /// @brief Print type-tagged {0} enum value
-/// @returns llvm::raw_ostream &
+/// @returns offload_ostream &
 )";
 
 static void ProcessEnum(const EnumRec &Enum, raw_ostream &OS) {
   OS << formatv(PrintTypeHeader, Enum.getName());
-  OS << formatv("inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, "
+  OS << formatv("inline offload_ostream &operator<<(offload_ostream &os, "
                 "enum {0} value) "
                 "{{\n" TAB_1 "switch (value) {{\n",
                 Enum.getName());
@@ -56,7 +56,7 @@ static void ProcessEnum(const EnumRec &Enum, raw_ostream &OS) {
   OS << formatv(PrintTaggedEnumHeader, Enum.getName());
 
   OS << formatv(R"""(template <>
-inline void printTagged(llvm::raw_ostream &os, const void *ptr, {0} value, size_t size) {{
+inline void printTagged(offload_ostream &os, const void *ptr, {0} value, size_t size) {{
   if (ptr == NULL) {{
     printPtr(os, ptr);
     return;
@@ -101,8 +101,8 @@ inline void printTagged(llvm::raw_ostream &os, const void *ptr, {0} value, size_
 
 static void EmitResultPrint(raw_ostream &OS) {
   OS << R""(
-inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os,
-                                const ol_error_struct_t *Err) {
+inline offload_ostream &operator<<(offload_ostream &os,
+                                   const ol_error_struct_t *Err) {
   if (Err == nullptr) {
     os << "OL_SUCCESS";
   } else {
@@ -120,7 +120,7 @@ static void EmitFunctionParamStructPrint(const FunctionRec &Func,
   }
 
   OS << formatv(R"(
-inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const struct {0} *params) {{
+inline offload_ostream &operator<<(offload_ostream &os, const struct {0} *params) {{
 )",
                 Func.getParamStructName());
 
@@ -164,7 +164,7 @@ void ProcessStruct(const StructRec &Struct, raw_ostream &OS) {
   }
   OS << formatv(PrintTypeHeader, Struct.getName());
   OS << formatv(R"(
-inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const struct {0} params) {{
+inline offload_ostream &operator<<(offload_ostream &os, const struct {0} params) {{
 )",
                 Struct.getName());
   OS << formatv(TAB_1 "os << \"(struct {0}){{\";\n", Struct.getName());
@@ -192,11 +192,19 @@ void EmitOffloadPrintHeader(const RecordKeeper &Records, raw_ostream &OS) {
 #pragma once
 
 #include <OffloadAPI.h>
+#include <type_traits>
+
+#ifdef OFFLOAD_PRINT_USE_STD_OSTREAM
+#include <ostream>
+using offload_ostream = std::ostream;
+#else
 #include <llvm/Support/raw_ostream.h>
+using offload_ostream = llvm::raw_ostream;
+#endif
 
 
-template <typename T> inline ol_result_t printPtr(llvm::raw_ostream &os, const T *ptr);
-template <typename T> inline void printTagged(llvm::raw_ostream &os, const void *ptr, T value, size_t size);
+template <typename T> inline ol_result_t printPtr(offload_ostream &os, const T *ptr);
+template <typename T> inline void printTagged(offload_ostream &os, const void *ptr, T value, size_t size);
 )""";
 
   // ==========
@@ -214,12 +222,12 @@ template <typename T> inline void printTagged(llvm::raw_ostream &os, const void
   // use each other.
   OS << "\n";
   for (auto *R : Records.getAllDerivedDefinitions("Enum")) {
-    OS << formatv("inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, "
+    OS << formatv("inline offload_ostream &operator<<(offload_ostream &os, "
                   "enum {0} value);\n",
                   EnumRec{R}.getName());
   }
   for (auto *R : Records.getAllDerivedDefinitions("Struct")) {
-    OS << formatv("inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, "
+    OS << formatv("inline offload_ostream &operator<<(offload_ostream &os, "
                   "const struct {0} param);\n",
                   StructRec{R}.getName());
   }
@@ -245,7 +253,7 @@ template <typename T> inline void printTagged(llvm::raw_ostream &os, const void
   OS << R"""(
 ///////////////////////////////////////////////////////////////////////////////
 // @brief Print pointer value
-template <typename T> inline ol_result_t printPtr(llvm::raw_ostream &os, const T *ptr) {
+template <typename T> inline ol_result_t printPtr(offload_ostream &os, const T *ptr) {
     if (ptr == nullptr) {
         os << "nullptr";
     } else if constexpr (std::is_pointer_v<T>) {

>From 53ce3b7c8116a61644e6e6351c0896d6c2ea7b8c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Trusi=C5=82=C5=82o?= <jan.trusillo at intel.com>
Date: Wed, 9 Sep 2026 08:46:57 +0000
Subject: [PATCH 2/2] move type alias to detail namespace

---
 .../offload-tblgen/functions_ranged_param.td  |  2 +-
 .../test/tools/offload-tblgen/print_enum.td   | 10 ++--
 .../tools/offload-tblgen/print_function.td    |  2 +-
 .../tools/offload-tblgen/type_tagged_enum.td  |  4 +-
 offload/tools/offload-tblgen/PrintGen.cpp     | 46 +++++++++++--------
 5 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/offload/test/tools/offload-tblgen/functions_ranged_param.td b/offload/test/tools/offload-tblgen/functions_ranged_param.td
index d80fc80de2ab0..2caae904e71a1 100644
--- a/offload/test/tools/offload-tblgen/functions_ranged_param.td
+++ b/offload/test/tools/offload-tblgen/functions_ranged_param.td
@@ -23,7 +23,7 @@ def FunctionA : Function {
   let returns = [];
 }
 
-// CHECK: inline offload_ostream &operator<<(offload_ostream &os, const struct function_a_params_t *params) {
+// CHECK: operator<<(offload::detail::print_ostream &os, const struct function_a_params_t *params) {
 // CHECK:   os << ".OutPtr = ";
 // CHECK:   for (size_t i = 0; i < *params->pOutCount; i++) {
 // CHECK:     if (i > 0) {
diff --git a/offload/test/tools/offload-tblgen/print_enum.td b/offload/test/tools/offload-tblgen/print_enum.td
index 6662196fb827f..6101e959d5d55 100644
--- a/offload/test/tools/offload-tblgen/print_enum.td
+++ b/offload/test/tools/offload-tblgen/print_enum.td
@@ -7,10 +7,14 @@ include "APIDefs.td"
 // CHECK: #include <type_traits>
 // CHECK: #ifdef OFFLOAD_PRINT_USE_STD_OSTREAM
 // CHECK-NEXT: #include <ostream>
-// CHECK-NEXT: using offload_ostream = std::ostream;
+// CHECK-NEXT: namespace offload::detail {
+// CHECK-NEXT: using print_ostream = std::ostream;
+// CHECK-NEXT: }
 // CHECK-NEXT: #else
 // CHECK-NEXT: #include <llvm/Support/raw_ostream.h>
-// CHECK-NEXT: using offload_ostream = llvm::raw_ostream;
+// CHECK-NEXT: namespace offload::detail {
+// CHECK-NEXT: using print_ostream = llvm::raw_ostream;
+// CHECK-NEXT: }
 // CHECK-NEXT: #endif
 
 def my_enum_t : Enum {
@@ -23,7 +27,7 @@ def my_enum_t : Enum {
   ];
 }
 
-// CHECK: inline offload_ostream &operator<<(offload_ostream &os, enum my_enum_t value)
+// CHECK: inline offload::detail::print_ostream &operator<<(offload::detail::print_ostream &os, enum my_enum_t value)
 // CHECK: switch (value) {
 // CHECK: case MY_ENUM_VALUE_ONE:
 // CHECK:   os << "MY_ENUM_VALUE_ONE";
diff --git a/offload/test/tools/offload-tblgen/print_function.td b/offload/test/tools/offload-tblgen/print_function.td
index 22783b98b44ee..311fdf49b3758 100644
--- a/offload/test/tools/offload-tblgen/print_function.td
+++ b/offload/test/tools/offload-tblgen/print_function.td
@@ -25,7 +25,7 @@ def FunctionA : Function {
 // CHECK-API-NEXT: ol_foo_handle_t* pParamHandle;
 // CHECK-API-NEXT: uint32_t** pParamPointer;
 
-// CHECK-PRINT: inline offload_ostream &operator<<(offload_ostream &os, const struct function_a_params_t *params)
+// CHECK-PRINT: operator<<(offload::detail::print_ostream &os, const struct function_a_params_t *params)
 // CHECK-PRINT: os << ".ParamValue = ";
 // CHECK-PRINT: os << *params->pParamValue;
 // CHECK-PRINT: os << ", ";
diff --git a/offload/test/tools/offload-tblgen/type_tagged_enum.td b/offload/test/tools/offload-tblgen/type_tagged_enum.td
index 5d4068740c9fe..9fd33f4a29660 100644
--- a/offload/test/tools/offload-tblgen/type_tagged_enum.td
+++ b/offload/test/tools/offload-tblgen/type_tagged_enum.td
@@ -47,7 +47,7 @@ def FunctionA : Function {
 }
 
 // Check that a tagged enum print function definition is generated
-// CHECK-PRINT: void printTagged(offload_ostream &os, const void *ptr, my_type_tagged_enum_t value, size_t size) {
+// CHECK-PRINT: void printTagged(offload::detail::print_ostream &os, const void *ptr, my_type_tagged_enum_t value, size_t size) {
 // CHECK-PRINT: case MY_TYPE_TAGGED_ENUM_VALUE_ONE: {
 // CHECK-PRINT:   const uint32_t * const tptr = (const uint32_t * const)ptr;
 // CHECK-PRINT:   os << (const void *)tptr << " (";
@@ -68,6 +68,6 @@ def FunctionA : Function {
 // CHECK-PRINT: }
 
 // Check that the tagged type information is used when printing function parameters
-// CHECK-PRINT: offload_ostream &operator<<(offload_ostream &os, const struct function_a_params_t *params) {
+// CHECK-PRINT: operator<<(offload::detail::print_ostream &os, const struct function_a_params_t *params) {
 // CHECK-PRINT: os << ".PropValue = " 
 // CHECK-PRINT-NEXT: printTagged(os, *params->pPropValue, *params->pPropName, *params->pPropSize);
diff --git a/offload/tools/offload-tblgen/PrintGen.cpp b/offload/tools/offload-tblgen/PrintGen.cpp
index 3bdf24d64b765..6db5f5b5c9701 100644
--- a/offload/tools/offload-tblgen/PrintGen.cpp
+++ b/offload/tools/offload-tblgen/PrintGen.cpp
@@ -23,21 +23,22 @@ using namespace offload::tblgen;
 constexpr auto PrintTypeHeader =
     R"(///////////////////////////////////////////////////////////////////////////////
 /// @brief Print operator for the {0} type
-/// @returns offload_ostream &
+/// @returns offload::detail::print_ostream &
 )";
 
 constexpr auto PrintTaggedEnumHeader =
     R"(///////////////////////////////////////////////////////////////////////////////
 /// @brief Print type-tagged {0} enum value
-/// @returns offload_ostream &
+/// @returns offload::detail::print_ostream &
 )";
 
 static void ProcessEnum(const EnumRec &Enum, raw_ostream &OS) {
   OS << formatv(PrintTypeHeader, Enum.getName());
-  OS << formatv("inline offload_ostream &operator<<(offload_ostream &os, "
-                "enum {0} value) "
-                "{{\n" TAB_1 "switch (value) {{\n",
-                Enum.getName());
+  OS << formatv(
+      "inline offload::detail::print_ostream &"
+      "operator<<(offload::detail::print_ostream &os, enum {0} value) "
+      "{{\n" TAB_1 "switch (value) {{\n",
+      Enum.getName());
 
   for (const auto &Val : Enum.getValues()) {
     auto Name = Enum.getEnumValNamePrefix() + "_" + Val.getName();
@@ -56,7 +57,7 @@ static void ProcessEnum(const EnumRec &Enum, raw_ostream &OS) {
   OS << formatv(PrintTaggedEnumHeader, Enum.getName());
 
   OS << formatv(R"""(template <>
-inline void printTagged(offload_ostream &os, const void *ptr, {0} value, size_t size) {{
+inline void printTagged(offload::detail::print_ostream &os, const void *ptr, {0} value, size_t size) {{
   if (ptr == NULL) {{
     printPtr(os, ptr);
     return;
@@ -101,8 +102,9 @@ inline void printTagged(offload_ostream &os, const void *ptr, {0} value, size_t
 
 static void EmitResultPrint(raw_ostream &OS) {
   OS << R""(
-inline offload_ostream &operator<<(offload_ostream &os,
-                                   const ol_error_struct_t *Err) {
+inline offload::detail::print_ostream &
+operator<<(offload::detail::print_ostream &os,
+           const ol_error_struct_t *Err) {
   if (Err == nullptr) {
     os << "OL_SUCCESS";
   } else {
@@ -120,7 +122,8 @@ static void EmitFunctionParamStructPrint(const FunctionRec &Func,
   }
 
   OS << formatv(R"(
-inline offload_ostream &operator<<(offload_ostream &os, const struct {0} *params) {{
+inline offload::detail::print_ostream &
+operator<<(offload::detail::print_ostream &os, const struct {0} *params) {{
 )",
                 Func.getParamStructName());
 
@@ -164,7 +167,8 @@ void ProcessStruct(const StructRec &Struct, raw_ostream &OS) {
   }
   OS << formatv(PrintTypeHeader, Struct.getName());
   OS << formatv(R"(
-inline offload_ostream &operator<<(offload_ostream &os, const struct {0} params) {{
+inline offload::detail::print_ostream &
+operator<<(offload::detail::print_ostream &os, const struct {0} params) {{
 )",
                 Struct.getName());
   OS << formatv(TAB_1 "os << \"(struct {0}){{\";\n", Struct.getName());
@@ -196,15 +200,19 @@ void EmitOffloadPrintHeader(const RecordKeeper &Records, raw_ostream &OS) {
 
 #ifdef OFFLOAD_PRINT_USE_STD_OSTREAM
 #include <ostream>
-using offload_ostream = std::ostream;
+namespace offload::detail {
+using print_ostream = std::ostream;
+}
 #else
 #include <llvm/Support/raw_ostream.h>
-using offload_ostream = llvm::raw_ostream;
+namespace offload::detail {
+using print_ostream = llvm::raw_ostream;
+}
 #endif
 
 
-template <typename T> inline ol_result_t printPtr(offload_ostream &os, const T *ptr);
-template <typename T> inline void printTagged(offload_ostream &os, const void *ptr, T value, size_t size);
+template <typename T> inline ol_result_t printPtr(offload::detail::print_ostream &os, const T *ptr);
+template <typename T> inline void printTagged(offload::detail::print_ostream &os, const void *ptr, T value, size_t size);
 )""";
 
   // ==========
@@ -222,12 +230,14 @@ template <typename T> inline void printTagged(offload_ostream &os, const void *p
   // use each other.
   OS << "\n";
   for (auto *R : Records.getAllDerivedDefinitions("Enum")) {
-    OS << formatv("inline offload_ostream &operator<<(offload_ostream &os, "
+    OS << formatv("inline offload::detail::print_ostream &"
+                  "operator<<(offload::detail::print_ostream &os, "
                   "enum {0} value);\n",
                   EnumRec{R}.getName());
   }
   for (auto *R : Records.getAllDerivedDefinitions("Struct")) {
-    OS << formatv("inline offload_ostream &operator<<(offload_ostream &os, "
+    OS << formatv("inline offload::detail::print_ostream &"
+                  "operator<<(offload::detail::print_ostream &os, "
                   "const struct {0} param);\n",
                   StructRec{R}.getName());
   }
@@ -253,7 +263,7 @@ template <typename T> inline void printTagged(offload_ostream &os, const void *p
   OS << R"""(
 ///////////////////////////////////////////////////////////////////////////////
 // @brief Print pointer value
-template <typename T> inline ol_result_t printPtr(offload_ostream &os, const T *ptr) {
+template <typename T> inline ol_result_t printPtr(offload::detail::print_ostream &os, const T *ptr) {
     if (ptr == nullptr) {
         os << "nullptr";
     } else if constexpr (std::is_pointer_v<T>) {



More information about the llvm-commits mailing list