Skip to content

WIP: [SYCL][PI] Make PI an independent library #1983

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
wants to merge 1 commit into from
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
49 changes: 41 additions & 8 deletions sycl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,43 @@ else()
)
endif()

if(MSVC)
set(shared_library_dirname "bin")
else()
set(shared_library_dirname "lib${LLVM_LIBDIR_SUFFIX}")
endif()

# Retrieves the name of the library
# For example:
# get_library_path(libname "OpenCL" "external" SHARED)
# would likely produce (depending on the system):
# set(libname "external/libOpenCL.so")
function(get_library_path out name directory library_type)
set(library_prefix "${CMAKE_${library_type}_LIBRARY_PREFIX}")
set(library_suffix "${CMAKE_${library_type}_LIBRARY_SUFFIX}")
if(NOT ("${directory}" STREQUAL ""))
set(directory "${directory}/")
endif()
set(${out}
"${directory}${library_prefix}${name}${library_suffix}" PARENT_SCOPE)
endfunction()

# Retrieves the name of the library needed when linking
# See get_library_path
function(get_link_library_path out name directory)
if(MSVC)
get_library_path(tmp_out "${name}" "${directory}" STATIC)
else()
get_library_path(tmp_out "${name}" "${directory}" SHARED)
endif()
set(${out} "${tmp_out}" PARENT_SCOPE)
endfunction()

if( NOT OpenCL_LIBRARIES )
message(STATUS "OpenCL_LIBRARIES is missing. Will try to download OpenCL ICD Loader from github.com")
get_link_library_path(OpenCL_LIBRARIES "OpenCL" "${LLVM_LIBRARY_OUTPUT_INTDIR}")
if(MSVC)
set(OpenCL_LIBRARIES
"${LLVM_LIBRARY_OUTPUT_INTDIR}/${CMAKE_STATIC_LIBRARY_PREFIX}OpenCL${CMAKE_STATIC_LIBRARY_SUFFIX}")
list(APPEND AUX_CMAKE_FLAGS -DOPENCL_ICD_LOADER_REQUIRE_WDK=OFF)
else()
set(OpenCL_LIBRARIES
"${LLVM_LIBRARY_OUTPUT_INTDIR}/${CMAKE_SHARED_LIBRARY_PREFIX}OpenCL${CMAKE_SHARED_LIBRARY_SUFFIX}")
endif()
if (CMAKE_C_COMPILER)
list(APPEND AUX_CMAKE_FLAGS -DCMAKE_C_COMPILER=${CMAKE_C_COMPILER})
Expand Down Expand Up @@ -260,6 +288,9 @@ if (LLVM_ENABLE_ASSERTIONS AND NOT SYCL_DISABLE_STL_ASSERTIONS)
endif()
endif()

include(piapi.cmake)
add_piapi_library()

set(SYCL_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# SYCL runtime library
Expand Down Expand Up @@ -311,9 +342,6 @@ 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 Expand Up @@ -362,6 +390,11 @@ if(SYCL_BUILD_PI_CUDA)
"CUDA support requires adding \"libclc\" to the CMake argument \"LLVM_ENABLE_PROJECTS\"")
endif()

find_package(CUDA 10.1 REQUIRED)

set_target_properties(pi_cuda PROPERTIES
INTERFACE_LINK_LIBRARIES cudadrv
)
add_dependencies(sycl-toolchain libspirv-builtins pi_cuda)
list(APPEND SYCL_TOOLCHAIN_DEPLOY_COMPONENTS libspirv-builtins pi_cuda)
endif()
Expand Down
13 changes: 12 additions & 1 deletion sycl/cmake/modules/AddSYCLExecutable.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ macro(add_sycl_executable ARG_TARGET_NAME)
cmake_parse_arguments(ARG
""
""
"OPTIONS;SOURCES;LIBRARIES;DEPENDANTS"
"OPTIONS;SOURCES;LIBRARIES;STATIC_LIBS;DEPENDANTS"
${ARGN})

set(CXX_COMPILER clang++)
Expand All @@ -11,13 +11,24 @@ macro(add_sycl_executable ARG_TARGET_NAME)
set(LIB_POSTFIX ".lib")
else()
set(LIB_PREFIX "-l")
set(SPLIT_LINK_PATH ON)
endif()
set(DEVICE_COMPILER_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/${CXX_COMPILER})

# TODO add support for target_link_libraries(... PUBLIC ...)
foreach(_lib ${ARG_LIBRARIES})
list(APPEND LINKED_LIBS "${LIB_PREFIX}${_lib}${LIB_POSTFIX}")
endforeach()
foreach(_lib ${ARG_STATIC_LIBS})
if(SPLIT_LINK_PATH)
# Note this has to be added separately so that CMake doesn't get confused
# by the space in between the two arguments
list(APPEND LINKED_LIBS "-L$<TARGET_FILE_DIR:${_lib}>")
list(APPEND LINKED_LIBS "-l:$<TARGET_FILE_NAME:${_lib}>")
else()
list(APPEND LINKED_LIBS $<TARGET_FILE:${_lib}>)
endif()
endforeach()

