Skip to content

Commit

Permalink
[NFC] [Serialization] Packing more bits and refactor AbbrevToUse
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ChuanqiXu9 committed Dec 15, 2023
1 parent f930497 commit 9cdb825
Show file tree
Hide file tree
Showing 7 changed files with 788 additions and 364 deletions.
2 changes: 2 additions & 0 deletions clang/include/clang/Serialization/ASTReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,8 @@ class BitsUnpacker {
CurrentBitsIndex = 0;
}

void advance(uint32_t BitsWidth) { CurrentBitsIndex += BitsWidth; }

bool getNextBit() {
assert(isValid());
return Value & (1 << CurrentBitsIndex++);
Expand Down
89 changes: 60 additions & 29 deletions clang/include/clang/Serialization/ASTWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,11 +564,25 @@ class ASTWriter : public ASTDeserializationListener,
unsigned DeclEnumAbbrev = 0;
unsigned DeclObjCIvarAbbrev = 0;
unsigned DeclCXXMethodAbbrev = 0;
unsigned DeclDependentNonTemplateCXXMethodAbbrev = 0;
unsigned DeclTemplateCXXMethodAbbrev = 0;
unsigned DeclMemberSpecializedCXXMethodAbbrev = 0;
unsigned DeclTemplateSpecializedCXXMethodAbbrev = 0;
unsigned DeclDependentSpecializationCXXMethodAbbrev = 0;
unsigned DeclTemplateTypeParmAbbrev = 0;
unsigned DeclUsingShadowAbbrev = 0;

unsigned DeclRefExprAbbrev = 0;
unsigned CharacterLiteralAbbrev = 0;
unsigned IntegerLiteralAbbrev = 0;
unsigned ExprImplicitCastAbbrev = 0;
unsigned BinaryOperatorAbbrev = 0;
unsigned CompoundAssignOperatorAbbrev = 0;
unsigned CallExprAbbrev = 0;
unsigned CXXOperatorCallExprAbbrev = 0;
unsigned CXXMemberCallExprAbbrev = 0;

unsigned CompoundStmtAbbrev = 0;

void WriteDeclAbbrevs();
void WriteDecl(ASTContext &Context, Decl *D);
Expand Down Expand Up @@ -735,12 +749,42 @@ class ASTWriter : public ASTDeserializationListener,
unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
unsigned getDeclCXXMethodAbbrev(FunctionDecl::TemplatedKind Kind) const {
switch (Kind) {
case FunctionDecl::TK_NonTemplate:
return DeclCXXMethodAbbrev;
case FunctionDecl::TK_FunctionTemplate:
return DeclTemplateCXXMethodAbbrev;
case FunctionDecl::TK_MemberSpecialization:
return DeclMemberSpecializedCXXMethodAbbrev;
case FunctionDecl::TK_FunctionTemplateSpecialization:
return DeclTemplateSpecializedCXXMethodAbbrev;
case FunctionDecl::TK_DependentNonTemplate:
return DeclDependentNonTemplateCXXMethodAbbrev;
case FunctionDecl::TK_DependentFunctionTemplateSpecialization:
return DeclDependentSpecializationCXXMethodAbbrev;
default:
llvm_unreachable("Unknwon Template Kind!");
}
}
unsigned getDeclTemplateTypeParmAbbrev() const {
return DeclTemplateTypeParmAbbrev;
}
unsigned getDeclUsingShadowAbbrev() const { return DeclUsingShadowAbbrev; }

unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
unsigned getBinaryOperatorAbbrev() const { return BinaryOperatorAbbrev; }
unsigned getCompoundAssignOperatorAbbrev() const {
return CompoundAssignOperatorAbbrev;
}
unsigned getCallExprAbbrev() const { return CallExprAbbrev; }
unsigned getCXXOperatorCallExprAbbrev() { return CXXOperatorCallExprAbbrev; }
unsigned getCXXMemberCallExprAbbrev() { return CXXMemberCallExprAbbrev; }

unsigned getCompoundStmtAbbrev() const { return CompoundStmtAbbrev; }

bool hasChain() const { return Chain; }
ASTReader *getChain() const { return Chain; }
Expand Down Expand Up @@ -841,46 +885,33 @@ class BitsPacker {
BitsPacker(BitsPacker &&) = delete;
BitsPacker operator=(const BitsPacker &) = delete;
BitsPacker operator=(BitsPacker &&) = delete;
~BitsPacker() {
assert(!hasUnconsumedValues() && "There are unprocessed bits!");
~BitsPacker() = default;

bool canWriteNextNBits(uint32_t BitsWidth) const {
return CurrentBitIndex + BitsWidth < BitIndexUpbound;
}

void reset(uint32_t Value) {
UnderlyingValue = Value;
CurrentBitIndex = 0;
}

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

if (CurrentBitIndex + BitsWidth >= BitIndexUpbound) {
Values.push_back(0);
CurrentBitIndex = 0;
}

assert(CurrentBitIndex < BitIndexUpbound);
Values.back() |= Value << CurrentBitIndex;
UnderlyingValue |= Value << CurrentBitIndex;
CurrentBitIndex += BitsWidth;
}

bool hasUnconsumedValues() const {
return ConsumingValueIndex < Values.size();
}
uint32_t getNextValue() {
assert(hasUnconsumedValues());
return Values[ConsumingValueIndex++];
}

// We can convert the packer to an uint32_t if there is only one values.
operator uint32_t() {
assert(Values.size() == 1);
return getNextValue();
}
operator uint32_t() { return UnderlyingValue; }

private:
SmallVector<uint64_t, 4> Values;
uint16_t ConsumingValueIndex = 0;
// Initialize CurrentBitIndex with an invalid value
// to make it easier to update Values. See the implementation
// of `addBits` to see the details.
uint16_t CurrentBitIndex = BitIndexUpbound;
uint32_t UnderlyingValue = 0;
uint32_t CurrentBitIndex = 0;
};

} // namespace clang
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Serialization/ASTReaderDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,7 @@ void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {

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

if (Record.readBool()) {
if (D->hasTypeConstraint()) {
ConceptReference *CR = nullptr;
if (Record.readBool())
CR = Record.readConceptReference();
Expand Down

0 comments on commit 9cdb825

Please sign in to comment.