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

via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 01:46:35 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: Jan Trusiłło (311Volt)

<details>
<summary>Changes</summary>

Allow generated `OffloadPrint.hpp` to use `std::ostream` instead of `llvm::raw_ostream`, no longer forcing a dependency on LLVM Support.

If `OFFLOAD_PRINT_USE_STD_OSTREAM` is defined before including OffloadPrint.hpp, the standard library header `<ostream>` will be transitively included instead of `<llvm/Support/raw_ostream.h>`, and the type alias used by all generated functions will resolve to `std::ostream&`.

To not break existing code, the mechanism is opt-in. Without the `#define`, the header behaves as before.

Assisted-by: Codex

---
Full diff: https://github.com/llvm/llvm-project/pull/222269.diff


5 Files Affected:

- (modified) offload/test/tools/offload-tblgen/functions_ranged_param.td (+1-1) 
- (modified) offload/test/tools/offload-tblgen/print_enum.td (+10-1) 
- (modified) offload/test/tools/offload-tblgen/print_function.td (+1-1) 
- (modified) offload/test/tools/offload-tblgen/type_tagged_enum.td (+2-2) 
- (modified) offload/tools/offload-tblgen/PrintGen.cpp (+21-13) 


``````````diff
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>) {

``````````

</details>


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


More information about the llvm-commits mailing list