Skip to content
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

[SYCL][FPGA] Add clang support for buffer_location property #2166

Merged
merged 14 commits into from
Aug 5, 2020

Conversation

MrSidims
Copy link
Contributor

@MrSidims MrSidims commented Jul 23, 2020

This is a compiler-time known accessor property which serves as an
optimization hint for a compiler on where exactly buffer was allocated.
This is needed when a board has multiple disjoint global memories that
must be managed explicitly by a programmer.

When the property is added as a template parameter of an accessor -
SemaSYCL will implicitly add an attribute to an OpenCL kernel parameters,
which were generated from SYCL kernel object. It is not allowed to use the
attribute explicitly in SYCL code.

When the attribute is applied, clang generates metadata attached to OpenCL
kernel. Number of values stored in the metadata is the same as number of kernel
parameters. Order of metadata values is following the order of pointer
kernel parameters. Metadata values are of an integer type and is being set
accordingly values passed through accessor property buffer_location.
This values are mapped in hardware backend to the actual locations of buffers
(DDR, QDR etc). Default value passed in the metadata is '-1'.

Signed-off-by: Dmitry Sidorov dmitry.sidorov@intel.com

clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
@MrSidims MrSidims force-pushed the private/MrSidims/BufferLocationAttr branch from 174a54d to 076b49d Compare July 23, 2020 13:57
@MrSidims MrSidims marked this pull request as ready for review July 24, 2020 10:58
@MrSidims MrSidims requested a review from erichkeane July 24, 2020 10:58
MrSidims added 3 commits July 24, 2020 14:06
This is a compiler-time known accessor property which serves as an
optimization hint for a compiler on where exactly buffer was allocated.
This is needed when a board has multiple disjoint global memories that
must be managed explicitly by a programmer.

When the property is added as a template parameter of an accessor -
SemaSYCL will implicitly add ``intelfpga::kernel_arg_buffer_location``
attribute to an OpenCL kernel generated from SYCL kernel object.
It is not allowed to use the attribute explicitly in SYCL code.

When the attribute is applied, clang generates metadata attached to OpenCL
kernel. Number of values stored in the metadata is the same as number of kernel
parameters. Order of metadata values is following the order of pointer
kernel parameters. Metadata values are of an integer type and is being set
accordingly values passed through accessor property ``buffer_location``.
This values are mapped in hardware backend to the actual locations of buffers
(DDR, QDR etc). Default value passed in the metadata is '-1'.

Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
@MrSidims MrSidims force-pushed the private/MrSidims/BufferLocationAttr branch from 5a5ee02 to 49dce75 Compare July 24, 2020 11:07
Copy link
Contributor

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

I'm not a big fan of this approach. The info you really care about is at the parameter level.

First, don't name attributes we don't expect our users to spell.

Second, are we sure this isn't something we can calculate during codegen directly, rather than putting it in sema?

Third, if we cannot, than this is better expressed in the AST as an attribute on the ParmVarDecl instead. So each parameter of the BufferLocation type should have an attribute with the integer value attached to it (again, ONLY if we are sure we cannot figure this out during codegen).

Finally, this patch seems to make assumptions about the layout of the library. We have to be more defensive about this.

We don't necessarily control our the library (a 3rd party could write a version of the library!), nor do we have any guarantee that the version necessarily matches our release. We need to diagnose to protect our assumptions.

clang/include/clang/Basic/DiagnosticSemaKinds.td Outdated Show resolved Hide resolved
clang/lib/CodeGen/CodeGenFunction.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
Copy link
Contributor Author

@MrSidims MrSidims left a comment

Choose a reason for hiding this comment

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

@erichkeane thanks for the early feedback !

Second, are we sure this isn't something we can calculate during codegen directly, rather than putting it in sema?

