Skip to content

[llvm][clang] Cherry-pick performance improvements to command line generation #7233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions clang-tools-extra/clangd/CompileCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,8 @@ llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
// Also grab prefixes for each option, these are not fully exposed.
const char *const *Prefixes[DriverID::LastOption] = {nullptr};
#define PREFIX(NAME, VALUE) static const char *const NAME[] = VALUE;
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELP, METAVAR, VALUES) \
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, PARAM, HELP, METAVAR, VALUES) \
Prefixes[DriverID::OPT_##ID] = PREFIX;
#include "clang/Driver/Options.inc"
#undef OPTION
Expand All @@ -478,8 +478,8 @@ llvm::ArrayRef<ArgStripper::Rule> ArgStripper::rulesFor(llvm::StringRef Arg) {
DriverID AliasID;
const void *AliasArgs;
} AliasTable[] = {
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELP, METAVAR, VALUES) \
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, PARAM, HELP, METAVAR, VALUES) \
{DriverID::OPT_##ID, DriverID::OPT_##ALIAS, ALIASARGS},
#include "clang/Driver/Options.inc"
#undef OPTION
Expand Down
13 changes: 5 additions & 8 deletions clang/include/clang/Driver/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
#ifndef LLVM_CLANG_DRIVER_OPTIONS_H
#define LLVM_CLANG_DRIVER_OPTIONS_H

namespace llvm {
namespace opt {
class OptTable;
}
}
#include "llvm/Option/OptTable.h"

namespace clang {
namespace driver {
Expand Down Expand Up @@ -42,9 +38,10 @@ enum ClangFlags {

enum ID {
OPT_INVALID = 0, // This is not an option ID.
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
OPT_##ID,
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, PARAM, HELP, METAVAR, VALUES) \
LLVM_MAKE_OPT_ID(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, PARAM, HELP, METAVAR, VALUES),
#include "clang/Driver/Options.inc"
LastOption
#undef OPTION
Expand Down
35 changes: 26 additions & 9 deletions clang/include/clang/Frontend/CompilerInvocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class CompilerInvocation : public CompilerInvocationRefBase,
/// identifying the conditions under which the module was built.
std::string getModuleHash(DiagnosticsEngine &Diags) const;

using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
using StringAllocator = llvm::function_ref<const char *(const Twine &)>;
/// Generate cc1-compatible command line arguments from this instance.
///
/// \param [out] Args - The generated arguments. Note that the caller is
Expand All @@ -252,7 +252,21 @@ class CompilerInvocation : public CompilerInvocationRefBase,
/// command line argument and return a pointer to the newly allocated string.
/// The returned pointer is what gets appended to Args.
void generateCC1CommandLine(llvm::SmallVectorImpl<const char *> &Args,
StringAllocator SA) const;
StringAllocator SA) const {
generateCC1CommandLine([&](const Twine &Arg) {
// No need to allocate static string literals.
Args.push_back(Arg.isSingleStringLiteral()
? Arg.getSingleStringRef().data()
: SA(Arg));
});
}

using ArgumentConsumer = llvm::function_ref<void(const Twine &)>;
/// Generate cc1-compatible command line arguments from this instance.
///
/// \param Consumer - Callback that gets invoked for every single generated
/// command line argument.
void generateCC1CommandLine(ArgumentConsumer Consumer) const;

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

/// Generate command line options from CASOptions.
static void GenerateCASArgs(const CASOptions &Opts,
ArgumentConsumer Consumer);
static void GenerateCASArgs(const CASOptions &Opts,
SmallVectorImpl<const char *> &Args,
CompilerInvocation::StringAllocator SA);
CompilerInvocation::StringAllocator SA) {
GenerateCASArgs(Opts, [&](const Twine &Arg) { Args.push_back(SA(Arg)); });
}

private:
static bool CreateFromArgsImpl(CompilerInvocation &Res,
Expand All @@ -295,8 +313,8 @@ class CompilerInvocation : public CompilerInvocationRefBase,

/// Generate command line options from DiagnosticOptions.
static void GenerateDiagnosticArgs(const DiagnosticOptions &Opts,
SmallVectorImpl<const char *> &Args,
StringAllocator SA, bool DefaultDiagColor);
ArgumentConsumer Consumer,
bool DefaultDiagColor);

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

private:
Expand All @@ -324,8 +341,8 @@ class CompilerInvocation : public CompilerInvocationRefBase,

// Generate command line options from CodeGenOptions.
static void GenerateCodeGenArgs(const CodeGenOptions &Opts,
SmallVectorImpl<const char *> &Args,
StringAllocator SA, const llvm::Triple &T,
ArgumentConsumer Consumer,
const llvm::Triple &T,
const std::string &OutputFile,
const LangOptions *LangOpts);
};
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ Driver::Driver(StringRef ClangExecutable, StringRef TargetTriple,
}

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

llvm::StringRef clang::driver::getDriverMode(StringRef ProgName,
ArrayRef<const char *> Args) {
static const std::string OptName =
static StringRef OptName =
getDriverOptTable().getOption(options::OPT_driver_mode).getPrefixedName();
llvm::StringRef Opt;
for (StringRef Arg : Args) {
Expand Down
8 changes: 4 additions & 4 deletions clang/lib/Driver/DriverOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ using namespace llvm::opt;
#undef PREFIX

static const OptTable::Info InfoTable[] = {
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
{PREFIX, NAME, HELPTEXT, METAVAR, OPT_##ID, Option::KIND##Class, \
PARAM, FLAGS, OPT_##GROUP, OPT_##ALIAS, ALIASARGS, VALUES},
#define OPTION(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, \
FLAGS, PARAM, HELP, METAVAR, VALUES) \
LLVM_CONSTRUCT_OPT_INFO(PREFIX, PREFIXED_NAME, ID, KIND, GROUP, ALIAS, \
ALIASARGS, FLAGS, PARAM, HELP, METAVAR, VALUES),
#include "clang/Driver/Options.inc"
#undef OPTION
};
Expand Down
Loading