Skip to content

Commit fcb7395

Browse files
committed
Teach main builtin TableGen to use direct enums, strings, and info
This moves the main builtins and several targets to use nice generated string tables and info structures rather than X-macros. Even without obvious prefixes to factor out, the resulting tables are significantly smaller and much cheaper to compile with out all the X-macro overhead. This leaves the X-macros in place for atomic builtins which have a wide range of uses that don't seem reasonable to fold into TableGen. As future work, these should move to their own file (whether as X-macros or just generated patterns) so the AST headers don't have to include all the data for other builtins.
1 parent 7fa4b2b commit fcb7395

File tree

12 files changed

+277
-200
lines changed

12 files changed

+277
-200
lines changed

clang/include/clang/AST/Expr.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6678,7 +6678,6 @@ class PseudoObjectExpr final
66786678
class AtomicExpr : public Expr {
66796679
public:
66806680
enum AtomicOp {
6681-
#define BUILTIN(ID, TYPE, ATTRS)
66826681
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) AO ## ID,
66836682
#include "clang/Basic/Builtins.inc"
66846683
// Avoid trailing comma
@@ -6742,7 +6741,6 @@ class AtomicExpr : public Expr {
67426741
AtomicOp getOp() const { return Op; }
67436742
StringRef getOpAsString() const {
67446743
switch (Op) {
6745-
#define BUILTIN(ID, TYPE, ATTRS)
67466744
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
67476745
case AO##ID: \
67486746
return #ID;

clang/include/clang/Basic/Builtins.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,10 @@ struct HeaderDesc {
6464

6565
namespace Builtin {
6666
enum ID {
67-
NotBuiltin = 0, // This is not a builtin function.
68-
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
67+
NotBuiltin = 0, // This is not a builtin function.
68+
#define GET_BUILTIN_ENUMERATORS
6969
#include "clang/Basic/Builtins.inc"
70+
#undef GET_BUILTIN_ENUMERATORS
7071
FirstTSBuiltin
7172
};
7273

@@ -75,14 +76,14 @@ struct Info {
7576
// Rather than store pointers to the string literals describing these four
7677
// aspects of builtins, we store offsets into a common string table.
7778
struct StrOffsets {
78-
llvm::StringTable::Offset Name;
79-
llvm::StringTable::Offset Type;
80-
llvm::StringTable::Offset Attributes;
81-
llvm::StringTable::Offset Features;
79+
llvm::StringTable::Offset Name = 0;
80+
llvm::StringTable::Offset Type = 0;
81+
llvm::StringTable::Offset Attributes = 0;
82+
llvm::StringTable::Offset Features = 0;
8283
} Offsets;
8384

84-
HeaderDesc Header;
85-
LanguageID Langs;
85+
HeaderDesc Header = HeaderDesc::NO_HEADER;
86+
LanguageID Langs = ALL_LANGUAGES;
8687
};
8788

8889
/// A constexpr function to construct an infos array from X-macros.

clang/include/clang/Basic/IdentifierTable.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ enum class InterestingIdentifier {
101101
NUM_OBJC_KEYWORDS_AND_NOTABLE_IDENTIFIERS,
102102

103103
NotBuiltin,
104-
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
104+
#define GET_BUILTIN_ENUMERATORS
105105
#include "clang/Basic/Builtins.inc"
106+
#undef GET_BUILTIN_ENUMERATORS
106107
FirstTSBuiltin,
107108

108109
NotInterestingIdentifier = 65534

clang/include/clang/Basic/TargetBuiltins.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ namespace clang {
9393
namespace BPF {
9494
enum {
9595
LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1,
96-
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
97-
#include "clang/Basic/BuiltinsBPF.inc"
96+
#define GET_BUILTIN_ENUMERATORS
97+
#include "clang/Basic/BuiltinsBPF.inc"
98+
#undef GET_BUILTIN_ENUMERATORS
9899
LastTSBuiltin
99100
};
100101
}
@@ -133,8 +134,9 @@ namespace clang {
133134
namespace X86 {
134135
enum {
135136
LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1,
136-
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
137+
#define GET_BUILTIN_ENUMERATORS
137138
#include "clang/Basic/BuiltinsX86.inc"
139+
#undef GET_BUILTIN_ENUMERATORS
138140
FirstX86_64Builtin,
139141
LastX86CommonBuiltin = FirstX86_64Builtin - 1,
140142
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
@@ -172,8 +174,9 @@ namespace clang {
172174
LastTIBuiltin = clang::Builtin::FirstTSBuiltin - 1,
173175
FirstRVVBuiltin = clang::Builtin::FirstTSBuiltin,
174176
LastRVVBuiltin = RISCVVector::FirstTSBuiltin - 1,
175-
#define BUILTIN(ID, TYPE, ATTRS) BI##ID,
177+
#define GET_BUILTIN_ENUMERATORS
176178
#include "clang/Basic/BuiltinsRISCV.inc"
179+
#undef GET_BUILTIN_ENUMERATORS
177180
LastTSBuiltin
178181
};
179182
} // namespace RISCV

clang/lib/AST/StmtPrinter.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,6 @@ void StmtPrinter::VisitPseudoObjectExpr(PseudoObjectExpr *Node) {
19281928
void StmtPrinter::VisitAtomicExpr(AtomicExpr *Node) {
19291929
const char *Name = nullptr;
19301930
switch (Node->getOp()) {
1931-
#define BUILTIN(ID, TYPE, ATTRS)
19321931
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
19331932
case AtomicExpr::AO ## ID: \
19341933
Name = #ID "("; \

clang/lib/Basic/Builtins.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,23 +29,19 @@ const char *HeaderDesc::getName() const {
2929
llvm_unreachable("Unknown HeaderDesc::HeaderID enum");
3030
}
3131

32-
static constexpr llvm::StringTable BuiltinStrings =
33-
CLANG_BUILTIN_STR_TABLE_START
34-
// We inject a non-builtin string into the table.
35-
CLANG_BUILTIN_STR_TABLE("not a builtin function", "", "")
36-
#define BUILTIN CLANG_BUILTIN_STR_TABLE
32+
static constexpr unsigned NumBuiltins = Builtin::FirstTSBuiltin;
33+
34+
#define GET_BUILTIN_STR_TABLE
3735
#include "clang/Basic/Builtins.inc"
38-
;
39-
static_assert(BuiltinStrings.size() < 100'000);
40-
41-
static constexpr auto BuiltinInfos =
42-
Builtin::MakeInfos<Builtin::FirstTSBuiltin>(
43-
{CLANG_BUILTIN_ENTRY("not a builtin function", "", "")
44-
#define BUILTIN CLANG_BUILTIN_ENTRY
45-
#define LANGBUILTIN CLANG_LANGBUILTIN_ENTRY
46-
#define LIBBUILTIN CLANG_LIBBUILTIN_ENTRY
36+
#undef GET_BUILTIN_STR_TABLE
37+
38+
static constexpr Builtin::Info BuiltinInfos[] = {
39+
Builtin::Info{}, // No-builtin info entry.
40+
#define GET_BUILTIN_INFOS
4741
#include "clang/Basic/Builtins.inc"
48-
});
42+
#undef GET_BUILTIN_INFOS
43+
};
44+
static_assert(std::size(BuiltinInfos) == NumBuiltins);
4945

5046
std::pair<const Builtin::InfosShard &, const Builtin::Info &>
5147
Builtin::Context::getShardAndInfo(unsigned ID) const {

clang/lib/Basic/Targets/BPF.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@ using namespace clang::targets;
2222
static constexpr int NumBuiltins =
2323
clang::BPF::LastTSBuiltin - Builtin::FirstTSBuiltin;
2424

25-
static constexpr llvm::StringTable BuiltinStrings =
26-
CLANG_BUILTIN_STR_TABLE_START
27-
#define BUILTIN CLANG_BUILTIN_STR_TABLE
25+
#define GET_BUILTIN_STR_TABLE
2826
#include "clang/Basic/BuiltinsBPF.inc"
29-
;
27+
#undef GET_BUILTIN_STR_TABLE
3028

31-
static constexpr auto BuiltinInfos = Builtin::MakeInfos<NumBuiltins>({
32-
#define BUILTIN CLANG_BUILTIN_ENTRY
29+
static constexpr Builtin::Info BuiltinInfos[] = {
30+
#define GET_BUILTIN_INFOS
3331
#include "clang/Basic/BuiltinsBPF.inc"
34-
});
32+
#undef GET_BUILTIN_INFOS
33+
};
34+
static_assert(std::size(BuiltinInfos) == NumBuiltins);
3535

3636
void BPFTargetInfo::getTargetDefines(const LangOptions &Opts,
3737
MacroBuilder &Builder) const {

clang/lib/Basic/Targets/RISCV.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -277,18 +277,16 @@ static constexpr std::array<Builtin::Info, NumRVVSiFiveBuiltins> BuiltinInfos =
277277
};
278278
} // namespace RVVSiFive
279279

280-
static constexpr llvm::StringTable BuiltinStrings =
281-
CLANG_BUILTIN_STR_TABLE_START
282-
#define BUILTIN CLANG_BUILTIN_STR_TABLE
283-
#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_STR_TABLE
280+
#define GET_BUILTIN_STR_TABLE
284281
#include "clang/Basic/BuiltinsRISCV.inc"
285-
;
282+
#undef GET_BUILTIN_STR_TABLE
286283

287-
static constexpr auto BuiltinInfos = Builtin::MakeInfos<NumRISCVBuiltins>({
288-
#define BUILTIN CLANG_BUILTIN_ENTRY
289-
#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_ENTRY
284+
static constexpr Builtin::Info BuiltinInfos[] = {
285+
#define GET_BUILTIN_INFOS
290286
#include "clang/Basic/BuiltinsRISCV.inc"
291-
});
287+
#undef GET_BUILTIN_INFOS
288+
};
289+
static_assert(std::size(BuiltinInfos) == NumRISCVBuiltins);
292290

293291
llvm::SmallVector<Builtin::InfosShard>
294292
RISCVTargetInfo::getTargetBuiltins() const {

clang/lib/Basic/Targets/X86.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ static constexpr int NumX86_64Builtins =
3131
static constexpr int NumBuiltins = X86::LastTSBuiltin - Builtin::FirstTSBuiltin;
3232
static_assert(NumBuiltins == (NumX86Builtins + NumX86_64Builtins));
3333

34-
static constexpr llvm::StringTable BuiltinX86Strings =
35-
CLANG_BUILTIN_STR_TABLE_START
36-
#define BUILTIN CLANG_BUILTIN_STR_TABLE
37-
#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_STR_TABLE
38-
#define TARGET_HEADER_BUILTIN CLANG_TARGET_HEADER_BUILTIN_STR_TABLE
34+
namespace X86 {
35+
#define GET_BUILTIN_STR_TABLE
3936
#include "clang/Basic/BuiltinsX86.inc"
40-
;
37+
#undef GET_BUILTIN_STR_TABLE
38+
39+
static constexpr Builtin::Info BuiltinInfos[] = {
40+
#define GET_BUILTIN_INFOS
41+
#include "clang/Basic/BuiltinsX86.inc"
42+
#undef GET_BUILTIN_INFOS
43+
};
44+
static_assert(std::size(BuiltinInfos) == NumX86Builtins);
45+
} // namespace X86
4146

4247
static constexpr llvm::StringTable BuiltinX86_64Strings =
4348
CLANG_BUILTIN_STR_TABLE_START
@@ -47,13 +52,6 @@ static constexpr llvm::StringTable BuiltinX86_64Strings =
4752
#include "clang/Basic/BuiltinsX86_64.def"
4853
;
4954

50-
static constexpr auto BuiltinX86Infos = Builtin::MakeInfos<NumX86Builtins>({
51-
#define BUILTIN CLANG_BUILTIN_ENTRY
52-
#define TARGET_BUILTIN CLANG_TARGET_BUILTIN_ENTRY
53-
#define TARGET_HEADER_BUILTIN CLANG_TARGET_HEADER_BUILTIN_ENTRY
54-
#include "clang/Basic/BuiltinsX86.inc"
55-
});
56-
5755
static constexpr auto BuiltinX86_64Infos =
5856
Builtin::MakeInfos<NumX86_64Builtins>({
5957
#define BUILTIN CLANG_BUILTIN_ENTRY
@@ -1879,13 +1877,13 @@ ArrayRef<TargetInfo::AddlRegName> X86TargetInfo::getGCCAddlRegNames() const {
18791877

18801878
llvm::SmallVector<Builtin::InfosShard>
18811879
X86_32TargetInfo::getTargetBuiltins() const {
1882-
return {{&BuiltinX86Strings, BuiltinX86Infos}};
1880+
return {{&X86::BuiltinStrings, X86::BuiltinInfos}};
18831881
}
18841882

18851883
llvm::SmallVector<Builtin::InfosShard>
18861884
X86_64TargetInfo::getTargetBuiltins() const {
18871885
return {
1888-
{&BuiltinX86Strings, BuiltinX86Infos},
1886+
{&X86::BuiltinStrings, X86::BuiltinInfos},
18891887
{&BuiltinX86_64Strings, BuiltinX86_64Infos},
18901888
};
18911889
}

clang/lib/Sema/SemaChecking.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,6 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
24462446
CheckNonNullArgument(*this, TheCall->getArg(0), TheCall->getExprLoc());
24472447
break;
24482448
}
2449-
#define BUILTIN(ID, TYPE, ATTRS)
24502449
#define ATOMIC_BUILTIN(ID, TYPE, ATTRS) \
24512450
case Builtin::BI##ID: \
24522451
return AtomicOpsOverloaded(TheCallResult, AtomicExpr::AO##ID);

0 commit comments

Comments
 (0)