Skip to content

Commit 5e6d56a

Browse files
authored
Merge pull request #7233 from apple/jan_svoboda/stable-20221013-cmdline-cherry-pick
[llvm][clang] Cherry-pick performance improvements to command line generation
2 parents c6d1b19 + 72de458 commit 5e6d56a

File tree

52 files changed

+915
-834
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+915
-834
lines changed

clang-tools-extra/clangd/CompileCommands.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,8 @@ llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
466466
// Also grab prefixes for each option, these are not fully exposed.
467467
const char *const *Prefixes[DriverID::LastOption] = {nullptr};
468468
#define PREFIX(NAME, VALUE) static const char *const NAME[] = VALUE;
469-
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
470-
HELP, METAVAR, VALUES) \
469+
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
470+
FLAGS, PARAM, HELP, METAVAR, VALUES) \
471471
Prefixes[DriverID::OPT_##ID] = PREFIX;
472472
#include "clang/Driver/Options.inc"
473473
#undef OPTION
@@ -478,8 +478,8 @@ llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
478478
DriverID AliasID;
479479
const void *AliasArgs;
480480
} AliasTable[] = {
481-
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
482-
HELP, METAVAR, VALUES) \
481+
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
482+
FLAGS, PARAM, HELP, METAVAR, VALUES) \
483483
{DriverID::OPT_##ID, DriverID::OPT_##ALIAS, ALIASARGS},
484484
#include "clang/Driver/Options.inc"
485485
#undef OPTION

clang/include/clang/Driver/Options.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,7 @@
99
#ifndef LLVM_CLANG_DRIVER_OPTIONS_H
1010
#define LLVM_CLANG_DRIVER_OPTIONS_H
1111

12-
namespace llvm {
13-
namespace opt {
14-
class OptTable;
15-
}
16-
}
12+
#include "llvm/Option/OptTable.h"
1713

1814
namespace clang {
1915
namespace driver {
@@ -42,9 +38,10 @@ enum ClangFlags {
4238

4339
enum ID {
4440
OPT_INVALID = 0, // This is not an option ID.
45-
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
46-
HELPTEXT, METAVAR, VALUES) \
47-
OPT_##ID,
41+
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
42+
FLAGS, PARAM, HELP, METAVAR, VALUES) \
43+
LLVM_MAKE_OPT_ID(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
44+
FLAGS, PARAM, HELP, METAVAR, VALUES),
4845
#include "clang/Driver/Options.inc"
4946
LastOption
5047
#undef OPTION

clang/include/clang/Frontend/CompilerInvocation.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,
242242
/// identifying the conditions under which the module was built.
243243
std::string getModuleHash(DiagnosticsEngine &Diags) const;
244244

245-
using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
245+
using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
246246
/// Generate cc1-compatible command line arguments from this instance.
247247
///
248248
/// \param [out] Args - The generated arguments. Note that the caller is
@@ -252,7 +252,21 @@ class CompilerInvocation : public CompilerInvocationRefBase,
252252
/// command line argument and return a pointer to the newly allocated string.
253253
/// The returned pointer is what gets appended to Args.
254254
void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
255-
StringAllocator SA) const;
255+
StringAllocator SA) const {
256+
generateCC1CommandLine([&](const Twine &Arg) {
257+
// No need to allocate static string literals.
258+
Args.push_back(Arg.isSingleStringLiteral()
259+
? Arg.getSingleStringRef().data()
260+
: SA(Arg));
261+
});
262+
}
263+
264+
using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
265+
/// Generate cc1-compatible command line arguments from this instance.
266+
///
267+
/// \param Consumer - Callback that gets invoked for every single generated
268+
/// command line argument.
269+
void generateCC1CommandLine(ArgumentConsumer Consumer) const;
256270

257271
/// Generate cc1-compatible command line arguments from this instance,
258272
/// wrapping the result as a std::vector<std::string>.
@@ -284,9 +298,13 @@ class CompilerInvocation : public CompilerInvocationRefBase,
284298
DiagnosticsEngine &Diags);
285299

286300
/// Generate command line options from CASOptions.
301+
static void GenerateCASArgs(const CASOptions &Opts,
302+
ArgumentConsumer Consumer);
287303
static void GenerateCASArgs(const CASOptions &Opts,
288304
SmallVectorImpl<const char *> &Args,
289-
CompilerInvocation::StringAllocator SA);
305+
CompilerInvocation::StringAllocator SA) {
306+
GenerateCASArgs(Opts, [&](const Twine &Arg) { Args.push_back(SA(Arg)); });
307+
}
290308

291309
private:
292310
static bool CreateFromArgsImpl(CompilerInvocation &Res,
@@ -295,8 +313,8 @@ class CompilerInvocation : public CompilerInvocationRefBase,
295313

296314
/// Generate command line options from DiagnosticOptions.
297315
static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
298-
SmallVectorImpl<const char *> &Args,
299-
StringAllocator SA, bool DefaultDiagColor);
316+
ArgumentConsumer Consumer,
317+
bool DefaultDiagColor);
300318

301319
/// Parse command line options that map to LangOptions.
302320
static bool ParseLangArgs(LangOptions &Opts, llvm::opt::ArgList &Args,
@@ -307,8 +325,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,
307325
public:
308326
/// Generate command line options from LangOptions.
309327
static void GenerateLangArgs(const LangOptions &Opts,
310-
SmallVectorImpl<const char *> &Args,
311-
StringAllocator SA, const llvm::Triple &T,
328+
ArgumentConsumer Consumer, const llvm::Triple &T,
312329
InputKind IK);
313330

314331
private:
@@ -324,8 +341,8 @@ class CompilerInvocation : public CompilerInvocationRefBase,
324341

325342
// Generate command line options from CodeGenOptions.
326343
static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
327-
SmallVectorImpl<const char *> &Args,
328-
StringAllocator SA, const llvm::Triple &T,
344+
ArgumentConsumer Consumer,
345+
const llvm::Triple &T,
329346
const std::string &OutputFile,
330347
const LangOptions *LangOpts);
331348
};

clang/lib/Driver/Driver.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
228228
}
229229

230230
void Driver::setDriverMode(StringRef Value) {
231-
static const std::string OptName =
231+
static StringRef OptName =
232232
getOpts().getOption(options::OPT_driver_mode).getPrefixedName();
233233
if (auto M = llvm::StringSwitch<llvm::Optional<DriverMode>>(Value)
234234
.Case("gcc", GCCMode)
@@ -6322,7 +6322,7 @@ bool clang::driver::willEmitRemarks(const ArgList &Args) {
63226322

63236323
llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
63246324
ArrayRef<const char *> Args) {
6325-
static const std::string OptName =
6325+
static StringRef OptName =
63266326
getDriverOptTable().getOption(options::OPT_driver_mode).getPrefixedName();
63276327
llvm::StringRef Opt;
63286328
for (StringRef Arg : Args) {

clang/lib/Driver/DriverOptions.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ using namespace llvm::opt;
2121
#undef PREFIX
2222

2323
static const OptTable::Info InfoTable[] = {
24-
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
25-
HELPTEXT, METAVAR, VALUES) \
26-
{PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, \
27-
PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS, VALUES},
24+
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
25+
FLAGS, PARAM, HELP, METAVAR, VALUES) \
26+
LLVM_CONSTRUCT_OPT_INFO(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, \
27+
ALIASARGS, FLAGS, PARAM, HELP, METAVAR, VALUES),
2828
#include "clang/Driver/Options.inc"
2929
#undef OPTION
3030
};

0 commit comments

Comments
 (0)