Skip to content

Commit b472c30

Browse files
maksfbmemfrob
authored andcommitted
[BOLT] Fix function order output option
Summary: Add support to output both function order and section order files as the former is useful for offloading functions sorting and the latter is useful for linker script generation: -generate-function-order=<file> -generate-link-sections=<file> (cherry picked from FBD6078446)
1 parent 2dd2909 commit b472c30

File tree

1 file changed

+52
-18
lines changed

1 file changed

+52
-18
lines changed

bolt/Passes/ReorderFunctions.cpp

Lines changed: 52 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ GenerateFunctionOrderFile("generate-function-order",
7575
"reordering"),
7676
cl::cat(BoltOptCategory));
7777

78+
static cl::opt<std::string>
79+
LinkSectionsFile("generate-link-sections",
80+
cl::desc("generate a list of function sections in a format suitable for "
81+
"inclusion in a linker script"),
82+
cl::cat(BoltOptCategory));
83+
7884
static cl::opt<bool>
7985
UseEdgeCounts("use-edge-counts",
8086
cl::desc("use edge count data when doing clustering"),
@@ -408,16 +414,32 @@ void ReorderFunctions::runOnFunctions(BinaryContext &BC,
408414

409415
reorder(std::move(Clusters), BFs);
410416

417+
std::unique_ptr<std::ofstream> FuncsFile;
411418
if (!opts::GenerateFunctionOrderFile.empty()) {
412-
std::ofstream FuncsFile(opts::GenerateFunctionOrderFile, std::ios::out);
419+
FuncsFile =
420+
llvm::make_unique<std::ofstream>(opts::GenerateFunctionOrderFile,
421+
std::ios::out);
413422
if (!FuncsFile) {
414-
errs() << "Ordered functions file \"" << opts::GenerateFunctionOrderFile
415-
<< "\" can't be opened.\n";
423+
errs() << "BOLT-ERROR: ordered functions file "
424+
<< opts::GenerateFunctionOrderFile << " cannot be opened\n";
416425
exit(1);
417426
}
427+
}
418428

419-
std::vector<BinaryFunction *> SortedFunctions(BFs.size());
429+
std::unique_ptr<std::ofstream> LinkSectionsFile;
430+
if (!opts::LinkSectionsFile.empty()) {
431+
LinkSectionsFile =
432+
llvm::make_unique<std::ofstream>(opts::LinkSectionsFile,
433+
std::ios::out);
434+
if (!LinkSectionsFile) {
435+
errs() << "BOLT-ERROR: link sections file "
436+
<< opts::LinkSectionsFile << " cannot be opened\n";
437+
exit(1);
438+
}
439+
}
420440

441+
if (FuncsFile || LinkSectionsFile) {
442+
std::vector<BinaryFunction *> SortedFunctions(BFs.size());
421443
std::transform(BFs.begin(),
422444
BFs.end(),
423445
SortedFunctions.begin(),
@@ -446,25 +468,37 @@ void ReorderFunctions::runOnFunctions(BinaryContext &BC,
446468
break;
447469
if (Func->isPLTFunction())
448470
continue;
449-
const char *Indent = "";
450-
for (auto Name : Func->getNames()) {
451-
const auto SlashPos = Name.find('/');
452-
if (SlashPos != std::string::npos) {
453-
// Avoid duplicates for local functions.
454-
if (Name.find('/', SlashPos + 1) != std::string::npos)
455-
continue;
456-
Name = Name.substr(0, SlashPos);
471+
472+
if (FuncsFile)
473+
*FuncsFile << Func->getSymbol()->getName().data() << "\n";
474+
475+
if (LinkSectionsFile) {
476+
const char *Indent = "";
477+
for (auto Name : Func->getNames()) {
478+
const auto SlashPos = Name.find('/');
479+
if (SlashPos != std::string::npos) {
480+
// Avoid duplicates for local functions.
481+
if (Name.find('/', SlashPos + 1) != std::string::npos)
482+
continue;
483+
Name = Name.substr(0, SlashPos);
484+
}
485+
*LinkSectionsFile << Indent << ".text." << Name << "\n";
486+
Indent = " ";
457487
}
458-
FuncsFile << Indent << ".text." << Name << "\n";
459-
Indent = " ";
460488
}
461489
}
462-
FuncsFile.close();
463490

464-
outs() << "BOLT-INFO: dumped function order to \""
465-
<< opts::GenerateFunctionOrderFile << "\"\n";
491+
if (FuncsFile) {
492+
FuncsFile->close();
493+
outs() << "BOLT-INFO: dumped function order to "
494+
<< opts::GenerateFunctionOrderFile << '\n';
495+
}
466496

467-
exit(0);
497+
if (LinkSectionsFile) {
498+
LinkSectionsFile->close();
499+
outs() << "BOLT-INFO: dumped linker section order to "
500+
<< opts::LinkSectionsFile << '\n';
501+
}
468502
}
469503
}
470504

0 commit comments

Comments
 (0)