CUDA/HIP: Use kernel name to map to symbol
authorDaniele Castagna <dcastagna@rivosinc.com>
Sun, 25 Dec 2022 20:36:41 +0000 (12:36 -0800)
committerDaniele Castagna <dcastagna@rivosinc.com>
Thu, 19 Jan 2023 23:02:14 +0000 (15:02 -0800)
commit32c26e27b6fcd12703dcd00adf178330d0ad8449
treecf184879e13814d25dd6cce8a76a056e58048595
parent1f08d3bc3a9a018534e52a47027dfb05cb4f55b3
CUDA/HIP: Use kernel name to map to symbol

Currently CGCUDANV uses an llvm::Function as a key to map kernels to a
symbol in host code.  HIP adds one level of indirection and uses the
llvm::Function to map to a global variable that will be initialized to
the kernel stub ptr.

Unfortunately there is no garantee that the llvm::Function created
by GetOrCreateLLVMFunction will be the same.  In fact, the first
time we encounter GetOrCrateLLVMFunction for a kernel, the type
might not be completed yet, and the type of llvm::Function will be
a generic {}, since the complete type is not required to get a symbol
to a function.  In this case we end up creating two global variables,
one for the llvm::Function with the incomplete type and one for the
function with the complete type. The first global variable will be
declared by not defined, resulting in a linking error.

This change uses the mangled name of the llvm::Function as key in the
KernelHandles map, in this way the same llvm::Function will be
associated to the same kernel handle even if they types are different.

Reviewed By: yaxunl

Differential Revision: https://reviews.llvm.org/D140663
clang/lib/CodeGen/CGCUDANV.cpp
clang/test/CodeGenCUDA/incomplete-func-ptr-type.cu [new file with mode: 0644]