Skip to content

[SYCL] Plugin Interface Changes to query a list of function pointers. #731

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

Closed
Closed
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
2 changes: 2 additions & 0 deletions sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,12 @@ install(DIRECTORY "${sycl_inc_dir}/." DESTINATION "${LLVM_INST_INC_DIRECTORY}" C

# SYCL runtime library
add_subdirectory(source)
add_subdirectory(plugins)

# SYCL toolchain builds all components: compiler, libraries, headers, etc.
add_custom_target( sycl-toolchain
DEPENDS sycl
pi_opencl
clang
clang-offload-wrapper
clang-offload-bundler
Expand Down
275 changes: 141 additions & 134 deletions sycl/include/CL/sycl/detail/pi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,169 +13,176 @@
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/os_util.hpp>
#include <CL/sycl/detail/pi.h>
#include <cassert>
#include <string>

// Function to load the shared library
// Implementation is OS dependent.
void *loadOsLibrary(const std::string &library);

// Function to get Address of a symbol defined in the shared
// library, Implementation is OS dependent.
void *getOsLibraryFuncAddress(void *Library, const std::string &FunctionName);

namespace cl {
namespace sycl {
namespace detail {
namespace pi {
// For selection of SYCL RT back-end, now manually through the "SYCL_BE"
// environment variable.
//
enum Backend {
SYCL_BE_PI_OPENCL,
SYCL_BE_PI_OTHER
};

// Check for manually selected BE at run-time.
bool useBackend(Backend Backend);

using PiResult = ::pi_result;
using PiPlatform = ::pi_platform;
using PiDevice = ::pi_device;
using PiDeviceType = ::pi_device_type;
using PiDeviceInfo = ::pi_device_info;
using PiDeviceBinaryType = ::pi_device_binary_type;
using PiContext = ::pi_context;
using PiProgram = ::pi_program;
using PiKernel = ::pi_kernel;
using PiQueue = ::pi_queue;
using PiQueueProperties = ::pi_queue_properties;
using PiMem = ::pi_mem;
using PiMemFlags = ::pi_mem_flags;
using PiEvent = ::pi_event;
using PiSampler = ::pi_sampler;
using PiSamplerInfo = ::pi_sampler_info;
using PiSamplerProperties = ::pi_sampler_properties;
using PiSamplerAddressingMode = ::pi_sampler_addressing_mode;
using PiSamplerFilterMode = ::pi_sampler_filter_mode;
using PiMemImageFormat = ::pi_image_format;
using PiMemImageDesc = ::pi_image_desc;
using PiMemImageInfo = ::pi_image_info;
using PiMemObjectType = ::pi_mem_type;
using PiMemImageChannelOrder = ::pi_image_channel_order;
using PiMemImageChannelType = ::pi_image_channel_type;

// Get a string representing a _pi_platform_info enum
std::string platformInfoToString(pi_platform_info info);

// Report error and no return (keeps compiler happy about no return statements).
[[noreturn]] void die(const char *Message);
void assertion(bool Condition, const char *Message = nullptr);

// Want all the needed casts be explicit, do not define conversion operators.
template<class To, class From>
To cast(From value);

// Forward declarations of the PI dispatch entries.
// For selection of SYCL RT back-end, now manually through the "SYCL_BE"
// environment variable.
//
enum Backend { SYCL_BE_PI_OPENCL, SYCL_BE_PI_OTHER };

#ifdef SYCL_RT_OS_WINDOWS
#define PLUGIN_NAME "pi_opencl.dll"
#else
#define PLUGIN_NAME "libpi_opencl.so"
#endif

// Check for manually selected BE at run-time.
bool useBackend(Backend Backend);

using PiResult = ::pi_result;
using PiPlatform = ::pi_platform;
using PiDevice = ::pi_device;
using PiDeviceType = ::pi_device_type;
using PiDeviceInfo = ::pi_device_info;
using PiDeviceBinaryType = ::pi_device_binary_type;
using PiContext = ::pi_context;
using PiProgram = ::pi_program;
using PiKernel = ::pi_kernel;
using PiQueue = ::pi_queue;
using PiQueueProperties = ::pi_queue_properties;
using PiMem = ::pi_mem;
using PiMemFlags = ::pi_mem_flags;
using PiEvent = ::pi_event;
using PiSampler = ::pi_sampler;
using PiSamplerInfo = ::pi_sampler_info;
using PiSamplerProperties = ::pi_sampler_properties;
using PiSamplerAddressingMode = ::pi_sampler_addressing_mode;
using PiSamplerFilterMode = ::pi_sampler_filter_mode;
using PiMemImageFormat = ::pi_image_format;
using PiMemImageDesc = ::pi_image_desc;
using PiMemImageInfo = ::pi_image_info;
using PiMemObjectType = ::pi_mem_type;
using PiMemImageChannelOrder = ::pi_image_channel_order;
using PiMemImageChannelType = ::pi_image_channel_type;

// Get a string representing a _pi_platform_info enum
std::string platformInfoToString(pi_platform_info info);

// Report error and no return (keeps compiler happy about no return statements).
[[noreturn]] void die(const char *Message);
void assertion(bool Condition, const char *Message = nullptr);

// Want all the needed casts be explicit, do not define conversion operators.
template <class To, class From> To cast(From value);

// Forward declarations of the PI dispatch entries.
#define _PI_API(api) __SYCL_EXPORTED extern decltype(::api) *(api);
#include <CL/sycl/detail/pi.def>

// Performs PI one-time initialization.
void initialize();

// The PiCall helper structure facilitates performing a call to PI.
// It holds utilities to do the tracing and to check the returned result.
// TODO: implement a more mature and controllable tracing of PI calls.
class PiCall {
PiResult m_Result;
static bool m_TraceEnabled;

public:
explicit PiCall(const char *Trace = nullptr);
~PiCall();
PiResult get(PiResult Result);
template<typename Exception>
void check(PiResult Result);
};

// The run-time tracing of PI calls.
// TODO: replace PiCall completely with this one (PiTrace)
//
template <typename T> inline
void print(T val) {
std::cout << "<unknown> : " << val;
}
// Performs PI one-time initialization.
void initialize();

// The PiCall helper structure facilitates performing a call to PI.
// It holds utilities to do the tracing and to check the returned result.
// TODO: implement a more mature and controllable tracing of PI calls.
class PiCall {
PiResult m_Result;
static bool m_TraceEnabled;

public:
explicit PiCall(const char *Trace = nullptr);
~PiCall();
PiResult get(PiResult Result);
template <typename Exception> void check(PiResult Result);
};

// The run-time tracing of PI calls.
// TODO: replace PiCall completely with this one (PiTrace)
//
template <typename T> inline void print(T val) {
std::cout << "<unknown> : " << val;
}

template<> inline void print<> (PiPlatform val) { std::cout << "pi_platform : " << val; }
template<> inline void print<> (PiResult val) {
std::cout << "pi_result : ";
if (val == PI_SUCCESS)
std::cout << "PI_SUCCESS";
else
std::cout << val;
}

inline void printArgs(void) {}
template <typename Arg0, typename... Args>
void printArgs(Arg0 arg0, Args... args) {
std::cout << std::endl << " ";
print(arg0);
printArgs(std::forward<Args>(args)...);
template <> inline void print<>(PiPlatform val) {
std::cout << "pi_platform : " << val;
}
template <> inline void print<>(PiResult val) {
std::cout << "pi_result : ";
if (val == PI_SUCCESS)
std::cout << "PI_SUCCESS";
else
std::cout << val;
}

inline void printArgs(void) {}
template <typename Arg0, typename... Args>
void printArgs(Arg0 arg0, Args... args) {
std::cout << std::endl << " ";
print(arg0);
printArgs(std::forward<Args>(args)...);
}

template <typename FnType> class Trace {
private:
FnType m_FnPtr;
static bool m_TraceEnabled;

public:
Trace(FnType FnPtr, const std::string &FnName) : m_FnPtr(FnPtr) {
if (m_TraceEnabled)
std::cout << "---> " << FnName << "(";
}

template <typename FnType>
class Trace {
private:
FnType m_FnPtr;
static bool m_TraceEnabled;
public:
Trace(FnType FnPtr, const std::string &FnName) : m_FnPtr(FnPtr) {
if (m_TraceEnabled)
std::cout << "---> " << FnName << "(";
}

template <typename... Args>
typename std::result_of<FnType(Args...)>::type
operator() (Args... args) {
if (m_TraceEnabled)
printArgs(args...);

initialize();
auto r = m_FnPtr(args...);

if (m_TraceEnabled) {
std::cout << ") ---> ";
std::cout << (print(r),"") << "\n";
}
return r;

template <typename... Args>
typename std::result_of<FnType(Args...)>::type operator()(Args... args) {
if (m_TraceEnabled)
printArgs(args...);

initialize();
auto r = m_FnPtr(args...);

if (m_TraceEnabled) {
std::cout << ") ---> ";
std::cout << (print(r), "") << "\n";
}
};
return r;
}
};

template <typename FnType>
bool Trace<FnType>::m_TraceEnabled = (std::getenv("SYCL_PI_TRACE") != nullptr);
template <typename FnType>
bool Trace<FnType>::m_TraceEnabled = (std::getenv("SYCL_PI_TRACE") != nullptr);

} // namespace pi

namespace RT = cl::sycl::detail::pi;

#define PI_ASSERT(cond, msg) \
RT::assertion((cond), "assert: " msg);
#define PI_ASSERT(cond, msg) RT::assertion((cond), "assert: " msg);

#define PI_TRACE(func) RT::Trace<decltype(func)>(func, #func)

// This does the call, the trace and the check for no errors.
#define PI_CALL(pi) \
RT::initialize(), \
RT::PiCall(#pi).check<cl::sycl::runtime_error>( \
RT::cast<detail::RT::PiResult>(pi))
#define PI_CALL(pi) \
RT::initialize(), RT::PiCall(#pi).check<cl::sycl::runtime_error>( \
RT::cast<detail::RT::PiResult>(pi))

// This does the trace, the call, and returns the result
#define PI_CALL_RESULT(pi) \
RT::PiCall(#pi).get(detail::RT::cast<detail::RT::PiResult>(pi))
#define PI_CALL_RESULT(pi) \
RT::PiCall(#pi).get(detail::RT::cast<detail::RT::PiResult>(pi))

// This does the check for no errors and possibly throws
#define PI_CHECK(pi) \
RT::PiCall().check<cl::sycl::runtime_error>( \
RT::cast<detail::RT::PiResult>(pi))
#define PI_CHECK(pi) \
RT::PiCall().check<cl::sycl::runtime_error>( \
RT::cast<detail::RT::PiResult>(pi))

// This does the check for no errors and possibly throws x
#define PI_CHECK_THROW(pi, x) \
RT::PiCall().check<x>( \
RT::cast<detail::RT::PiResult>(pi))
#define PI_CHECK_THROW(pi, x) \
RT::PiCall().check<x>(RT::cast<detail::RT::PiResult>(pi))

// Want all the needed casts be explicit, do not define conversion operators.
template<class To, class From>
To pi::cast(From value) {
template <class To, class From> To pi::cast(From value) {
// TODO: see if more sanity checks are possible.
PI_ASSERT(sizeof(From) == sizeof(To), "cast failed size check");
return (To)(value);
Expand Down
Loading