Skip to content

Commit 84e07bd

Browse files
Change to detail::string_view and allow lazy init of compile time info
1 parent 23fa732 commit 84e07bd

File tree

9 files changed

+139
-99
lines changed

9 files changed

+139
-99
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//==--------------------- get_device_kernel_info.hpp -----------------------==//
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+
#pragma once
9+
10+
#include <sycl/detail/kernel_desc.hpp>
11+
#include <sycl/detail/string_view.hpp>
12+
13+
namespace sycl {
14+
inline namespace _V1 {
15+
namespace detail {
16+
inline namespace compile_time_kernel_info_v1 {
17+
18+
// This is being passed across ABI boundary, so we don't use std::string_view,
19+
// at least for as long as we support user apps built with GNU libstdc++'s
20+
// pre-C++11 ABI.
21+
struct CompileTimeKernelInfoTy {
22+
detail::string_view Name;
23+
unsigned NumParams = 0;
24+
bool IsESIMD = false;
25+
detail::string_view FileName;
26+
detail::string_view FunctionName;
27+
unsigned LineNumber = 0;
28+
unsigned ColumnNumber = 0;
29+
int64_t KernelSize = 0;
30+
using ParamDescGetterT = kernel_param_desc_t (*)(int);
31+
ParamDescGetterT ParamDescGetter = nullptr;
32+
bool HasSpecialCaptures = true;
33+
};
34+
35+
template <class Kernel>
36+
inline constexpr CompileTimeKernelInfoTy CompileTimeKernelInfo{
37+
std::string_view(getKernelName<Kernel>()),
38+
getKernelNumParams<Kernel>(),
39+
isKernelESIMD<Kernel>(),
40+
std::string_view(getKernelFileName<Kernel>()),
41+
std::string_view(getKernelFunctionName<Kernel>()),
42+
getKernelLineNumber<Kernel>(),
43+
getKernelColumnNumber<Kernel>(),
44+
getKernelSize<Kernel>(),
45+
&getKernelParamDesc<Kernel>,
46+
hasSpecialCaptures<Kernel>()};
47+
48+
} // namespace compile_time_kernel_info_v1
49+
} // namespace detail
50+
} // namespace _V1
51+
} // namespace sycl
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//==--------------------- get_device_kernel_info.hpp -----------------------==//
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+
#pragma once
9+
10+
#include <sycl/detail/compile_time_kernel_info.hpp>
11+
#include <sycl/detail/kernel_desc.hpp>
12+
13+
namespace sycl {
14+
inline namespace _V1 {
15+
namespace detail {
16+
17+
class DeviceKernelInfo;
18+
// Lifetime of the underlying `DeviceKernelInfo` is tied to the availability of
19+
// the `sycl_device_binaries` corresponding to this kernel. In other words, once
20+
// user library is unloaded (see __sycl_unregister_lib), program manager destoys
21+
// this `DeviceKernelInfo` object and the reference returned from here becomes
22+
// stale.
23+
__SYCL_EXPORT DeviceKernelInfo &
24+
getDeviceKernelInfo(const CompileTimeKernelInfoTy &);
25+
26+
template <class Kernel> DeviceKernelInfo &getDeviceKernelInfo() {
27+
static DeviceKernelInfo &Info =
28+
getDeviceKernelInfo(CompileTimeKernelInfo<Kernel>);
29+
return Info;
30+
}
31+
32+
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
33+
class KernelNameBasedCacheT;
34+
__SYCL_EXPORT KernelNameBasedCacheT *createKernelNameBasedCache();
35+
#endif
36+
37+
} // namespace detail
38+
} // namespace _V1
39+
} // namespace sycl

