Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SYCL] Don't include iostream from sycl.hpp #11373

Open
wants to merge 22 commits into
base: sycl
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions sycl/doc/developer/ContributeToDPCPP.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ to signify the component changed, e.g.: `[PI]`, `[CUDA]`, `[Doc]`.
According to
[LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html#include-iostream-is-forbidden),
the use `#include <iostream>` is forbidden in library files. Instead, the
sycl/detail/iostream_proxy.hpp header offers the functionality of <iostream>
detail/iostream_proxy.hpp header offers the functionality of <iostream>
without its static constructor.
This header should be used in place of <iostream> in DPC++ headers and runtime
library files.
This header should be used in place of <iostream> in DPC++ runtime
library files. In DPC++ header files only <iosfwd> should be used.

## Tests development

Expand Down
32 changes: 3 additions & 29 deletions sycl/include/sycl/backend_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#include <sycl/detail/defines_elementary.hpp> // for __SYCL2020_DEPRECATED

#include <ostream> // for operator<<, ostream
#include <iosfwd> // for operator<<, ostream
#include <sycl/detail/export.hpp>

namespace sycl {
inline namespace _V1 {
Expand All @@ -36,34 +37,7 @@ template <backend Backend, typename SYCLObjectT>
using backend_return_t =
typename backend_traits<Backend>::template return_type<SYCLObjectT>;

inline std::ostream &operator<<(std::ostream &Out, backend be) {
switch (be) {
case backend::host:
Out << "host";
break;
case backend::opencl:
Out << "opencl";
break;
case backend::ext_oneapi_level_zero:
Out << "ext_oneapi_level_zero";
break;
case backend::ext_oneapi_cuda:
Out << "ext_oneapi_cuda";
break;
case backend::ext_intel_esimd_emulator:
Out << "ext_intel_esimd_emulator";
break;
case backend::ext_oneapi_hip:
Out << "ext_oneapi_hip";
break;
case backend::ext_native_cpu:
Out << "ext_native_cpu";
break;
case backend::all:
Out << "all";
}
return Out;
}
__SYCL_EXPORT std::ostream &operator<<(std::ostream &Out, backend be);

} // namespace _V1
} // namespace sycl
9 changes: 5 additions & 4 deletions sycl/include/sycl/bit_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
#include <version> // defines __cpp_lib_bit_cast
#endif

#include <sycl/detail/defines_elementary.hpp> // __has_builtin

#if __cpp_lib_bit_cast
#include <bit>
#elif !defined(__has_builtin) || !__has_builtin(__builtin_bit_cast)
#elif !__has_builtin(__builtin_bit_cast)
#include <sycl/detail/memcpy.hpp>
#endif

namespace sycl {
inline namespace _V1 {

template <typename To, typename From>
#if __cpp_lib_bit_cast || \
(defined(__has_builtin) && __has_builtin(__builtin_bit_cast))
#if __cpp_lib_bit_cast || __has_builtin(__builtin_bit_cast)
constexpr
#endif
std::enable_if_t<sizeof(To) == sizeof(From) &&
Expand All @@ -37,7 +38,7 @@ constexpr
return std::bit_cast<To>(from);
#else // __cpp_lib_bit_cast

#if defined(__has_builtin) && __has_builtin(__builtin_bit_cast)
#if __has_builtin(__builtin_bit_cast)
return __builtin_bit_cast(To, from);
#else // __has_builtin(__builtin_bit_cast)
static_assert(std::is_trivially_default_constructible<To>::value,
Expand Down
36 changes: 24 additions & 12 deletions sycl/include/sycl/detail/accessor_iterator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#include <sycl/id.hpp> // for id

#include <cstddef> // for size_t
#include <iosfwd> // for operator<<, ostream, ptrdiff_t
#include <iterator> // for random_access_iterator_tag
#include <ostream> // for operator<<, ostream, ptrdiff_t

/// \file accessor_iterator.hpp
/// The file contains implementation of accessor iterator class.
Expand Down Expand Up @@ -45,6 +45,10 @@ class accessor;

namespace detail {

struct accessor_iterator_data;
__SYCL_EXPORT std::ostream &operator<<(std::ostream &os,
const accessor_iterator_data &it);

template <typename DataT, int Dimensions> class accessor_iterator {
public:
using difference_type = std::ptrdiff_t;
Expand Down Expand Up @@ -331,22 +335,30 @@ template <typename DataT, int Dimensions> class accessor_iterator {
#ifndef NDEBUG
// Could be useful for debugging, but not a part of the official API,
// therefore only available in builds with assertions enabled.
friend struct accessor_iterator_data;
friend std::ostream &operator<<(std::ostream &os,
const accessor_iterator &it) {
os << "accessor_iterator {\n";
os << "\tMLinearId: " << it.MLinearId << "\n";
os << "\tMEnd: " << it.MEnd << "\n";
os << "\tMStaticOffset: " << it.MStaticOffset << "\n";
os << "\tMPerRowOffset: " << it.MPerRowOffset << "\n";
os << "\tMPerSliceOffset: " << it.MPerSliceOffset << "\n";
os << "\tMRowSize: " << it.MRowSize << "\n";
os << "\tMSliceSize: " << it.MSliceSize << "\n";
os << "\tMAccessorIsRanged: " << it.MAccessorIsRanged << "\n";
os << "}";
return os;
return os << accessor_iterator_data(it);
}
#endif // NDEBUG
};
// non-templated copy of accessor_iterator to pass to library
struct accessor_iterator_data {
template <typename DataT, int Dimensions>
accessor_iterator_data(accessor_iterator<DataT, Dimensions> it)
: MLinearId(it.MLinearId), MEnd(it.MEnd), MStaticOffset(it.MStaticOffset),
MPerSliceOffset(it.MPerSliceOffset), MPerRowOffset(it.MPerRowOffset),
MRowSize(it.MRowSize), MSliceSize(it.MSliceSize),
MAccessorIsRanged(it.MAccessorIsRanged) {}
size_t MLinearId;
size_t MEnd;
size_t MStaticOffset;
size_t MPerSliceOffset;
size_t MPerRowOffset;
size_t MRowSize;
size_t MSliceSize;
bool MAccessorIsRanged;
};
} // namespace detail
} // namespace _V1
} // namespace sycl
6 changes: 3 additions & 3 deletions sycl/include/sycl/detail/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,14 +173,14 @@ inline std::string codeToString(pi_int32 code) {
"Native API returns: "

#ifndef __SYCL_SUPPRESS_PI_ERROR_REPORT
#include <sycl/detail/iostream_proxy.hpp>
// TODO: rename all names with direct use of OCL/OPENCL to be backend agnostic.
#define __SYCL_REPORT_PI_ERR_TO_STREAM(expr) \
{ \
auto code = expr; \
if (code != PI_SUCCESS) { \
std::cerr << __SYCL_PI_ERROR_REPORT << sycl::detail::codeToString(code) \
<< std::endl; \
fprintf(stderr, __SYCL_PI_ERROR_REPORT "%s\n", \
sycl::detail::codeToString(code).c_str()); \
fflush(stderr); \
} \
}
#endif
Expand Down
46 changes: 5 additions & 41 deletions sycl/include/sycl/detail/device_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
#include <sycl/detail/defines.hpp>
#include <sycl/info/info_desc.hpp>

#include <iosfwd>
#include <optional>
#include <ostream>
#include <string>

namespace sycl {
Expand All @@ -23,11 +23,6 @@ namespace detail {
// ---------------------------------------
// ONEAPI_DEVICE_SELECTOR support

template <typename T>
std::ostream &operator<<(std::ostream &os, std::optional<T> const &opt) {
return opt ? os << opt.value() : os << "not set ";
}

// the ONEAPI_DEVICE_SELECTOR string gets broken down into these targets
// will will match devices. If the target is negative, such as !opencl:*
// then matching devices will not be made available to the user.
Expand Down Expand Up @@ -77,8 +72,8 @@ struct device_filter {

device_filter(){};
device_filter(const std::string &FilterString);
friend std::ostream &operator<<(std::ostream &Out,
const device_filter &Filter);
__SYCL_EXPORT friend std::ostream &operator<<(std::ostream &Out,
const device_filter &Filter);
};

class device_filter_list {
Expand All @@ -93,41 +88,10 @@ class device_filter_list {
bool backendCompatible(backend Backend);
bool deviceTypeCompatible(info::device_type DeviceType);
bool deviceNumberCompatible(int DeviceNum);
friend std::ostream &operator<<(std::ostream &Out,
const device_filter_list &List);
__SYCL_EXPORT friend std::ostream &operator<<(std::ostream &Out,
const device_filter_list &List);
};

inline std::ostream &operator<<(std::ostream &Out,
const device_filter &Filter) {
Out << Filter.Backend << ":";
if (Filter.DeviceType == info::device_type::host) {
Out << "host";
} else if (Filter.DeviceType == info::device_type::cpu) {
Out << "cpu";
} else if (Filter.DeviceType == info::device_type::gpu) {
Out << "gpu";
} else if (Filter.DeviceType == info::device_type::accelerator) {
Out << "accelerator";
} else if (Filter.DeviceType == info::device_type::all) {
Out << "*";
} else {
Out << "unknown";
}
if (Filter.DeviceNum) {
Out << ":" << Filter.DeviceNum.value();
}
return Out;
}

inline std::ostream &operator<<(std::ostream &Out,
const device_filter_list &List) {
for (const device_filter &Filter : List.FilterList) {
Out << Filter;
Out << ",";
}
return Out;
}

} // namespace detail
} // namespace _V1
} // namespace sycl
17 changes: 0 additions & 17 deletions sycl/include/sycl/detail/pi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

#include <cstdint> // for uint64_t, uint32_t
#include <memory> // for shared_ptr
#include <sstream> // for operator<<, basic_ostream, string...
#include <stddef.h> // for size_t
#include <string> // for char_traits, string
#include <type_traits> // for false_type, true_type
Expand Down Expand Up @@ -108,22 +107,6 @@ bool trace(TraceLevel level);

__SYCL_EXPORT void assertion(bool Condition, const char *Message = nullptr);

template <typename T>
void handleUnknownParamName(const char *functionName, T parameter) {
std::stringstream stream;
stream << "Unknown parameter " << parameter << " passed to " << functionName
<< "\n";
auto str = stream.str();
auto msg = str.c_str();
die(msg);
}

// This macro is used to report invalid enumerators being passed to PI API
// GetInfo functions. It will print the name of the function that invoked it
// and the value of the unknown enumerator.
#define __SYCL_PI_HANDLE_UNKNOWN_PARAM_NAME(parameter) \
{ sycl::detail::pi::handleUnknownParamName(__func__, parameter); }

using PiPlugin = ::pi_plugin;
using PiResult = ::pi_result;
using PiPlatform = ::pi_platform;
Expand Down
20 changes: 3 additions & 17 deletions sycl/include/sycl/exception_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@

// 4.9.2 Exception Class Interface

#include <sycl/detail/export.hpp> // for __SYCL_EXPORT
#include <sycl/detail/iostream_proxy.hpp> // for cerr
#include <sycl/detail/export.hpp> // for __SYCL_EXPORT

#include <cstddef> // for size_t
#include <exception> // for exception_ptr, exception
#include <ostream> // for operator<<, basic_ostream
#include <iosfwd> // for operator<<, basic_ostream
#include <vector> // for vector

namespace sycl {
Expand Down Expand Up @@ -55,20 +54,7 @@ class __SYCL_EXPORT exception_list {
namespace detail {
// Default implementation of async_handler used by queue and context when no
// user-defined async_handler is specified.
inline void defaultAsyncHandler(exception_list Exceptions) {
std::cerr << "Default async_handler caught exceptions:";
for (auto &EIt : Exceptions) {
try {
if (EIt) {
std::rethrow_exception(EIt);
}
} catch (const std::exception &E) {
std::cerr << "\n\t" << E.what();
}
}
std::cerr << std::endl;
std::terminate();
}
__SYCL_EXPORT void defaultAsyncHandler(exception_list Exceptions);
} // namespace detail
} // namespace _V1
} // namespace sycl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

#include <sycl/half_type.hpp>

#include <istream>
#include <ostream>

/// @cond ESIMD_DETAIL

namespace sycl {
Expand Down
6 changes: 2 additions & 4 deletions sycl/include/sycl/ext/intel/esimd/simd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

#pragma once

#include <ostream>

#include <sycl/ext/intel/esimd/detail/simd_mask_impl.hpp>
#include <sycl/ext/intel/esimd/detail/simd_obj_impl.hpp>

Expand All @@ -21,10 +23,6 @@

#include <sycl/ext/oneapi/experimental/invoke_simd.hpp>

#ifndef __SYCL_DEVICE_ONLY__
#include <sycl/detail/iostream_proxy.hpp>
#endif // __SYCL_DEVICE_ONLY__

namespace sycl {
inline namespace _V1 {
namespace ext::intel::esimd {
Expand Down
Loading