Skip to content

Commit 765b307

Browse files
committed
Reapply "Switch builtin strings to use string tables" (llvm#118734)
This reverts commit ca79ff0. It also updates the original PR to use the newly added `StringTable` abstraction for string tables, and simplifies the construction to build the string table and info arrays separately. This should reduce any `constexpr` compile time memory or CPU cost of the original PR while significantly improving the APIs throughout.
1 parent 70f326c commit 765b307

Some content is hidden

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

48 files changed

+641
-305
lines changed

clang/include/clang/Basic/Builtins.h

Lines changed: 163 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/ADT/ArrayRef.h"
1919
#include "llvm/ADT/StringMap.h"
2020
#include "llvm/ADT/StringRef.h"
21+
#include "llvm/ADT/StringTable.h"
2122
#include <cstring>
2223

2324
// VC++ defines 'alloca' as an object-like macro, which interferes with our
@@ -55,6 +56,7 @@ struct HeaderDesc {
5556
#undef HEADER
5657
} ID;
5758

59+
constexpr HeaderDesc() : ID() {}
5860
constexpr HeaderDesc(HeaderID ID) : ID(ID) {}
5961

6062
const char *getName() const;
@@ -68,23 +70,144 @@ enum ID {
6870
FirstTSBuiltin
6971
};
7072

73+
// The info used to represent each builtin.
7174
struct Info {
72-
llvm::StringLiteral Name;
73-
const char *Type, *Attributes;
74-
const char *Features;
75+
// Rather than store pointers to the string literals describing these four
76+
// aspects of builtins, we store offsets into a common string table.
77+
struct StrOffsets {
78+
llvm::StringTable::Offset Name;
79+
llvm::StringTable::Offset Type;
80+
llvm::StringTable::Offset Attributes;
81+
llvm::StringTable::Offset Features;
82+
} Offsets;
83+
7584
HeaderDesc Header;
7685
LanguageID Langs;
7786
};
7887

88+
// A constexpr function to construct an infos array from X-macros.
89+
//
90+
// The input array uses the same data structure, but the offsets are actually
91+
// _lengths_ when input. This is all we can compute from the X-macro approach to
92+
// builtins. This function will convert these lengths into actual offsets to a
93+
// string table built up through sequentially appending strings with the given
94+
// lengths.
95+
template <size_t N>
96+
static constexpr std::array<Info, N> MakeInfos(std::array<Info, N> Infos) {
97+
// Translate lengths to offsets. We start past the initial empty string at
98+
// offset zero.
99+
unsigned Offset = 1;
100+
for (auto &I : Infos) {
101+
Info::StrOffsets NewOffsets = {};
102+
NewOffsets.Name = Offset;
103+
Offset += I.Offsets.Name.value();
104+
NewOffsets.Type = Offset;
105+
Offset += I.Offsets.Type.value();
106+
NewOffsets.Attributes = Offset;
107+
Offset += I.Offsets.Attributes.value();
108+
NewOffsets.Features = Offset;
109+
Offset += I.Offsets.Features.value();
110+
I.Offsets = NewOffsets;
111+
}
112+
return Infos;
113+
}
114+
115+
// A detail macro used below to emit a string literal that, after string literal
116+
// concatenation, ends up triggering the `-Woverlength-strings` warning. While
117+
// the warning is useful in general to catch accidentally excessive strings,
118+
// here we are creating them intentionally.
119+
//
120+
// This relies on a subtle aspect of `_Pragma`: that the *diagnostic* ones don't
121+
// turn into actual tokens that would disrupt string literal concatenation.
122+
#ifdef __clang__
123+
#define CLANG_BUILTIN_DETAIL_STR_TABLE(S) \
124+
_Pragma("clang diagnostic push") \
125+
_Pragma("clang diagnostic ignored \"-Woverlength-strings\"") \
126+
S _Pragma("clang diagnostic pop")
127+
#else
128+
#define CLANG_BUILTIN_DETAIL_STR_TABLE(S) S
129+
#endif
130+
131+
// We require string tables to start with an empty string so that a `0` offset
132+
// can always be used to refer to an empty string. To satisfy that when building
133+
// string tables with X-macros, we use this start macro prior to expanding the
134+
// X-macros.
135+
#define CLANG_BUILTIN_STR_TABLE_START CLANG_BUILTIN_DETAIL_STR_TABLE("\0")
136+
137+
// A macro that can be used with `Builtins.def` and similar files as an X-macro
138+
// to add the string arguments to a builtin string table. This is typically the
139+
// target for the `BUILTIN`, `LANGBUILTIN`, or `LIBBUILTIN` macros in those
140+
// files.
141+
#define CLANG_BUILTIN_STR_TABLE(ID, TYPE, ATTRS) \
142+
CLANG_BUILTIN_DETAIL_STR_TABLE(#ID "\0" TYPE "\0" ATTRS "\0" /*FEATURE*/ "\0")
143+
144+
// A macro that can be used with target builtin `.def` and `.inc` files as an
145+
// X-macro to add the string arguments to a builtin string table. this is
146+
// typically the target for the `TARGET_BUILTIN` macro.
147+
#define CLANG_TARGET_BUILTIN_STR_TABLE(ID, TYPE, ATTRS, FEATURE) \
148+
CLANG_BUILTIN_DETAIL_STR_TABLE(#ID "\0" TYPE "\0" ATTRS "\0" FEATURE "\0")
149+
150+
// A macro that can be used with target builtin `.def` and `.inc` files as an
151+
// X-macro to add the string arguments to a builtin string table. this is
152+
// typically the target for the `TARGET_HEADER_BUILTIN` macro. We can't delegate
153+
// to `TARGET_BUILTIN` because the `FEATURE` string changes position.
154+
#define CLANG_TARGET_HEADER_BUILTIN_STR_TABLE(ID, TYPE, ATTRS, HEADER, LANGS, \
155+
FEATURE) \
156+
CLANG_BUILTIN_DETAIL_STR_TABLE(#ID "\0" TYPE "\0" ATTRS "\0" FEATURE "\0")
157+
158+
// A detail macro used internally to compute the desired string table
159+
// `StrOffsets` struct for arguments to `MakeInfos`.
160+
#define CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS) \
161+
Builtin::Info::StrOffsets { \
162+
sizeof(#ID), sizeof(TYPE), sizeof(ATTRS), sizeof("") \
163+
}
164+
165+
// A detail macro used internally to compute the desired string table
166+
// `StrOffsets` struct for arguments to `Storage::Make`.
167+
#define CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE) \
168+
Builtin::Info::StrOffsets { \
169+
sizeof(#ID), sizeof(TYPE), sizeof(ATTRS), sizeof(FEATURE) \
170+
}
171+
172+
// A set of macros that can be used with builtin `.def' files as an X-macro to
173+
// create an `Info` struct for a particular builtin. It both computes the
174+
// `StrOffsets` value for the string table (the lengths here, translated to
175+
// offsets by the `MakeInfos` function), and the other metadata for each
176+
// builtin.
177+
//
178+
// There is a corresponding macro for each of `BUILTIN`, `LANGBUILTIN`,
179+
// `LIBBUILTIN`, `TARGET_BUILTIN`, and `TARGET_HEADER_BUILTIN`.
180+
#define CLANG_BUILTIN_ENTRY(ID, TYPE, ATTRS) \
181+
Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS), \
182+
HeaderDesc::NO_HEADER, ALL_LANGUAGES},
183+
#define CLANG_LANGBUILTIN_ENTRY(ID, TYPE, ATTRS, LANG) \
184+
Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS), \
185+
HeaderDesc::NO_HEADER, LANG},
186+
#define CLANG_LIBBUILTIN_ENTRY(ID, TYPE, ATTRS, HEADER, LANG) \
187+
Builtin::Info{CLANG_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS), \
188+
HeaderDesc::HEADER, LANG},
189+
#define CLANG_TARGET_BUILTIN_ENTRY(ID, TYPE, ATTRS, FEATURE) \
190+
Builtin::Info{ \
191+
CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE), \
192+
HeaderDesc::NO_HEADER, ALL_LANGUAGES},
193+
#define CLANG_TARGET_HEADER_BUILTIN_ENTRY(ID, TYPE, ATTRS, HEADER, LANG, \
194+
FEATURE) \
195+
Builtin::Info{ \
196+
CLANG_TARGET_BUILTIN_DETAIL_STR_OFFSETS(ID, TYPE, ATTRS, FEATURE), \
197+
HeaderDesc::HEADER, LANG},
198+
79199
/// Holds information about both target-independent and
80200
/// target-specific builtins, allowing easy queries by clients.
81201
///
82202
/// Builtins from an optional auxiliary target are stored in
83203
/// AuxTSRecords. Their IDs are shifted up by TSRecords.size() and need to
84204
/// be translated back with getAuxBuiltinID() before use.
85205
class Context {
86-
llvm::ArrayRef<Info> TSRecords;
87-
llvm::ArrayRef<Info> AuxTSRecords;
206+
const llvm::StringTable *TSStrTable = nullptr;
207+
const llvm::StringTable *AuxTSStrTable = nullptr;
208+
209+
llvm::ArrayRef<Info> TSInfos;
210+
llvm::ArrayRef<Info> AuxTSInfos;
88211

89212
public:
90213
Context() = default;
@@ -100,10 +223,13 @@ class Context {
100223

101224
/// Return the identifier name for the specified builtin,
102225
/// e.g. "__builtin_abs".
103-
llvm::StringRef getName(unsigned ID) const { return getRecord(ID).Name; }
226+
llvm::StringRef getName(unsigned ID) const;
104227

105228
/// Get the type descriptor string for the specified builtin.
106-
const char *getTypeString(unsigned ID) const { return getRecord(ID).Type; }
229+
const char *getTypeString(unsigned ID) const;
230+
231+
/// Get the attributes descriptor string for the specified builtin.
232+
const char *getAttributesString(unsigned ID) const;
107233

108234
/// Return true if this function is a target-specific builtin.
109235
bool isTSBuiltin(unsigned ID) const {
@@ -112,40 +238,40 @@ class Context {
112238

113239
/// Return true if this function has no side effects.
114240
bool isPure(unsigned ID) const {
115-
return strchr(getRecord(ID).Attributes, 'U') != nullptr;
241+
return strchr(getAttributesString(ID), 'U') != nullptr;
116242
}
117243

118244
/// Return true if this function has no side effects and doesn't
119245
/// read memory.
120246
bool isConst(unsigned ID) const {
121-
return strchr(getRecord(ID).Attributes, 'c') != nullptr;
247+
return strchr(getAttributesString(ID), 'c') != nullptr;
122248
}
123249

124250
/// Return true if we know this builtin never throws an exception.
125251
bool isNoThrow(unsigned ID) const {
126-
return strchr(getRecord(ID).Attributes, 'n') != nullptr;
252+
return strchr(getAttributesString(ID), 'n') != nullptr;
127253
}
128254

129255
/// Return true if we know this builtin never returns.
130256
bool isNoReturn(unsigned ID) const {
131-
return strchr(getRecord(ID).Attributes, 'r') != nullptr;
257+
return strchr(getAttributesString(ID), 'r') != nullptr;
132258
}
133259

134260
/// Return true if we know this builtin can return twice.
135261
bool isReturnsTwice(unsigned ID) const {
136-
return strchr(getRecord(ID).Attributes, 'j') != nullptr;
262+
return strchr(getAttributesString(ID), 'j') != nullptr;
137263
}
138264

139265
/// Returns true if this builtin does not perform the side-effects
140266
/// of its arguments.
141267
bool isUnevaluated(unsigned ID) const {
142-
return strchr(getRecord(ID).Attributes, 'u') != nullptr;
268+
return strchr(getAttributesString(ID), 'u') != nullptr;
143269
}
144270

145271
/// Return true if this is a builtin for a libc/libm function,
146272
/// with a "__builtin_" prefix (e.g. __builtin_abs).
147273
bool isLibFunction(unsigned ID) const {
148-
return strchr(getRecord(ID).Attributes, 'F') != nullptr;
274+
return strchr(getAttributesString(ID), 'F') != nullptr;
149275
}
150276

151277
/// Determines whether this builtin is a predefined libc/libm
@@ -156,29 +282,29 @@ class Context {
156282
/// they do not, but they are recognized as builtins once we see
157283
/// a declaration.
158284
bool isPredefinedLibFunction(unsigned ID) const {
159-
return strchr(getRecord(ID).Attributes, 'f') != nullptr;
285+
return strchr(getAttributesString(ID), 'f') != nullptr;
160286
}
161287

162288
/// Returns true if this builtin requires appropriate header in other
163289
/// compilers. In Clang it will work even without including it, but we can emit
164290
/// a warning about missing header.
165291
bool isHeaderDependentFunction(unsigned ID) const {
166-
return strchr(getRecord(ID).Attributes, 'h') != nullptr;
292+
return strchr(getAttributesString(ID), 'h') != nullptr;
167293
}
168294

169295
/// Determines whether this builtin is a predefined compiler-rt/libgcc
170296
/// function, such as "__clear_cache", where we know the signature a
171297
/// priori.
172298
bool isPredefinedRuntimeFunction(unsigned ID) const {
173-
return strchr(getRecord(ID).Attributes, 'i') != nullptr;
299+
return strchr(getAttributesString(ID), 'i') != nullptr;
174300
}
175301

176302
/// Determines whether this builtin is a C++ standard library function
177303
/// that lives in (possibly-versioned) namespace std, possibly a template
178304
/// specialization, where the signature is determined by the standard library
179305
/// declaration.
180306
bool isInStdNamespace(unsigned ID) const {
181-
return strchr(getRecord(ID).Attributes, 'z') != nullptr;
307+
return strchr(getAttributesString(ID), 'z') != nullptr;
182308
}
183309

184310
/// Determines whether this builtin can have its address taken with no
@@ -192,33 +318,33 @@ class Context {
192318

193319
/// Determines whether this builtin has custom typechecking.
194320
bool hasCustomTypechecking(unsigned ID) const {
195-
return strchr(getRecord(ID).Attributes, 't') != nullptr;
321+
return strchr(getAttributesString(ID), 't') != nullptr;
196322
}
197323

198324
/// Determines whether a declaration of this builtin should be recognized
199325
/// even if the type doesn't match the specified signature.
200326
bool allowTypeMismatch(unsigned ID) const {
201-
return strchr(getRecord(ID).Attributes, 'T') != nullptr ||
327+
return strchr(getAttributesString(ID), 'T') != nullptr ||
202328
hasCustomTypechecking(ID);
203329
}
204330

205331
/// Determines whether this builtin has a result or any arguments which
206332
/// are pointer types.
207333
bool hasPtrArgsOrResult(unsigned ID) const {
208-
return strchr(getRecord(ID).Type, '*') != nullptr;
334+
return strchr(getTypeString(ID), '*') != nullptr;
209335
}
210336

211337
/// Return true if this builtin has a result or any arguments which are
212338
/// reference types.
213339
bool hasReferenceArgsOrResult(unsigned ID) const {
214-
return strchr(getRecord(ID).Type, '&') != nullptr ||
215-
strchr(getRecord(ID).Type, 'A') != nullptr;
340+
return strchr(getTypeString(ID), '&') != nullptr ||
341+
strchr(getTypeString(ID), 'A') != nullptr;
216342
}
217343

218344
/// If this is a library function that comes from a specific
219345
/// header, retrieve that header name.
220346
const char *getHeaderName(unsigned ID) const {
221-
return getRecord(ID).Header.getName();
347+
return getInfo(ID).Header.getName();
222348
}
223349

224350
/// Determine whether this builtin is like printf in its
@@ -243,27 +369,25 @@ class Context {
243369
/// Such functions can be const when the MathErrno lang option and FP
244370
/// exceptions are disabled.
245371
bool isConstWithoutErrnoAndExceptions(unsigned ID) const {
246-
return strchr(getRecord(ID).Attributes, 'e') != nullptr;
372+
return strchr(getAttributesString(ID), 'e') != nullptr;
247373
}
248374

249375
bool isConstWithoutExceptions(unsigned ID) const {
250-
return strchr(getRecord(ID).Attributes, 'g') != nullptr;
376+
return strchr(getAttributesString(ID), 'g') != nullptr;
251377
}
252378

253-
const char *getRequiredFeatures(unsigned ID) const {
254-
return getRecord(ID).Features;
255-
}
379+
const char *getRequiredFeatures(unsigned ID) const;
256380

257381
unsigned getRequiredVectorWidth(unsigned ID) const;
258382

259383
/// Return true if builtin ID belongs to AuxTarget.
260384
bool isAuxBuiltinID(unsigned ID) const {
261-
return ID >= (Builtin::FirstTSBuiltin + TSRecords.size());
385+
return ID >= (Builtin::FirstTSBuiltin + TSInfos.size());
262386
}
263387

264388
/// Return real builtin ID (i.e. ID it would have during compilation
265389
/// for AuxTarget).
266-
unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSRecords.size(); }
390+
unsigned getAuxBuiltinID(unsigned ID) const { return ID - TSInfos.size(); }
267391

268392
/// Returns true if this is a libc/libm function without the '__builtin_'
269393
/// prefix.
@@ -275,16 +399,21 @@ class Context {
275399

276400
/// Return true if this function can be constant evaluated by Clang frontend.
277401
bool isConstantEvaluated(unsigned ID) const {
278-
return strchr(getRecord(ID).Attributes, 'E') != nullptr;
402+
return strchr(getAttributesString(ID), 'E') != nullptr;
279403
}
280404

281405
/// Returns true if this is an immediate (consteval) function
282406
bool isImmediate(unsigned ID) const {
283-
return strchr(getRecord(ID).Attributes, 'G') != nullptr;
407+
return strchr(getAttributesString(ID), 'G') != nullptr;
284408
}
285409

286410
private:
287-
const Info &getRecord(unsigned ID) const;
411+
std::pair<const llvm::StringTable &, const Info &>
412+
getStrTableAndInfo(unsigned ID) const;
413+
414+
const Info &getInfo(unsigned ID) const {
415+
return getStrTableAndInfo(ID).second;
416+
}
288417

289418
/// Helper function for isPrintfLike and isScanfLike.
290419
bool isLike(unsigned ID, unsigned &FormatIdx, bool &HasVAListArg,

clang/include/clang/Basic/BuiltinsPPC.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1138,5 +1138,6 @@ UNALIASED_CUSTOM_BUILTIN(mma_pmxvbf16ger2nn, "vW512*VVi15i15i3", true,
11381138
// FIXME: Obviously incomplete.
11391139

11401140
#undef BUILTIN
1141+
#undef TARGET_BUILTIN
11411142
#undef CUSTOM_BUILTIN
11421143
#undef UNALIASED_CUSTOM_BUILTIN

0 commit comments

Comments
 (0)