Skip to content

Commit af5d24c

Browse files
committed
[IR][ARM64EC][NFC] Clean up and document ARM64EC mangling helpers.
1 parent 519b369 commit af5d24c

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

llvm/include/llvm/IR/Mangler.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
5353
void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
5454
const Triple &T, Mangler &M);
5555

56+
/// Returns the ARM64EC mangled function name unless the input is already
57+
/// mangled.
5658
std::optional<std::string> getArm64ECMangledFunctionName(StringRef Name);
59+
60+
/// Returns the ARM64EC demangled function name, unless the input is not
61+
/// mangled.
5762
std::optional<std::string> getArm64ECDemangledFunctionName(StringRef Name);
5863

5964
} // End llvm namespace

llvm/lib/IR/Mangler.cpp

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -291,39 +291,41 @@ void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
291291
}
292292

293293
std::optional<std::string> llvm::getArm64ECMangledFunctionName(StringRef Name) {
294-
bool IsCppFn = Name[0] == '?';
295-
if (IsCppFn && Name.contains("$$h"))
296-
return std::nullopt;
297-
if (!IsCppFn && Name[0] == '#')
298-
return std::nullopt;
294+
if (Name[0] != '?') {
295+
// For non-C++ symbols, prefix the name with "#" unless it's already
296+
// mangled.
297+
if (Name[0] == '#')
298+
return std::nullopt;
299+
return std::optional<std::string>(("#" + Name).str());
300+
}
299301

300-
StringRef Prefix = "$$h";
302+
// Insert the ARM64EC "$$h" tag after the mangled function name.
303+
if (Name.contains("$$h"))
304+
return std::nullopt;
301305
size_t InsertIdx = 0;
302-
if (IsCppFn) {
303-
InsertIdx = Name.find("@@");
304-
size_t ThreeAtSignsIdx = Name.find("@@@");
305-
if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
306-
InsertIdx += 2;
307-
} else {
308-
InsertIdx = Name.find("@");
309-
if (InsertIdx != std::string::npos)
310-
InsertIdx++;
311-
}
306+
InsertIdx = Name.find("@@");
307+
size_t ThreeAtSignsIdx = Name.find("@@@");
308+
if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
309+
InsertIdx += 2;
312310
} else {
313-
Prefix = "#";
311+
InsertIdx = Name.find("@");
312+
if (InsertIdx != std::string::npos)
313+
InsertIdx++;
314314
}
315315

316316
return std::optional<std::string>(
317-
(Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str());
317+
(Name.substr(0, InsertIdx) + "$$h" + Name.substr(InsertIdx)).str());
318318
}
319319

320320
std::optional<std::string>
321321
llvm::getArm64ECDemangledFunctionName(StringRef Name) {
322+
// For non-C++ names, drop the "#" prefix.
322323
if (Name[0] == '#')
323324
return std::optional<std::string>(Name.substr(1));
324325
if (Name[0] != '?')
325326
return std::nullopt;
326327

328+
// Drop the ARM64EC "$$h" tag.
327329
std::pair<StringRef, StringRef> Pair = Name.split("$$h");
328330
if (Pair.second.empty())
329331
return std::nullopt;

0 commit comments

Comments
 (0)