Skip to content

[SYCL] Re-use OpenCL address space attributes for SYCL #1581

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

Conversation

Fznamznon
Copy link
Contributor

Today we re-use OpenCL parsed attributes, but have separate SYCL address
space semantic attributes as current implementation of OpenCL semantics
breaks valid C++. This patch enables re-use of OpenCL semantic
attributes by allowing conversions between types qualified with OpenCL
address spaces and type w/o address space qualifiers. Clang compiler
(almost) always adds address space qualifiers in OpenCL mode, so it
should not affect OpenCL mode.

This is rebased and updated version of #1039

bader and others added 5 commits April 23, 2020 17:16
Today we re-use OpenCL parsed attributes, but have separate SYCL address
space semantic attributes as current implementation of OpenCL semantics
breaks valid C++. This patch enables re-use of OpenCL semantic
attributes by allowing conversions between types qualified with OpenCL
address spaces and type w/o address space qualifiers. Clang compiler
(almost) always adds address space qualifiers in OpenCL mode, so it
should not affect OpenCL mode.

NOTE: this change also disables implicit conversion between the
unqualified types and types qualified with
`__attribute__((address_space(N)))`, enabled by one of the previous SYCL
patches.

Signed-off-by: Alexey Bader <[email protected]>
Signed-off-by: Mariya Podchishchaeva <[email protected]>
Signed-off-by: Mariya Podchishchaeva <[email protected]>
Signed-off-by: Mariya Podchishchaeva <[email protected]>
@@ -26,9 +26,9 @@ SYCL_EXTERNAL size_t __spirv_LocalInvocationId_z();
#else // __SYCL_NVPTX__

typedef size_t size_t_vec __attribute__((ext_vector_type(3)));
extern "C" const __attribute__((opencl_constant))
extern "C" const __attribute__((opencl_global))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you clarify why this is changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Once we re-use OpenCL address space attributes, we re-use diagnostics for them. It seems that OpenCL doesn't allow uninitialized variables in constant address space.

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we use default address space here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think yes. Such change doesn't break my local check-sycl run on CPU device and I don't see any address space requirements in SPIR-V representation in LLVM IR doc .

Copy link
Contributor

Choose a reason for hiding this comment

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

I was troubled by this as well. I couldn't find a requirement from the OpenCL environment spec.

@bashbaug shouldn't the env spec specify the storage class of builtin variables ?

Copy link
Contributor

Choose a reason for hiding this comment

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

shouldn't the env spec specify the storage class of builtin variables ?

Yes, I believe it should. Would you mind filing an issue against the SPIR-V Environment spec on https://github.com/khronosgroup/opencl-docs to get this fixed?

Signed-off-by: Mariya Podchishchaeva <[email protected]>
Naghasan
Naghasan previously approved these changes Apr 24, 2020
erichkeane
erichkeane previously approved these changes Apr 24, 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.

I forgot the justification for the change this is reverting (and now the justification for the revert...) but I remember being properly motivated the last time. The code itself looks fine.

@Fznamznon
Copy link
Contributor Author

@bader , should I fix clang-format fail?
It points to changes disabling OpenCL C++ test.

@bader
Copy link
Contributor

bader commented Apr 24, 2020

Yes formatting issues must be fixed.

@Fznamznon Fznamznon dismissed stale reviews from erichkeane and Naghasan via 8b6519f April 24, 2020 15:31
@bader bader requested review from erichkeane and asavonic April 24, 2020 17:58
@rolandschulz
Copy link
Contributor

I'm a bit confused about the plans after this patch. And it seems like the future direction is important to understand whether using the same set of attributes for OCL and SYCL is a good idea.

