Skip to content

Commit b22192a

Browse files
[SYCL] Add platform enumeration and info query using liboffload (#2)
This is part of the SYCL support upstreaming effort. The relevant RFCs can be found here: https://discourse.llvm.org/t/rfc-add-full-support-for-the-sycl-programming-model/74080 https://discourse.llvm.org/t/rfc-sycl-runtime-upstreaming/74479 The SYCL runtime is device-agnostic and uses liboffload for offloading to GPU. This commit adds a dependency on liboffload, implementation of platform::get_platforms, platform::get_backend and platform::get_info methods, initial implementation of sycl-ls tool for manual testing of added functionality. Plan for next PR: device/context impl, rest of platform test infrastructure (depends on L0 liboffload plugin CI, our effort is joined) ABI tests
1 parent d07a4fe commit b22192a

27 files changed

+1292
-31
lines changed

libsycl/CMakeLists.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ option(LIBSYCL_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF)
3737

3838
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
3939

40-
set(LIBSYCL_SHARED_OUTPUT_NAME "sycl" CACHE STRING "Output name for the shared libsycl runtime library.")
41-
4240
if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
4341
set(LIBSYCL_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
4442
if(LIBSYCL_LIBDIR_SUBDIR)
@@ -65,7 +63,7 @@ set(LIBSYCL_SOURCE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
6563

6664
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBSYCL_LIBRARY_DIR})
6765
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBSYCL_LIBRARY_DIR})
68-
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBSYCL_LIBRARY_DIR})
66+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_TOOLS_BINARY_DIR})
6967