if (LLVM_ENABLE_ASSERTIONS AND NOT SYCL_DISABLE_STL_ASSERTIONS)
if(SYCL_USE_LIBCXX)
Expand Down
9 changes: 8 additions & 1 deletion sycl/cmake/modules/AddSYCLUnitTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,19 @@ macro(add_sycl_unittest_with_device test_dirname link_variant)
endif()

if ("${link_variant}" MATCHES "OBJECT")
# TODO piapi integration should be fixed,
# replace it with target_link_libraries(${test_dirname} PRIVATE piapi::piapi)
# once add_sycl_executable supports that
set(pi_include_dir "${sycl_inc_dir}/../piapi/include")
add_sycl_executable(${test_dirname}
OPTIONS -nolibsycl ${COMMON_OPTS} ${LLVM_PTHREAD_LIB} ${TERMINFO_LIB}
OPTIONS -nolibsycl ${COMMON_OPTS} ${LLVM_PTHREAD_LIB} ${TERMINFO_LIB} ${piapi_options} -I "${pi_include_dir}" -DPI_DPCPP_INTEGRATION
SOURCES ${ARGN} $<TARGET_OBJECTS:${sycl_obj_target}>
LIBRARIES gtest_main gtest LLVMSupport LLVMTestingSupport OpenCL ${EXTRA_LIBS}
STATIC_LIBS piapi
DEPENDANTS SYCLUnitTests)
add_dependencies(${test_dirname}_exec piapi)
else()
# TODO support shared library case.
endif()
#target_link_libraries(${test_dirname}_exec PRIVATE piapi::piapi)
endmacro()
6 changes: 3 additions & 3 deletions sycl/doc/PluginInterface.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ SYCL_PI_TRACE=-1 lists all PI Traces above and more debug messages.

#### Plugin binary interface
Plugins should implement all the Interface APIs required for the PI Version
it supports. There is [pi.def](../include/CL/sycl/detail/pi.def)/
[pi.h](../include/CL/sycl/detail/pi.h) file listing all PI API names that can be
it supports. There is [pi.def](../piapi/include/pi/pi.def)/
[pi.h](../piapi/include/pi/pi.h) file listing all PI API names that can be
called by the specific version of Plugin Interface.
It exports a function - "piPluginInit" that returns the plugin details and
function pointer table containing the list of pointers to implemented Interface
Expand Down Expand Up @@ -136,7 +136,7 @@ into
- **Interoperability API** which allows interoperability with underlying
runtimes such as OpenCL.

See [pi.h](../include/CL/sycl/detail/pi.h) header for the full list and
See [pi.h](../piapi/include/pi/pi.h) header for the full list and
descriptions of PI APIs.

### The Core OpenCL-based PI APIs
Expand Down
3 changes: 2 additions & 1 deletion sycl/include/CL/sycl/backend/cuda.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <CL/sycl/backend_types.hpp>
#include <CL/sycl/context.hpp>
#include <CL/sycl/detail/defines.hpp>
#include <CL/sycl/detail/pi_sycl.hpp>
#include <CL/sycl/device.hpp>
#include <CL/sycl/event.hpp>
#include <CL/sycl/queue.hpp>
Expand Down Expand Up @@ -58,4 +59,4 @@ struct interop<backend::cuda, accessor<DataT, Dimensions, AccessMode,
};

} // namespace sycl
} // namespace cl
} // __SYCL_INLINE_NAMESPACE(cl)
21 changes: 14 additions & 7 deletions sycl/include/CL/sycl/backend_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,14 @@
#pragma once

#include <CL/sycl/detail/defines.hpp>
#include <CL/sycl/detail/pi_sycl.hpp>

#include <fstream>
#include <iostream>
#include <istream>
#include <string>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {

enum class backend : char { host, opencl, level_zero, cuda, all };

template <backend name, typename SYCLObjectT> struct interop;

