Skip to content

Commit 95684af

Browse files
authored
[IR][ARM64EC][NFC] Clean up and document ARM64EC mangling helpers. (#107230)
1 parent 3299bc8 commit 95684af

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
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 & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -291,39 +291,40 @@ 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";
301-
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-
}
302+
// Insert the ARM64EC "$$h" tag after the mangled function name.
303+
if (Name.contains("$$h"))
304+
return std::nullopt;
305+
size_t InsertIdx = Name.find("@@");
306+
size_t ThreeAtSignsIdx = Name.find("@@@");
307+
if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
308+
InsertIdx += 2;
312309
} else {
313-
Prefix = "#";
310+
InsertIdx = Name.find("@");
311+
if (InsertIdx != std::string::npos)
312+
InsertIdx++;
314313
}
315314

316315
return std::optional<std::string>(
317-
(Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str());
316+
(Name.substr(0, InsertIdx) + "$$h" + Name.substr(InsertIdx)).str());
318317
}
319318

320319
std::optional<std::string>
321320
llvm::getArm64ECDemangledFunctionName(StringRef Name) {
321+
// For non-C++ names, drop the "#" prefix.
322322
if (Name[0] == '#')
323323
return std::optional<std::string>(Name.substr(1));
324324
if (Name[0] != '?')
325325
return std::nullopt;
326326

327+
// Drop the ARM64EC "$$h" tag.
327328
std::pair<StringRef, StringRef> Pair = Name.split("$$h");
328329
if (Pair.second.empty())
329330
return std::nullopt;

0 commit comments

Comments
 (0)