Skip to content

Commit 565e5e8

Browse files
committed
Recommit [NFC] [Serialization] Packing more bits and refactor AbbrevToUse
This patch tries to pack more bits into a value to reduce the size of .pcm files. Also, after we introduced BitsPackers, it may slightly better to adjust the way we use Abbrev. After this patch, the size of the BMI for std module reduce from 28.94MB to 28.1 MB. This was reverted due to it broke the build of lldb. The reason that we skip the serialization of a source location incorrectly. And this patch now fixes that.
1 parent 9e012c7 commit 565e5e8

File tree

7 files changed

+786
-363
lines changed

7 files changed

+786
-363
lines changed

clang/include/clang/Serialization/ASTReader.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,8 @@ class BitsUnpacker {
24222422
CurrentBitsIndex = 0;
24232423
}
24242424

2425+
void advance(uint32_t BitsWidth) { CurrentBitsIndex += BitsWidth; }
2426+
24252427
bool getNextBit() {
24262428
assert(isValid());
24272429
return Value & (1 << CurrentBitsIndex++);

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,25 @@ class ASTWriter : public ASTDeserializationListener,
564564
unsigned DeclEnumAbbrev = 0;
565565
unsigned DeclObjCIvarAbbrev = 0;
566566
unsigned DeclCXXMethodAbbrev = 0;
567+
unsigned DeclDependentNonTemplateCXXMethodAbbrev = 0;
568+
unsigned DeclTemplateCXXMethodAbbrev = 0;
569+
unsigned DeclMemberSpecializedCXXMethodAbbrev = 0;
570+
unsigned DeclTemplateSpecializedCXXMethodAbbrev = 0;
571+
unsigned DeclDependentSpecializationCXXMethodAbbrev = 0;
572+
unsigned DeclTemplateTypeParmAbbrev = 0;
573+
unsigned DeclUsingShadowAbbrev = 0;
567574

568575
unsigned DeclRefExprAbbrev = 0;
569576
unsigned CharacterLiteralAbbrev = 0;
570577
unsigned IntegerLiteralAbbrev = 0;
571578
unsigned ExprImplicitCastAbbrev = 0;
579+
unsigned BinaryOperatorAbbrev = 0;
580+
unsigned CompoundAssignOperatorAbbrev = 0;
581+
unsigned CallExprAbbrev = 0;
582+
unsigned CXXOperatorCallExprAbbrev = 0;
583+
unsigned CXXMemberCallExprAbbrev = 0;
584+
585+
unsigned CompoundStmtAbbrev = 0;
572586

573587
void WriteDeclAbbrevs();
574588
void WriteDecl(ASTContext &Context, Decl *D);
@@ -735,12 +749,41 @@ class ASTWriter : public ASTDeserializationListener,
735749
unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
736750
unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
737751
unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
738-
unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
752+
unsigned getDeclCXXMethodAbbrev(FunctionDecl::TemplatedKind Kind) const {
753+
switch (Kind) {
754+
case FunctionDecl::TK_NonTemplate:
755+
return DeclCXXMethodAbbrev;
756+
case FunctionDecl::TK_FunctionTemplate:
757+
return DeclTemplateCXXMethodAbbrev;
758+
case FunctionDecl::TK_MemberSpecialization:
759+
return DeclMemberSpecializedCXXMethodAbbrev;
760+
case FunctionDecl::TK_FunctionTemplateSpecialization:
761+
return DeclTemplateSpecializedCXXMethodAbbrev;
762+
case FunctionDecl::TK_DependentNonTemplate:
763+
return DeclDependentNonTemplateCXXMethodAbbrev;
764+
case FunctionDecl::TK_DependentFunctionTemplateSpecialization:
765+
return DeclDependentSpecializationCXXMethodAbbrev;
766+
}
767+
llvm_unreachable("Unknwon Template Kind!");
768+
}
769+
unsigned getDeclTemplateTypeParmAbbrev() const {
770+
return DeclTemplateTypeParmAbbrev;
771+
}
772+
unsigned getDeclUsingShadowAbbrev() const { return DeclUsingShadowAbbrev; }
739773

740774
unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
741775
unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
742776
unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
743777
unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
778+
unsigned getBinaryOperatorAbbrev() const { return BinaryOperatorAbbrev; }
779+
unsigned getCompoundAssignOperatorAbbrev() const {
780+
return CompoundAssignOperatorAbbrev;
781+
}
782+
unsigned getCallExprAbbrev() const { return CallExprAbbrev; }
783+
unsigned getCXXOperatorCallExprAbbrev() { return CXXOperatorCallExprAbbrev; }
784+
unsigned getCXXMemberCallExprAbbrev() { return CXXMemberCallExprAbbrev; }
785+
786+
unsigned getCompoundStmtAbbrev() const { return CompoundStmtAbbrev; }
744787

745788
bool hasChain() const { return Chain; }
746789
ASTReader *getChain() const { return Chain; }
@@ -841,46 +884,33 @@ class BitsPacker {
841884
BitsPacker(BitsPacker &&) = delete;
842885
BitsPacker operator=(const BitsPacker &) = delete;
843886
BitsPacker operator=(BitsPacker &&) = delete;
844-
~BitsPacker() {
845-
assert(!hasUnconsumedValues() && "There are unprocessed bits!");
887+
~BitsPacker() = default;
888+
889+
bool canWriteNextNBits(uint32_t BitsWidth) const {
890+
return CurrentBitIndex + BitsWidth < BitIndexUpbound;
891+
}
892+
893+
void reset(uint32_t Value) {
894+
UnderlyingValue = Value;
895+
CurrentBitIndex = 0;
846896
}
847897

848898
void addBit(bool Value) { addBits(Value, 1); }
849899
void addBits(uint32_t Value, uint32_t BitsWidth) {
850900
assert(BitsWidth < BitIndexUpbound);
851901
assert((Value < (1u << BitsWidth)) && "Passing narrower bit width!");
902+
assert(canWriteNextNBits(BitsWidth) &&
903+
"Inserting too much bits into a value!");
852904

853-
if (CurrentBitIndex + BitsWidth >= BitIndexUpbound) {
854-
Values.push_back(0);
855-
CurrentBitIndex = 0;
856-
}
857-
858-
assert(CurrentBitIndex < BitIndexUpbound);
859-
Values.back() |= Value << CurrentBitIndex;
905+
UnderlyingValue |= Value << CurrentBitIndex;
860906
CurrentBitIndex += BitsWidth;
861907
}
862908

863-
bool hasUnconsumedValues() const {
864-
return ConsumingValueIndex < Values.size();
865-
}
866-
uint32_t getNextValue() {
867-
assert(hasUnconsumedValues());
868-
return Values[ConsumingValueIndex++];
869-
}
870-
871-
// We can convert the packer to an uint32_t if there is only one values.
872-
operator uint32_t() {
873-
assert(Values.size() == 1);
874-
return getNextValue();
875-
}
909+
operator uint32_t() { return UnderlyingValue; }
876910

877911
private:
878-
SmallVector<uint64_t, 4> Values;
879-
uint16_t ConsumingValueIndex = 0;
880-
// Initialize CurrentBitIndex with an invalid value
881-
// to make it easier to update Values. See the implementation
882-
// of `addBits` to see the details.
883-
uint16_t CurrentBitIndex = BitIndexUpbound;
912+
uint32_t UnderlyingValue = 0;
913+
uint32_t CurrentBitIndex = 0;
884914
};
885915

886916
} // namespace clang

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2660,7 +2660,7 @@ void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
26602660

26612661
D->setDeclaredWithTypename(Record.readInt());
26622662

2663-
if (Record.readBool()) {
2663+
if (D->hasTypeConstraint()) {
26642664
ConceptReference *CR = nullptr;
26652665
if (Record.readBool())
26662666
CR = Record.readConceptReference();

0 commit comments

Comments
 (0)