Skip to content

[SYCL] add clang diagnostic for illegal types of kernel free function arguments #19244

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

Open
wants to merge 13 commits into
base: sycl
Choose a base branch
from

Conversation

dklochkov-emb
Copy link
Contributor

@dklochkov-emb dklochkov-emb commented Jul 1, 2025

This PR adds clang diagnostic message in case if:

  1. kernel free function argument type is virtually derived on any level of inheritance
  2. kernel free function argument type has virtual methods

Also, tests were added according to test plan

@dklochkov-emb dklochkov-emb requested a review from a team as a code owner July 1, 2025 15:07
Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

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

Does the free function spec not allow such kernel arguments? At least regarding virtual functions AFAIK in SYCL 2020 such an object can be captured to the device code and is fine unless a virtual function is called.
See
https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#sec:language.restrictions.kernels

The odr-use of polymorphic classes and classes with virtual inheritance is allowed. However, no virtual member functions are allowed to be called in a device function.

@dklochkov-emb
Copy link
Contributor Author

Does the free function spec not allow such kernel arguments? At least regarding virtual functions AFAIK in SYCL 2020 such an object can be captured to the device code and is fine unless a virtual function is called. See https://registry.khronos.org/SYCL/specs/sycl-2020/html/sycl-2020.html#sec:language.restrictions.kernels

The odr-use of polymorphic classes and classes with virtual inheritance is allowed. However, no virtual member functions are allowed to be called in a device function.

@Fznamznon kernel free function docs tell that:
Each of the function’s arguments must have a type that is an allowed kernel parameter type as specified in section 4.12.4 "Rules for parameter passing to kernels" of the core SYCL specification. The function must not be declared with parameters of type reducer or kernel_handler. These special kernel arguments cannot be passed to a free function kernel
.12.4 "Rules for parameter passing to kernels" contains information about virtual only the next:
Regardless of how the parameter is passed, the following rules define the allowable types for a kernel parameter: ... A class type S with a non-virtual base class of type T is a legal parameter type if T is a legal parameter type and if S would otherwise be a legal parameter type aside from this base class.

i.e. S is allowed only if it has non-virtual inheritance from allowed type which means virtual inheritance is not allowed, does not it?

@Fznamznon
Copy link
Contributor

@dklochkov-emb , yeah, that vaguely reads that virtual bases are not allowed in kernel arguments. I mailed spec folks to double check. Two concerns though:

  1. This seems to have nothing to do with virtual functions. Classes with virtual functions should be allowed in kernel arguments.
  2. The restriction is not limited to free functions, meaning the infrastructure of visitors in SemaSYCL is better to be used instead of manually recursing through bases of all kernel arguments. What I'm saying is that we probably need to extend SyclKernelFieldChecker class to check for virtual bases.

@@ -1569,6 +1576,27 @@ class KernelObjVisitor {
else if (ParamTy->isStructureOrClassType()) {
if (KP_FOR_EACH(handleStructType, Param, ParamTy)) {
CXXRecordDecl *RD = ParamTy->getAsCXXRecordDecl();
llvm::SmallVector<const CXXRecordDecl *, 8> WorkList;
Copy link
Contributor

@Fznamznon Fznamznon Jul 14, 2025

Choose a reason for hiding this comment

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

Hmm, this is not exactly what I meant about using visitors infrastructure. What I meant is that we should extend SyclKernelFieldChecker class, so it checks bases. It has methods that accept CXXBaseSpecifier AST node which represent base specifier. Each CXXBaseSpecifier knows whether it is virtual. I think SyclKernelFieldChecker should be already recursively visiting each struct it meets. We just need to diagnose from some of method of SyclKernelFieldChecker that accepts CXXBaseSpecifier . Because this is not even specific for free function kernels, you don't even need to invent a method specific for free function kernels and accepting CXXBaseSpecifier.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think this one is resolved.

Copy link
Contributor

@uditagarwal97 uditagarwal97 left a comment

Choose a reason for hiding this comment

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

Changes in sycl/test-e2e/FreeFunctionKernels/virtual_methods.cpp LGTM.

@dklochkov-emb dklochkov-emb self-assigned this Jul 18, 2025
@dklochkov-emb
Copy link
Contributor Author

@dpcpp-cfe-reviewers please, look at PR - @Fznamznon is on vacation until 28 July.

@dklochkov-emb
Copy link
Contributor Author

@dpcpp-cfe-reviewers please, look at PR

@Fznamznon
Copy link
Contributor

@dpcpp-cfe-reviewers please, look at PR

See #19244 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants