[PATCH] D89909: [SYCL] Implement SYCL address space attributes handling

Anastasia Stulova via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 14 04:35:50 PDT 2021


Anastasia added inline comments.


================
Comment at: clang/lib/AST/ItaniumMangle.cpp:2379
       unsigned TargetAS = Context.getASTContext().getTargetAddressSpace(AS);
-      if (TargetAS != 0)
+      if (TargetAS != 0 || Context.getASTContext().getLangOpts().SYCLIsDevice)
         ASString = "AS" + llvm::utostr(TargetAS);
----------------
bader wrote:
> Anastasia wrote:
> > bader wrote:
> > > Anastasia wrote:
> > > > Any reason not to use OpenCL mangling? If you do then you might be able to link against libraries compiled for OpenCL. Also you will get more stable naming i.e. it would not differ from target to target. 
> > > > Any reason not to use OpenCL mangling? If you do then you might be able to link against libraries compiled for OpenCL. Also you will get more stable naming i.e. it would not differ from target to target. 
> > > 
> > > I'm not sure I understand your suggestion. Could you elaborate on "OpenCL mangling", please?
> > > 
> > > Let me clarify the problem this change addresses. The test case covering it is located in `clang/test/CodeGenSYCL/address-space-parameter-conversions.cpp` lines 86-91.
> > > 
> > > ```
> > > template <typename T>
> > > void tmpl(T t) {}
> > > 
> > > int *NoAS;
> > > __attribute__((opencl_private)) int *PRIV;
> > > 
> > > tmpl(PRIV);
> > > // CHECK-DAG: [[PRIV_LOAD5:%[a-zA-Z0-9]+]] = load i32*, i32* addrspace(4)* [[PRIV]].ascast
> > > // CHECK-DAG: call spir_func void [[PRIV_TMPL:@[a-zA-Z0-9_]+]](i32* [[PRIV_LOAD5]])
> > > tmpl(NoAS);
> > > // CHECK-DAG: [[NoAS_LOAD5:%[a-zA-Z0-9]+]] = load i32 addrspace(4)*, i32 addrspace(4)* addrspace(4)* [[NoAS]].ascast
> > > // CHECK-DAG: call spir_func void [[GEN_TMPL:@[a-zA-Z0-9_]+]](i32 addrspace(4)* [[NoAS_LOAD5]])
> > > ```
> > > Clang has separate code paths for mangling types w/ and w/o address space attributes (i.e. using `Default` address space).
> > > 
> > > Address space is not mangled if there is no AS attribute (`Default`) or if address space attribute is maps to `0` target address space. SPIR target maps `*_private` address space to `0`, which causes name conflict for the example above.
> > > 
> > > This change for SYCL compiler enables mangling for non-default address space attributes regardless of their mapping to target address space.
> > It's just that all language address spaces are mangled with the source spelling in Italium ABI right now, if you check the `else` statement. I don't think it is part of the official spec yet but it might be better to stick to the same pattern if possible.
> > It's just that all language address spaces are mangled with the source spelling in Italium ABI right now, if you check the `else` statement. I don't think it is part of the official spec yet but it might be better to stick to the same pattern if possible.
> 
> I would really love to avoid changes to the mangler (e.g. to be able to link binaries produced by different front-end like SYCL/OpenCL/CUDA), but I don't know the better way to address the issue 
> Sorry, I don't get what do you suggest here. Could you clarify what exactly I should change, please?
For now I am just trying to understand why you are not adopting similar mangling scheme as for other language address spaces since it gives more stable mangling irrespective from the target compiled for.

If you plan to link libraries from other frontends i.e. OpenCL or CUDA the mangling you use is different from what they produce. Just have a look at the  line 2470 that explains OpenCL mangling or line 2494 explaining CUDA mangling. FYI similar scheme applies to other language address spaces, so the `AS<num>` was only really used for the address spaces that have no source spelling i.e. no language semantics.


================
Comment at: clang/lib/Basic/Targets/SPIR.h:129
+    TargetInfo::adjust(Opts);
+    setAddressSpaceMap(/*DefaultIsGeneric=*/Opts.SYCLIsDevice);
+  }
----------------
bader wrote:
> Anastasia wrote:
> > Anastasia wrote:
> > > Ok, do you still plan to add a `FIXME` as mentioned previously explaining why we need to reset the map here?
> > Btw I was just thinking of another alternative here.
> > 
> > What do you think about just setting a value in `Default` AS entry then we wouldn't need any extra map at all in this case? So something like:
> > 
> > 
> > ```
> > AddrSpaceMap[LangAS::Default] = AddrSpaceMap[LangAS::opencl_generic];
> > ```
> > with a good explanation in `FIXME`? :)
> > 
> > Btw I was just thinking of another alternative here.
> > 
> > What do you think about just setting a value in `Default` AS entry then we wouldn't need any extra map at all in this case? So something like:
> > 
> > 
> > ```
> > AddrSpaceMap[LangAS::Default] = AddrSpaceMap[LangAS::opencl_generic];
> > ```
> > with a good explanation in `FIXME`? :)
> > 
> 
> I think this won't work because AddrSpaceMap is a constant.
> Regarding `FIXME`, could you point where it was previously mentioned, please? I think it might miss it. I can add a link to the SYCL spec - https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#subsec:genericAddressSpace. Will it be sufficient?
I mean the FIXME is about the issue with Clang design:
https://reviews.llvm.org/D89909#inline-941733

Basically, just explain why we need to reset the map and that it is only needed for `Default` address space.


