[llvm] ecc8a95 - [CommandLine] Use DenseMap instead of StringMap (NFC) (#174988)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 9 06:22:33 PST 2026
Author: Nikita Popov
Date: 2026-01-09T15:22:28+01:00
New Revision: ecc8a955af76458e14fe231447e42bb08d75246b
URL: https://github.com/llvm/llvm-project/commit/ecc8a955af76458e14fe231447e42bb08d75246b
DIFF: https://github.com/llvm/llvm-project/commit/ecc8a955af76458e14fe231447e42bb08d75246b.diff
LOG: [CommandLine] Use DenseMap instead of StringMap (NFC) (#174988)
StringMap duplicates the option name to a new allocation for every
option, which is not necessary. Instead we can use the same StringRef
that the Option already uses inside a DenseMap. This reduces the amount
of allocations when loading libLLVM.
Added:
Modified:
llvm/include/llvm/Support/CommandLine.h
llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
llvm/lib/Support/CommandLine.cpp
llvm/tools/llvm-ar/llvm-ar.cpp
llvm/tools/llvm-remarkutil/RemarkSummary.cpp
llvm/unittests/Support/CommandLineInit/CommandLineInitTest.cpp
llvm/unittests/Support/CommandLineTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.h
index d737fbcf891b3..5355b90eb19b3 100644
--- a/llvm/include/llvm/Support/CommandLine.h
+++ b/llvm/include/llvm/Support/CommandLine.h
@@ -23,7 +23,6 @@
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallVector.h"
-#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/iterator_range.h"
@@ -233,7 +232,7 @@ class SubCommand {
SmallVector<Option *, 4> PositionalOpts;
SmallVector<Option *, 4> SinkOpts;
- StringMap<Option *> OptionsMap;
+ DenseMap<StringRef, Option *> OptionsMap;
Option *ConsumeAfterOpt = nullptr; // The ConsumeAfter option if it exists.
};
@@ -2050,10 +2049,10 @@ LLVM_ABI void printBuildConfig(raw_ostream &OS);
// Public interface for accessing registered options.
//
-/// Use this to get a StringMap to all registered named options
+/// Use this to get a map of all registered named options
/// (e.g. -help).
///
-/// \return A reference to the StringMap used by the cl APIs to parse options.
+/// \return A reference to the map used by the cl APIs to parse options.
///
/// Access to unnamed arguments (i.e. positional) are not provided because
/// it is expected that the client already has access to these.
@@ -2061,7 +2060,8 @@ LLVM_ABI void printBuildConfig(raw_ostream &OS);
/// Typical usage:
/// \code
/// main(int argc,char* argv[]) {
-/// StringMap<llvm::cl::Option*> &opts = llvm::cl::getRegisteredOptions();
+/// DenseMap<llvm::StringRef, llvm::cl::Option*> &opts =
+/// llvm::cl::getRegisteredOptions();
/// assert(opts.count("help") == 1)
/// opts["help"]->setDescription("Show alphabetical help information")
/// // More code
@@ -2077,7 +2077,7 @@ LLVM_ABI void printBuildConfig(raw_ostream &OS);
/// Hopefully this API can be deprecated soon. Any situation where options need
/// to be modified by tools or libraries should be handled by sane APIs rather
/// than just handing around a global list.
-LLVM_ABI StringMap<Option *> &
+LLVM_ABI DenseMap<StringRef, Option *> &
getRegisteredOptions(SubCommand &Sub = SubCommand::getTopLevel());
/// Use this to get all registered SubCommands from the provided parser.
diff --git a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
index ff45af5843597..52a2996fc5aba 100644
--- a/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
+++ b/llvm/lib/ExecutionEngine/Orc/JITTargetMachineBuilder.cpp
@@ -8,6 +8,7 @@
#include "llvm/ExecutionEngine/Orc/JITTargetMachineBuilder.h"
+#include "llvm/ADT/StringMap.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TargetParser/Host.h"
diff --git a/llvm/lib/Support/CommandLine.cpp b/llvm/lib/Support/CommandLine.cpp
index 5095b298fd42d..f4adb00557f1a 100644
--- a/llvm/lib/Support/CommandLine.cpp
+++ b/llvm/lib/Support/CommandLine.cpp
@@ -146,6 +146,7 @@ static inline bool isPrefixedOrGrouping(const Option *O) {
O->getFormattingFlag() == cl::AlwaysPrefix;
}
+using OptionsMapTy = DenseMap<StringRef, Option *>;
namespace {
@@ -279,7 +280,7 @@ class CommandLineParser {
auto End = Sub.OptionsMap.end();
for (auto Name : OptionNames) {
auto I = Sub.OptionsMap.find(Name);
- if (I != End && I->getValue() == O)
+ if (I != End && I->second == O)
Sub.OptionsMap.erase(I);
}
@@ -374,7 +375,7 @@ class CommandLineParser {
O->hasArgStr())
addOption(O, sub);
else
- addLiteralOption(*O, sub, E.first());
+ addLiteralOption(*O, sub, E.first);
}
}
@@ -588,7 +589,7 @@ SubCommand *CommandLineParser::LookupSubCommand(StringRef Name,
/// (after an equal sign) return that as well. This assumes that leading dashes
/// have already been stripped.
static Option *LookupNearestOption(StringRef Arg,
- const StringMap<Option *> &OptionsMap,
+ const OptionsMapTy &OptionsMap,
std::string &NearestString) {
// Reject all dashes.
if (Arg.empty())
@@ -734,9 +735,9 @@ bool llvm::cl::ProvidePositionalOption(Option *Handler, StringRef Arg, int i) {
//
static Option *getOptionPred(StringRef Name, size_t &Length,
bool (*Pred)(const Option *),
- const StringMap<Option *> &OptionsMap) {
- StringMap<Option *>::const_iterator OMI = OptionsMap.find(Name);
- if (OMI != OptionsMap.end() && !Pred(OMI->getValue()))
+ const OptionsMapTy &OptionsMap) {
+ auto OMI = OptionsMap.find(Name);
+ if (OMI != OptionsMap.end() && !Pred(OMI->second))
OMI = OptionsMap.end();
// Loop while we haven't found an option and Name still has at least two
@@ -745,7 +746,7 @@ static Option *getOptionPred(StringRef Name, size_t &Length,
while (OMI == OptionsMap.end() && Name.size() > 1) {
Name = Name.drop_back();
OMI = OptionsMap.find(Name);
- if (OMI != OptionsMap.end() && !Pred(OMI->getValue()))
+ if (OMI != OptionsMap.end() && !Pred(OMI->second))
OMI = OptionsMap.end();
}
@@ -760,10 +761,9 @@ static Option *getOptionPred(StringRef Name, size_t &Length,
/// with at least one '-') does not fully match an available option. Check to
/// see if this is a prefix or grouped option. If so, split arg into output an
/// Arg/Value pair and return the Option to parse it with.
-static Option *
-HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
- bool &ErrorParsing,
- const StringMap<Option *> &OptionsMap) {
+static Option *HandlePrefixedOrGroupedOption(StringRef &Arg, StringRef &Value,
+ bool &ErrorParsing,
+ const OptionsMapTy &OptionsMap) {
if (Arg.size() == 1)
return nullptr;
@@ -2297,13 +2297,12 @@ static int SubNameCompare(const std::pair<const char *, SubCommand *> *LHS,
}
// Copy Options into a vector so we can sort them as we like.
-static void sortOpts(StringMap<Option *> &OptMap,
+static void sortOpts(OptionsMapTy &OptMap,
SmallVectorImpl<std::pair<const char *, Option *>> &Opts,
bool ShowHidden) {
SmallPtrSet<Option *, 32> OptionSet; // Duplicate option detection.
- for (StringMap<Option *>::iterator I = OptMap.begin(), E = OptMap.end();
- I != E; ++I) {
+ for (auto I = OptMap.begin(), E = OptMap.end(); I != E; ++I) {
// Ignore really-hidden options.
if (I->second->getOptionHiddenFlag() == ReallyHidden)
continue;
@@ -2317,7 +2316,7 @@ static void sortOpts(StringMap<Option *> &OptMap,
continue;
Opts.push_back(
- std::pair<const char *, Option *>(I->getKey().data(), I->second));
+ std::pair<const char *, Option *>(I->first.data(), I->second));
}
// Sort the options list alphabetically.
@@ -2819,7 +2818,7 @@ void cl::AddExtraVersionPrinter(VersionPrinterTy func) {
CommonOptions->ExtraVersionPrinters.push_back(func);
}
-StringMap<Option *> &cl::getRegisteredOptions(SubCommand &Sub) {
+OptionsMapTy &cl::getRegisteredOptions(SubCommand &Sub) {
initCommonOptions();
auto &Subs = GlobalParser->RegisteredSubCommands;
(void)Subs;
diff --git a/llvm/tools/llvm-ar/llvm-ar.cpp b/llvm/tools/llvm-ar/llvm-ar.cpp
index ac3824419537a..320a903b59e87 100644
--- a/llvm/tools/llvm-ar/llvm-ar.cpp
+++ b/llvm/tools/llvm-ar/llvm-ar.cpp
@@ -12,6 +12,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringExtras.h"
+#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/BinaryFormat/Magic.h"
#include "llvm/IR/LLVMContext.h"
diff --git a/llvm/tools/llvm-remarkutil/RemarkSummary.cpp b/llvm/tools/llvm-remarkutil/RemarkSummary.cpp
index 124bd51720d17..c3879d9e56d0e 100644
--- a/llvm/tools/llvm-remarkutil/RemarkSummary.cpp
+++ b/llvm/tools/llvm-remarkutil/RemarkSummary.cpp
@@ -80,8 +80,7 @@ class SummaryStrategy {
/// Check if any summary strategy options are explicitly enabled.
static bool isAnyStrategyRequested() {
- StringMap<cl::Option *> Opts = cl::getRegisteredOptions(SummarySub);
- for (auto &[_, Opt] : Opts) {
+ for (auto &[_, Opt] : cl::getRegisteredOptions(SummarySub)) {
if (!is_contained(Opt->Categories, &SummaryStrategyCat))
continue;
if (!Opt->getNumOccurrences())
diff --git a/llvm/unittests/Support/CommandLineInit/CommandLineInitTest.cpp b/llvm/unittests/Support/CommandLineInit/CommandLineInitTest.cpp
index dce79b2a3d2c7..6881d7884b344 100644
--- a/llvm/unittests/Support/CommandLineInit/CommandLineInitTest.cpp
+++ b/llvm/unittests/Support/CommandLineInit/CommandLineInitTest.cpp
@@ -50,7 +50,7 @@ int main(int argc, char **argv) {
}
TEST(CommandLineInitTest, GetPresetOptions) {
- StringMap<cl::Option *> &Map =
+ DenseMap<StringRef, cl::Option *> &Map =
cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
for (auto *Str :
diff --git a/llvm/unittests/Support/CommandLineTest.cpp b/llvm/unittests/Support/CommandLineTest.cpp
index 7f538f155be15..9e8a165fe136f 100644
--- a/llvm/unittests/Support/CommandLineTest.cpp
+++ b/llvm/unittests/Support/CommandLineTest.cpp
@@ -100,7 +100,7 @@ TEST(CommandLineTest, ModifyExisitingOption) {
static const char ArgString[] = "new-test-option";
static const char ValueString[] = "Integer";
- StringMap<cl::Option *> &Map =
+ DenseMap<StringRef, cl::Option *> &Map =
cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
ASSERT_EQ(Map.count("test-option"), 1u) << "Could not find option in map.";
@@ -421,7 +421,7 @@ TEST(CommandLineTest, HideUnrelatedOptions) {
ASSERT_EQ(cl::NotHidden, TestOption2.getOptionHiddenFlag())
<< "Hid extra option that should be visable.";
- StringMap<cl::Option *> &Map =
+ DenseMap<StringRef, cl::Option *> &Map =
cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
ASSERT_TRUE(Map.count("help") == (size_t)0 ||
cl::NotHidden == Map["help"]->getOptionHiddenFlag())
@@ -447,7 +447,7 @@ TEST(CommandLineTest, HideUnrelatedOptionsMulti) {
ASSERT_EQ(cl::NotHidden, TestOption3.getOptionHiddenFlag())
<< "Hid extra option that should be visable.";
- StringMap<cl::Option *> &Map =
+ DenseMap<StringRef, cl::Option *> &Map =
cl::getRegisteredOptions(cl::SubCommand::getTopLevel());
ASSERT_TRUE(Map.count("help") == (size_t)0 ||
cl::NotHidden == Map["help"]->getOptionHiddenFlag())
More information about the llvm-commits
mailing list