7068
set(LIBSYCL_MAJOR_VERSION 0)
7169
set(LIBSYCL_MINOR_VERSION 1)
@@ -117,10 +115,22 @@ add_custom_command(
117115
install(DIRECTORY "${LIBSYCL_SOURCE_INCLUDE_DIR}/sycl" DESTINATION ${LIBSYCL_INCLUDE_DIR} COMPONENT sycl-headers)
118116
install(DIRECTORY "${LIBSYCL_SOURCE_INCLUDE_DIR}/CL" DESTINATION ${LIBSYCL_INCLUDE_DIR} COMPONENT sycl-headers)
119117

120-
set(LIBSYCL_RT_LIBS ${LIBSYCL_SHARED_OUTPUT_NAME})
118+
set(LIBSYCL_LIB_NAME "sycl")
119+
set(LIBSYCL_SHARED_OUTPUT_NAME "${LIBSYCL_LIB_NAME}")
120+
if (CMAKE_SYSTEM_NAME STREQUAL Windows)
121+
if (CMAKE_MSVC_RUNTIME_LIBRARY AND (NOT CMAKE_MSVC_RUNTIME_LIBRARY MATCHES "DLL$"))
122+
message(FATAL_ERROR "libsycl requires a DLL version of the MSVC CRT.")
123+
endif()
124+
if ((NOT CMAKE_MSVC_RUNTIME_LIBRARY AND uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
125+
OR (CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDebugDLL"))
126+
set(LIBSYCL_SHARED_OUTPUT_NAME "${LIBSYCL_SHARED_OUTPUT_NAME}d")
127+
endif()
128+
endif()
121129

122130
add_subdirectory(src)
123131

132+
set(LIBSYCL_RT_LIBS ${LIBSYCL_SHARED_OUTPUT_NAME})
124133
add_custom_target(libsycl-runtime-libraries
125134
DEPENDS ${LIBSYCL_RT_LIBS}
126135
)
136+
add_subdirectory(tools)

libsycl/docs/index.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,17 @@ To build LLVM with libsycl runtime enabled the following script can be used.
6969
mkdir -p $installprefix
7070
7171
cmake -G Ninja -S $llvm/llvm -B $build_llvm \
72-
-DLLVM_ENABLE_PROJECTS="clang;clang-tools-extra" \
72+
-DLLVM_ENABLE_PROJECTS="clang" \
7373
-DLLVM_INSTALL_UTILS=ON \
7474
-DCMAKE_INSTALL_PREFIX=$installprefix \
75-
-DLLVM_ENABLE_RUNTIMES="libcxx;libcxxabi;libsycl;libunwind" \
75+
-DLLVM_ENABLE_RUNTIMES="offload;openmp;libsycl" \
7676
-DCMAKE_BUILD_TYPE=Release
7777
7878
ninja -C $build_llvm install
79-
79+
80+
81+
Limitations
82+
========
83+
84+
SYCL runtime is not tested and is not guaranteed to work on Windows because offloading runtime (liboffload) used by SYCL runtime doesn't currently support Windows.
85+
The limitation to be revised once liboffload will add support for Windows.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains the declaration of the SYCL enum class backend that is
11+
/// implementation-defined and is populated with a unique identifier for each
12+
/// SYCL backend that the SYCL implementation can support.
13+
///
14+
//===----------------------------------------------------------------------===//
15+
16+
#ifndef _LIBSYCL___IMPL_BACKEND_HPP
17+
#define _LIBSYCL___IMPL_BACKEND_HPP
18+
19+
#include <sycl/__impl/detail/config.hpp>
20+
21+
#include <string_view>
22+
#include <type_traits>
23+
24+
_LIBSYCL_BEGIN_NAMESPACE_SYCL
25+
26+
// 4.1. Backends
27+
enum class backend : char {
28+
opencl = 1,
29+
level_zero = 2,
30+
cuda = 3,
31+
hip = 4,
32+
all = 5,
33+
};
34+
35+
namespace detail {
36+
template <typename T> struct is_backend_info_desc : std::false_type {};
37+
} // namespace detail
38+
39+
// 4.5.1.1. Type traits backend_traits
40+
template <backend Backend> class backend_traits;
41+
42+
template <backend Backend, typename SYCLObjectT>
43+
using backend_input_t =
44+
typename backend_traits<Backend>::template input_type<SYCLObjectT>;
45+
template <backend Backend, typename SYCLObjectT>
46+
using backend_return_t =
47+
typename backend_traits<Backend>::template return_type<SYCLObjectT>;
48+
49+
namespace detail {
50+
inline std::string_view get_backend_name(const backend &Backend) {
51+
switch (Backend) {
52+
case backend::opencl:
53+
return "opencl";
54+
case backend::level_zero:
55+
return "level_zero";
56+
case backend::cuda:
57+
return "cuda";
58+
case backend::hip:
59+
return "hip";
60+
case backend::all:
61+
return "all";
62+
}
63+
64+
return "";
65+
}
66+
} // namespace detail
67+
68+
_LIBSYCL_END_NAMESPACE_SYCL
69+
70+
#endif // _LIBSYCL___IMPL_BACKEND_HPP

libsycl/include/sycl/__impl/detail/config.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141

4242
# else // _WIN32
4343

44-
# define _LIBSYCL_DLL_LOCAL [[__gnu__::__visibility__("hidden")]]
45-
# define _LIBSYCL_EXPORT [[__gnu__::__visibility__("default")]]
44+
# define _LIBSYCL_DLL_LOCAL __attribute__((visibility("hidden")))
45+
# define _LIBSYCL_EXPORT __attribute__((visibility("default")))
4646

4747
# endif // _WIN32
4848
# endif // _LIBSYCL_EXPORT
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains macro definitions used in SYCL implementation.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef _LIBSYCL___IMPL_DETAIL_MACRO_DEFINITIONS_HPP
15+
#define _LIBSYCL___IMPL_DETAIL_MACRO_DEFINITIONS_HPP
16+
17+
#ifndef __SYCL2020_DEPRECATED
18+
# if SYCL_LANGUAGE_VERSION == 202012L && \
19+
!defined(SYCL2020_DISABLE_DEPRECATION_WARNINGS)
20+
# define __SYCL2020_DEPRECATED(message) [[deprecated(message)]]
21+
# else
22+
# define __SYCL2020_DEPRECATED(message)
23+
# endif
24+
#endif // __SYCL2020_DEPRECATED
25+
26+
static_assert(__cplusplus >= 201703L,
27+
"SYCL RT does not support C++ version earlier than C++17.");
28+
29+
#if defined(_WIN32) && !defined(_DLL) && !defined(__SYCL_DEVICE_ONLY__)
30+
// SYCL library is designed such a way that STL objects cross DLL boundary,
31+
// which is guaranteed to work properly only when the application uses the same
32+
// C++ runtime that SYCL library uses.
33+
// The appplications using sycl.dll must be linked with dynamic/release C++ MSVC
34+
// runtime, i.e. be compiled with /MD switch. Similarly, the applications using
35+
// sycld.dll must be linked with dynamic/debug C++ runtime and be compiled with
36+
// /MDd switch.
37+
// Compiler automatically adds /MD or /MDd when -fsycl switch is used.
38+
// The options /MD and /MDd that make the code to use dynamic runtime also
39+
// define the _DLL macro.
40+
# define ERROR_MESSAGE \
41+
"SYCL library is designed to work safely with dynamic C++ runtime." \
42+
"Please use /MD switch with sycl.dll, /MDd switch with sycld.dll, " \
43+
"or -fsycl switch to set C++ runtime automatically."
44+
# if defined(_MSC_VER)
45+
# pragma message(ERROR_MESSAGE)
46+
# else
47+
# warning ERROR_MESSAGE
48+
# endif
49+
# undef ERROR_MESSAGE
50+
#endif // defined(_WIN32) && !defined(_DLL) && !defined(__SYCL_DEVICE_ONLY__)
51+
52+
#endif //_LIBSYCL___IMPL_DETAIL_MACRO_DEFINITIONS_HPP
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains helper functions for tranformation between implementation
11+
/// and SYCL's interface objects.
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef _LIBSYCL___IMPL_DETAIL_OBJ_BASE_HPP
16+
#define _LIBSYCL___IMPL_DETAIL_OBJ_BASE_HPP
17+
18+
#include <sycl/__impl/detail/config.hpp>
19+
20+
#include <cassert>
21+
#include <type_traits>
22+
#include <utility>
23+
24+
_LIBSYCL_BEGIN_NAMESPACE_SYCL
25+
26+
namespace detail {
27+
28+
template <class Impl, class SyclObject> class ObjBase {
29+
public:
30+
using ImplType = Impl;
31+
using Base = ObjBase<Impl, SyclObject>;
32+
33+
protected:
34+
ImplType &impl;
35+
36+
explicit ObjBase(ImplType &pImpl) : impl(pImpl) {}
37+
ObjBase() = default;
38+
39+
static SyclObject createSyclProxy(ImplType &impl) { return SyclObject(impl); }
40+
41+
template <class Obj>
42+
friend const typename Obj::ImplType &getSyclObjImpl(const Obj &Object);
43+
44+
template <class Obj>
45+
friend Obj createSyclObjFromImpl(
46+
std::add_lvalue_reference_t<typename Obj::ImplType> ImplObj);
47+
};
48+
49+
template <class Obj>
50+
const typename Obj::ImplType &getSyclObjImpl(const Obj &Object) {
51+
return Object.impl;
52+
}
53+
54+
template <class Obj>
55+
Obj createSyclObjFromImpl(
56+
std::add_lvalue_reference_t<typename Obj::ImplType> ImplObj) {
57+
return Obj::Base::createSyclProxy(ImplObj);
58+
}
59+
60+
} // namespace detail
61+
62+
_LIBSYCL_END_NAMESPACE_SYCL
63+
64+
#endif // _LIBSYCL___IMPL_DETAIL_OBJ_BASE_HPP
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
///
9+
/// \file
10+
/// This file contains the declaration of the SYCL 2020 Exception class
11+
/// interface (4.13.2.)
12+
///
13+
//===----------------------------------------------------------------------===//
14+
15+
#ifndef _LIBSYCL___IMPL_EXCEPTION_HPP
16+
#define _LIBSYCL___IMPL_EXCEPTION_HPP
17+
18+
#include <sycl/__impl/detail/config.hpp>
19+
20+
#include <exception>
21+
#include <memory>
22+
#include <string>
23+
#include <system_error>
24+
#include <type_traits>
25+
#include <vector>
26+
27+
_LIBSYCL_BEGIN_NAMESPACE_SYCL
28+
29+
class context;
30+
31+
enum class errc : int {
32+
success = 0,
33+
runtime = 1,
34+
kernel = 2,
35+
accessor = 3,
36+
nd_range = 4,
37+
event = 5,
38+
kernel_argument = 6,
39+
build = 7,
40+
invalid = 8,
41+
memory_allocation = 9,
42+
platform = 10,
43+
profiling = 11,
44+
feature_not_supported = 12,
45+
kernel_not_supported = 13,
46+
backend_mismatch = 14,
47+
};
48+
49+
/// Constructs an error code using E and sycl_category()
50+
_LIBSYCL_EXPORT std::error_code make_error_code(sycl::errc E) noexcept;
51+
52+
/// Obtains a reference to the static error category object for SYCL errors.
53+
_LIBSYCL_EXPORT const std::error_category &sycl_category() noexcept;
54+
55+
// Derive from std::exception so uncaught exceptions are printed in c++ default
56+
// exception handler.
57+
// Virtual inheritance is mandated by SYCL 2020.
58+
// 4.13.2. Exception class interface
59+
class _LIBSYCL_EXPORT exception : public virtual std::exception {
60+
public:
61+
exception(std::error_code, const char *);
62+
exception(std::error_code Ec, const std::string &Msg)
63+
: exception(Ec, Msg.c_str()) {}
64+
65+
exception(std::error_code EC) : exception(EC, "") {}
66+
exception(int EV, const std::error_category &ECat, const std::string &WhatArg)
67+
: exception(EV, ECat, WhatArg.c_str()) {}
68+
exception(int EV, const std::error_category &ECat, const char *WhatArg)
69+
: exception({EV, ECat}, WhatArg) {}
70+
exception(int EV, const std::error_category &ECat)
71+
: exception({EV, ECat}, "") {}
72+
73+
virtual ~exception();
74+
75+
const std::error_code &code() const noexcept;
76+
const std::error_category &category() const noexcept;
77+
78+
const char *what() const noexcept final;
79+
80+
bool has_context() const noexcept;
81+
82+
private:
83+
// Exceptions must be noexcept copy constructible, so cannot use std::string
84+
// directly.
85+
std::shared_ptr<std::string> MMessage;
86+
std::error_code MErrC = make_error_code(sycl::errc::invalid);
87+
};
88+
89+
/// Used as a container for a list of asynchronous exceptions
90+
///
91+
class _LIBSYCL_EXPORT exception_list {
92+
public:
93+
using value_type = std::exception_ptr;
94+
using reference = value_type &;
95+
using const_reference = const value_type &;
96+
using size_type = std::size_t;
97+
using iterator = std::vector<std::exception_ptr>::const_iterator;
98+
using const_iterator = std::vector<std::exception_ptr>::const_iterator;
99+
100+
size_type size() const;
101+
// first asynchronous exception
102+
iterator begin() const;
103+
// refer to past-the-end last asynchronous exception
104+
iterator end() const;
105+
106+
private:
107+
std::vector<std::exception_ptr> MList;
108+
};
109+
110+
_LIBSYCL_END_NAMESPACE_SYCL
111+
112+
namespace std {
113+
template <> struct is_error_code_enum<sycl::errc> : true_type {};
114+
} // namespace std
115+
116+
#endif // _LIBSYCL___IMPL_EXCEPTION_HPP
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef __SYCL_PARAM_TRAITS_SPEC
2+
static_assert(false, "__SYCL_PARAM_TRAITS_SPEC is required but not defined");
3+
#endif
4+
5+
// 4.6.2.4. Information descriptors
6+
__SYCL_PARAM_TRAITS_SPEC(platform, version, std::string, OL_PLATFORM_INFO_VERSION)
7+
__SYCL_PARAM_TRAITS_SPEC(platform, name, std::string, OL_PLATFORM_INFO_NAME)
8+
__SYCL_PARAM_TRAITS_SPEC(platform, vendor, std::string, OL_PLATFORM_INFO_VENDOR_NAME)

0 commit comments

Comments
 (0)