Skip to content

Commit 0e6b0ee

Browse files
committed
[Serialization] Store DeclID in two slots to utilize VBR6 format
1 parent 79b0966 commit 0e6b0ee

File tree

11 files changed

+175
-108
lines changed

11 files changed

+175
-108
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ using unaligned_decl_id_t =
285285
serialization::DeclID, llvm::endianness::native,
286286
llvm::support::unaligned>;
287287

288+
/// The number of slots needed to record a DeclID in bitstreams.
289+
const unsigned int DeclIDSerialiazedSize = 2;
290+
288291
/// The number of predefined preprocessed entity IDs.
289292
const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1;
290293

clang/include/clang/Serialization/ASTReader.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ class ASTReader
592592

593593
/// An array of lexical contents of a declaration context, as a sequence of
594594
/// Decl::Kind, DeclID pairs.
595-
using LexicalContents = ArrayRef<serialization::unaligned_decl_id_t>;
595+
using LexicalContents = ArrayRef<uint32_t>;
596596

597597
/// Map from a DeclContext to its lexical contents.
598598
llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
@@ -945,8 +945,7 @@ class ASTReader
945945
SmallVector<uint64_t, 8> DelayedDeleteExprs;
946946

947947
// A list of late parsed template function data with their module files.
948-
SmallVector<std::pair<ModuleFile *, SmallVector<uint64_t, 1>>, 4>
949-
LateParsedTemplates;
948+
SmallVector<std::pair<ModuleFile *, RecordData>, 4> LateParsedTemplates;
950949

951950
/// The IDs of all decls to be checked for deferred diags.
952951
///

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class ASTRecordReader
8686
/// Skips the specified number of values.
8787
void skipInts(unsigned N) { Idx += N; }
8888

