Skip to content

Commit 53a18a6

Browse files
committed
[hip] Create an alias to the host-side device kernel stub.
- In the host compilation, `hipLaunchKernel` needs the symbol of a device kernel to have the same as the device compilation. But, so far, that device stub is appended with `__device_stub_`. That causes the link error if the source code using `hipLaunchKernel` is compiled as regular C or C++ source code by either gcc or clang. - To solve this issue, an alias is created to that host-side device kernel stub but as a regular data type instead of a function. Change-Id: I1fb8fcb50aefd80ed47f33b49382e139385b4123
1 parent 5429d66 commit 53a18a6

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

clang/lib/CodeGen/CGCUDANV.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/IR/Constants.h"
2323
#include "llvm/IR/DerivedTypes.h"
2424
#include "llvm/Support/Format.h"
25+
#include <cstring>
2526

2627
using namespace clang;
2728
using namespace CodeGen;
@@ -245,6 +246,21 @@ void CGNVCUDARuntime::emitDeviceStub(CodeGenFunction &CGF,
245246
emitDeviceStubBodyNew(CGF, Args);
246247
else
247248
emitDeviceStubBodyLegacy(CGF, Args);
249+
250+
StringRef FnName = CGF.CurFn->getName();
251+
if (FnName.startswith("__device_stub_")) {
252+
StringRef OrigName = FnName.substr(std::strlen("__device_stub_"));
253+
// If the device stub name is different from the original device-side kernel
254+
// name, create an alias from the original kernel name to the device stub
255+
// but as a regular data type instead of function.
256+
llvm::Function *Fn = CGF.CurFn;
257+
llvm::PointerType *Ty = Fn->getType();
258+
llvm::PointerType *AliasTy =
259+
llvm::PointerType::get(CGM.Int8Ty, Ty->getAddressSpace());
260+
llvm::GlobalAlias::create(
261+
CGM.Int8Ty, Ty->getAddressSpace(), Fn->getLinkage(), OrigName,
262+
llvm::ConstantExpr::getBitCast(Fn, AliasTy), &CGM.getModule());
263+
}
248264
}
249265

250266
// CUDA 9.0+ uses new way to launch kernels. Parameters are packed in a local

0 commit comments

Comments
 (0)