Skip to content

Commit ba4b2c7

Browse files
[SYCL] Ignore host_access attribute for non-device_image_scope device_global (#11194)
This commit makes a `device_global` without the `device_image_scope` property ignore the `host_access` property in the kernel information, as the runtime will need to access the `device_global` to set the pointer inside and as such cannot respect the related promise. The headers will still consider the `device_global` as having the property. --------- Signed-off-by: Larsen, Steffen <[email protected]>
1 parent fd40b7b commit ba4b2c7

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

sycl/include/sycl/ext/oneapi/device_global/device_global.hpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@
2828
Op " is not supported on host device.");
2929
#endif
3030

31+
// Helper macro for conditional device_global property meta info filtering. This
32+
// lets us ignore certain properties under specified conditions, e.g. ignoring
33+
// host_access if device_image_scope isn't also present.
34+
#define __SYCL_DEVICE_GLOBAL_PROP_META_INFO(Props) \
35+
detail::ConditionalPropertyMetaInfo< \
36+
Props, detail::DeviceGlobalMetaInfoFilter< \
37+
Props, detail::properties_t<Props...>>::value>
38+
3139
namespace sycl {
3240
inline namespace _V1 {
3341
namespace ext::oneapi::experimental {
@@ -120,8 +128,9 @@ class
120128
#ifdef __SYCL_DEVICE_ONLY__
121129
[[__sycl_detail__::global_variable_allowed, __sycl_detail__::device_global,
122130
__sycl_detail__::add_ir_attributes_global_variable(
123-
"sycl-device-global-size", detail::PropertyMetaInfo<Props>::name...,
124-
sizeof(T), detail::PropertyMetaInfo<Props>::value...)]]
131+
"sycl-device-global-size",
132+
__SYCL_DEVICE_GLOBAL_PROP_META_INFO(Props)::name..., sizeof(T),
133+
__SYCL_DEVICE_GLOBAL_PROP_META_INFO(Props)::value...)]]
125134
#endif
126135
device_global<T, detail::properties_t<Props...>>
127136
: public detail::device_global_base<T, detail::properties_t<Props...>> {
@@ -176,16 +185,16 @@ class
176185

177186
template <class RelayT = T>
178187
std::remove_reference_t<
179-
decltype(std::declval<RelayT>()[std::declval<std::ptrdiff_t>()])>
180-
&operator[](std::ptrdiff_t idx) noexcept {
188+
decltype(std::declval<RelayT>()[std::declval<std::ptrdiff_t>()])> &
189+
operator[](std::ptrdiff_t idx) noexcept {
181190
__SYCL_HOST_NOT_SUPPORTED("Subscript operator")
182191
return (*this->get_ptr())[idx];
183192
}
184193

185194
template <class RelayT = T>
186195
const std::remove_reference_t<
187-
decltype(std::declval<RelayT>()[std::declval<std::ptrdiff_t>()])>
188-
&operator[](std::ptrdiff_t idx) const noexcept {
196+
decltype(std::declval<RelayT>()[std::declval<std::ptrdiff_t>()])> &
197+
operator[](std::ptrdiff_t idx) const noexcept {
189198
__SYCL_HOST_NOT_SUPPORTED("Subscript operator")
190199
return (*this->get_ptr())[idx];
191200
}
@@ -222,3 +231,4 @@ class
222231
} // namespace sycl
223232

224233
#undef __SYCL_HOST_NOT_SUPPORTED
234+
#undef __SYCL_DEVICE_GLOBAL_PROP_META_INFO

sycl/include/sycl/ext/oneapi/device_global/properties.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,19 @@ struct PropertyMetaInfo<implement_in_csr_key::value_t<Enable>> {
132132
static constexpr bool value = Enable;
133133
};
134134

135+
// Filter allowing additional conditions for selecting when to include meta
136+
// information for properties for device_global.
137+
template <typename PropT, typename Properties>
138+
struct DeviceGlobalMetaInfoFilter : std::true_type {};
139+
140+
// host_access cannot be honored for device_global variables without the
141+
// device_image_scope property, as the runtime needs to write the common USM
142+
// pointer during first launch.
143+
template <host_access_enum Access, typename Properties>
144+
struct DeviceGlobalMetaInfoFilter<host_access_key::value_t<Access>, Properties>
145+
: std::bool_constant<
146+
Properties::template has_property<device_image_scope_key>()> {};
147+
135148
} // namespace detail
136149
} // namespace ext::oneapi::experimental
137150
} // namespace _V1

sycl/include/sycl/ext/oneapi/properties/property_utils.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,24 @@ template <typename PropertiesT>
342342
struct NoConflictingProperties
343343
: NoConflictingPropertiesHelper<PropertiesT, PropertiesT> {};
344344

345+
//******************************************************************************
346+
// Conditional property meta-info
347+
//******************************************************************************
348+
349+
// Base class for property meta info that is ignored when propagating
350+
// information through the compiler.
351+
struct IgnoredPropertyMetaInfo {
352+
static constexpr const char *name = "";
353+
static constexpr std::nullptr_t value = nullptr;
354+
};
355+
356+
// Trait for picking either property meta information for PropT if Condition is
357+
// true or ignored information if Condition is false.
358+
template <typename PropT, bool Condition>
359+
struct ConditionalPropertyMetaInfo
360+
: std::conditional_t<Condition, PropertyMetaInfo<PropT>,
361+
IgnoredPropertyMetaInfo> {};
362+
345363
} // namespace detail
346364
} // namespace ext::oneapi::experimental
347365
} // namespace _V1
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clangxx -S -emit-llvm -fsycl-device-only %s -o - | FileCheck %s
2+
3+
// Checks that the host_access property doesn't get represented when there is no
4+
// device_image_scope property.
5+
6+
#include <sycl/sycl.hpp>
7+
8+
using namespace sycl::ext::oneapi::experimental;
9+
10+
static device_global<int,
11+
decltype(properties(device_image_scope, host_access_read))>
12+
DeviceGlobalDeviceImageScoped;
13+
static device_global<int, decltype(properties(host_access_read))>
14+
DeviceGlobalFullScoped;
15+
16+
int main() {
17+
sycl::queue Q;
18+
Q.single_task([]() {
19+
DeviceGlobalFullScoped = 42;
20+
DeviceGlobalDeviceImageScoped = 42;
21+
});
22+
}
23+
24+
// CHECK-DAG: @_ZL29DeviceGlobalDeviceImageScoped = {{.*}} #[[DISAttrs:[0-9]+]]
25+
// CHECK-DAG: @_ZL22DeviceGlobalFullScoped = {{.*}} #[[FSAttrs:[0-9]+]]
26+
// CHECK-DAG: attributes #[[DISAttrs:[0-9]+]] = { {{.*}}"sycl-host-access"
27+
// CHECK-NOT: attributes #[[FSAttrs:[0-9]+]] = { {{.*}}"sycl-host-access"

0 commit comments

Comments
 (0)