Skip to content

Commit 31a3c5f

Browse files
committed
[clang] use string tables for static diagnostic descriptions
Using a pointer for the description string in StaticDiagInfoRec causes several problems: 1. We don't need to use a whole pointer to represent the string; 2. The use of pointers incurs runtime relocations for those pointers; the relocations take up space on disk and represent runtime overhead; 3. The need to relocate data implies that, on some platforms, the entire array containing StaticDiagInfoRecs cannot be shared between processes. This patch changes the storage scheme for the diagnostic descriptions to avoid these problems. We instead generate (effectively) one large string and then StaticDiagInfoRec conceptually holds offsets into the string. We elected to also move the storage of those offsets into a separate array to further reduce the space required. On x86-64 Linux, this change removes about 120KB of relocations and moves about 60KB from the non-shareable .data.rel.ro section to shareable .rodata. (The array is about 80KB before this, but we eliminated 4 bytes/entry by using offsets rather than pointers.) We actually reap this benefit twice, because these tables show up in both libclang.so and libclang-cpp.so and we get the reduction in both places. Differential Revision: https://reviews.llvm.org/D81865
1 parent 2830363 commit 31a3c5f

File tree

1 file changed

+92
-8
lines changed

1 file changed

+92
-8
lines changed

clang/lib/Basic/DiagnosticIDs.cpp

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,78 @@ using namespace clang;
2626

