Skip to content

[SYCL] Refactor stream class handing implementation #3646

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 34 commits into from
Jun 2, 2021

Conversation

zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Apr 28, 2021

Currently the stream class when passed from host to device requires additional handling.
It is handled as a wrapper struct for several accessors. In order to create the AST, the stream class is processed as a whole and each accessor is processed individually.
This patch is to simplify the process. An __init and __finalize methods are introduced that eliminate the need to process the field individually. The steps are as follows:

  1. Look up for the member function sycl::stream::__init
  2. Copy the sycl::stream::__init arguments into the kernel function argument list
  3. Allocate a sycl::stream object and call sycl::stream::__init with the kernel argument
  4. Call sycl::stream::__finalize with the kernel argument at the end of the kernel

For this simple stream class:
sycl::handler H;
struct HasStreams {
// stream(totalBufferSize, workItemBufferSize, handler)
sycl::stream s1{0, 0, H};

};
int main() {
sycl::stream in_lambda{0, 0, H};

myQueue.submit([&](sycl::handler &h) {
h.single_task(= {
in_lambda.use();
});
});
return 0;
}

This is the resulting AST:

// Function Declaration
// CHECK: FunctionDecl {{.}}stream_test{{.}}

// CHECK: InitListExpr {{.}} '(lambda at
// CHECK-NEXT: CXXConstructExpr {{.
}} 'sycl::stream':'sycl::stream' 'void () noexcept'

// CHECK: CXXMemberCallExpr {{.}} 'void'
// CHECK-NEXT: MemberExpr {{.
}} 'void (__global char , range<1>, range<1>, id<1>, int)' lvalue .__init
// CHECK-NEXT: MemberExpr {{.
}} 'sycl::stream':'sycl::stream' lvalue .
// CHECK-NEXT: DeclRefExpr {{.}} '(lambda at
// CHECK-NEXT: ImplicitCastExpr {{.
}} '__global char '
// CHECK-NEXT: DeclRefExpr {{.
}} '__global char *' lvalue ParmVar

// CHECK: CXXMemberCallExpr {{.}} 'void'
// CHECK-NEXT: MemberExpr {{.
}} 'void ()' lvalue .__finalize
// CHECK-NEXT: MemberExpr {{.}} 'sycl::stream':'sycl::stream' lvalue .
// CHECK-NEXT: DeclRefExpr {{.
}} '(lambda at

Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@zahiraam zahiraam changed the title Stream handing implementation - PR2268 Stream handing implementation - (PR2268) Apr 28, 2021
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@zahiraam zahiraam changed the title Stream handing implementation - (PR2268) Stream handing implementation Apr 29, 2021
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@zahiraam zahiraam changed the title Stream handing implementation [SYCL]Stream handing implementation Apr 29, 2021
@zahiraam zahiraam requested a review from AaronBallman April 29, 2021 12:24
@AaronBallman
Copy link
Contributor

FWIW, I don't feel particularly qualified to review this, so I'd recommend adding @elizabethandrews and @premanandrao as reviewers as well.

@bader
Copy link
Contributor

bader commented Apr 29, 2021

@erichkeane, @Fznamznon, FYI.

@zahiraam
Copy link
Contributor Author

This is not quite working yet (draft)! I am still in the process of understanding the code.
There stream.cpp on the sema side is failing and on the codegen side the __finalize function is not generated. If any of you can take a look, that would be great.

@zahiraam zahiraam requested a review from premanandrao April 29, 2021 14:06
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.

From a quick look, FE changes look like it is heading in right direction. I haven't looked at the new stream implementation in headers. I suspect you also need to change AST tests with streams in them, since the InitListExpr generated now will be different. If these tests don't exist please add one to ensure AST generated for OpenCL kernel with streams is correct.

@elizabethandrews
Copy link
Contributor

FWIW, I don't feel particularly qualified to review this, so I'd recommend adding @elizabethandrews and @premanandrao as reviewers as well.

@AaronBallman the gist of this change is - stream was defined as a class holding accessors (@zahiraam please correct me if my memory serves me wrong). In SemaSYCL, we used to 'process' the stream class as a whole, and then step into this class to process each accessor individually. This change is introducing a __init( ) method in stream definition which makes it unnecessary to step into stream class and process its individual fields. We can now just call the __init( ) method to handle stream arguments like we do with other special types.

Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
Copy link
Contributor

@keryell keryell left a comment

Choose a reason for hiding this comment

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

Always nice to simplify the code.
Perhaps the PR title should summarize more what is done in the PR?
Just curious: is it possible to have a stream to be hidden in some class, say a std::tuple in some application using meta-programming and the stream work if the object is capture by the kernel lambda?

zahiraam added 2 commits May 3, 2021 05:03
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@zahiraam
Copy link
Contributor Author

zahiraam commented May 3, 2021

@Fznamznon, @elizabethandrews This is not quite complete. But before going further (stream class LIT test needs editing) I want to make sure that this is moving in the right direction. Previously, the __init and __finalize of the fields were generated together, now each field has an __init and __finalize back to back. Is that correct? Did I put the createSpecialMethodCall with the finalize argument in the right place?
Thanks.

@zahiraam
Copy link
Contributor Author

zahiraam commented May 3, 2021