================
Comment at: clang/lib/CodeGen/TargetInfo.cpp:9985
+         "Address space agnostic languages only");
+  LangAS DefaultGlobalAS = getLangASFromTargetAS(
+      CGM.getContext().getTargetAddressSpace(LangAS::sycl_global));
----------------
bader wrote:
> Anastasia wrote:
> > bader wrote:
> > > Anastasia wrote:
> > > > Since you are using SYCL address space you should probably guard this line by SYCL mode...  Btw the same seems to apply to the code below as it implements SYCL sematics?
> > > > 
> > > > Can you add spec references here too.
> > > > 
> > > > Also there seems to be nothing target specific in the code here as you are implementing what is specified by the language semantics. Should this not be moved to `GetGlobalVarAddressSpace` along with the other language handling?
> > > > 
> > > > I am not very familiar with this part of address space handling though. I would be more comfortable if @rjmccall could take a look too.
> > > This code assigns target address space "global variables w/o address space attribute". 
> > > SYCL says it's "implementation defined" (from https://www.khronos.org/registry/SYCL/specs/sycl-2020/html/sycl-2020.html#subsec:commonAddressSpace):
> > > 
> > > > Namespace scope
> > > > If the type is const, the address space the declaration is assigned to is implementation-defined. If the target of the SYCL backend can represent the generic address space, then the assigned address space must be compatible with the generic address space.
> > > > Namespace scope non-const declarations cannot be used within a kernel, as restricted in Section 5.4. This means that non-const global variables cannot be accessed by any device kernel or code called by the device kernel.
> > > 
> > > I added clarification that SPIR target allocates global variables in global address space to https://reviews.llvm.org/D99488 (see line #248).
> > > 
> > > @rjmccall, mentioned in the mailing list discussion that this callbacks were developed for compiling C++ to AMDGPU target, so this not necessary designed only for SYCL, but it works for SYCL as well.
> > After all what objects are allowed to bind to non-default address space here is defined in SYCL spec even if the exact address spaces are not defined so it is not completely a target-specific behavior.
> > 
> > My understanding of the API you are extending (judging from its use) is that it allows you to extend the language sematics with some target-specific setup. I.e. you could add extra address spaces to C++ or OpenCL or any other language. But here you are setting the language address spaces instead that are mapped to the target at some point implicitly.
> > 
> > It seems like this change better fits to `CodeGenModule::GetGlobalVarAddressSpace` that already contains very similar logic?
> > 
> > Otherwise, it makes more sense to use target address spaces directly instead of SYCL language address spaces. But either way, we should guard it by SYCL mode somehow as we have not established this as a universal logic for SPIR. 
> > It seems like this change better fits to `CodeGenModule::GetGlobalVarAddressSpace` that already contains very similar logic?
> 
> This was the original implementation (see https://reviews.llvm.org/D89909?id=299795), but @rjmccall suggested to use this callback instead.
> Both ways work for me, but the implementation proposed by John is easier to maintain.
> 
> > Otherwise, it makes more sense to use target address spaces directly instead of SYCL language address spaces. But either way, we should guard it by SYCL mode somehow as we have not established this as a universal logic for SPIR.
> 
> I've updated the code to use target address space. I also added an assertion for SYCL language mode, although I think SPIR doesn't support global variables in address spaces other than global or constant regardless of the language mode, so I think the logic is universal.
> This was the original implementation (see https://reviews.llvm.org/D89909?id=299795), but @rjmccall suggested to use this callback instead.

Did you mean to link some particular conversation? Currently, it resets to the top of the review.

>  Both ways work for me, but the implementation proposed by John is easier to maintain.

I can't see why the same code would be harder to maintain in the caller. If anything it should reduce the maintenance because the same logic won't need to be implemented by every target.

> I also added an assertion for SYCL language mode, although I think SPIR doesn't support global variables in address spaces other than global or constant regardless of the language mode, so I think the logic is universal.

Asserts don't guard this logic to be applied universally. And since the IR was generated like this for about 10 years I don't feel comfortable about just changing it silently.

To my memory SPIR spec never put restrictions to the address spaces. It only described the generation for OpenCL C. So if you compile from C you would have everything in the default address space. And even OpenCL rules doesn't seem to be quite accurate in your patch as in OpenCL C globals can be in `__global`, `__constant` or `__local`. However, the SPIR spec was discontinued quite a while ago and the implementation of SPIR has evolved so I am not sure how relevant the spec is now.

Personally, I feel the behavior you are implementing is governed by the language soI think it is more logical to encapsulate it to avoid interfering with other language modes.



================
Comment at: clang/test/CodeGenSYCL/address-space-parameter-conversions.cpp:80
+  // Ensure that we still get 3 different template instantiations.
+  tmpl(GLOB);
+  // CHECK-DAG: [[GLOB_LOAD4:%[a-zA-Z0-9]+]] = load i32 addrspace(1)*, i32 addrspace(1)* addrspace(4)* [[GLOB]].ascast
----------------
bader wrote:
> Anastasia wrote:
> > bader wrote:
> > > Anastasia wrote:
> > > > What functionality in the patch does this test?
> > > > What functionality in the patch does this test?
> > > 
> > > As I mentioned in the comment for clang/lib/AST/ItaniumMangle.cpp, there was a problem with instantiating templates for parameters which differs only by address space attribute. Lines 79-91 cover mangling changes.
> > You are not testing the mangling though?
> > You are not testing the mangling though?
> 
> Right, what we are checking here is that each instantiation emits a different function in LLVM IR. Exact mangling is not relevant to the check.
Ok, this is the kind of change that we normally expect tested explicitly. I don't believe you would be ok that we modify that freely as we like after you commit the patch.




Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D89909/new/

https://reviews.llvm.org/D89909



More information about the cfe-commits mailing list