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] Add global_device and global_host address spaces #1704

Merged
merged 12 commits into from
Jun 5, 2020

Conversation

MrSidims
Copy link
Contributor

@MrSidims MrSidims commented May 18, 2020

This patch introduces 2 new address spaces in OpenCL: global_device
and global_host which are a subset of a global address space.

We want to give the user a way to tell the compiler the allocation
type of a USM pointer for optimization purposes. While it is usually
easy for our compiler to distinguish loads or stores that access local
memory from those that access global memory, distinguishing USM pointers
that access host memory from those that access device memory or even
distinguishing USM pointers that access host memory from accessors that
access global memory is currently impossible. This is because all
host code has been stripped out before we reach the backend and both
accessors and USM pointers are presenting in LLVM IR as pointers in the
global address space in the kernel's arguments. Being able to distinguish
between these types of pointers at compile time is valuable because it allows
us to instantiate simpler load-store units to perform memory transactions.

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

@bader
Copy link
Contributor

bader commented May 18, 2020

This patch introduces 2 new address spaces in OpenCL: global_device
and host_device which are a subset of a global address space.

host_device? You meant to say global_host? I think usm_device and usm_host names better reflect the purpose of new address spaces.

We want to give the user a way to tell the compiler the allocation
type of a USM pointer for optimization purposes. While it is usually
easy for our compiler to distinguish loads or stores that access local
memory from those that access global memory, distinguishing USM pointers
that access host memory from those that access device memory or even
distinguishing USM pointers that access host memory from accessors that
access global memory is currently impossible.

Do we really need to separate all three types of allocation or just two is enough?

This is because all
host code has been stripped out before we reach the backend and both
accessors and USM pointers are presenting in LLVM IR as pointers in the
global address space in the kernel's arguments.

This is inaccurate statement. Even if we preserve the host code it won't be possible to disambiguate the allocation type w/o additional annotations if allocation is done in separate translation unit.

Tagging @GarveyJoe.

clang/include/clang/AST/Type.h Show resolved Hide resolved
clang/lib/Basic/Targets/NVPTX.h Outdated Show resolved Hide resolved
clang/lib/Basic/Targets/TCE.h Outdated Show resolved Hide resolved
@Naghasan
Copy link
Contributor

We want to give the user a way to tell the compiler the allocation
type of a USM pointer for optimization purposes. While it is usually
easy for our compiler to distinguish loads or stores that access local
memory from those that access global memory, distinguishing USM pointers
that access host memory from those that access device memory or even
distinguishing USM pointers that access host memory from accessors that
access global memory is currently impossible.

Do we really need to separate all three types of allocation or just two is enough?

This makes global a "virtual" address space (like generic), IMO it makes sense to have the 3 of them (2 concretes + the overlapping one).

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.

global_device and host_device which are a subset of a global address space.

Agree with @Naghasan, I don't see the logic which makes them subset of global address space and allows conversions. Is this intention?

Comment on lines 3553 to 3554
in global memory on device. It is supposed to be used only in SYCL headers and
not in the actual OpenCL/SYCL user code. It helps distinguishing USM pointers
Copy link
Contributor

Choose a reason for hiding this comment

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

It is strange that attributes named OpenCLAddressSpace... can appear only in SYCL headers. I like the idea with usm* naming.

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.

@bader @Fznamznon : I don't know enough about the address spaces, so I hope that you and @Naghasan can make sure you review that part for me.

clang/include/clang/Basic/AttrDocs.td Outdated Show resolved Hide resolved
clang/include/clang/Basic/AttrDocs.td Outdated Show resolved Hide resolved
clang/lib/AST/MicrosoftMangle.cpp Outdated Show resolved Hide resolved
@bader bader requested a review from GarveyJoe May 18, 2020 13:06
This patch introduces 2 new address spaces in OpenCL: usm_device
and usm_host which are a subset of a global address space.

We want to give the user a way to tell the compiler the allocation
type of a USM pointer for optimization purposes. While it is usually
easy for our compiler to distinguish loads or stores that access local
memory from those that access global memory, distinguishing USM pointers
that access host memory from those that access device memory or even
distinguishing USM pointers that access host memory from accessors that
access global memory is currently impossible. This is because all
host code has been stripped out before we reach the backend and both
accessors and USM pointers are presenting in LLVM IR as pointers in the
global address space in the kernel's arguments. Being able to distinguish
between these types of pointers at compile time is valuable because it allows
us to instantiate simpler load-store units to perform memory transactions.

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

MrSidims commented May 18, 2020

Thanks for the early feedback!
Some comments on general question/proposals:

  1. Renaming to usm_

It indeed seems to be a better naming, will rename.

  1. Do we really need to separate all three types of allocation or just two is enough?

@Naghasan is right, global AS will become kinda a "virtual" address space with one remark: at this point we don't have any plans to change the rules for OpenCL built-in types and therefore it's just another +1 to leaving global AS.

  1. global to global_device and global_host (usm_device and usm_host) AS conventions