sycl/include/sycl/detail/kernel_desc.hpp

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -277,53 +277,6 @@ template <typename KernelNameType> constexpr bool hasSpecialCaptures() {
277277
}
278278
return FoundSpecialCapture;
279279
}
280-
inline namespace compile_time_kernel_info_v1 {
281-
282-
// This is being passed across ABI boundary, so we don't use std::string_view,
283-
// at least for as long as we support user apps built with GNU libstdc++'s
284-
// pre-C++11 ABI.
285-
struct CompileTimeKernelInfoTy {
286-
const char *const Name = nullptr;
287-
const unsigned NumParams = 0;
288-
const bool IsESIMD = false;
289-
const char *const FileName = "";
290-
const char *const FunctionName = "";
291-
const unsigned LineNumber = 0;
292-
const unsigned ColumnNumber = 0;
293-
const int64_t KernelSize = 0;
294-
kernel_param_desc_t (*const ParamDescGetter)(int) = nullptr;
295-
const bool HasSpecialCaptures = false;
296-
};
297-
298-
template <class Kernel>
299-
inline constexpr CompileTimeKernelInfoTy CompileTimeKernelInfo{
300-
getKernelName<Kernel>(), getKernelNumParams<Kernel>(),
301-
isKernelESIMD<Kernel>(), getKernelFileName<Kernel>(),
302-
getKernelFunctionName<Kernel>(), getKernelLineNumber<Kernel>(),
303-
getKernelColumnNumber<Kernel>(), getKernelSize<Kernel>(),
304-
&getKernelParamDesc<Kernel>, hasSpecialCaptures<Kernel>()};
305-
} // namespace compile_time_kernel_info_v1
306-
307-
class DeviceKernelInfo;
308-
// Lifetime of the underlying `DeviceKernelInfo` is tied to the availability of
309-
// the `sycl_device_binaries` corresponding to this kernel. In other words, once
310-
// user library is unloaded (see __sycl_unregister_lib), program manager destoys
311-
// this `DeviceKernelInfo` object and the reference returned from here becomes
312-
// stale.
313-
__SYCL_EXPORT DeviceKernelInfo &
314-
getDeviceKernelInfo(const CompileTimeKernelInfoTy &);
315-
316-
template <class Kernel> DeviceKernelInfo &getDeviceKernelInfo() {
317-
static DeviceKernelInfo &Info =
318-
getDeviceKernelInfo(CompileTimeKernelInfo<Kernel>);
319-
return Info;
320-
}
321-
322-
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
323-
class KernelNameBasedCacheT;
324-
__SYCL_EXPORT KernelNameBasedCacheT *createKernelNameBasedCache();
325-
#endif
326-
327280
} // namespace detail
328281
} // namespace _V1
329282
} // namespace sycl

sycl/include/sycl/handler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <sycl/detail/common.hpp>
1515
#include <sycl/detail/defines_elementary.hpp>
1616
#include <sycl/detail/export.hpp>
17+
#include <sycl/detail/get_device_kernel_info.hpp>
1718
#include <sycl/detail/id_queries_fit_in_int.hpp>
1819
#include <sycl/detail/impl_utils.hpp>
1920
#include <sycl/detail/kernel_desc.hpp>

sycl/source/detail/device_kernel_info.cpp

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ DeviceKernelInfo::DeviceKernelInfo(const CompileTimeKernelInfoTy &Info)
1616
: CompileTimeKernelInfoTy(Info)
1717
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
1818
,
19-
Name(Info.Name)
19+
Name(Info.Name.data())
2020
#endif
2121
{
22-
init(Name);
22+
init(Name.data());
2323
}
2424