A couple of questions regarding the following example (for SYCL compiled with -fsycl-device-only -std=c++17, OCL version is https://godbolt.org/z/AeqF4T).

#include <type_traits>

#define __private __attribute__((opencl_private))
#define __local   __attribute__((opencl_local))

//struct S { void f() __private; }; //should be OK?                                                                                                                                                                

void t() {
    //__private int i1; //should be OK?                                                                                                                                                                            
    int i2;
    static_assert(std::is_same_v<decltype(&i2), int*>); //intentional difference                                                                                                                                   
    __private int* p;
    __local int* l;
    p-l; //should be error "non-overlapping address spaces"?                                                                                                                                                       
}

Why does it error where OpenCL for C++ doesn't and not error in cases where OpenCL does? To me the behavior of OpenCL makes much more sense in all three cases. Are these differences intentional? And if so why? Or are are they considered bugs? If so should those be fixed as part of this change or in follow up changes?

On the other hand there are intentional differences like the one tested with the static_assert. How do we make sure that it'll be easy to maintain those difference going forward if we reuse the same attribute?

@Fznamznon
Copy link
Contributor Author

Fznamznon commented Apr 27, 2020

Why does it error where OpenCL for C++ doesn't and not error in cases where OpenCL does? To me the behavior of OpenCL makes much more sense in all three cases. Are these differences intentional? And if so why? Or are are they considered bugs? If so should those be fixed as part of this change or in follow up changes?

I can see errors in OpenCL for C++ (https://godbolt.org/z/3aaoVQ), but there is no errors in SYCL. It happens because the corresponding diagnostic is wrapped with if (OpenCL) and Sema just doesn't run them for SYCL (see

if (S.getLangOpts().OpenCL && isLHSPointer && isRHSPointer) {
). I think we just didn't pay attention to re-using all the diagnostic for OpenCL address space attributes and I'm not sure that missing those diagnostics in SYCL is a problem at all, because SYCL language doesn't define those attributes for user.

On the other hand there are intentional differences like the one tested with the static_assert. How do we make sure that it'll be easy to maintain those difference going forward if we reuse the same attribute?

Why it can be a problem? Do you have any examples/arguments?

@Naghasan
Copy link
Contributor

Naghasan commented Apr 27, 2020

On the other hand there are intentional differences like the one tested with the static_assert. How do we make sure that it'll be easy to maintain those difference going forward if we reuse the same attribute?

Why it can be a problem? Do you have any examples/arguments?

This introduce a semantic difference between SYCL and OpenCL that needs to be maintained.
So is it realistic to maintain, on a long term, this semantic difference ? Might be good to understand what make this static_assert fails. Looking at the ast dump, i2's type is __private int and int* implicitly gets promoted to __generic int*, so I'm wondering if this is fully contained in SemaType.

On the other hand, is it realistic (especially with the upstreaming objective) to clone the whole logic just because there is a few cases where the logic diverges ? Cloning the logic has plenty of other consequences, and I would personally be much more concerned about maintaining a fully cloned logic rather than maintaining a few and controlled differences.

@Fznamznon
Copy link
Contributor Author

On the other hand there are intentional differences like the one tested with the static_assert. How do we make sure that it'll be easy to maintain those difference going forward if we reuse the same attribute?

Why it can be a problem? Do you have any examples/arguments?

This introduce a semantic difference between SYCL and OpenCL that needs to be maintained.
So is it realistic to maintain, on a long term, this semantic difference ? Might be good to understand what make this static_assert fails. Looking at the ast dump, i2's type is __private int and int* implicitly gets promoted to __generic int*, so I'm wondering if this is fully contained in SemaType.

I think this difference happens because in OpenCL the compiler is intended to add address space qualifiers to all (at least almost all) pointers. AFAIK it is done during parsing, there is a good explanation #1039 (comment). We don't do this for SYCL because it can lead to problems in C++ code (I think @erichkeane can explain it much more better than me).

@rolandschulz
Copy link
Contributor

On the other hand, is it realistic (especially with the upstreaming objective) to clone the whole logic just because there is a few cases where the logic diverges ? Cloning the logic has plenty of other consequences, and I would personally be much more concerned about maintaining a fully cloned logic rather than maintaining a few and controlled differences.

Makes sense.

@rolandschulz
Copy link
Contributor

I think we just didn't pay attention to re-using all the diagnostic for OpenCL address space attributes and I'm not sure that missing those diagnostics in SYCL is a problem at all, because SYCL language doesn't define those attributes for user.

SYCL doesn't define the attributes but it defines multi_ptr::pointer_ptr for the user to use them. Therefore we should fix the any unintentional differences (not just the missing error but also the two cases where it errors when it shouldn't). But it doesn't have to happen as part of this patch.

@tadeaustria
Copy link

since the merge of this I get following errors on compiling a simple sycl program
/CL/__spirv/spirv_vars.hpp:167:10: error: variable in constant address space must be initialized uint32_t __spirv_BuiltInSubgroupLocalInvocationId;

@bader
Copy link
Contributor

bader commented Apr 28, 2020

since the merge of this I get following errors on compiling a simple sycl program
/CL/__spirv/spirv_vars.hpp:167:10: error: variable in constant address space must be initialized uint32_t __spirv_BuiltInSubgroupLocalInvocationId;

This is strange...
This variable is not in constant address space https://github.com/intel/llvm/blob/sycl/sycl/include/CL/__spirv/spirv_vars.hpp#L136.
I suspect that you might be using new compiler + old headers.
@tadeaustria, could you make sure that the spirv_vars.hpp header is up-to-date, please?

@tadeaustria
Copy link

This is strange...
This variable is not in constant address space https://github.com/intel/llvm/blob/sycl/sycl/include/CL/__spirv/spirv_vars.hpp#L136.
I suspect that you might be using new compiler + old headers.
@tadeaustria, could you make sure that the spirv_vars.hpp header is up-to-date, please?

Ah, yes, thanks for the hint! the compiler used outdated headers. Works fine after updating them

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.

10 participants