Skip to content

Commit

Permalink
Dev/lto sections (#29)
Browse files Browse the repository at this point in the history
* Add options to show only externally-visible symbols and defined symbols
  • Loading branch information
cme authored and farazs-github committed Dec 18, 2023
1 parent e714da3 commit 7e223d3
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions llvm/tools/llvm-lto-predict-sections/llvm-lto-predict-sections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,51 @@

using namespace llvm;

static cl::OptionCategory OptionsCategory("Section Options");
static cl::OptionCategory ReportOptionsCategory("Report Options");
static cl::OptionCategory CompileOptionsCategory("Compile-style Options");

static cl::opt<std::string> InputFilename(cl::Positional,
cl::desc("<input bitcode file>"),
cl::init("-"),
cl::value_desc("filename"),
cl::cat(OptionsCategory));
cl::cat(ReportOptionsCategory)
);

static cl::opt<bool>
FFunctionSections("ffunction-sections", cl::Prefix, cl::init(false),
cl::desc("Place each function in its own section"),
cl::cat(OptionsCategory));
cl::cat(CompileOptionsCategory));

static cl::opt<bool>
FDataSections("fdata-sections", cl::Prefix, cl::init(false),
cl::desc("Place each data object in its own section"),
cl::cat(OptionsCategory));
cl::cat(CompileOptionsCategory));

static cl::opt<bool> SymbolsReport("symbols", cl::Prefix, cl::init(false),
cl::desc("Report symbol names"),
cl::cat(OptionsCategory));
cl::cat(ReportOptionsCategory));

static cl::opt<bool> SectionsReport("sections", cl::Prefix, cl::init(false),
cl::desc("Report section names"),
cl::cat(OptionsCategory));
cl::cat(ReportOptionsCategory));

static cl::opt<bool>
MapReport("map", cl::Prefix, cl::init(false),
cl::desc("Report mapping of symbols to sections"),
cl::cat(OptionsCategory));
cl::cat(ReportOptionsCategory));

std::string FunctionSectionName(Function &F) {
static cl::opt<bool>
DefsOnly("defs-only", cl::Prefix, cl::init(false),
cl::desc("Only report defined symbols, not declarations"),
cl::cat(ReportOptionsCategory));

static cl::opt<bool>
ExternOnly("externally-visible", cl::Prefix, cl::init(false),
cl::desc("Only report externally visible symbols"),
cl::cat(ReportOptionsCategory));


static std::string FunctionSectionName(Function &F) {
if (F.hasSection()) {
return F.getSection().str();
} else {
Expand All @@ -69,7 +82,7 @@ std::string FunctionSectionName(Function &F) {
}
}

std::string GlobalSectionName(GlobalVariable &G) {
static std::string GlobalSectionName(GlobalVariable &G) {
if (G.hasSection()) {
return G.getSection().str();
} else {
Expand All @@ -95,14 +108,22 @@ std::string GlobalSectionName(GlobalVariable &G) {
}
}

std::map<std::string, std::string> SymbolsToSectionsMap;
std::set<std::string> SectionNames;
std::set<std::string> SymbolNames;
static std::map<std::string, std::string> SymbolsToSectionsMap;
static std::set<std::string> SectionNames;
static std::set<std::string> SymbolNames;

static bool ShouldReport(GlobalObject &G) {
if (ExternOnly && !G.hasExternalLinkage())
return false;
else if (DefsOnly && G.isDeclaration())
return false;
return true;
}

int main(int argc, char **argv) {
LLVMContext Context;
SMDiagnostic Err;
cl::HideUnrelatedOptions({&OptionsCategory});
cl::HideUnrelatedOptions({&ReportOptionsCategory, &CompileOptionsCategory});
cl::ParseCommandLineOptions(argc, argv, "LLVM LTO section tool\n");

std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
Expand All @@ -125,11 +146,13 @@ int main(int argc, char **argv) {
};

for (GlobalVariable &G : M->globals()) {
RecordSymbol(G.getName().str(), GlobalSectionName(G));
if (ShouldReport(G))
RecordSymbol(G.getName().str(), GlobalSectionName(G));
}

for (Function &F : M->functions()) {
RecordSymbol(F.getName().str(), FunctionSectionName(F));
if (ShouldReport(F))
RecordSymbol(F.getName().str(), FunctionSectionName(F));
}

if (SectionsReport) {
Expand Down

0 comments on commit 7e223d3

Please sign in to comment.