[PATCH] D80858: [CUDA][HIP] Support accessing static device variable in host code

Michael Liao via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Jul 28 00:28:43 PDT 2020


hliao added a comment.

In D80858#2177104 <https://reviews.llvm.org/D80858#2177104>, @yaxunl wrote:

> In D80858#2171266 <https://reviews.llvm.org/D80858#2171266>, @hliao wrote:
>
>> We may try to solve the issue without RDC firstly, where we don't need to change that static variable name (if the runtime maintains the device binaries correctly.) We only need to ensure the linker won't remove their symbols.
>
> This is essentially externalizing it in linker. However, it may not work.
>
> Consider a static device var was accessed in host code through hipMemCpyToSymbol or hipMemCpyFromSymbol. In device code, this static var may be initialized through host code, or its final value may be read by host code after kernel execution. The existence of these operations mean that this static device variable `is` actually having `external` linkage, instead of `internal` linkage, since it is accessed by external modules. Fail to reflect this truth in IR will cause optimization passes to make incorrect assumptions about this variable and perform incorrect optimizations on it. e.g. the llvm passes can assume the value in this static var never changes, or its final value will not be used, then the llvm passes may simply remove it. Marking it as `used` will not solve the issue, since the llvm passes may still assume its value never changes after initialization, whereas in reality it may be changed by hipMemCpyToSymbol before kernel execution.

That's what I mean by "externalizing" that variable. In fact, `nvcc` doesn't the same thing and generate `global` symbol in PTX.

To get file-scope static device variables support, we have to 2 separate issues:

- externalize variable to ensure backend/linker preserve their symbols for runtime to query. The major concern is that we'd better check whether a static device variable is referenced using host APIs. Otherwise, aggressive IPO optimizations may not be able to be applied. As those are file-scope static variables, roughly checking their reference on the host side should be doable.
- rename device static variables. Such renaming is only necessary when RDC is enabled. Without RDC, there would be conflicting names and renaming is unnecessary. For renaming, relying on developer supplied CUID is not reliable and we should extend name mangler to include source name, digest, or whatever automatically managed by the compiler. If we support static variable access on the host side, we should not rely on developer supplied option to function correctly. Also, the mangled name should be demangled into a meaningful name if possible. I remembered GCC has special support for names from anonymous namespaces if they have conflicting names. We may follow the similar scheme.


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

https://reviews.llvm.org/D80858



More information about the cfe-commits mailing list