Skip to content

[SYCL] Update validation of annotated_arg/annotated_ptr parameterization #10437

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ __SYCL_TYPE(annotated_arg) annotated_arg<T *, detail::properties_t<Props...>> {
public:
static_assert(is_property_list<property_list_t>::value,
"Property list is invalid.");
static_assert(
validate_annotated_type<annotated_arg<T *, property_list_t>>::value,
"The property list contains invalid property.");
Copy link
Contributor

Choose a reason for hiding this comment

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

I hope in a future update we can make this better? Which property is invalid? Why is it invalid? Not gating on this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see better errors are bellow.


annotated_arg() noexcept = default;
annotated_arg(const annotated_arg &) = default;
Expand Down Expand Up @@ -178,8 +181,9 @@ __SYCL_TYPE(annotated_arg) annotated_arg<T, detail::properties_t<Props...>> {
static_assert(is_device_copyable_v<T>, "Type T must be device copyable.");
static_assert(is_property_list<property_list_t>::value,
"Property list is invalid.");
static_assert(check_property_list<T, Props...>::value,
"The property list contains invalid property.");
static_assert(
validate_annotated_type<annotated_arg<T, property_list_t>>::value,
"The property list contains invalid property.");

annotated_arg() noexcept = default;
annotated_arg(const annotated_arg &) = default;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ __SYCL_TYPE(annotated_ptr) annotated_ptr<T, detail::properties_t<Props...>> {
public:
static_assert(is_property_list<property_list_t>::value,
"Property list is invalid.");
static_assert(
validate_annotated_type<annotated_ptr<T, property_list_t>>::value,
"The property list contains invalid property.");

annotated_ptr() noexcept = default;
annotated_ptr(const annotated_ptr &) = default;
Expand Down
61 changes: 55 additions & 6 deletions sycl/include/sycl/ext/oneapi/annotated_arg/properties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,60 @@ struct is_valid_property<T, conduit_key::value_t> : std::true_type {};
template <typename T>
struct is_valid_property<T, stable_key::value_t> : std::true_type {};

// Check if a given property is supported for annotated_arg
// e.g. `alignment` is not supported for annotated_arg
template <typename PropT>
using is_property_value_of_annotated_arg =
is_property_value_of<PropT,
annotated_arg<void, detail::empty_properties_t>>;

// Check if a given property is supported for annotated_ptr
template <typename PropT>
using is_property_value_of_annotated_ptr =
is_property_value_of<PropT,
annotated_ptr<void, detail::empty_properties_t>>;

// Validate anntoated_arg/annotated_ptr
template <typename T> struct validate_annotated_type : std::false_type {};

// Partial specializations for validating annotated_arg
template <typename T, typename... Props>
struct check_property_list : std::true_type {};
struct validate_annotated_type<annotated_arg<T, detail::properties_t<Props...>>>
: std::true_type {};
Comment on lines 347 to +349
Copy link
Contributor

@steffenlarsen steffenlarsen Jul 20, 2023

Choose a reason for hiding this comment

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

Isn't the detail::properties_t<> the only case that hits this? If so, we might as well make that clear in the definition:

template <typename T>
struct validate_annotated_type<annotated_arg<T, detail::properties_t<>>>
    : std::true_type {};


template <typename T, typename Prop, typename... Props>
struct check_property_list<T, Prop, Props...>
: std::conditional_t<is_valid_property<T, Prop>::value,
check_property_list<T, Props...>, std::false_type> {
static_assert(is_valid_property<T, Prop>::value,
"Property is invalid for the given type.");
struct validate_annotated_type<
annotated_arg<T, detail::properties_t<Prop, Props...>>>
: std::conditional_t<is_valid_property<T, Prop>::value &&
is_property_value_of_annotated_arg<Prop>::value,
validate_annotated_type<
annotated_arg<T, detail::properties_t<Props...>>>,
std::false_type> {
static_assert(
is_valid_property<T, Prop>::value,
"Property is invalid for the underlying type of annotated_arg.");
static_assert(is_property_value_of_annotated_arg<Prop>::value,
"Property is not supported for annotated_arg.");
};
Comment on lines +352 to +364
Copy link
Contributor

Choose a reason for hiding this comment

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

Due to the static_assert inside the body, it seems unnecessary to check them in the conditional too. It would make sense if std::conditional didn't evaluate both branches, but it does. However, I believe you can prevent further checks if you move the recursive use of validate_annotated_type to after the static_assert.

That is, I would prefer one of:

Suggested change
struct validate_annotated_type<
annotated_arg<T, detail::properties_t<Prop, Props...>>>
: std::conditional_t<is_valid_property<T, Prop>::value &&
is_property_value_of_annotated_arg<Prop>::value,
validate_annotated_type<
annotated_arg<T, detail::properties_t<Props...>>>,
std::false_type> {
static_assert(
is_valid_property<T, Prop>::value,
"Property is invalid for the underlying type of annotated_arg.");
static_assert(is_property_value_of_annotated_arg<Prop>::value,
"Property is not supported for annotated_arg.");
};
struct validate_annotated_type<
annotated_arg<T, detail::properties_t<Prop, Props...>>>
: validate_annotated_type<annotated_arg<T, detail::properties_t<Props...>>> {
static_assert(
is_valid_property<T, Prop>::value,
"Property is invalid for the underlying type of annotated_arg.");
static_assert(is_property_value_of_annotated_arg<Prop>::value,
"Property is not supported for annotated_arg.");
};

or

Suggested change
struct validate_annotated_type<
annotated_arg<T, detail::properties_t<Prop, Props...>>>
: std::conditional_t<is_valid_property<T, Prop>::value &&
is_property_value_of_annotated_arg<Prop>::value,
validate_annotated_type<
annotated_arg<T, detail::properties_t<Props...>>>,
std::false_type> {
static_assert(
is_valid_property<T, Prop>::value,
"Property is invalid for the underlying type of annotated_arg.");
static_assert(is_property_value_of_annotated_arg<Prop>::value,
"Property is not supported for annotated_arg.");
};
struct validate_annotated_type<
annotated_arg<T, detail::properties_t<Prop, Props...>>> {
static_assert(
is_valid_property<T, Prop>::value,
"Property is invalid for the underlying type of annotated_arg.");
static_assert(is_property_value_of_annotated_arg<Prop>::value,
"Property is not supported for annotated_arg.");
static constexpr bool value = validate_annotated_type<annotated_arg<T, detail::properties_t<Props...>>>::value;
};

where (to my knowledge) the latter would stop at the first invalid property.


// Partial specializations for validating annotated_arg
template <typename T, typename... Props>
struct validate_annotated_type<annotated_ptr<T, detail::properties_t<Props...>>>
: std::true_type {};

template <typename T, typename Prop, typename... Props>
struct validate_annotated_type<
annotated_ptr<T, detail::properties_t<Prop, Props...>>>
: std::conditional_t<is_valid_property<T *, Prop>::value &&
is_property_value_of_annotated_ptr<Prop>::value,
validate_annotated_type<
annotated_ptr<T, detail::properties_t<Props...>>>,
std::false_type> {
static_assert(
is_valid_property<T *, Prop>::value,
"Property is invalid for the underlying type of annotated_ptr.");
static_assert(is_property_value_of_annotated_ptr<Prop>::value,
"Property is not supported for annotated_ptr.");
};

//===----------------------------------------------------------------------===//
Expand All @@ -354,6 +399,10 @@ template <typename T, typename PropertyListT>
struct is_property_key_of<alignment_key, annotated_ptr<T, PropertyListT>>
: std::true_type {};

template <typename T, int K>
struct is_valid_property<T, alignment_key::value_t<K>>
: std::bool_constant<std::is_pointer<T>::value> {};

namespace detail {

template <> struct PropertyToKind<alignment_key> {
Expand Down