2525
void DeviceKernelInfo::init(KernelNameStrRefT KernelName) {
@@ -38,6 +38,32 @@ void DeviceKernelInfo::initIfNeeded(KernelNameStrRefT KernelName) {
3838
}
3939
#endif
4040

41+
template <typename OtherTy>
42+
inline constexpr bool operator==(const CompileTimeKernelInfoTy &LHS,
43+
const OtherTy &RHS) {
44+
45+
// TODO replace with std::tie(...) == std::tie(...) once there is
46+
// implicit conversion from detail to std string_view.
47+
return std::string_view{LHS.Name} == std::string_view{RHS.Name} &&
48+
LHS.NumParams == RHS.NumParams && LHS.IsESIMD == RHS.IsESIMD &&
49+
std::string_view{LHS.FileName} == std::string_view{RHS.FileName} &&
50+
std::string_view{LHS.FunctionName} ==
51+
std::string_view{RHS.FunctionName} &&
52+
LHS.LineNumber == RHS.LineNumber &&
53+
LHS.ColumnNumber == RHS.ColumnNumber &&
54+
LHS.KernelSize == RHS.KernelSize &&
55+
LHS.ParamDescGetter == RHS.ParamDescGetter &&
56+
LHS.HasSpecialCaptures == RHS.HasSpecialCaptures;
57+
}
58+
59+
void DeviceKernelInfo::setCompileTimeInfoIfNeeded(
60+
const CompileTimeKernelInfoTy &Info) {
61+
if (isCompileTimeInfoSet())
62+
CompileTimeKernelInfoTy::operator=(Info);
63+
assert(isCompileTimeInfoSet());
64+
assert(Info == *this);
65+
}
66+
4167
FastKernelSubcacheT &DeviceKernelInfo::getKernelSubcache() {
4268
assertInitialized();
4369
return MFastKernelSubcache;
@@ -51,6 +77,8 @@ const std::optional<int> &DeviceKernelInfo::getImplicitLocalArgPos() {
5177
return MImplicitLocalArgPos;
5278
}
5379

80+
bool DeviceKernelInfo::isCompileTimeInfoSet() const { return KernelSize != 0; }
81+
5482
void DeviceKernelInfo::assertInitialized() {
5583
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
5684
assert(MInitialized.load() && "Data needs to be initialized before use");

sycl/source/detail/device_kernel_info.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <detail/hashers.hpp>
1111
#include <detail/kernel_arg_mask.hpp>
1212
#include <emhash/hash_table8.hpp>
13-
#include <sycl/detail/kernel_desc.hpp>
13+
#include <sycl/detail/compile_time_kernel_info.hpp>
1414
#include <sycl/detail/kernel_name_str_t.hpp>
1515
#include <sycl/detail/spinlock.hpp>
1616
#include <sycl/detail/ur.hpp>
@@ -90,7 +90,8 @@ struct FastKernelSubcacheT {
9090
class DeviceKernelInfo : public CompileTimeKernelInfoTy {
9191
public:
9292
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
93-
// https://github.com/intel/llvm/pull/19117/files#r2294511096
93+
// Needs to own the kernel name string in non-preview builds since we pass it
94+
// using a temporary string instead of a string view there.
9495
std::string Name;
9596
#endif
9697

@@ -103,12 +104,15 @@ class DeviceKernelInfo : public CompileTimeKernelInfoTy {
103104
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
104105
void initIfNeeded(KernelNameStrRefT KernelName);
105106
#endif
107+
void setCompileTimeInfoIfNeeded(const CompileTimeKernelInfoTy &Info);
108+
106109
FastKernelSubcacheT &getKernelSubcache();
107110
bool usesAssert();
108111
const std::optional<int> &getImplicitLocalArgPos();
109112

110113
private:
111114
void assertInitialized();
115+
bool isCompileTimeInfoSet() const;
112116

113117
#ifndef __INTEL_PREVIEW_BREAKING_CHANGES
114118
std::atomic<bool> MInitialized = false;

sycl/source/detail/program_manager/program_manager.cpp

Lines changed: 9 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,53 +1820,18 @@ ProgramManager::kernelImplicitLocalArgPos(KernelNameStrRefT KernelName) const {
18201820
return {};
18211821
}
18221822

1823-
template <typename OtherTy>
1824-
inline constexpr bool operator==(const CompileTimeKernelInfoTy &LHS,
1825-
const OtherTy &RHS) {
1826-
// This header states STL includes aren't allowed here, so can't use
1827-
// `std::tie(...) == std::tie(...)` idiom, and no C++20 for
1828-
// `operator==(...) = default`.
1829-
return std::string_view{LHS.Name} == std::string_view{RHS.Name} &&
1830-
LHS.NumParams == RHS.NumParams && LHS.IsESIMD == RHS.IsESIMD &&
1831-
std::string_view{LHS.FileName} == std::string_view{RHS.FileName} &&
1832-
std::string_view{LHS.FunctionName} ==
1833-
std::string_view{RHS.FunctionName} &&
1834-
LHS.LineNumber == RHS.LineNumber &&
1835-
LHS.ColumnNumber == RHS.ColumnNumber &&
1836-
LHS.KernelSize == RHS.KernelSize &&
1837-
LHS.ParamDescGetter == RHS.ParamDescGetter &&
1838-
LHS.HasSpecialCaptures == RHS.HasSpecialCaptures;
1839-
}
1840-
template <typename OtherTy>
1841-
inline constexpr bool operator!=(const CompileTimeKernelInfoTy &LHS,
1842-
const OtherTy &RHS) {
1843-
return !(LHS == RHS);
1844-
}
1845-
template <typename InfoTy> inline void print(const InfoTy &Info) {
1846-
std::cout << "CompileTimeKernelInfoTy:"
1847-
<< "\n Name: " << Info.Name << "\n NumParams: " << Info.NumParams
1848-
<< "\n IsESIMD: " << Info.IsESIMD
1849-
<< "\n FileName: " << Info.FileName
1850-
<< "\n FunctionName: " << Info.FunctionName
1851-
<< "\n LineNumber: " << Info.LineNumber
1852-
<< "\n ColumnNumber: " << Info.ColumnNumber
1853-
<< "\n KernelSize: " << Info.KernelSize
1854-
<< "\n ParamDescGetter: " << Info.ParamDescGetter
1855-
<< "\n HasSpecialCaptures: " << Info.HasSpecialCaptures
1856-
<< std::endl;
1857-
}
18581823
DeviceKernelInfo &ProgramManager::getOrCreateDeviceKernelInfo(
18591824
const CompileTimeKernelInfoTy &Info) {
18601825
auto Result =
1861-
m_DeviceKernelInfoMap.try_emplace(KernelNameStrT{Info.Name}, Info);
1862-
if (Info != Result.first->second) {
1863-
std::cout << "Info:" << std::endl;
1864-
print(Info);
1865-
std::cout << "Result:" << std::endl;
1866-
print(Result.first->second);
1867-
}
1868-
assert(Info == Result.first->second ||
1869-
Info == CompileTimeKernelInfoTy{Info.Name});
1826+
m_DeviceKernelInfoMap.try_emplace(KernelNameStrT{Info.Name.data()}, Info);
1827+
Result.first->second.setCompileTimeInfoIfNeeded(Info);
1828+
return Result.first->second;
1829+
}
1830+
1831+
DeviceKernelInfo &
1832+
ProgramManager::getOrCreateDeviceKernelInfo(KernelNameStrRefT KernelName) {
1833+
auto Result = m_DeviceKernelInfoMap.try_emplace(
1834+
KernelName, CompileTimeKernelInfoTy{std::string_view(KernelName)});
18701835
return Result.first->second;
18711836
}
18721837

sycl/source/detail/program_manager/program_manager.hpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,7 @@ class ProgramManager {
378378

379379
DeviceKernelInfo &
380380
getOrCreateDeviceKernelInfo(const CompileTimeKernelInfoTy &Info);
381-
DeviceKernelInfo &getOrCreateDeviceKernelInfo(KernelNameStrRefT KernelName) {
382-
return getOrCreateDeviceKernelInfo(
383-
CompileTimeKernelInfoTy{KernelName.data()});
384-
}
381+
DeviceKernelInfo &getOrCreateDeviceKernelInfo(KernelNameStrRefT KernelName);
385382

386383
std::set<const RTDeviceBinaryImage *>
387384
getRawDeviceImages(const std::vector<kernel_id> &KernelIDs);

sycl/test/include_deps/sycl_detail_core.hpp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@
130130
// CHECK-NEXT: CL/cl_version.h
131131
// CHECK-NEXT: CL/cl_platform.h
132132
// CHECK-NEXT: CL/cl_ext.h
133+
// CHECK-NEXT: detail/get_device_kernel_info.hpp
134+
// CHECK-NEXT: detail/compile_time_kernel_info.hpp
133135
// CHECK-NEXT: detail/id_queries_fit_in_int.hpp
134136
// CHECK-NEXT: detail/kernel_launch_helper.hpp
135137
// CHECK-NEXT: ext/intel/experimental/fp_control_kernel_properties.hpp

0 commit comments

Comments
 (0)