I think it's too late to do in CodeGen since we are constructing OpenCL kernel in Sema. In CodeGen we already see kernel declarations having pointer parameters instead of accessor objects, so no original information stored in the appropriate QualType can be queried. Also we can't even distinguish accessor pointers from USM pointers in CodeGen! I agree, that this attribute is looking nasty, but I believe for this we shall move OpenCL kernel generation out of Sema to actually CodeGen (where it belongs).

Third, if we cannot, than this is better expressed in the AST as an attribute on the ParmVarDecl instead. So each parameter of the BufferLocation type should have an attribute with the integer value attached to it (again, ONLY if we are sure we cannot figure this out during codegen).

I will think about it.

Finally, this patch seems to make assumptions about the layout of the library. We have to be more defensive about this.

We don't necessarily control our the library (a 3rd party could write a version of the library!), nor do we have any guarantee that the version necessarily matches our release. We need to diagnose to protect our assumptions.

Thanks! I'll add some (warnings?) diagnostics then.

clang/include/clang/Basic/DiagnosticSemaKinds.td Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
@erichkeane
Copy link
Contributor

They'd likely need to be errors, and stop execution. If the buffer isn't the right layout it seems that it should just be an ill-formed program.

@MrSidims
Copy link
Contributor Author

They'd likely need to be errors, and stop execution. If the buffer isn't the right layout it seems that it should just be an ill-formed program.

I disagree. As for now accessor properties are Intel extension and serves only for optimization purposes (semantically they have no affect, but metadata generation). Hence if some one wants to use Intel compiler and a third-party library I see no reasons to deny them this opportunity (emitting warning and stop whatever compiler was up to do with this property is good enough).

@erichkeane
Copy link
Contributor

They'd likely need to be errors, and stop execution. If the buffer isn't the right layout it seems that it should just be an ill-formed program.

I disagree. As for now accessor properties are Intel extension and serves only for optimization purposes (semantically they have no affect, but metadata generation). Hence if some one wants to use Intel compiler and a third-party library I see no reasons to deny them this opportunity (emitting warning and stop whatever compiler was up to do with this property is good enough).

In that case, you absolutely cannot make any assumptions on the layout of them. The purpose of errors is to be an assertion type-thing, where we would require this buffer_location property to be a certain format. If we don't emit those as errors, NO assumptions on them being valid can be made. I'm alright with this, but it means you have to be much more careful with the implementation.

@MrSidims
Copy link
Contributor Author

They'd likely need to be errors, and stop execution. If the buffer isn't the right layout it seems that it should just be an ill-formed program.

I disagree. As for now accessor properties are Intel extension and serves only for optimization purposes (semantically they have no affect, but metadata generation). Hence if some one wants to use Intel compiler and a third-party library I see no reasons to deny them this opportunity (emitting warning and stop whatever compiler was up to do with this property is good enough).

In that case, you absolutely cannot make any assumptions on the layout of them. The purpose of errors is to be an assertion type-thing, where we would require this buffer_location property to be a certain format. If we don't emit those as errors, NO assumptions on them being valid can be made. I'm alright with this, but it means you have to be much more careful with the implementation.

Sure, thanks! For now this implementation doesn't break any SYCL tests, even we have no support for this feature in the headers, so I assume I was quite careful :)

@erichkeane
Copy link
Contributor

They'd likely need to be errors, and stop execution. If the buffer isn't the right layout it seems that it should just be an ill-formed program.

I disagree. As for now accessor properties are Intel extension and serves only for optimization purposes (semantically they have no affect, but metadata generation). Hence if some one wants to use Intel compiler and a third-party library I see no reasons to deny them this opportunity (emitting warning and stop whatever compiler was up to do with this property is good enough).

In that case, you absolutely cannot make any assumptions on the layout of them. The purpose of errors is to be an assertion type-thing, where we would require this buffer_location property to be a certain format. If we don't emit those as errors, NO assumptions on them being valid can be made. I'm alright with this, but it means you have to be much more careful with the implementation.

Sure, thanks! For now this implementation doesn't break any SYCL tests, even we have no support for this feature in the headers, so I assume I was quite careful :)

