Skip to content

Commit

Permalink
[OptTable] Precompute OptTable prefixes union table through tablegen
Browse files Browse the repository at this point in the history
This avoid rediscovering this table when reading each options, providing
a sensible 2% speedup when processing and empty file, and a measurable
speedup on typical workloads, see:

This is optional, the legacy, on-the-fly, approach can still be used
through the GenericOptTable class, while the new one is used through
PrecomputedOptTable.

https://llvm-compile-time-tracker.com/compare.php?from=4da6cb3202817ee2897d6b690e4af950459caea4&to=19a492b704e8f5c1dea120b9c0d3859bd78796be&stat=instructions:u

Differential Revision: https://reviews.llvm.org/D140800
  • Loading branch information
serge-sans-paille committed Jan 12, 2023
1 parent bbe463d commit 07bb29d
Show file tree
Hide file tree
Showing 38 changed files with 256 additions and 143 deletions.
14 changes: 10 additions & 4 deletions clang/lib/Driver/DriverOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ using namespace llvm::opt;
#include "clang/Driver/Options.inc"
#undef PREFIX

static constexpr const llvm::StringLiteral PrefixTable_init[] =
#define PREFIX_UNION(VALUES) VALUES
#include "clang/Driver/Options.inc"
#undef PREFIX_UNION
;
static constexpr const llvm::ArrayRef<llvm::StringLiteral>
PrefixTable(PrefixTable_init, std::size(PrefixTable_init) - 1);

