<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8">
</head>
<body>
<div style="font-family:sans-serif"><div style="white-space:normal">
<p dir="auto">On 6 Aug 2020, at 15:11, Anastasia Stulova wrote:</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">Just to make it clear - C++ libraries or any existing C++ code doesn't<br>
necessarily stop to compile if address space attributes are being used. It only<br>
fails on illegal use of address spaces defined by Embedded C spec from where<br>
the implementation originates.</p>
</blockquote></div>
<div style="white-space:normal">
<p dir="auto">Alexey is correct that C and C++ require that if a variable <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">x</code> has type<br>
<code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">T</code>, the expression <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">&x</code> must have type <code style="background-color:#F7F7F7; border-radius:3px; margin:0; padding:0 0.4em" bgcolor="#F7F7F7">T *</code>. Anything else is not<br>
strictly conformant, whether it compiles or not. OpenCL is therefore<br>
not strictly conformant to the C/C++ specification, and that’s fine<br>
because OpenCL is an independent language, not just a language extension;<br>
it happens to be defined using C/C++ as a base specification, but its<br>
modifications are always authoritative. But as I understand it, Alexey<br>
just wants a conformant C++ implementation on a different target, and<br>
so needs to follow the rules.</p>
<p dir="auto">It’s good that libc++ is broadly permissive about types qualified<br>
with address spaces, but that doesn’t really change anything.</p>
<p dir="auto">John.</p>
</div>
<div style="white-space:normal"><blockquote style="border-left:2px solid #777; color:#777; margin:0 0 5px; padding-left:5px"><p dir="auto">For example, we have made an evaluation by running type trait tests from libcxx<br>
with a standard header in OpenCL mode and only a few tests failed due to<br>
the address spaces. None of those required changes in the libcxx code. Instead,<br>
they have revealed bugs in clang (that were fixed in release 11 btw) and issues in<br>
tests due to illegal behavior. These issues are expected for the address space<br>
attributes as there are extra restriction and rules on types attributed by the<br>
address spaces that come from Embedded C. If these restrictions are not desirable<br>
I feel the desirable behavior might be significantly different from the intended use<br>
of address space attributes and therefore it might be better to look at alternative<br>
approaches including adding a dedicated attribute that doesn't propagate into a type<br>
qualifier at all.<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">casting a names address space pointer to "generic" pointer is valid operation</p>
</blockquote><p dir="auto">in all GPGPU programming models I'm aware of (including OpenCL)<br>
<br>
I would like to highlight that generic address space and address space<br>
conversions have only been added in OpenCL 2.0. Furthermore, generic address<br>
space is becoming optional functionality in OpenCL 3.0.<br>
<br>
Cheers,<br>
Anastasia<br>
<br>
________________________________<br>
From: Bader, Alexey <alexey.bader@intel.com><br>
Sent: 30 July 2020 20:51<br>
To: David Rector <davrecthreads@gmail.com>; Anastasia Stulova <Anastasia.Stulova@arm.com><br>
Cc: cfe-dev (cfe-dev@lists.llvm.org) <cfe-dev@lists.llvm.org>; rjmccall@apple.com <rjmccall@apple.com>; nd <nd@arm.com><br>
Subject: RE: [cfe-dev] [RFC] Re-use OpenCL address space attributes for SYCL<br>
<br>
<br>
David's understanding is right. We would like to be able to call existing C++<br>
<br>
functions from SYCL applications as much as possible.<br>
<br>
<br>
<br>
I don't know if analogy with the "const" qualifier is a perfect match here, but<br>
<br>
casting a names address space pointer to "generic" pointer is valid operation in<br>
<br>
all GPGPU programming models I'm aware of (including OpenCL). "generic" means<br>
<br>
that memory can be allocation in any address space, so compiler can't assume any<br>
<br>
specific address space and must generate code which is valid for any address<br>
<br>
space. Usually it implies runtime overhead on checking the value of "generic"<br>
<br>
pointer to handle it properly.<br>
<br>
<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">Alexey et al's alternative to prevent this breakage, if I understand</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">correctly, is to remove the type qualifier, and instead handle all address</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">space semantics in CodeGen (I believe this is what you refer to as keeping the</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">address space "flat").</p>
</blockquote><p dir="auto">Not exactly. It works as what you described below - "the ideal". We keep address<br>
<br>
space qualifier if user explicitly applied it, but the difference with OpenCL<br>
<br>
mode is that "implicit" address space is the same as in regular C++ (i.e.<br>
<br>
default) and we allow conversion to "default" C++ address spaces from explicitly<br>
<br>
set named address spaces. C++ "default" is treated as "generic" w/o changing C++<br>
<br>
default address space to OpenCL "generic" address space in Sema type system.<br>
<br>
When SYCL users access memory though SYCL library API memory pointers will be<br>
<br>
annotated with address space attributes to ensure good performance. Users can<br>
<br>
obtain a "raw" pointer from SYCL objects to pass it to some generic C++ function<br>
<br>
and this use case is enabled by the patch <a href="https://reviews.llvm.org/D80932" style="color:#777">https://reviews.llvm.org/D80932</a>.<br>
<br>
<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">It seems to me that approach is not ideal, though, because</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> a) it seems they would lose the type-checking benefits of implementing as a</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> type qualifier (e.g. imagine if "const" qualifiers were removed and handled</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> instead in CodeGen), and</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> b) I think it really is important for the AST to maintain a clear</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> representation of all target-independent semantics, including address</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> spaces, so that users can easily reason about their code in AST matchers</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto"> etc.</p>
</blockquote><p dir="auto">Address space attributes are preserved in AST if they are applied explicitly on<br>
<br>
source code, but they are not implicitly applied to all types.<br>
<br>
Type-checking is performed for OpenCL address space attributes (e.g. casts<br>
<br>
between "named" address spaces are not allowed) and C++ type qualifiers like<br>
<br>
"const" are respected.<br>
<br>
<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">So the ideal, it seems to me, for everyone’s sake, would be for OpenCL</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">qualifiers to behave more like "const" — there would be a default, a la</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">"non-const", that is applied to all types not explicitly qualified, so that</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">one could enable OpenCL mode and regular code would still work by default.</p>
</blockquote><p dir="auto">I believe it's what we have in our implementation.<br>
<br>
<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">In reality though, I imagine this has all already been thought over</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">thoroughly, and it has been determined it really is unavoidable to break</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">standard C++ semantics in order to support address spaces; that there really</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">is no default that could be inferred for arbitrary types like those used in</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">template arguments.</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">But perhaps it is worthwhile to think it through one more time, to question</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">whether there may be some way to deduce type qualifiers properly in every</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">case, because the issue that Alexey et al raise is a good one I think.</p>
</blockquote><p dir="auto">There is LLVM transformation pass which infers address space information at LLVM<br>
<br>
IR level: <a href="https://llvm.org/doxygen/InferAddressSpaces_8cpp_source.html" style="color:#777">https://llvm.org/doxygen/InferAddressSpaces_8cpp_source.html</a><br>
<br>
It helps to avoid performance overhead for regular C++ code called from SYCL<br>
<br>
annotated parts.<br>
<br>
<br>
<br>
From: David Rector <davrecthreads@gmail.com><br>
Sent: Thursday, July 30, 2020 2:33 AM<br>
To: Anastasia Stulova <Anastasia.Stulova@arm.com><br>
Cc: Bader, Alexey <alexey.bader@intel.com>; cfe-dev (cfe-dev@lists.llvm.org) <cfe-dev@lists.llvm.org>; rjmccall@apple.com; nd <nd@arm.com><br>
Subject: Re: [cfe-dev] [RFC] Re-use OpenCL address space attributes for SYCL<br>
<br>
<br>
<br>
If I understand their proposal correctly, from the discussion Alexey linked to in <a href="https://reviews.llvm.org/D80932#2073542" style="color:#777">https://reviews.llvm.org/D80932#2073542</a>, 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.<br>
<br>
<br>
<br>
That definitely seems like a worthy goal if it could be properly accomplished.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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").<br>
<br>
<br>
<br>
It seems to me that approach is not ideal, though, because<br>
<br>
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<br>
<br>
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.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
On Jul 29, 2020, at 5:42 PM, Anastasia Stulova <Anastasia.Stulova@arm.com<<a href="mailto:Anastasia.Stulova@arm.com" style="color:#777">mailto:Anastasia.Stulova@arm.com</a>>> wrote:<br>
<br>
<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">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.?</p>
</blockquote><p dir="auto">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.<br>
<br>
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.<br>
<br>
<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">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</p>
</blockquote><p dir="auto">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.<br>
<br>
<br>
<br>
________________________________<br>
<br>
From: David Rector <davrecthreads@gmail.com<<a href="mailto:davrecthreads@gmail.com" style="color:#777">mailto:davrecthreads@gmail.com</a>>><br>
Sent: 27 July 2020 22:32<br>
To: Bader, Alexey <alexey.bader@intel.com<<a href="mailto:alexey.bader@intel.com" style="color:#777">mailto:alexey.bader@intel.com</a>>><br>
Cc: Anastasia Stulova <Anastasia.Stulova@arm.com<<a href="mailto:Anastasia.Stulova@arm.com" style="color:#777">mailto:Anastasia.Stulova@arm.com</a>>>; cfe-dev (cfe-dev@lists.llvm.org<<a href="mailto:cfe-dev@lists.llvm.org" style="color:#777">mailto:cfe-dev@lists.llvm.org</a>>) <cfe-dev@lists.llvm.org<<a href="mailto:cfe-dev@lists.llvm.org" style="color:#777">mailto:cfe-dev@lists.llvm.org</a>>>; rjmccall@apple.com<<a href="mailto:rjmccall@apple.com" style="color:#777">mailto:rjmccall@apple.com</a>> <rjmccall@apple.com<<a href="mailto:rjmccall@apple.com" style="color:#777">mailto:rjmccall@apple.com</a>>>; nd <nd@arm.com<<a href="mailto:nd@arm.com" style="color:#777">mailto:nd@arm.com</a>>><br>
Subject: Re: [cfe-dev] [RFC] Re-use OpenCL address space attributes for SYCL<br>
<br>
<br>
<br>
On Jul 27, 2020, at 12:18 PM, Bader, Alexey via cfe-dev <cfe-dev@lists.llvm.org<<a href="mailto:cfe-dev@lists.llvm.org" style="color:#777">mailto:cfe-dev@lists.llvm.org</a>>> wrote:<br>
</p>
<blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">I don't think (2) deal with language semantics. I assume we both talking about</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">the same case when variable declaration is not explicitly annotated with address</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">space attribute. According to language semantics such objects are allocated in</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">generic address space, but the problem is that most OpenCL implementations have</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">problems with consuming SPIR-V files with global variables in generic address</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">space. As an alternative to CodeGen changes we can consider handling this issue</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><blockquote style="border-left:2px solid #777; color:#BBB; margin:0 0 5px; padding-left:5px; border-left-color:#BBB"><p dir="auto">in SPIR-V translator tool.</p>
</blockquote></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">I am not really a CodeGen expert, maybe it will be ok. I think it's better if you discuss</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">it with John McCall or someone who is more experienced with CodeGen architecture.</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"></blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">Why don't you just do regular address space deduction in Sema and then cast the</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">deduced address space to generic straight after? You already add similar casts for</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">pointers that are annotated with address spaces through the user code, right?</p>
</blockquote><blockquote style="border-left:2px solid #777; color:#999; margin:0 0 5px; padding-left:5px; border-left-color:#999"><p dir="auto">This approach will probably allow to reuse the logic from OpenCL and simplify CodeGen.</p>
</blockquote><p dir="auto">I don't see how it can be done without breaking C++ semantics demonstrated in<br>
<br>
<a href="https://reviews.llvm.org/D80932#2073542" style="color:#777">https://reviews.llvm.org/D80932#2073542</a>.<br>
<br>
<br>
<br>
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.?<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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.<br>
<br>
<br>
<br>
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.</p>
</blockquote></div>
<div style="white-space:normal">
</div>
</div>
</body>
</html>