You weren't particularly, I see a number of assumptions you make, like that the template arguments are types, etc. I pointed out a few, but everything you pull from the buffer_location property isn't something that can be counted on.

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.

Any spec links for this one?
Seems like it is exposed to user.

clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
1. Move attribute from FuncDecl to ParamVarDecl
2. Add several diagnostics
3. Reimplement property handler

Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
@MrSidims
Copy link
Contributor Author

What have changed:
0. I figured out, that my previous implementation version is incorrect: instead of passing buffer_location property to accessor's template parameter, users will pass property_list there, so the implementation was adjusted.

  1. The attribute now is spellingless, also it's being applied to ParmVarDecl instead of Function declaration;
  2. Lowered number of assumptions (or at least I think so), added several diagnostics. @erichkeane some of these diagnostics remains untested, because current implementation of sycl.hpp disallows on CPP level some use cases, when they can fire. I can also add something like fake-sycl.hpp with other accessor/property_list possible implementations to test them, what do you think?

@Fznamznon spec for compiletime known properties can be found here: #1925 . For buffer location itself only SPIR-V spec is public now: #2084 , ping @mohammadfawaz and @GarveyJoe to make SYCL spec public.

@MrSidims MrSidims requested review from erichkeane and Fznamznon July 28, 2020 15:49
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
@MrSidims
Copy link
Contributor Author

Addressed the comments, major:

  • No more '-1' in SemaSYCL, yet I slightly lean towards having it as a default MD value;
  • handleBufferLocationProperty was split into handleAccessorPropertyList and handleBufferLocationProperty

I left @Fznamznon comment #2166 (comment) unaddressed, since I don't really know, how to improve special type detection

Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
@MrSidims MrSidims requested review from Fznamznon and erichkeane July 30, 2020 18:26
clang/include/clang/Basic/AttrDocs.td Outdated Show resolved Hide resolved
clang/lib/CodeGen/CodeGenModule.cpp Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
@MrSidims
Copy link
Contributor Author

Seems like CPU RT in our CI isn't ready for this change SPIR-V-wise, so I have reverted SPIR-V support of the feature for testing.

@MrSidims MrSidims changed the title [SYCL][FPGA] Add clang support for buffer_location property [SYCL][FPGA][DO NOT MERGE] Add clang support for buffer_location property Jul 31, 2020
@MrSidims MrSidims changed the title [SYCL][FPGA][DO NOT MERGE] Add clang support for buffer_location property [SYCL][FPGA] Add clang support for buffer_location property Aug 2, 2020
@MrSidims
Copy link
Contributor Author

MrSidims commented Aug 2, 2020

This patch needs to be merged first: #2240

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 please get approve from @erichkeane first.

Copy link
Contributor

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

See a few missed diagnostics.

clang/lib/Sema/SemaSYCL.cpp Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Outdated Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Show resolved Hide resolved
clang/lib/Sema/SemaSYCL.cpp Show resolved Hide resolved
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
Copy link
Contributor

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

This seems correct now, thanks.

@MrSidims MrSidims requested a review from Fznamznon August 5, 2020 12:56
@vladimirlaz vladimirlaz merged commit 0c38b35 into intel:sycl Aug 5, 2020
jsji pushed a commit that referenced this pull request Oct 5, 2023
…es (#2166)

Implement translation via SPIR-V friendly calls, as:

the LLVM instructions are not capable to accept target extension types;
matrix arithmetic instructions require additional carry additional rules, which LLVM can not perform (for example while technically Add for vectors and (flattened) matrices is the same - yet for matrices we need to perform extra checks, also mul instruction is complitely different).
As for now some BE would need to recognize and define what to do with a call to __spirv_FMul(matrixA, matrixB). Better option is to map such SPIR-V to an intrinsic or define an appropriate type in LLVM (hence defining rules for GEP and other instructions) , but it's off the table now.

Original commit:
KhronosGroup/SPIRV-LLVM-Translator@fdc961f
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.

5 participants