Skip to content

Commit

Permalink
Fix how mangled C++ functions are handled
Browse files Browse the repository at this point in the history
Reviewed By: jaybean-dev

Differential Revision: D56683446

fbshipit-source-id: d9cfec00f5d9260a241b6deaebbe4c168216586c
  • Loading branch information
Roman Levenstein authored and facebook-github-bot committed Apr 29, 2024
1 parent f9079b1 commit d028b17
Showing 1 changed file with 12 additions and 5 deletions.
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

0 comments on commit d028b17

Please sign in to comment.