[cfe-dev] [RFC] Re-use OpenCL address space attributes for SYCL

David Rector via cfe-dev cfe-dev at lists.llvm.org
Wed Jul 29 16:33:08 PDT 2020


If I understand their proposal correctly, from the discussion Alexey linked to in https://reviews.llvm.org/D80932#2073542 <https://reviews.llvm.org/D80932#2073542>, the primary motivation for their implementation — its main distinction from OpenCL’s approach -- is that they want to support address spaces in such a way that existing C++ files without any address space markings can still compile as is.

That definitely seems like a worthy goal if it could be properly accomplished. 

Take, for instance, the "const" qualifier.  Code which never uses it whatsoever still works by default; only when we start adding "const" into our code could we possibly start breaking other code.  That is the ideal way to introduce a language feature: old code still works, but now people can opt in to the new stuff.

If instead const-ness had been implemented by allowing each type to be either "const" or (let’s say) "mutable" *or neither*, and what is more we implicitly added "mutable" when no such marking was provided to some *but not all* types, then the users would not have the option of ignoring it altogether, it would be a real headache.

This seems to be how OpenCL is implemented, as Alexey et al identify in the discussion linked above: certain VarDecl types get an implicit __private qualifier, but e.g. template argument types, and certain other types (they give another example beyond std::is_same which presents problems) get no such implicit qualifier, resulting in possible breakage in any code whose address spaces have not been closely considered.

Alexey et al's alternative to prevent this breakage, if I understand correctly, is to remove the type qualifier, and instead handle all address space semantics in CodeGen (I believe this is what you refer to as keeping the address space "flat").  

It seems to me that approach is not ideal, though, because 
  a) it seems they would lose the type-checking benefits of implementing as a type qualifier (e.g. imagine if "const" qualifiers were removed and handled instead in CodeGen), and 
  b) I think it really is important for the AST to maintain a clear representation of all target-independent semantics, including address spaces, so that users can easily reason about their code in AST matchers etc.

So the ideal, it seems to me, for everyone’s sake, would be for OpenCL qualifiers to behave more like "const" — there would be a default, a la "non-const", that is applied to all types not explicitly qualified, so that one could enable OpenCL mode and regular code would still work by default.

In reality though, I imagine this has all already been thought over thoroughly, and it has been determined it really is unavoidable to break standard C++ semantics in order to support address spaces; that there really is no default that could be inferred for arbitrary types like those used in template arguments.  

But perhaps it is worthwhile to think it through one more time, to question whether there may be some way to deduce type qualifiers properly in every case, because the issue that Alexey et al raise is a good one I think.

> On Jul 29, 2020, at 5:42 PM, Anastasia Stulova <Anastasia.Stulova at arm.com> wrote:
> 
> > I am not well-versed in this, but just thinking about these as arbitrary type qualifiers: could the issue be simply that the implicitly-generated address space qualifiers are *only* being added to the types of VarDecls, rather than to *every* type, including pointee types, template argument types, etc.?
> 
> It is a little bit more complex than that. Most of the types used with objects in OpenCL will get an address space deduced including pointer types. This is because OpenCL is a language dialect for memory segmented architectures and the memory segments pose some limitations resulting in extra language rules. Clang strictly follows OpenCL language spec and  I don't see any issue in the examples Alexey has referred to. If the types differ by address space is_same is expected to return false.
> What I struggle to understand how does this affects SYCL at all? The deduction of address spaces is guarded by OpenCL language mode and it is not set for SYCL as far as I am aware.
> 
> > If it did, I believe those examples would all compile, and code would only break when the user specified began specifying non-default address spaces
> 
> If I understand the design Alexey is proposing correctly the user-specified address spaces are cast to the default address spaces "hiddenly" and the AST always ends up to be in flat address space. This is why I don't see the address space as a good fit. However, I am not sure this is explained explicitly in the RFC, I might have remembered this from some other discussions.
> 
> From: David Rector <davrecthreads at gmail.com>
> Sent: 27 July 2020 22:32
> To: Bader, Alexey <alexey.bader at intel.com>
> Cc: Anastasia Stulova <Anastasia.Stulova at arm.com>; cfe-dev (cfe-dev at lists.llvm.org) <cfe-dev at lists.llvm.org>; rjmccall at apple.com <rjmccall at apple.com>; nd <nd at arm.com>
> Subject: Re: [cfe-dev] [RFC] Re-use OpenCL address space attributes for SYCL
>  
> On Jul 27, 2020, at 12:18 PM, Bader, Alexey via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
> > > I don't think (2) deal with language semantics. I assume we both talking about
> > > the same case when variable declaration is not explicitly annotated with address
> > > space attribute. According to language semantics such objects are allocated in
> > > generic address space, but the problem is that most OpenCL implementations have
> > > problems with consuming SPIR-V files with global variables in generic address
> > > space. As an alternative to CodeGen changes we can consider handling this issue
> > > in SPIR-V translator tool.
> > 
> > 
> > I am not really a CodeGen expert, maybe it will be ok. I think it's better if you discuss
> > it with John McCall or someone who is more experienced with CodeGen architecture.
> > 
> > Why don't you just do regular address space deduction in Sema and then cast the
> > deduced address space to generic straight after? You already add similar casts for
> > pointers that are annotated with address spaces through the user code, right?
> > This approach will probably allow to reuse the logic from OpenCL and simplify CodeGen.
>  
> I don't see how it can be done without breaking C++ semantics demonstrated in
> https://reviews.llvm.org/D80932#2073542 <https://reviews.llvm.org/D80932#2073542>. 
> 
> I am not well-versed in this, but just thinking about these as arbitrary type qualifiers: could the issue be simply that the implicitly-generated address space qualifiers are *only* being added to the types of VarDecls, rather than to *every* type, including pointee types, template argument types, etc.?
> 
> I.e., referring to the examples linked to above: perhaps the problem is *not* that that OpenCL changes `int var` to `__private int var`, but rather that it does not *also* change `int* ptr1 = &var` to `__private int* __private ptr1 = &var` (or whatever the proper default qualifiers are) and `std::is_same<T, int>` to `std::is_same<T, __private int>` when in OpenCL (or SYCL) mode.
> 
> If it did, I believe those examples would all compile, and code would only break when the user specified began specifying non-default address spaces, i.e. when they actually used the feature in their code.  In this way, the non-standard semantics could be represented in the AST without affecting the standard semantics.
> 
> In any case that is the form of the ideal solution: sure, don’t break the standard C++ semantics, but also, try to keep a clear representation of any supported-but-non-standard semantics in the AST, I think.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20200729/0a639838/attachment-0001.html>


More information about the cfe-dev mailing list