I was planning to add this functionality in a different PR, but since current implementation makes the commit message be misleading (and I don't really want to change the part, where new address spaces are claimed to be a subset of global address space), I'll bring it here as well.

  1. Breaking other targets, like NVPTX

Good catch, thanks! Some thought about possibility to break existing SYCL code designs for different targets and design notes. There will be an extension in SPIR-V added that will bring SPIR-V representation of the address spaces (most likely new storage classes). In my POC I've also made a patch in clang driver, that denies support of this extension for all devices but FPGA (aka only for AOT compilation for FPGA this extension will be added to be supported, until adoption by other devices' backends). So in response for the new address spaces in SPIR-V we would see CrossWorkgroup storage class and in reversed translation we would see in LLVM IR addrspace(1) as for just global.

  1. Mangling

Thanks Erich!

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

Addressed the comments

@MrSidims MrSidims changed the title [SYCL] Add global_device and global_host address spaces [SYCL] Add usm_device and usm_host address spaces May 19, 2020
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.

Other than the one change, I think this is OK. I want @Naghasan's approval first though, the details of the address space aren't as familiar to me as they seem to be to him.

clang/include/clang/Basic/AttrDocs.td Outdated Show resolved Hide resolved
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
Fznamznon
Fznamznon previously approved these changes May 19, 2020
clang/include/clang/AST/Type.h Outdated Show resolved Hide resolved
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
@GarveyJoe
Copy link
Contributor

One argument in favor of global_device/global_host instead of usm_device/usm_host is that we'd also like accessor pointers to be in the *_device address space. Thus it could be misleading if we prefix it with "usm" when pointers in that AS aren't always allocated through USM mechanisms.

@bader
Copy link
Contributor

bader commented May 20, 2020

One argument in favor of global_device/global_host instead of usm_device/usm_host is that we'd also like accessor pointers to be in the *_device address space. Thus it could be misleading if we prefix it with "usm" when pointers in that AS aren't always allocated through USM mechanisms.

I'm not sure I follow this use case. @GarveyJoe, could you elaborate on that or provide a short code snippet, please?

@GarveyJoe
Copy link
Contributor

GarveyJoe commented May 20, 2020

I'm not sure I follow this use case. @GarveyJoe, could you elaborate on that or provide a short code snippet, please?

One motivation for adding these new address spaces is to (in a subsequent change) automatically have the frontend put all pointers that come from accessors into the device address space. This will allow backends that can exploit this information to perform additional optimization. Without this information, backends must assume that all accessor-derived pointers can point into host memory as they are in the global address space.
Of course, as @MrSidims mentioned, this will need to be done carefully so that targets that don't understand these address spaces don't choke on them. His suggestion was to only enable them to be generated if the target is AoT FPGA. I think a cleaner approach would be to always generate them from the frontend and to preserve them during IR->SPIR-V translation but to convert them to address space 1 during SPIR-V->IR translation if the given target doesn't support them. I.e. an opt-in flag would need to be passed to translator during reverse translation to preserve the new address spaces. I like this approach more because it plays nicely with JiT; in this way you don't need to know ahead of time if your target will be able to take advantage of this information.

@bader
Copy link
Contributor

bader commented May 20, 2020

I'm definitely missing something. According to my understanding, the problem we are trying to solve by adding new attributes is to generate more efficient FPGA code for memory access to allocations other than "allocation on the host in USM mode". This justifies the need for usm_host attribute.
What is not clear:

  • do we need differentiate "allocation on the device in USM mode" from clCreateBuffer alloation?
  • Why global_device/global_host better names for new attributes than usm_device/usm_host?

@GarveyJoe
Copy link
Contributor

@bader and I synched up offline. I think the part he was missing was that we are currently putting USM pointers in global and consequently they can't be distinguished from accessors.

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.

Looking through the code, it seems OK, but I don't know enough about how we use address spaces to approve this commit. @Fznamznon and @bader will have to do that I believe.

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.

I'm a bit confused. It seems those address spaces are not only for USM, but there is still some USM namings.
The code overall looks ok, but it would be great if @Naghasan take a looks as well.

clang/include/clang/Basic/Attr.td Outdated Show resolved Hide resolved
clang/include/clang/AST/Type.h Outdated Show resolved Hide resolved
clang/lib/AST/ItaniumMangle.cpp Outdated Show resolved Hide resolved
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
Fznamznon
Fznamznon previously approved these changes Jun 3, 2020
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.

Small nit, otherwise LGTM

clang/include/clang/AST/Type.h Outdated Show resolved Hide resolved
Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
@bader
Copy link
Contributor

bader commented Jun 3, 2020

@Naghasan, could you take a look at updates, please?

Copy link
Contributor

@Naghasan Naghasan left a comment

Choose a reason for hiding this comment

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

Sorry for the late feedback.

LGTM, to avoid mangling breakage (and assert/crash) it may be good to query if it is supported by the target in Sema (so store a flag in TargetInfo and querry when parsing the address space). But maybe this can be done later.

SYCL NVPTX is not going through SPIR-V, so the round trip is not going to protect it.

@bader
Copy link
Contributor

bader commented Jun 5, 2020

Thanks!

@MrSidims, please, update the description, so I can used it as commit message for squashed commit.

@MrSidims MrSidims changed the title [SYCL] Add usm_device and usm_host address spaces [SYCL] Add global_device and global_host address spaces Jun 5, 2020
@MrSidims
Copy link
Contributor Author

MrSidims commented Jun 5, 2020

Thanks!

@MrSidims, please, update the description, so I can used it as commit message for squashed commit.

Done

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.

7 participants