Skip to content

[SYCL] Support multiple call operators in kernel functor #8525

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

Merged
merged 29 commits into from
Apr 14, 2023

Conversation

srividya-sundaram
Copy link
Contributor

@srividya-sundaram srividya-sundaram commented Mar 2, 2023

A named function object that is used to define a SYCL kernel, can have more than one definition of function call operator 'operator()()'.

For such cases, prior to this change, the compiler front end always returned the first available 'operator()()' call found in the functor, regardless of whether that was the one the user invoked from within the kernel invocation function.
This resulted in incorrect kernel invocation and incorrect attribute collection ( if the 'operator()()' calls had attributes applied on them).

This is best illustrated by the following example:

   class MyKernel {
    public:
      void operator()() const {
        // code
      }

      [[intel::reqd_sub_group_size(4)]] void operator()(sycl::id<1> id) const {
        // code
      }
    };

    int main() {
    Q.submit([&](sycl::handler& cgh) {
      MyKernel kernelFunctorObject;
      cgh.parallel_for(sycl::range<1>(16), kernelFunctorObject);
    });
      return 0;
    }

In the above example, the paralle_for kernel invocation function has a range argument, in addition to the functor.
In the simplified DPC++ header implementation of parallel_for below,

template <typename KernelName = auto_name, typename KernelType, int Dims>
  void parallel_for(range<Dims> numWorkItems, const KernelType &kernelFunc) {
    using NameT = typename get_kernel_name_t<KernelName, KernelType>::name;
    kernel_parallel_for<NameT, KernelType, Dims>(kernelFunc);


template <typename KernelName, typename KernelType, int Dims>
ATTR_SYCL_KERNEL void
kernel_parallel_for(const KernelType &KernelFunc) {
  KernelFunc(id<Dims>());
}

KernelFunc() is invoked with 'id' as an argument because 'parallel_for' invocation has a 'range' passed to it.

However, for the example above, the CFE returns 'operator()()' instead of 'void operator()(sycl::id<1> id) const'.
This results in incorrect kernel being invoked as well as losing the intel::reqd_sub_group_size(4)] attribute, the user intended to use.

The root of the kernel is the function marked with “[[clang::sycl_kernel]]” and the user’s kernel is “KernelFunc”, and any C++ attributes the user declared for the kernel are on that function.

In the new implementation, we use RecursiveASTVisitor Visitor to traverse the nodes from the root of the kernel function (e.g. “kernel_parallel_for”) and find the version of “operator()()” that is called by “KernelFunc()”. There will only be one call to “KernelFunc()” in that call graph because the DPC++ headers are structured such that the user’s kernel function is only called once.

This will result in the correct kernel/operator()() call being invoked by the CFE.

@srividya-sundaram srividya-sundaram changed the title [SYCL] Support multiple call operators in kernel [WIP][SYCL] Support multiple call operators in kernel Mar 2, 2023
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 7, 2023 06:36 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 8, 2023 20:11 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 8, 2023 21:13 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram changed the title [WIP][SYCL] Support multiple call operators in kernel [SYCL] Support multiple call operators in kernel functor Mar 8, 2023
@srividya-sundaram
Copy link
Contributor Author

@intel/dpcpp-cfe-reviewers I would like some early feedback on the PR while I update the test. Thanks!

@srividya-sundaram srividya-sundaram marked this pull request as ready for review March 8, 2023 21:28
@srividya-sundaram srividya-sundaram requested a review from a team as a code owner March 8, 2023 21:28
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 10, 2023 05:19 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 10, 2023 05:48 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram changed the title [SYCL] Support multiple call operators in kernel functor [WIP] [SYCL] Support multiple call operators in kernel functor Mar 10, 2023
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 15, 2023 21:18 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 15, 2023 21:56 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 16, 2023 05:41 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws March 16, 2023 06:20 — with GitHub Actions Inactive
@srividya-sundaram
Copy link
Contributor Author

@intel/dpcpp-cfe-reviewers ping

@srividya-sundaram srividya-sundaram temporarily deployed to aws April 3, 2023 20:04 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws April 3, 2023 21:20 — with GitHub Actions Inactive
@Fznamznon
Copy link
Contributor

Also, please pay attention to pre-commit testing, it seems something is wrong with it. Maybe merging sycl back to your branch will help.

@srividya-sundaram srividya-sundaram temporarily deployed to aws April 6, 2023 06:11 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws April 6, 2023 19:14 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws April 6, 2023 21:02 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws April 6, 2023 21:03 — with GitHub Actions Inactive
@srividya-sundaram
Copy link
Contributor Author

ping @intel/dpcpp-cfe-reviewers

@srividya-sundaram srividya-sundaram temporarily deployed to aws April 8, 2023 20:52 — with GitHub Actions Inactive
@srividya-sundaram srividya-sundaram temporarily deployed to aws April 8, 2023 20:53 — with GitHub Actions Inactive
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.

Looks ok, but I would wait for approval from other codeowners too.

@srividya-sundaram
Copy link
Contributor Author

Looks ok, but I would wait for approval from other codeowners too.

ping @intel/dpcpp-cfe-reviewers

Copy link
Contributor

@elizabethandrews elizabethandrews left a comment

Choose a reason for hiding this comment

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

LGTM

@srividya-sundaram
Copy link
Contributor Author

@intel/llvm-gatekeepers Can you please merge this PR. Thanks!

@steffenlarsen steffenlarsen merged commit 84cc1e1 into intel:sycl Apr 14, 2023
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.

6 participants