namespace pi {
inline std::ostream &operator<<(std::ostream &Out, backend be) {
switch (be) {
case backend::host:
Expand All @@ -41,6 +36,18 @@ inline std::ostream &operator<<(std::ostream &Out, backend be) {
}
return Out;
}
} // namespace pi

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {

using backend = pi::backend;

template <backend name, typename SYCLObjectT> struct interop;

inline std::ostream &operator<<(std::ostream &Out, backend be) {
return pi::operator<<(Out, be);
}

} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
18 changes: 13 additions & 5 deletions sycl/include/CL/sycl/detail/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,26 @@
#define __SYCL_STRINGIFY_LINE_HELP(s) #s
#define __SYCL_STRINGIFY_LINE(s) __SYCL_STRINGIFY_LINE_HELP(s)

#ifdef XPTI_ENABLE_INSTRUMENTATION
namespace pi {
// We define a sycl stream name and this will be used by the instrumentation
// framework
extern const char *SYCL_STREAM_NAME;
} // namespace pi
#endif // XPTI_ENABLE_INSTRUMENTATION

// Default signature enables the passing of user code location information to
// public methods as a default argument. If the end-user wants to disable the
// code location information, they must compile the code with
// -DDISABLE_SYCL_INSTRUMENTATION_METADATA flag
__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {
// We define a sycl stream name and this will be used by the instrumentation
// framework
constexpr const char *SYCL_STREAM_NAME = "sycl";
// Stream name being used for traces generated from the SYCL plugin layer
constexpr const char *SYCL_PICALL_STREAM_NAME = "sycl.pi";

#ifdef XPTI_ENABLE_INSTRUMENTATION
using pi::SYCL_STREAM_NAME;
#endif // XPTI_ENABLE_INSTRUMENTATION

// Data structure that captures the user code location information using the
// builtin capabilities of the compiler
struct code_location {
Expand Down
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/detail/device_binary_image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#pragma once

#include <CL/sycl/detail/os_util.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/detail/pi_sycl.hpp>

#include <memory>

Expand Down
43 changes: 12 additions & 31 deletions sycl/include/CL/sycl/detail/device_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,44 +11,16 @@
#include <CL/sycl/backend_types.hpp>
#include <CL/sycl/detail/defines.hpp>
#include <CL/sycl/info/info_desc.hpp>
#include <pi/device_filter.hpp>

#include <iostream>
#include <string>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

struct device_filter {
backend Backend = backend::all;
info::device_type DeviceType = info::device_type::all;
int DeviceNum = 0;
bool HasBackend = false;
bool HasDeviceType = false;
bool HasDeviceNum = false;
int MatchesSeen = 0;

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

class device_filter_list {
std::vector<device_filter> FilterList;

public:
device_filter_list() {}
device_filter_list(const std::string &FilterString);
device_filter_list(device_filter &Filter);
void addFilter(device_filter &Filter);
std::vector<device_filter> &get() { return FilterList; }
friend std::ostream &operator<<(std::ostream &Out,
const device_filter_list &List);
};
namespace pi {

inline std::ostream &operator<<(std::ostream &Out,
const device_filter &Filter) {
namespace info = cl::sycl::info;
Out << Filter.Backend << ":";
if (Filter.DeviceType == info::device_type::host) {
Out << "host";
Expand Down Expand Up @@ -78,6 +50,15 @@ inline std::ostream &operator<<(std::ostream &Out,
return Out;
}

} // namespace pi

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

using pi::device_filter;
using pi::device_filter_list;

} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/detail/helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <CL/sycl/access/access.hpp>
#include <CL/sycl/detail/common.hpp>
#include <CL/sycl/detail/export.hpp>
#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/detail/pi_sycl.hpp>
#include <CL/sycl/detail/type_traits.hpp>

#if __cpp_lib_bit_cast
Expand Down
29 changes: 29 additions & 0 deletions sycl/include/CL/sycl/detail/pi_sycl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//==---------------- pi_sycl.hpp - SYCL wrapper for PI ---------*- C++ -*---==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// ===--------------------------------------------------------------------=== //

#pragma once

#include <CL/sycl/detail/defines.hpp>
#include <pi/pi.hpp>

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {

namespace RT = ::pi;

namespace detail {

namespace RT = ::pi;
using PiApiKind = ::PiApiKind;
namespace pi {
using namespace ::pi;
}

} // namespace detail
} // namespace sycl
} // __SYCL_INLINE_NAMESPACE(cl)
2 changes: 1 addition & 1 deletion sycl/include/CL/sycl/detail/sycl_mem_obj_i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#pragma once

#include <CL/sycl/detail/pi.hpp>
#include <CL/sycl/detail/pi_sycl.hpp>
#include <CL/sycl/stl.hpp>

__SYCL_INLINE_NAMESPACE(cl) {
Expand Down
7 changes: 5 additions & 2 deletions sycl/include/CL/sycl/detail/sycl_mem_obj_t.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@
#include <cstring>
#include <type_traits>

namespace pi {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, define internal things inside cl::sycl::detail namespace.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The PI library needs to be independent of SYCL, this code is forward declaring some PI members.

Copy link
Contributor

Choose a reason for hiding this comment

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

So, then please, make sure this header is not directly or indirectly included when the user includes sycl.hpp.

class plugin;
}

__SYCL_INLINE_NAMESPACE(cl) {
namespace sycl {
namespace detail {

// Forward declarations
class context_impl;
class event_impl;
class plugin;

using ContextImplPtr = shared_ptr_class<context_impl>;
using EventImplPtr = shared_ptr_class<event_impl>;
Expand Down Expand Up @@ -86,7 +89,7 @@ class __SYCL_EXPORT SYCLMemObjT : public SYCLMemObjI {

virtual ~SYCLMemObjT() = default;

const plugin &getPlugin() const;
const pi::plugin &getPlugin() const;

__SYCL_DLL_LOCAL size_t getSize() const override { return MSizeInBytes; }
__SYCL_DLL_LOCAL size_t get_count() const {
Expand Down
Loading