Skip to content
This repository was archived by the owner on Jul 1, 2025. It is now read-only.

Fix how mangled C++ functions are handled #6175

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions lib/LLVMIRCodeGen/LLVMIRGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ static std::string createName(const std::string &name, ElemKind elemTy) {
void LLVMIRGen::initLLVMFunctionNameToMangledNameMap() {
CHECK(llvmFunctionNameToMangledName_.empty());
constexpr size_t maxFnBaseNameLen = 4096;
char *fnNameBuf = static_cast<char *>(std::malloc(maxFnBaseNameLen));
char fnNameBuf[maxFnBaseNameLen];
// Build a map from names to the list of matching mangled names.
for (llvm::Function &F : getModule()) {
auto mangledName = F.getName().str();
Expand All @@ -805,6 +805,17 @@ void LLVMIRGen::initLLVMFunctionNameToMangledNameMap() {
continue;
}
size_t fnNameLen = maxFnBaseNameLen;
size_t fnContextLen = maxFnBaseNameLen;
// Skip C++ functions that have names like a::b::c. It helps to avoid name
// conflicts with kernels that may be called just c and conflict with C++
// functions.
fnNameBuf[0] = '\0';
char *contextNamePtr =
Mangler.getFunctionDeclContextName(fnNameBuf, &fnContextLen);
if (contextNamePtr && fnContextLen != 0 && contextNamePtr[0]) {
continue;
}
fnNameBuf[0] = '\0';
char *demangledNamePtr = Mangler.getFunctionBaseName(fnNameBuf, &fnNameLen);
if (!demangledNamePtr || fnNameLen == 0) {
continue;
Expand All @@ -817,10 +828,6 @@ void LLVMIRGen::initLLVMFunctionNameToMangledNameMap() {
}
llvmFunctionNameToMangledName_[demangledFnName].push_back(mangledName);
}
// Free up the memory.
if (fnNameBuf) {
free(fnNameBuf);
}
DEBUG_GLOW({
// Dump the map for debugging purposes.
llvm::dbgs() << "Mapping between function names and matching LLVM function "
Expand Down