2727
namespace {
2828

29+
struct StaticDiagInfoRec;
30+
31+
// Store the descriptions in a separate table to avoid pointers that need to
32+
// be relocated, and also decrease the amount of data needed on 64-bit
33+
// platforms. See "How To Write Shared Libraries" by Ulrich Drepper.
34+
struct StaticDiagInfoDescriptionStringTable {
35+
#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
36+
SHOWINSYSHEADER, DEFERRABLE, CATEGORY) \
37+
char ENUM##_desc[sizeof(DESC)];
38+
// clang-format off
39+
#include "clang/Basic/DiagnosticCommonKinds.inc"
40+
#include "clang/Basic/DiagnosticDriverKinds.inc"
41+
#include "clang/Basic/DiagnosticFrontendKinds.inc"
42+
#include "clang/Basic/DiagnosticSerializationKinds.inc"
43+
#include "clang/Basic/DiagnosticLexKinds.inc"
44+
#include "clang/Basic/DiagnosticParseKinds.inc"
45+
#include "clang/Basic/DiagnosticASTKinds.inc"
46+
#include "clang/Basic/DiagnosticCommentKinds.inc"
47+
#include "clang/Basic/DiagnosticCrossTUKinds.inc"
48+
#include "clang/Basic/DiagnosticSemaKinds.inc"
49+
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
50+
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
51+
// clang-format on
52+
#undef DIAG
53+
};
54+
55+
const StaticDiagInfoDescriptionStringTable StaticDiagInfoDescriptions = {
56+
#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
57+
SHOWINSYSHEADER, DEFERRABLE, CATEGORY) \
58+
DESC,
59+
// clang-format off
60+
#include "clang/Basic/DiagnosticCommonKinds.inc"
61+
#include "clang/Basic/DiagnosticDriverKinds.inc"
62+
#include "clang/Basic/DiagnosticFrontendKinds.inc"
63+
#include "clang/Basic/DiagnosticSerializationKinds.inc"
64+
#include "clang/Basic/DiagnosticLexKinds.inc"
65+
#include "clang/Basic/DiagnosticParseKinds.inc"
66+
#include "clang/Basic/DiagnosticASTKinds.inc"
67+
#include "clang/Basic/DiagnosticCommentKinds.inc"
68+
#include "clang/Basic/DiagnosticCrossTUKinds.inc"
69+
#include "clang/Basic/DiagnosticSemaKinds.inc"
70+
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
71+
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
72+
// clang-format on
73+
#undef DIAG
74+
};
75+
76+
extern const StaticDiagInfoRec StaticDiagInfo[];
77+
78+
// Stored separately from StaticDiagInfoRec to pack better. Otherwise,
79+
// StaticDiagInfoRec would have extra padding on 64-bit platforms.
80+
const uint32_t StaticDiagInfoDescriptionOffsets[] = {
81+
#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
82+
SHOWINSYSHEADER, DEFERRABLE, CATEGORY) \
83+
offsetof(StaticDiagInfoDescriptionStringTable, ENUM##_desc),
84+
// clang-format off
85+
#include "clang/Basic/DiagnosticCommonKinds.inc"
86+
#include "clang/Basic/DiagnosticDriverKinds.inc"
87+
#include "clang/Basic/DiagnosticFrontendKinds.inc"
88+
#include "clang/Basic/DiagnosticSerializationKinds.inc"
89+
#include "clang/Basic/DiagnosticLexKinds.inc"
90+
#include "clang/Basic/DiagnosticParseKinds.inc"
91+
#include "clang/Basic/DiagnosticASTKinds.inc"
92+
#include "clang/Basic/DiagnosticCommentKinds.inc"
93+
#include "clang/Basic/DiagnosticCrossTUKinds.inc"
94+
#include "clang/Basic/DiagnosticSemaKinds.inc"
95+
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
96+
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
97+
// clang-format on
98+
#undef DIAG
99+
};
100+
29101
// Diagnostic classes.
30102
enum {
31103
CLASS_NOTE = 0x01,
@@ -48,14 +120,16 @@ struct StaticDiagInfoRec {
48120
uint16_t OptionGroupIndex;
49121

50122
uint16_t DescriptionLen;
51-
const char *DescriptionStr;
52123

53124
unsigned getOptionGroupIndex() const {
54125
return OptionGroupIndex;
55126
}
56127

57128
StringRef getDescription() const {
58-
return StringRef(DescriptionStr, DescriptionLen);
129+
size_t MyIndex = this - &StaticDiagInfo[0];
130+
uint32_t StringOffset = StaticDiagInfoDescriptionOffsets[MyIndex];
131+
const char* Table = reinterpret_cast<const char*>(&StaticDiagInfoDescriptions);
132+
return StringRef(&Table[StringOffset], DescriptionLen);
59133
}
60134

61135
diag::Flavor getFlavor() const {
@@ -93,14 +167,21 @@ VALIDATE_DIAG_SIZE(REFACTORING)
93167
#undef VALIDATE_DIAG_SIZE
94168
#undef STRINGIFY_NAME
95169

96-
} // namespace anonymous
97-
98-
static const StaticDiagInfoRec StaticDiagInfo[] = {
170+
const StaticDiagInfoRec StaticDiagInfo[] = {
99171
#define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
100172
SHOWINSYSHEADER, DEFERRABLE, CATEGORY) \
101-
{diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, \
102-
NOWERROR, SHOWINSYSHEADER, DEFERRABLE, CATEGORY, \
103-
GROUP, STR_SIZE(DESC, uint16_t), DESC},
173+
{ \
174+
diag::ENUM, \
175+
DEFAULT_SEVERITY, \
176+
CLASS, \
177+
DiagnosticIDs::SFINAE, \
178+
NOWERROR, \
179+
SHOWINSYSHEADER, \
180+
DEFERRABLE, \
181+
CATEGORY, \
182+
GROUP, \
183+
STR_SIZE(DESC, uint16_t)},
184+
// clang-format off
104185
#include "clang/Basic/DiagnosticCommonKinds.inc"
105186
#include "clang/Basic/DiagnosticDriverKinds.inc"
106187
#include "clang/Basic/DiagnosticFrontendKinds.inc"
@@ -113,9 +194,12 @@ static const StaticDiagInfoRec StaticDiagInfo[] = {
113194
#include "clang/Basic/DiagnosticSemaKinds.inc"
114195
#include "clang/Basic/DiagnosticAnalysisKinds.inc"
115196
#include "clang/Basic/DiagnosticRefactoringKinds.inc"
197+
// clang-format on
116198
#undef DIAG
117199
};
118200

201+
} // namespace
202+
119203
static const unsigned StaticDiagInfoSize = llvm::array_lengthof(StaticDiagInfo);
120204

121205
/// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,

0 commit comments

Comments
 (0)