Skip to content

Commit 0207819

Browse files
authored
[IR][Object][NFC] Move ARM64EC name mangling helpers to Mangler.h. (llvm#87191)
Move the implementation to llvm:Core to avoids including `COFF.h` in `Mangler.cpp`.
1 parent 11a411a commit 0207819

File tree

5 files changed

+46
-44
lines changed

5 files changed

+46
-44
lines changed

llvm/include/llvm/IR/Mangler.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#define LLVM_IR_MANGLER_H
1515

1616
#include "llvm/ADT/DenseMap.h"
17+
#include "llvm/ADT/StringRef.h"
1718

1819
namespace llvm {
1920

@@ -52,6 +53,9 @@ void emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
5253
void emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
5354
const Triple &T, Mangler &M);
5455

56+
std::optional<std::string> getArm64ECMangledFunctionName(StringRef Name);
57+
std::optional<std::string> getArm64ECDemangledFunctionName(StringRef Name);
58+
5559
} // End llvm namespace
5660

5761
#endif

llvm/include/llvm/Object/COFF.h

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,47 +1362,6 @@ class SectionStrippedError
13621362
SectionStrippedError() { setErrorCode(object_error::section_stripped); }
13631363
};
13641364

1365-
inline std::optional<std::string>
1366-
getArm64ECMangledFunctionName(StringRef Name) {
1367-
bool IsCppFn = Name[0] == '?';
1368-
if (IsCppFn && Name.find("$$h") != std::string::npos)
1369-
return std::nullopt;
1370-
if (!IsCppFn && Name[0] == '#')
1371-
return std::nullopt;
1372-
1373-
StringRef Prefix = "$$h";
1374-
size_t InsertIdx = 0;
1375-
if (IsCppFn) {
1376-
InsertIdx = Name.find("@@");
1377-
size_t ThreeAtSignsIdx = Name.find("@@@");
1378-
if (InsertIdx != std::string::npos && InsertIdx != ThreeAtSignsIdx) {
1379-
InsertIdx += 2;
1380-
} else {
1381-
InsertIdx = Name.find("@");
1382-
if (InsertIdx != std::string::npos)
1383-
InsertIdx++;
1384-
}
1385-
} else {
1386-
Prefix = "#";
1387-
}
1388-
1389-
return std::optional<std::string>(
1390-
(Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str());
1391-
}
1392-
1393-
inline std::optional<std::string>
1394-
getArm64ECDemangledFunctionName(StringRef Name) {
1395-
if (Name[0] == '#')
1396-
return std::string(Name.substr(1));
1397-
if (Name[0] != '?')
1398-
return std::nullopt;
1399-
1400-
std::pair<StringRef, StringRef> Pair = Name.split("$$h");
1401-
if (Pair.second.empty())
1402-
return std::nullopt;
1403-
return (Pair.first + Pair.second).str();
1404-
}
1405-
14061365
} // end namespace object
14071366

14081367
} // end namespace llvm

llvm/include/llvm/Object/COFFImportFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define LLVM_OBJECT_COFFIMPORTFILE_H
1818

1919
#include "llvm/ADT/ArrayRef.h"
20+
#include "llvm/IR/Mangler.h"
2021
#include "llvm/Object/COFF.h"
2122
#include "llvm/Object/ObjectFile.h"
2223
#include "llvm/Object/SymbolicFile.h"

llvm/lib/IR/Mangler.cpp

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/IR/DerivedTypes.h"
1919
#include "llvm/IR/Function.h"
2020
#include "llvm/IR/Module.h"
21-
#include "llvm/Object/COFF.h"
2221
#include "llvm/Support/raw_ostream.h"
2322
#include "llvm/TargetParser/Triple.h"
2423

@@ -242,7 +241,7 @@ void llvm::emitLinkerFlagsForGlobalCOFF(raw_ostream &OS, const GlobalValue *GV,
242241
// typically functions correctly; the linker can resolve the export
243242
// with the demangled alias.
244243
if (std::optional<std::string> demangledName =
245-
object::getArm64ECDemangledFunctionName(GV->getName()))
244+
getArm64ECDemangledFunctionName(GV->getName()))
246245
OS << ",EXPORTAS," << *demangledName;
247246
}
248247
if (NeedQuotes)
@@ -291,3 +290,42 @@ void llvm::emitLinkerFlagsForUsedCOFF(raw_ostream &OS, const GlobalValue *GV,
291290
OS << "\"";
292291
}
293292

293+
std::optional<std::string> llvm::getArm64ECMangledFunctionName(StringRef Name) {
294+
bool IsCppFn = Name[0] == '?';
295+
if (IsCppFn && Name.find("$$h") != std::string::npos)
296+
return std::nullopt;
297+
if (!IsCppFn && Name[0] == '#')
298+
return std::nullopt;
299+
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+
}
312+
} else {
313+
Prefix = "#";
314+
}
315+
316+
return std::optional<std::string>(
317+
(Name.substr(0, InsertIdx) + Prefix + Name.substr(InsertIdx)).str());
318+
}
319+
320+
std::optional<std::string>
321+
llvm::getArm64ECDemangledFunctionName(StringRef Name) {
322+
if (Name[0] == '#')
323+
return std::optional<std::string>(Name.substr(1));
324+
if (Name[0] != '?')
325+
return std::nullopt;
326+
327+
std::pair<StringRef, StringRef> Pair = Name.split("$$h");
328+
if (Pair.second.empty())
329+
return std::nullopt;
330+
return std::optional<std::string>((Pair.first + Pair.second).str());
331+
}

llvm/lib/Target/AArch64/AArch64Arm64ECCallLowering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/IR/CallingConv.h"
2424
#include "llvm/IR/IRBuilder.h"
2525
#include "llvm/IR/Instruction.h"
26+
#include "llvm/IR/Mangler.h"
2627
#include "llvm/InitializePasses.h"
2728
#include "llvm/Object/COFF.h"
2829
#include "llvm/Pass.h"
@@ -31,7 +32,6 @@
3132

3233
using namespace llvm;
3334
using namespace llvm::COFF;
34-
using namespace llvm::object;
3535

3636
using OperandBundleDef = OperandBundleDefT<Value *>;
3737

0 commit comments

Comments
 (0)