aboutsummaryrefslogtreecommitdiff
path: root/clang/lib/Frontend/CompilerInvocation.cpp
diff options
context:
space:
mode:
authorChandler Carruth <chandlerc@gmail.com>2024-12-11 15:44:44 -0800
committerGitHub <noreply@github.com>2024-12-11 15:44:44 -0800
commitdd647e3e608ed0b2bac7c588d5859b80ef4a5976 (patch)
treedd7cbf477b0831b2cab5eb7942fb59ea6e056e84 /clang/lib/Frontend/CompilerInvocation.cpp
parent151901c762b724ef6ffe6f3db163475071e7b215 (diff)
downloadllvm-dd647e3e608ed0b2bac7c588d5859b80ef4a5976.zip
llvm-dd647e3e608ed0b2bac7c588d5859b80ef4a5976.tar.gz
llvm-dd647e3e608ed0b2bac7c588d5859b80ef4a5976.tar.bz2
Rework the `Option` library to reduce dynamic relocations (#119198)
Apologies for the large change, I looked for ways to break this up and all of the ones I saw added real complexity. This change focuses on the option's prefixed names and the array of prefixes. These are present in every option and the dominant source of dynamic relocations for PIE or PIC users of LLVM and Clang tooling. In some cases, 100s or 1000s of them for the Clang driver which has a huge number of options. This PR addresses this by building a string table and a prefixes table that can be referenced with indices rather than pointers that require dynamic relocations. This removes almost 7k dynmaic relocations from the `clang` binary, roughly 8% of the remaining dynmaic relocations outside of vtables. For busy-boxing use cases where many different option tables are linked into the same binary, the savings add up a bit more. The string table is a straightforward mechanism, but the prefixes required some subtlety. They are encoded in a Pascal-string fashion with a size followed by a sequence of offsets. This works relatively well for the small realistic prefixes arrays in use. Lots of code has to change in order to land this though: both all the option library code has to be updated to use the string table and prefixes table, and all the users of the options library have to be updated to correctly instantiate the objects. Some follow-up patches in the works to provide an abstraction for this style of code, and to start using the same technique for some of the other strings here now that the infrastructure is in place.
Diffstat (limited to 'clang/lib/Frontend/CompilerInvocation.cpp')
-rw-r--r--clang/lib/Frontend/CompilerInvocation.cpp63
1 files changed, 43 insertions, 20 deletions
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 98136b7..23906d5 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -277,6 +277,14 @@ CowCompilerInvocation::getMutPreprocessorOutputOpts() {
using ArgumentConsumer = CompilerInvocation::ArgumentConsumer;
+#define OPTTABLE_STR_TABLE_CODE
+#include "clang/Driver/Options.inc"
+#undef OPTTABLE_STR_TABLE_CODE
+
+static llvm::StringRef lookupStrInTable(unsigned Offset) {
+ return &OptionStrTable[Offset];
+}
+
#define SIMPLE_ENUM_VALUE_TABLE
#include "clang/Driver/Options.inc"
#undef SIMPLE_ENUM_VALUE_TABLE
@@ -304,6 +312,11 @@ static std::optional<bool> normalizeSimpleNegativeFlag(OptSpecifier Opt,
/// unnecessary template instantiations and just ignore it with a variadic
/// argument.
static void denormalizeSimpleFlag(ArgumentConsumer Consumer,
+ unsigned SpellingOffset, Option::OptionClass,
+ unsigned, /*T*/...) {
+ Consumer(lookupStrInTable(SpellingOffset));
+}
+static void denormalizeSimpleFlag(ArgumentConsumer Consumer,
const Twine &Spelling, Option::OptionClass,
unsigned, /*T*/...) {
Consumer(Spelling);
@@ -343,10 +356,10 @@ static auto makeBooleanOptionNormalizer(bool Value, bool OtherValue,
}
static auto makeBooleanOptionDenormalizer(bool Value) {
- return [Value](ArgumentConsumer Consumer, const Twine &Spelling,
+ return [Value](ArgumentConsumer Consumer, unsigned SpellingOffset,
Option::OptionClass, unsigned, bool KeyPath) {
if (KeyPath == Value)
- Consumer(Spelling);
+ Consumer(lookupStrInTable(SpellingOffset));
};
}
@@ -372,6 +385,14 @@ static void denormalizeStringImpl(ArgumentConsumer Consumer,
}
template <typename T>
+static void
+denormalizeString(ArgumentConsumer Consumer, unsigned SpellingOffset,
+ Option::OptionClass OptClass, unsigned TableIndex, T Value) {
+ denormalizeStringImpl(Consumer, lookupStrInTable(SpellingOffset), OptClass,
+ TableIndex, Twine(Value));
+}
+
+template <typename T>
static void denormalizeString(ArgumentConsumer Consumer, const Twine &Spelling,
Option::OptionClass OptClass, unsigned TableIndex,
T Value) {
@@ -417,14 +438,14 @@ static std::optional<unsigned> normalizeSimpleEnum(OptSpecifier Opt,
}
static void denormalizeSimpleEnumImpl(ArgumentConsumer Consumer,
- const Twine &Spelling,
+ unsigned SpellingOffset,
Option::OptionClass OptClass,
unsigned TableIndex, unsigned Value) {
assert(TableIndex < SimpleEnumValueTablesSize);
const SimpleEnumValueTable &Table = SimpleEnumValueTables[TableIndex];
if (auto MaybeEnumVal = findValueTableByValue(Table, Value)) {
- denormalizeString(Consumer, Spelling, OptClass, TableIndex,
- MaybeEnumVal->Name);
+ denormalizeString(Consumer, lookupStrInTable(SpellingOffset), OptClass,
+ TableIndex, MaybeEnumVal->Name);
} else {
llvm_unreachable("The simple enum value was not correctly defined in "
"the tablegen option description");
@@ -433,11 +454,11 @@ static void denormalizeSimpleEnumImpl(ArgumentConsumer Consumer,
template <typename T>
static void denormalizeSimpleEnum(ArgumentConsumer Consumer,
- const Twine &Spelling,
+ unsigned SpellingOffset,
Option::OptionClass OptClass,
unsigned TableIndex, T Value) {
- return denormalizeSimpleEnumImpl(Consumer, Spelling, OptClass, TableIndex,
- static_cast<unsigned>(Value));
+ return denormalizeSimpleEnumImpl(Consumer, SpellingOffset, OptClass,
+ TableIndex, static_cast<unsigned>(Value));
}
static std::optional<std::string> normalizeString(OptSpecifier Opt,
@@ -473,7 +494,7 @@ normalizeStringVector(OptSpecifier Opt, int, const ArgList &Args,
}
static void denormalizeStringVector(ArgumentConsumer Consumer,
- const Twine &Spelling,
+ unsigned SpellingOffset,
Option::OptionClass OptClass,
unsigned TableIndex,
const std::vector<std::string> &Values) {
@@ -487,15 +508,16 @@ static void denormalizeStringVector(ArgumentConsumer Consumer,
CommaJoinedValue.append(Value);
}
}
- denormalizeString(Consumer, Spelling, Option::OptionClass::JoinedClass,
- TableIndex, CommaJoinedValue);
+ denormalizeString(Consumer, SpellingOffset,
+ Option::OptionClass::JoinedClass, TableIndex,
+ CommaJoinedValue);
break;
}
case Option::JoinedClass:
case Option::SeparateClass:
case Option::JoinedOrSeparateClass:
for (const std::string &Value : Values)
- denormalizeString(Consumer, Spelling, OptClass, TableIndex, Value);
+ denormalizeString(Consumer, SpellingOffset, OptClass, TableIndex, Value);
break;
default:
llvm_unreachable("Cannot denormalize an option with option class "
@@ -532,10 +554,11 @@ static T extractMaskValue(T KeyPath) {
}
#define PARSE_OPTION_WITH_MARSHALLING( \
- ARGS, DIAGS, PREFIX_TYPE, SPELLING, ID, KIND, GROUP, ALIAS, ALIASARGS, \
- FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, VALUES, \
- SHOULD_PARSE, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, IMPLIED_CHECK, \
- IMPLIED_VALUE, NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, TABLE_INDEX) \
+ ARGS, DIAGS, PREFIX_TYPE, SPELLING_OFFSET, ID, KIND, GROUP, ALIAS, \
+ ALIASARGS, FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, \
+ METAVAR, VALUES, SHOULD_PARSE, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, \
+ IMPLIED_CHECK, IMPLIED_VALUE, NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, \
+ TABLE_INDEX) \
if ((VISIBILITY) & options::CC1Option) { \
KEYPATH = MERGER(KEYPATH, DEFAULT_VALUE); \
if (IMPLIED_CHECK) \
@@ -549,8 +572,8 @@ static T extractMaskValue(T KeyPath) {
// Capture the extracted value as a lambda argument to avoid potential issues
// with lifetime extension of the reference.
#define GENERATE_OPTION_WITH_MARSHALLING( \
- CONSUMER, PREFIX_TYPE, SPELLING, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
- VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, VALUES, \
+ CONSUMER, PREFIX_TYPE, SPELLING_OFFSET, ID, KIND, GROUP, ALIAS, ALIASARGS, \
+ FLAGS, VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, VALUES, \
SHOULD_PARSE, ALWAYS_EMIT, KEYPATH, DEFAULT_VALUE, IMPLIED_CHECK, \
IMPLIED_VALUE, NORMALIZER, DENORMALIZER, MERGER, EXTRACTOR, TABLE_INDEX) \
if ((VISIBILITY) & options::CC1Option) { \
@@ -559,8 +582,8 @@ static T extractMaskValue(T KeyPath) {
(Extracted != \
static_cast<decltype(KEYPATH)>((IMPLIED_CHECK) ? (IMPLIED_VALUE) \
: (DEFAULT_VALUE)))) \
- DENORMALIZER(CONSUMER, SPELLING, Option::KIND##Class, TABLE_INDEX, \
- Extracted); \
+ DENORMALIZER(CONSUMER, SPELLING_OFFSET, Option::KIND##Class, \
+ TABLE_INDEX, Extracted); \
}(EXTRACTOR(KEYPATH)); \
}