[llvm] 991d80d - [Option] Store the StringTable by value. NFC (#224818)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 19 00:28:29 PDT 2026
Author: Fangrui Song
Date: 2026-09-19T00:28:22-07:00
New Revision: 991d80d79b9d56b75c4d2bd07bf124e710dcbfaa
URL: https://github.com/llvm/llvm-project/commit/991d80d79b9d56b75c4d2bd07bf124e710dcbfaa
DIFF: https://github.com/llvm/llvm-project/commit/991d80d79b9d56b75c4d2bd07bf124e710dcbfaa.diff
LOG: [Option] Store the StringTable by value. NFC (#224818)
Suggested by
https://github.com/llvm/llvm-project/pull/224805#discussion_r4052408942
A StringTable is just a StringRef. Hold it by value and build it from
the storage array in optionTables(), so the generated OptionStrTable
object (16 bytes of .data.rel.ro and a relocation per table) is
unreferenced and discarded, and string lookups skip a load.
Aided by Opus 5
Added:
Modified:
llvm/include/llvm/Option/OptTable.h
llvm/lib/Option/OptTable.cpp
llvm/utils/TableGen/OptionParserEmitter.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Option/OptTable.h b/llvm/include/llvm/Option/OptTable.h
index 137be38251448..a8232e400c22d 100644
--- a/llvm/include/llvm/Option/OptTable.h
+++ b/llvm/include/llvm/Option/OptTable.h
@@ -148,7 +148,7 @@ class LLVM_ABI OptTable {
/// The tables TableGen emits for an option set under OPTTABLE_CODE.
struct Tables {
- const StringTable &StrTable;
+ StringTable StrTable;
ArrayRef<StringTable::Offset> PrefixesTable;
ArrayRef<Info> Infos;
ArrayRef<InfoExtra> InfoExtras;
@@ -174,7 +174,7 @@ class LLVM_ABI OptTable {
private:
// A unified string table for these options. Individual strings are stored as
// null terminated C-strings at offsets within this table.
- const StringTable *StrTable;
+ StringTable StrTable;
// A table of
diff erent sets of prefixes. Each set starts with the number of
// prefixes in that set followed by that many offsets into the string table
@@ -238,7 +238,7 @@ class LLVM_ABI OptTable {
}
StringRef getOptionValues(const Info &I) const {
- StringRef Values = (*StrTable)[getExtra(I).ValuesOffset];
+ StringRef Values = StrTable[getExtra(I).ValuesOffset];
if (Values.empty() && ValuesCodeFn)
Values = ValuesCodeFn(getOptionID(I));
return Values;
@@ -262,7 +262,7 @@ class LLVM_ABI OptTable {
virtual ~OptTable();
/// Return the string table used for option names.
- const StringTable &getStrTable() const { return *StrTable; }
+ const StringTable &getStrTable() const { return StrTable; }
ArrayRef<SubCommand> getSubCommands() const { return SubCommands; }
@@ -286,25 +286,25 @@ class LLVM_ABI OptTable {
/// Lookup the name of the given option.
StringRef getOptionName(OptSpecifier id) const {
- return getInfo(id).getName(*StrTable, PrefixesTable);
+ return getInfo(id).getName(StrTable, PrefixesTable);
}
/// Lookup the prefix of the given option.
StringRef getOptionPrefix(OptSpecifier id) const {
const Info &I = getInfo(id);
return I.hasNoPrefix() ? StringRef()
- : I.getPrefix(*StrTable, PrefixesTable, 0);
+ : I.getPrefix(StrTable, PrefixesTable, 0);
}
void appendOptionPrefixes(OptSpecifier id,
SmallVectorImpl<StringRef> &Prefixes) const {
const Info &I = getInfo(id);
- I.appendPrefixes(*StrTable, PrefixesTable, Prefixes);
+ I.appendPrefixes(StrTable, PrefixesTable, Prefixes);
}
/// Lookup the prefixed name of the given option.
StringRef getOptionPrefixedName(OptSpecifier id) const {
- return getInfo(id).getPrefixedName(*StrTable);
+ return getInfo(id).getPrefixedName(StrTable);
}
/// Get the kind of the given option.
@@ -327,18 +327,18 @@ class LLVM_ABI OptTable {
// visibility mask, use that text instead of the generic text.
StringRef getOptionHelpText(OptSpecifier id,
Visibility VisibilityMask) const {
- return (*StrTable)[getHelpTextOffset(getInfo(id), VisibilityMask)];
+ return StrTable[getHelpTextOffset(getInfo(id), VisibilityMask)];
}
/// Get the meta-variable name to use when describing
/// this options values in the help text.
StringRef getOptionMetaVar(OptSpecifier id) const {
- return (*StrTable)[getExtra(getInfo(id)).MetaVarOffset];
+ return StrTable[getExtra(getInfo(id)).MetaVarOffset];
}
/// Get the alias arguments as a \0 separated list, e.g. "foo\0bar\0".
const char *getOptionAliasArgs(OptSpecifier id) const {
- return StrTable->getCString(getExtra(getInfo(id)).AliasArgsOffset);
+ return StrTable.getCString(getExtra(getInfo(id)).AliasArgsOffset);
}
/// Specify the environment variable where initial options should be read.
diff --git a/llvm/lib/Option/OptTable.cpp b/llvm/lib/Option/OptTable.cpp
index ad7db113aa6e9..5725639eeb776 100644
--- a/llvm/lib/Option/OptTable.cpp
+++ b/llvm/lib/Option/OptTable.cpp
@@ -74,7 +74,7 @@ struct OptNameLess {
OptSpecifier::OptSpecifier(const Option *Opt) : ID(Opt->getID()) {}
OptTable::OptTable(const Tables &T, bool IgnoreCase)
- : StrTable(&T.StrTable), PrefixesTable(T.PrefixesTable),
+ : StrTable(T.StrTable), PrefixesTable(T.PrefixesTable),
OptionInfos(T.Infos), InfoExtrasTable(T.InfoExtras),
IgnoreCase(IgnoreCase), SubCommands(T.SubCommands),
SubCommandIDsTable(T.SubCommandIDs),
@@ -83,7 +83,7 @@ OptTable::OptTable(const Tables &T, bool IgnoreCase)
for (unsigned I = 0, E = PrefixesTable.size(); I != E;) {
unsigned Size = PrefixesTable[I++].value();
for (unsigned J = 0; J != Size; ++J) {
- StringRef Prefix = (*StrTable)[PrefixesTable[I++]];
+ StringRef Prefix = StrTable[PrefixesTable[I++]];
if (is_contained(PrefixesUnion, Prefix))
continue;
PrefixesUnion.push_back(Prefix);
@@ -121,7 +121,7 @@ OptTable::OptTable(const Tables &T, bool IgnoreCase)
// Check that options are in order.
for (unsigned i = FirstSearchableIndex + 1, e = getNumOptions(); i != e; ++i){
- if (!(OptNameLess(*StrTable, PrefixesTable)(getInfo(i), getInfo(i + 1)))) {
+ if (!(OptNameLess(StrTable, PrefixesTable)(getInfo(i), getInfo(i + 1)))) {
getOption(i).dump();
getOption(i + 1).dump();
llvm_unreachable("Options are not in order!");
@@ -188,7 +188,7 @@ OptTable::suggestValueCompletions(StringRef Option, StringRef Arg) const {
// Search all options and return possible values.
for (size_t I = FirstSearchableIndex, E = OptionInfos.size(); I < E; I++) {
const Info &In = OptionInfos[I];
- if (!optionMatches(*StrTable, PrefixesTable, In, Option))
+ if (!optionMatches(StrTable, PrefixesTable, In, Option))
continue;
StringRef Values = getOptionValues(In);
if (Values.empty())
@@ -219,11 +219,11 @@ OptTable::findByPrefix(StringRef Cur, Visibility VisibilityMask,
if (In.Flags & DisableFlags)
continue;
- StringRef Name = In.getName(*StrTable, PrefixesTable);
+ StringRef Name = In.getName(StrTable, PrefixesTable);
for (auto PrefixOffset : In.getPrefixOffsets(PrefixesTable)) {
- StringRef Prefix = (*StrTable)[PrefixOffset];
+ StringRef Prefix = StrTable[PrefixOffset];
std::string S = (Twine(Prefix) + Name + "\t").str();
- S += (*StrTable)[In.HelpTextOffset];
+ S += StrTable[In.HelpTextOffset];
if (StringRef(S).starts_with(Cur) && S != std::string(Cur) + "\t")
Ret.push_back(S);
}
@@ -270,7 +270,7 @@ unsigned OptTable::internalFindNearest(
for (const Info &CandidateInfo :
ArrayRef<Info>(OptionInfos).drop_front(FirstSearchableIndex)) {
- StringRef CandidateName = CandidateInfo.getName(*StrTable, PrefixesTable);
+ StringRef CandidateName = CandidateInfo.getName(StrTable, PrefixesTable);
// We can eliminate some option prefix/name pairs as candidates right away:
// * Ignore option candidates with empty names, such as "--", or names
@@ -305,7 +305,7 @@ unsigned OptTable::internalFindNearest(
// "--help" over "-help".
for (auto CandidatePrefixOffset :
CandidateInfo.getPrefixOffsets(PrefixesTable)) {
- StringRef CandidatePrefix = (*StrTable)[CandidatePrefixOffset];
+ StringRef CandidatePrefix = StrTable[CandidatePrefixOffset];
// If Candidate and NormalizedName have more than 'BestDistance'
// characters of
diff erence, no need to compute the edit distance, it's
// going to be greater than BestDistance. Don't bother computing Candidate
@@ -358,14 +358,14 @@ std::unique_ptr<Arg> OptTable::parseOneArgGrouped(InputArgList &Args,
StringRef Name = Str.ltrim(PrefixChars);
const Info *Start =
std::lower_bound(OptionInfos.data() + FirstSearchableIndex, End, Name,
- OptNameLess(*StrTable, PrefixesTable));
+ OptNameLess(StrTable, PrefixesTable));
const Info *Fallback = nullptr;
unsigned Prev = Index;
// Search for the option which matches Str.
for (; Start != End; ++Start) {
unsigned ArgSize =
- matchOption(*StrTable, PrefixesTable, Start, Str, IgnoreCase);
+ matchOption(StrTable, PrefixesTable, Start, Str, IgnoreCase);
if (!ArgSize)
continue;
@@ -448,7 +448,7 @@ std::unique_ptr<Arg> OptTable::internalParseOneArg(
// Search for the first next option which could be a prefix.
Start =
- std::lower_bound(Start, End, Name, OptNameLess(*StrTable, PrefixesTable));
+ std::lower_bound(Start, End, Name, OptNameLess(StrTable, PrefixesTable));
// Options are stored in sorted order, with '\0' at the end of the
// alphabet. Since the only options which can accept a string must
@@ -463,7 +463,7 @@ std::unique_ptr<Arg> OptTable::internalParseOneArg(
// Scan for first option which is a proper prefix.
for (; Start != End; ++Start)
if ((ArgSize =
- matchOption(*StrTable, PrefixesTable, Start, Str, IgnoreCase)))
+ matchOption(StrTable, PrefixesTable, Start, Str, IgnoreCase)))
break;
if (Start == End)
break;
@@ -819,7 +819,7 @@ void OptTable::internalPrintHelp(
getHelpTextOffset(getInfo(Alias.getID()), VisibilityMask);
}
- if (StringRef HelpText = (*StrTable)[HelpTextOffset]; !HelpText.empty()) {
+ if (StringRef HelpText = StrTable[HelpTextOffset]; !HelpText.empty()) {
StringRef HelpGroup = getOptionHelpGroup(*this, Id);
const std::string &OptName = getOptionHelpName(*this, Id);
GroupedOptionHelp[HelpGroup].push_back({OptName, HelpText});
diff --git a/llvm/utils/TableGen/OptionParserEmitter.cpp b/llvm/utils/TableGen/OptionParserEmitter.cpp
index d0abe1351c06e..2718f801c229d 100644
--- a/llvm/utils/TableGen/OptionParserEmitter.cpp
+++ b/llvm/utils/TableGen/OptionParserEmitter.cpp
@@ -481,8 +481,9 @@ static void emitOptionParser(const RecordKeeper &Records, raw_ostream &OS) {
}
OS << " };\n\n";
- OS << " return {OptionStrTable, OptionPrefixesTable, OptionInfoTable,\n";
- OS << " OptionInfoExtrasTable, OptionHelpTextVariantsTable, "
+ OS << " return {OptionStrTableStorage, OptionPrefixesTable,\n";
+ OS << " OptionInfoTable, OptionInfoExtrasTable, "
+ "OptionHelpTextVariantsTable, "
<< (SubCommands.empty() ? "{}" : "OptionSubCommands")
<< ", OptionSubCommandIDsTable};\n";
OS << "}\n";
More information about the llvm-commits
mailing list