Skip to content

[SYCL] Plugin Interface And Creation of OpenCL Plugin. #708

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

Merged
merged 1 commit into from
Nov 8, 2019
Merged
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
35 changes: 34 additions & 1 deletion sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,37 @@ if (MSVC)
list(APPEND SYCL_RT_LIBS sycld)
endif()

# This function allows building multiple libraries with the same options.
# Currently used by sycl and plugins library.
# Currently handles linking with libcxx support and gcc workaround
function( add_common_options LIB_NAME)
if (SYCL_USE_LIBCXX)
if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
(CMAKE_CXX_COMPILER_ID STREQUAL "Clang"))
target_compile_options(${LIB_NAME} PRIVATE -nostdinc++)
if ((NOT (DEFINED SYCL_LIBCXX_INCLUDE_PATH)) OR (NOT (DEFINED SYCL_LIBCXX_LIBRARY_PATH)))
message(FATAL_ERROR "When building with libc++ SYCL_LIBCXX_INCLUDE_PATHS and"
"SYCL_LIBCXX_LIBRARY_PATH should be set")
endif()
target_include_directories(${LIB_NAME} PRIVATE "${SYCL_LIBCXX_INCLUDE_PATH}")
target_link_libraries(${LIB_NAME} PRIVATE "-L${SYCL_LIBCXX_LIBRARY_PATH}" -nodefaultlibs -lc++ -lc++abi -lm -lc -lgcc_s -lgcc)
else()
message(FATAL_ERROR "Build with libc++ is not yet supported for this compiler")
endif()
else()

# Workaround for bug in GCC version 5 and higher.
# More information https://bugs.launchpad.net/ubuntu/+source/gcc-5/+bug/1568899
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND
CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 5.0)
target_link_libraries(${LIB_NAME} PRIVATE gcc_s gcc)
endif()

endif()
endfunction(add_common_options)

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

# SYCL toolchain builds all components: compiler, libraries, headers, etc.
add_custom_target( sycl-toolchain
Expand Down Expand Up @@ -173,6 +202,10 @@ option(SYCL_INCLUDE_TESTS
"Generate build targets for the SYCL unit tests."
${LLVM_INCLUDE_TESTS})


# Plugin Library
add_subdirectory( plugins )

add_subdirectory(tools)

if(SYCL_INCLUDE_TESTS)
Expand Down
276 changes: 142 additions & 134 deletions sycl/include/CL/sycl/detail/pi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,168 +14,176 @@
#include <CL/sycl/detail/os_util.hpp>
#include <CL/sycl/detail/pi.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add empty line before system headers.


#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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if the LLVM Support class DynamicLibrary could be used here - though there might be unintended interaction with the libraries loaded and globally stored by a LLVM runtime compiler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth a further investigation, but it seems to have its own semantics like unloading only at process end, or searching symbol by name in all loaded libraries, and not the specific one, which isn't suitable for our needs here.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, too much baggage and unwanted behavior.


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
1 change: 1 addition & 0 deletions sycl/plugins/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(opencl)
Loading