89+
/// Skips the specified number of DeclIDs.
90+
void skipDeclRefs(unsigned N) {
91+
Idx += N * serialization::DeclIDSerialiazedSize;
92+
}
93+
8994
/// Retrieve the global submodule ID its local ID number.
9095
serialization::SubmoduleID
9196
getGlobalSubmoduleID(unsigned LocalID) {
@@ -187,12 +192,26 @@ class ASTRecordReader
187192
/// Reads a declaration from the given position in a record in the
188193
/// given module, advancing Idx.
189194
Decl *readDecl() {
195+
#ifndef NDEBUG
196+
unsigned OldIdx = Idx;
197+
Decl *D = Reader->ReadDecl(*F, Record, Idx);
198+
assert(Idx - OldIdx == serialization::DeclIDSerialiazedSize);
199+
return D;
200+
#endif
190201
return Reader->ReadDecl(*F, Record, Idx);
191202
}
192203
Decl *readDeclRef() {
193204
return readDecl();
194205
}
195206

207+
template <class DeclKind, class Func> void readDeclArray(Func &&ConsumeFunc) {
208+
unsigned LengthOfArray = readInt();
209+
unsigned End = Idx + LengthOfArray;
210+
211+
while (Idx < End)
212+
ConsumeFunc(readDeclAs<DeclKind>());
213+
}
214+
196215
/// Reads a declaration from the given position in the record,
197216
/// advancing Idx.
198217
///

clang/include/clang/Serialization/ASTRecordWriter.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,39 @@ class ASTRecordWriter
234234

235235
/// Emit a reference to a declaration.
236236
void AddDeclRef(const Decl *D) {
237+
#ifndef NDEBUG
238+
unsigned OldSize = size();
239+
Writer->AddDeclRef(D, *Record);
240+
assert(size() - OldSize == serialization::DeclIDSerialiazedSize);
241+
return;
242+
#endif
237243
return Writer->AddDeclRef(D, *Record);
238244
}
239245
void writeDeclRef(const Decl *D) {
240246
AddDeclRef(D);
241247
}
242248

249+
void writeNullDeclRef() {
250+
#ifndef NDEBUG
251+
unsigned OldSize = size();
252+
#endif
253+
254+
push_back(0);
255+
push_back(0);
256+
257+
#ifndef NDEBUG
258+
assert(size() - OldSize == serialization::DeclIDSerialiazedSize);
259+
#endif
260+
}
261+
262+
template <class DeclKind> void writeDeclArray(ArrayRef<DeclKind *> Array) {
263+
unsigned ElementNum = Array.size();
264+
push_back(ElementNum * serialization::DeclIDSerialiazedSize);
265+
266+
for (DeclKind *D : Array)
267+
AddDeclRef(D);
268+
}
269+
243270
/// Emit a declaration name.
244271
void AddDeclarationName(DeclarationName Name) {
245272
writeDeclarationName(Name);

clang/lib/Serialization/ASTReader.cpp

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,9 +1306,8 @@ bool ASTReader::ReadLexicalDeclContextStorage(ModuleFile &M,
13061306
auto &Lex = LexicalDecls[DC];
13071307
if (!Lex.first) {
13081308
Lex = std::make_pair(
1309-
&M, llvm::ArrayRef(
1310-
reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
1311-
Blob.size() / sizeof(DeclID)));
1309+
&M, llvm::ArrayRef(reinterpret_cast<const uint32_t *>(Blob.data()),
1310+
Blob.size() / sizeof(uint32_t)));
13121311
}
13131312
DC->setHasExternalLexicalStorage(true);
13141313
return false;
@@ -3422,8 +3421,8 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
34223421
case TU_UPDATE_LEXICAL: {
34233422
DeclContext *TU = ContextObj->getTranslationUnitDecl();
34243423
LexicalContents Contents(
3425-
reinterpret_cast<const unaligned_decl_id_t *>(Blob.data()),
3426-
static_cast<unsigned int>(Blob.size() / sizeof(DeclID)));
3424+
reinterpret_cast<const uint32_t *>(Blob.data()),
3425+
static_cast<unsigned int>(Blob.size() / sizeof(uint32_t)));
34273426
TULexicalDecls.push_back(std::make_pair(&F, Contents));
34283427
TU->setHasExternalLexicalStorage(true);
34293428
break;
@@ -3696,7 +3695,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
36963695
break;
36973696

36983697
case VTABLE_USES:
3699-
if (Record.size() % 3 != 0)
3698+
if (Record.size() % (DeclIDSerialiazedSize + 1 + 1) != 0)
37003699
return llvm::createStringError(std::errc::illegal_byte_sequence,
37013700
"Invalid VTABLE_USES record");
37023701

@@ -3714,8 +3713,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
37143713
break;
37153714

37163715
case PENDING_IMPLICIT_INSTANTIATIONS:
3717-
3718-
if (Record.size() % 2 != 0)
3716+
if (Record.size() % (DeclIDSerialiazedSize + 1) != 0)
37193717
return llvm::createStringError(
37203718
std::errc::illegal_byte_sequence,
37213719
"Invalid PENDING_IMPLICIT_INSTANTIATIONS block");
@@ -3728,7 +3726,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
37283726
break;
37293727

37303728
case SEMA_DECL_REFS:
3731-
if (Record.size() != 3)
3729+
if (Record.size() != 3 * serialization::DeclIDSerialiazedSize)
37323730
return llvm::createStringError(std::errc::illegal_byte_sequence,
37333731
"Invalid SEMA_DECL_REFS block");
37343732
for (unsigned I = 0, N = Record.size(); I != N; /*in loop*/)
@@ -3786,7 +3784,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
37863784
}
37873785

37883786
case DECL_UPDATE_OFFSETS:
3789-
if (Record.size() % 2 != 0)
3787+
if (Record.size() % (DeclIDSerialiazedSize + 1) != 0)
37903788
return llvm::createStringError(
37913789
std::errc::illegal_byte_sequence,
37923790
"invalid DECL_UPDATE_OFFSETS block in AST file");
@@ -3803,7 +3801,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
38033801
break;
38043802

38053803
case DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD: {
3806-
if (Record.size() % 3 != 0)
3804+
if (Record.size() % (DeclIDSerialiazedSize + 2) != 0)
38073805
return llvm::createStringError(
38083806
std::errc::illegal_byte_sequence,
38093807
"invalid DELAYED_NAMESPACE_LEXICAL_VISIBLE_RECORD block in AST "
@@ -3898,7 +3896,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
38983896
break;
38993897

39003898
case UNDEFINED_BUT_USED:
3901-
if (Record.size() % 2 != 0)
3899+
if (Record.size() % (DeclIDSerialiazedSize + 1) != 0)
39023900
return llvm::createStringError(std::errc::illegal_byte_sequence,
39033901
"invalid undefined-but-used record");
39043902
for (unsigned I = 0, N = Record.size(); I != N; /* in loop */) {
@@ -7893,7 +7891,10 @@ GlobalDeclID ASTReader::ReadDeclID(ModuleFile &F, const RecordDataImpl &Record,
78937891
return GlobalDeclID(0);
78947892
}
78957893

7896-
return getGlobalDeclID(F, LocalDeclID::get(*this, F, Record[Idx++]));
7894+
uint32_t ModuleFileIndex = Record[Idx++];
7895+
uint32_t LocalDeclIndex = Record[Idx++];
7896+
return getGlobalDeclID(
7897+
F, LocalDeclID::get(*this, F, ModuleFileIndex, LocalDeclIndex));
78977898
}
78987899

78997900
/// Resolve the offset of a statement into a statement.
@@ -7922,25 +7923,26 @@ void ASTReader::FindExternalLexicalDecls(
79227923
SmallVectorImpl<Decl *> &Decls) {
79237924
bool PredefsVisited[NUM_PREDEF_DECL_IDS] = {};
79247925

7925-
auto Visit = [&] (ModuleFile *M, LexicalContents LexicalDecls) {
7926-
assert(LexicalDecls.size() % 2 == 0 && "expected an even number of entries");
7927-
for (int I = 0, N = LexicalDecls.size(); I != N; I += 2) {
7926+
auto Visit = [&](ModuleFile *M, LexicalContents LexicalDecls) {
7927+
assert(LexicalDecls.size() % 3 == 0 && "incorrect number of entries");
7928+
for (int I = 0, N = LexicalDecls.size(); I != N; I += 3) {
79287929
auto K = (Decl::Kind)+LexicalDecls[I];
79297930
if (!IsKindWeWant(K))
79307931
continue;
79317932

7932-
auto ID = (DeclID) + LexicalDecls[I + 1];
7933+
LocalDeclID ID =
7934+
LocalDeclID::get(*this, *M, LexicalDecls[I + 1], LexicalDecls[I + 2]);
79337935

79347936
// Don't add predefined declarations to the lexical context more
79357937
// than once.
79367938
if (ID < NUM_PREDEF_DECL_IDS) {
7937-
if (PredefsVisited[ID])
7939+
if (PredefsVisited[ID.getRawValue()])
79387940
continue;
79397941

7940-
PredefsVisited[ID] = true;
7942+
PredefsVisited[ID.getRawValue()] = true;
79417943
}
79427944

7943-
if (Decl *D = GetLocalDecl(*M, LocalDeclID::get(*this, *M, ID))) {
7945+
if (Decl *D = GetLocalDecl(*M, ID)) {
79447946
assert(D->getKind() == K && "wrong kind for lexical decl");
79457947
if (!DC->isDeclInLexicalTraversal(D))
79467948
Decls.push_back(D);
@@ -8837,7 +8839,7 @@ void ASTReader::ReadLateParsedTemplates(
88378839
&LPTMap) {
88388840
for (auto &LPT : LateParsedTemplates) {
88398841
ModuleFile *FMod = LPT.first;
8840-
RecordDataImpl &LateParsed = LPT.second;
8842+
RecordData &LateParsed = LPT.second;
88418843
for (unsigned Idx = 0, N = LateParsed.size(); Idx < N;
88428844
/* In loop */) {
88438845
FunctionDecl *FD = ReadDeclAs<FunctionDecl>(*FMod, LateParsed, Idx);

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,9 +1020,8 @@ void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
10201020
case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
10211021
// Templates.
10221022
UnresolvedSet<8> Candidates;
1023-
unsigned NumCandidates = Record.readInt();
1024-
while (NumCandidates--)
1025-
Candidates.addDecl(readDeclAs<NamedDecl>());
1023+
Record.readDeclArray<NamedDecl>(
1024+
[&Candidates](NamedDecl *ND) { Candidates.addDecl(ND); });
10261025

10271026
// Templates args.
10281027
TemplateArgumentListInfo TemplArgsWritten;
@@ -1152,11 +1151,9 @@ void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
11521151
FD->setIsPureVirtual(Pure);
11531152

11541153
// Read in the parameters.
1155-
unsigned NumParams = Record.readInt();
11561154
SmallVector<ParmVarDecl *, 16> Params;
1157-
Params.reserve(NumParams);
1158-
for (unsigned I = 0; I != NumParams; ++I)
1159-
Params.push_back(readDeclAs<ParmVarDecl>());
1155+
Record.readDeclArray<ParmVarDecl>(
1156+
[&Params](ParmVarDecl *ParmD) { Params.push_back(ParmD); });
11601157
FD->setParams(Reader.getContext(), Params);
11611158
}
11621159

@@ -2309,7 +2306,7 @@ void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
23092306
} else {
23102307
// We don't care about which declarations this used to override; we get
23112308
// the relevant information from the canonical declaration.
2312-
Record.skipInts(NumOverridenMethods);
2309+
Record.skipDeclRefs(NumOverridenMethods);
23132310
}
23142311
}
23152312

@@ -4354,8 +4351,9 @@ void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
43544351
// FIXME: We have several different dispatches on decl kind here; maybe
43554352
// we should instead generate one loop per kind and dispatch up-front?
43564353
Decl *MostRecent = FirstLocal;
4357-
for (unsigned I = 0, N = Record.size(); I != N; ++I) {
4358-
unsigned Idx = N - I - 1;
4354+
for (unsigned I = 0, N = Record.size(); I != N;
4355+
I += serialization::DeclIDSerialiazedSize) {
4356+
unsigned Idx = N - I - serialization::DeclIDSerialiazedSize;
43594357
auto *D = ReadDecl(*M, Record, Idx);
43604358
ASTDeclReader::attachPreviousDecl(*this, D, MostRecent, CanonDecl);
43614359
MostRecent = D;

clang/lib/Serialization/ASTReaderStmt.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,15 @@ void ASTStmtReader::VisitDeclStmt(DeclStmt *S) {
351351
S->setStartLoc(readSourceLocation());
352352
S->setEndLoc(readSourceLocation());
353353

354-
if (Record.size() - Record.getIdx() == 1) {
354+
unsigned NumDecls =
355+
(Record.size() - Record.getIdx()) / serialization::DeclIDSerialiazedSize;
356+
if (NumDecls == 1) {
355357
// Single declaration
356358
S->setDeclGroup(DeclGroupRef(readDecl()));
357359
} else {
358360
SmallVector<Decl *, 16> Decls;
359-
int N = Record.size() - Record.getIdx();
360-
Decls.reserve(N);
361-
for (int I = 0; I < N; ++I)
361+
Decls.reserve(NumDecls);
362+
for (unsigned I = 0; I < NumDecls; ++I)
362363
Decls.push_back(readDecl());
363364
S->setDeclGroup(DeclGroupRef(DeclGroup::Create(Record.getContext(),
364365
Decls.data(),

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3321,7 +3321,7 @@ uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
33213321
return 0;
33223322

33233323
uint64_t Offset = Stream.GetCurrentBitNo();
3324-
SmallVector<DeclID, 128> KindDeclPairs;
3324+
SmallVector<uint32_t, 128> KindDeclPairs;
33253325
for (const auto *D : DC->decls()) {
33263326
if (DoneWritingDeclsAndTypes && !wasDeclEmitted(D))
33273327
continue;
@@ -3336,7 +3336,9 @@ uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
33363336
continue;
33373337

33383338
KindDeclPairs.push_back(D->getKind());
3339-
KindDeclPairs.push_back(GetDeclRef(D).getRawValue());
3339+
LocalDeclID ID = GetDeclRef(D);
3340+
KindDeclPairs.push_back(ID.getModuleFileIndex());
3341+
KindDeclPairs.push_back(ID.getLocalDeclIndex());
33403342
}
33413343

33423344
++NumLexicalDeclContexts;
@@ -4451,8 +4453,9 @@ void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
44514453
DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
44524454

44534455
// Write the lookup table
4454-
RecordData::value_type Record[] = {UPDATE_VISIBLE,
4455-
getDeclID(cast<Decl>(DC)).getRawValue()};
4456+
LocalDeclID ID = getDeclID(cast<Decl>(DC));
4457+
RecordData::value_type Record[] = {UPDATE_VISIBLE, ID.getModuleFileIndex(),
4458+
ID.getLocalDeclIndex()};
44564459
Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
44574460
}
44584461

@@ -5243,9 +5246,10 @@ void ASTWriter::WriteSpecialDeclRecords(Sema &SemaRef) {
52435246
RecordData SemaDeclRefs;
52445247
if (SemaRef.StdNamespace || SemaRef.StdBadAlloc || SemaRef.StdAlignValT) {
52455248
auto AddEmittedDeclRefOrZero = [this, &SemaDeclRefs](Decl *D) {
5246-
if (!D || !wasDeclEmitted(D))
5249+
if (!D || !wasDeclEmitted(D)) {
52475250
SemaDeclRefs.push_back(0);
5248-
else
5251+
SemaDeclRefs.push_back(0);
5252+
} else
52495253
AddDeclRef(D, SemaDeclRefs);
52505254
};
52515255

@@ -5679,7 +5683,7 @@ void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
56795683
const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
56805684
// Create a lexical update block containing all of the declarations in the
56815685
// translation unit that do not come from other AST files.
5682-
SmallVector<DeclID, 128> NewGlobalKindDeclPairs;
5686+
SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
56835687
for (const auto *D : TU->noload_decls()) {
56845688
if (D->isFromASTFile())
56855689
continue;
@@ -5689,7 +5693,9 @@ void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
56895693
continue;
56905694

56915695
NewGlobalKindDeclPairs.push_back(D->getKind());
5692-
NewGlobalKindDeclPairs.push_back(GetDeclRef(D).getRawValue());
5696+
LocalDeclID ID = GetDeclRef(D);
5697+
NewGlobalKindDeclPairs.push_back(ID.getModuleFileIndex());
5698+
NewGlobalKindDeclPairs.push_back(ID.getLocalDeclIndex());
56935699
}
56945700

56955701
auto Abv = std::make_shared<llvm::BitCodeAbbrev>();
@@ -5704,6 +5710,7 @@ void ASTWriter::WriteDeclAndTypes(ASTContext &Context) {
57045710
Abv = std::make_shared<llvm::BitCodeAbbrev>();
57055711
Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
57065712
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
5713+
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
57075714
Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
57085715
UpdateVisibleAbbrev = Stream.EmitAbbrev(std::move(Abv));
57095716

@@ -6195,7 +6202,10 @@ void ASTWriter::AddEmittedDeclRef(const Decl *D, RecordDataImpl &Record) {
61956202
}
61966203

61976204
void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
6198-
Record.push_back(GetDeclRef(D).getRawValue());
6205+
LocalDeclID ID = GetDeclRef(D);
6206+
6207+
Record.push_back(ID.getModuleFileIndex());
6208+
Record.push_back(ID.getLocalDeclIndex());
61996209
}
62006210

62016211
LocalDeclID ASTWriter::GetDeclRef(const Decl *D) {

0 commit comments

Comments
 (0)