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

Yaxun Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri May 29 21:46:31 PDT 2020


yaxunl created this revision.
yaxunl added reviewers: tra, rjmccall.

nvcc supports accessing file-scope static device variables in host code by host APIs
like cudaMemcpyToSymbol etc.

HIP let users access device variables in host code by shadow variables. In host compilation,
clang emits a shadow variable for each device variable, and calls `__hipRegisterVariable` to
register it in init function. The address of the shadow variable and the device side mangled
name of the device variable is passed to `__hipRegisterVariable`. Runtime looks up the symbol
by name in the device binary (so called code object, which is actually elf format) to find the
address of the device variable.

The problem with static device variables is that they have internal linkage, therefore their
name may be changed by the linker if there are multiple symbols with the same name. Also
they end up as local symbols in the elf file, whereas the runtime only look up the global symbols.

To support accessing file-scope static device variables in host code, we need to give them
a unique name and external linkage. This can be done by postfixing each static device variable with
a unique CUID (Compilation Unit ID). Also we have to make sure the host compilation and device
compilation of the same compilation unit use identical CUID.

This patch added a 32bit unique CUID for each input file, which is represented by InputAction.
clang initially creates an InputAction for each input file for the host compilation. In HIP action
builder, each InputAction is cloned for each GPU arch, and the CUID is also cloned. In this way,
we guarantee the corresponding device and host compilation for the same file shared the
same CUID, therefore the postfixed device variable and shadow variable share the same name.
On the other hand, different compilation units have different CUID, therefore a static variable
with the same name but in a different compilation unit will have a different name.

Since the static device variables have different name across compilation units, now we let
them have external linkage so that they can be looked up by the runtime.


https://reviews.llvm.org/D80858

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Action.h
  clang/include/clang/Driver/CC1Options.td
  clang/lib/AST/ASTContext.cpp
  clang/lib/CodeGen/CGCUDANV.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Driver/Action.cpp
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCUDA/static-device-var.cu
  clang/test/Driver/hip-cuid.hip

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80858.267427.patch
Type: text/x-patch
Size: 11720 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200530/5c801455/attachment-0001.bin>


More information about the cfe-commits mailing list