Also should I add documentation only of a description of the init/finalize function creation in the AST or should it be a complete description as it is in #2091 ? Not sure if the work in this PR is already up-streamed?

@zahiraam
Copy link
Contributor Author

zahiraam commented May 4, 2021

Always nice to simplify the code.
Perhaps the PR title should summarize more what is done in the PR?
Just curious: is it possible to have a stream to be hidden in some class, say a std::tuple in some application using meta-programming and the stream work if the object is capture by the kernel lambda?

@keryell Thanks for the feedback. Let me know if the description is up to your expectations.
Not sure about your question. I can make some inquiries. Would you mind illustrating this with a test case?
Thanks.

zahiraam added 2 commits May 4, 2021 05:44
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@keryell
Copy link
Contributor

keryell commented May 4, 2021

Just curious: is it possible to have a stream to be hidden in some class, say a std::tuple in some application using meta-programming and the stream work if the object is capture by the kernel lambda?

Not sure about your question. I can make some inquiries. Would you mind illustrating this with a test case?

Sure!
Can the following

int main() {
  std::tuple<sycl::stream> in_lambda { {0, 0, H} };

  myQueue.submit([&](sycl::handler &h) {
    h.single_task([=] {
      std::get<0>(in_lambda).use();
    });
  });
  return 0;
}

just work?
There were a lot of discussions to have accessor working even when hidden in some classes so I was wondering whether we cannot have also this generalized to other SYCL magical types...

zahiraam added 2 commits May 18, 2021 04:07
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@zahiraam zahiraam mentioned this pull request May 18, 2021
@zahiraam
Copy link
Contributor Author

@premanandrao, @againull Review please. Thanks.

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.

In handleSyclStreamType for SyclOptReportCreator, there is a comment which can be removed with this change. Can you do that in this PR?

// CHECK-SAME: %[[ID_TYPE]]* byval(%[[ID_TYPE]]) align 4 [[ACC_ID:%[a-zA-Z0-9_]+]],
// CHECK-SAME: i32 [[ACC_INT:%[a-zA-Z0-9_]+]])

// CHECK: call spir_func void @{{.*}}__init{{.*}}(%{{.*}}cl::sycl::stream{{.*}} addrspace(4)* align 4 dereferenceable_or_null(16) %4, i8 addrspace(1)* %5, %[[RANGE_TYPE]]* byval(%[[RANGE_TYPE]]) {{.*}}%{{.*}}
Copy link
Contributor

Choose a reason for hiding this comment

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

%5 here is ACC_DATA right? Since the parameters to __init is important here, I think it will be useful to check it in this test (as opposed to just checking the call to __init).

zahiraam added 2 commits May 26, 2021 08:20
Signed-off-by: Zahira Ammarguellat <zahira.ammarguellat@intel.com>
@Fznamznon
Copy link
Contributor

/summary:run

Fznamznon
Fznamznon previously approved these changes May 27, 2021
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! Thanks!

@bader
Copy link
Contributor

bader commented Jun 2, 2021

@againull, @intel/llvm-reviewers-runtime, ping.

@bader bader changed the title [SYCL]Stream handing implementation [SYCL] Refactor stream class handing implementation Jun 2, 2021
Copy link
Contributor

@againull againull left a comment

Choose a reason for hiding this comment

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

Thank you! LGTM

@bader bader merged commit 15bbed8 into intel:sycl Jun 2, 2021
alexbatashev pushed a commit to alexbatashev/llvm that referenced this pull request Jun 4, 2021
* sycl: (320 commits)
  [SYCL] Silence a "local variable is initialized but not referenced" warning; NFC (intel#3870)
  [SYCL] Improve SYCL_DEVICE_ALLOWLIST (intel#3826)
  [SPIR-V] Change return value of mapType function (intel#3871)
  [SYCL] Fix post-commit failure in handler.hpp from unused-parameters. (intel#3874)
  [Driver][SYCL] Do not imply defaultlib msvcrt for Linux based driver on Windows (intel#3827)
  [SYCL] Unique stable name rebase (intel#3835)
  [SYCL] Align behavior of empty command groups with SYCL2020 (intel#3822)
  [SYCL][ESIMD] Make typenames and constants consistent with SYCL API style. (intel#3850)
  [SYCL] Allow __failed_assertion to support libstdc++-11 (intel#3774)
  [SYCL] Refactor stream class handing implementation (intel#3646)
  [SYCL] Fix syntax error introduced in intel#3401 (intel#3861)
  [SYCL] SYCL 2020 sub_group algorithms (intel#3786)
  [Buildbot][NFC] Add option to use LLD as linker (intel#3866)
  Revert "Emit correct location lists with basic block sections."
  [SPIRITTAnnotations] Fix debug info for ITT calls. (intel#3829)
  [SYCL][Doc] Fix build of Sphinx docs (intel#3863)
  [SYCL][FPGA][NFC] Tidy up intel_fpga_reg codegen test (intel#3810)
  [CODEOWNERS] Fix SPIRITTAnnnotations tests ownership (intel#3859)
  [SYCL][ESIMD] Host-compile simd.cpp test, fix errors & warnings. (intel#3846)
  [SYCL] Store pointers to memory allocations instead of iterators (intel#3860)
  ...
@zahiraam zahiraam deleted the stream-class branch October 22, 2021 13:18
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.

8 participants