[HIP] Register/unregister device fat binary only once
authorYaxun Liu <Yaxun.Liu@amd.com>
Fri, 20 Jul 2018 22:45:24 +0000 (22:45 +0000)
committerYaxun Liu <Yaxun.Liu@amd.com>
Fri, 20 Jul 2018 22:45:24 +0000 (22:45 +0000)
commitf99752b66b43a29e4ade199831f9e3f14d873090
tree0ec4df0d9b823f089578482e5120ca4bb8927e88
parent32f7fb57138ad72190abd579732d70103c5b0f52
[HIP] Register/unregister device fat binary only once

HIP generates one fat binary for all devices after linking. However, for each compilation
unit a ctor function is emitted which register the same fat binary. Measures need to be
taken to make sure the fat binary is only registered once.

Currently each ctor function calls __hipRegisterFatBinary and stores the returned value
to __hip_gpubin_handle. This patch changes the linkage of __hip_gpubin_handle to be linkonce
so that they are shared between LLVM modules. Then this patch adds check of value of
__hip_gpubin_handle to make sure __hipRegisterFatBinary is only called once. The code
is equivalent to

void *_gpubin_handle;
void ctor() {
  if (__hip_gpubin_handle == 0) {
    __hip_gpubin_handle = __hipRegisterFatBinary(...);
  }
  // register kernels and variables.
}
The patch also does similar change to dtors so that __hipUnregisterFatBinary
is called once.

Differential Revision: https://reviews.llvm.org/D49083

llvm-svn: 337631
clang/lib/CodeGen/CGCUDANV.cpp
clang/test/CodeGenCUDA/device-stub.cu