static constexpr OptTable::Info InfoTable[] = {
#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \
HELPTEXT, METAVAR, VALUES) \
Expand All @@ -38,12 +46,10 @@ static constexpr OptTable::Info InfoTable[] = {

namespace {

class DriverOptTable : public OptTable {
class DriverOptTable : public PrecomputedOptTable {
public:
DriverOptTable()
: OptTable(InfoTable) {}
DriverOptTable() : PrecomputedOptTable(InfoTable, PrefixTable) {}
};

}

const llvm::opt::OptTable &clang::driver::getDriverOptTable() {
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Driver/ToolChains/Gnu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clang/Driver/Options.h"
#include "clang/Driver/Tool.h"
#include "clang/Driver/ToolChain.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Option/ArgList.h"
#include "llvm/Support/CodeGen.h"
Expand Down
4 changes: 2 additions & 2 deletions clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ static constexpr OptTable::Info InfoTable[] = {
#undef OPTION
};

class WrapperOptTable : public opt::OptTable {
class WrapperOptTable : public opt::GenericOptTable {
public:
WrapperOptTable() : OptTable(InfoTable) {}
WrapperOptTable() : opt::GenericOptTable(InfoTable) {}
};

const OptTable &getOptTable() {
Expand Down
2 changes: 1 addition & 1 deletion lld/COFF/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ using llvm::COFF::MachineTypes;
using llvm::COFF::WindowsSubsystem;
using std::optional;

class COFFOptTable : public llvm::opt::OptTable {
class COFFOptTable : public llvm::opt::GenericOptTable {
public:
COFFOptTable();
};
Expand Down
2 changes: 1 addition & 1 deletion lld/COFF/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ static constexpr llvm::opt::OptTable::Info infoTable[] = {
#undef OPTION
};

COFFOptTable::COFFOptTable() : OptTable(infoTable, true) {}
COFFOptTable::COFFOptTable() : GenericOptTable(infoTable, true) {}

// Set color diagnostics according to --color-diagnostics={auto,always,never}
// or --no-color-diagnostics flags.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

namespace lld::elf {
// Parses command line options.
class ELFOptTable : public llvm::opt::OptTable {
class ELFOptTable : public llvm::opt::GenericOptTable {
public:
ELFOptTable();
llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static constexpr opt::OptTable::Info optInfo[] = {
#undef OPTION
};

ELFOptTable::ELFOptTable() : OptTable(optInfo) {}
ELFOptTable::ELFOptTable() : GenericOptTable(optInfo) {}

// Set color diagnostics according to --color-diagnostics={auto,always,never}
// or --no-color-diagnostics flags.
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace lld::macho {
class DylibFile;
class InputFile;

class MachOOptTable : public llvm::opt::OptTable {
class MachOOptTable : public llvm::opt::GenericOptTable {
public:
MachOOptTable();
llvm::opt::InputArgList parse(ArrayRef<const char *> argv);
Expand Down
2 changes: 1 addition & 1 deletion lld/MachO/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static constexpr OptTable::Info optInfo[] = {
#undef OPTION
};

MachOOptTable::MachOOptTable() : OptTable(optInfo) {}
MachOOptTable::MachOOptTable() : GenericOptTable(optInfo) {}

// Set color diagnostics according to --color-diagnostics={auto,always,never}
// or --no-color-diagnostics flags.
Expand Down
4 changes: 2 additions & 2 deletions lld/MinGW/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ static constexpr opt::OptTable::Info infoTable[] = {
};

namespace {
class MinGWOptTable : public opt::OptTable {
class MinGWOptTable : public opt::GenericOptTable {
public:
MinGWOptTable() : OptTable(infoTable, false) {}
MinGWOptTable() : opt::GenericOptTable(infoTable, false) {}
opt::InputArgList parse(ArrayRef<const char *> argv);
};
} // namespace
Expand Down
4 changes: 2 additions & 2 deletions lld/wasm/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ static constexpr opt::OptTable::Info optInfo[] = {
};

namespace {
class WasmOptTable : public llvm::opt::OptTable {
class WasmOptTable : public opt::GenericOptTable {
public:
WasmOptTable() : OptTable(optInfo) {}
WasmOptTable() : opt::GenericOptTable(optInfo) {}
opt::InputArgList parse(ArrayRef<const char *> argv);
};
} // namespace
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ static constexpr opt::OptTable::Info InfoTable[] = {
#undef OPTION
};

class LLDBOptTable : public opt::OptTable {
class LLDBOptTable : public opt::GenericOptTable {
public:
LLDBOptTable() : OptTable(InfoTable) {}
LLDBOptTable() : opt::GenericOptTable(InfoTable) {}
};
} // namespace

Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-server/lldb-gdbserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ static constexpr opt::OptTable::Info InfoTable[] = {
#undef OPTION
};

class LLGSOptTable : public opt::OptTable {
class LLGSOptTable : public opt::GenericOptTable {
public:
LLGSOptTable() : OptTable(InfoTable) {}
LLGSOptTable() : opt::GenericOptTable(InfoTable) {}

void PrintHelp(llvm::StringRef Name) {
std::string Usage =
Expand Down
4 changes: 2 additions & 2 deletions lldb/tools/lldb-vscode/lldb-vscode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ static constexpr llvm::opt::OptTable::Info InfoTable[] = {
#include "Options.inc"
#undef OPTION
};
class LLDBVSCodeOptTable : public llvm::opt::OptTable {
class LLDBVSCodeOptTable : public llvm::opt::GenericOptTable {
public:
LLDBVSCodeOptTable() : OptTable(InfoTable, true) {}
LLDBVSCodeOptTable() : llvm::opt::GenericOptTable(InfoTable, true) {}
};

typedef void (*RequestCallback)(const llvm::json::Object &command);
Expand Down
41 changes: 37 additions & 4 deletions llvm/include/llvm/Option/OptTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/Option/OptSpecifier.h"
#include "llvm/Support/StringSaver.h"
#include <cassert>
Expand Down Expand Up @@ -68,14 +67,17 @@ class OptTable {
unsigned InputOptionID = 0;
unsigned UnknownOptionID = 0;

protected:
/// The index of the first option which can be parsed (i.e., is not a
/// special option like 'input' or 'unknown', and is not an option group).
unsigned FirstSearchableIndex = 0;

/// The union of the first element of all option prefixes.
SmallString<8> PrefixChars;

/// The union of all option prefixes. If an argument does not begin with
/// one of these, it is an input.
StringSet<> PrefixesUnion;
SmallString<8> PrefixChars;
virtual ArrayRef<StringLiteral> getPrefixesUnion() const = 0;

private:
const Info &getInfo(OptSpecifier Opt) const {
Expand All @@ -88,10 +90,15 @@ class OptTable {
unsigned &Index) const;

protected:
/// Initialize OptTable using Tablegen'ed OptionInfos. Child class must
/// manually call \c buildPrefixChars once they are fully constructed.
OptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);

/// Build (or rebuild) the PrefixChars member.
void buildPrefixChars();

public:
~OptTable();
virtual ~OptTable();

/// Return the total number of option classes.
unsigned getNumOptions() const { return OptionInfos.size(); }
Expand Down Expand Up @@ -246,6 +253,32 @@ class OptTable {
bool ShowHidden = false, bool ShowAllAliases = false) const;
};

/// Specialization of OptTable
class GenericOptTable : public OptTable {
SmallVector<StringLiteral> PrefixesUnionBuffer;

protected:
GenericOptTable(ArrayRef<Info> OptionInfos, bool IgnoreCase = false);
ArrayRef<StringLiteral> getPrefixesUnion() const final {
return PrefixesUnionBuffer;
}
};

class PrecomputedOptTable : public OptTable {
ArrayRef<StringLiteral> PrefixesUnion;

protected:
PrecomputedOptTable(ArrayRef<Info> OptionInfos,
ArrayRef<StringLiteral> PrefixesTable,
bool IgnoreCase = false)
: OptTable(OptionInfos, IgnoreCase), PrefixesUnion(PrefixesTable) {
buildPrefixChars();
}
ArrayRef<StringLiteral> getPrefixesUnion() const final {
return PrefixesUnion;
}
};

} // end namespace opt

} // end namespace llvm
Expand Down
12 changes: 10 additions & 2 deletions llvm/lib/ExecutionEngine/JITLink/COFFDirectiveParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ using namespace jitlink;
#include "COFFOptions.inc"
#undef PREFIX

static constexpr const StringLiteral PrefixTable_init[] =
#define PREFIX_UNION(VALUES) VALUES
#include "COFFOptions.inc"
#undef PREFIX_UNION
;
static constexpr const ArrayRef<StringLiteral>
PrefixTable(PrefixTable_init, std::size(PrefixTable_init) - 1);

// Create table mapping all options defined in COFFOptions.td
static constexpr opt::OptTable::Info infoTable[] = {
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \
Expand All @@ -46,9 +54,9 @@ static constexpr opt::OptTable::Info infoTable[] = {
#undef OPTION
};

class COFFOptTable : public opt::OptTable {
class COFFOptTable : public opt::PrecomputedOptTable {
public:
COFFOptTable() : OptTable(infoTable, true) {}
COFFOptTable() : PrecomputedOptTable(infoTable, PrefixTable, true) {}
};

static COFFOptTable optTable;
Expand Down
Loading

0 comments on commit 07bb29d

Please sign in to comment.