Skip to content

Commit 15bde22

Browse files
committed
Add support in LLVM BitstreamWriter to automatically choose abbrevs.
This adds a new API EmitRecordAutoAbbrev, which chooses a valid abbreviation that can encode the provided record, if one exists, otherwise emits an unabbreviated record. Use this new functionality in Clang's ASTWriter, eliminating all error-prone manual specification of abbrevs. This PR was created as an alternative to the proposal to stop using abbrevs in Clang record emission, in https://discourse.llvm.org/t/rfc-c-modules-stop-using-abbrev-and-drop-the-maintainance. (Note, only after starting this did I discover that records encoded with a "Blob" abbrev vs those written as unabbreviated records are not interchangeable to the current BitstreamReader code. Given that, I'm wondering if I should remove blob support from EmitRecordAutoAbbrev, and switch the blob-encoding callers back to EmitRecordWithBlob.)
1 parent 551d6dd commit 15bde22

File tree

6 files changed

+405
-560
lines changed

6 files changed

+405
-560
lines changed

clang/include/clang/Serialization/ASTRecordWriter.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,20 +91,19 @@ class ASTRecordWriter
9191

9292
/// Emit the record to the stream, followed by its substatements, and
9393
/// return its offset.
94-
// FIXME: Allow record producers to suggest Abbrevs.
95-
uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
94+
uint64_t Emit(unsigned Code) {
9695
uint64_t Offset = Writer->Stream.GetCurrentBitNo();
9796
PrepareToEmit(Offset);
98-
Writer->Stream.EmitRecord(Code, *Record, Abbrev);
97+
Writer->Stream.EmitRecord(Code, *Record);
9998
FlushStmts();
10099
return Offset;
101100
}
102101

103102
/// Emit the record to the stream, preceded by its substatements.
104-
uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
103+
uint64_t EmitStmt(unsigned Code) {
105104
FlushSubStmts();
106105
PrepareToEmit(Writer->Stream.GetCurrentBitNo());
107-
Writer->Stream.EmitRecord(Code, *Record, Abbrev);
106+
Writer->Stream.EmitRecordAutoAbbrev(Code, *Record);
108107
return Writer->Stream.GetCurrentBitNo();
109108
}
110109

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -638,44 +638,6 @@ class ASTWriter : public ASTDeserializationListener,
638638
void WriteModuleFileExtension(Sema &SemaRef,
639639
ModuleFileExtensionWriter &Writer);
640640

641-
unsigned DeclParmVarAbbrev = 0;
642-
unsigned DeclContextLexicalAbbrev = 0;
643-
unsigned DeclContextVisibleLookupAbbrev = 0;
644-
unsigned DeclModuleLocalVisibleLookupAbbrev = 0;
645-
unsigned DeclTULocalLookupAbbrev = 0;
646-
unsigned UpdateVisibleAbbrev = 0;
647-
unsigned ModuleLocalUpdateVisibleAbbrev = 0;
648-
unsigned TULocalUpdateVisibleAbbrev = 0;
649-
unsigned DeclRecordAbbrev = 0;
650-
unsigned DeclTypedefAbbrev = 0;
651-
unsigned DeclVarAbbrev = 0;
652-
unsigned DeclFieldAbbrev = 0;
653-
unsigned DeclEnumAbbrev = 0;
654-
unsigned DeclObjCIvarAbbrev = 0;
655-
unsigned DeclCXXMethodAbbrev = 0;
656-
unsigned DeclSpecializationsAbbrev = 0;
657-
unsigned DeclPartialSpecializationsAbbrev = 0;
658-
659-
unsigned DeclDependentNonTemplateCXXMethodAbbrev = 0;
660-
unsigned DeclTemplateCXXMethodAbbrev = 0;
661-
unsigned DeclMemberSpecializedCXXMethodAbbrev = 0;
662-
unsigned DeclTemplateSpecializedCXXMethodAbbrev = 0;
663-
unsigned DeclDependentSpecializationCXXMethodAbbrev = 0;
664-
unsigned DeclTemplateTypeParmAbbrev = 0;
665-
unsigned DeclUsingShadowAbbrev = 0;
666-
667-
unsigned DeclRefExprAbbrev = 0;
668-
unsigned CharacterLiteralAbbrev = 0;
669-
unsigned IntegerLiteralAbbrev = 0;
670-
unsigned ExprImplicitCastAbbrev = 0;
671-
unsigned BinaryOperatorAbbrev = 0;
672-
unsigned CompoundAssignOperatorAbbrev = 0;
673-
unsigned CallExprAbbrev = 0;
674-
unsigned CXXOperatorCallExprAbbrev = 0;
675-
unsigned CXXMemberCallExprAbbrev = 0;
676-
677-
unsigned CompoundStmtAbbrev = 0;
678-
679641
void WriteDeclAbbrevs();
680642
void WriteDecl(ASTContext &Context, Decl *D);
681643

@@ -844,53 +806,6 @@ class ASTWriter : public ASTDeserializationListener,
844806

845807
void ClearSwitchCaseIDs();
846808

847-
unsigned getTypeExtQualAbbrev() const {
848-
return TypeExtQualAbbrev;
849-
}
850-
851-
unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
852-
unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
853-
unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
854-
unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
855-
unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
856-
unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
857-
unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
858-
unsigned getDeclCXXMethodAbbrev(FunctionDecl::TemplatedKind Kind) const {
859-
switch (Kind) {
860-
case FunctionDecl::TK_NonTemplate:
861-
return DeclCXXMethodAbbrev;
862-
case FunctionDecl::TK_FunctionTemplate:
863-
return DeclTemplateCXXMethodAbbrev;
864-
case FunctionDecl::TK_MemberSpecialization:
865-
return DeclMemberSpecializedCXXMethodAbbrev;
866-
case FunctionDecl::TK_FunctionTemplateSpecialization:
867-
return DeclTemplateSpecializedCXXMethodAbbrev;
868-
case FunctionDecl::TK_DependentNonTemplate:
869-
return DeclDependentNonTemplateCXXMethodAbbrev;
870-
case FunctionDecl::TK_DependentFunctionTemplateSpecialization:
871-
return DeclDependentSpecializationCXXMethodAbbrev;
872-
}
873-
llvm_unreachable("Unknwon Template Kind!");
874-
}
875-
unsigned getDeclTemplateTypeParmAbbrev() const {
876-
return DeclTemplateTypeParmAbbrev;
877-
}
878-
unsigned getDeclUsingShadowAbbrev() const { return DeclUsingShadowAbbrev; }
879-
880-
unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
881-
unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
882-
unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
883-
unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
884-
unsigned getBinaryOperatorAbbrev() const { return BinaryOperatorAbbrev; }
885-
unsigned getCompoundAssignOperatorAbbrev() const {
886-
return CompoundAssignOperatorAbbrev;
887-
}
888-
unsigned getCallExprAbbrev() const { return CallExprAbbrev; }
889-
unsigned getCXXOperatorCallExprAbbrev() { return CXXOperatorCallExprAbbrev; }
890-
unsigned getCXXMemberCallExprAbbrev() { return CXXMemberCallExprAbbrev; }
891-
892-
unsigned getCompoundStmtAbbrev() const { return CompoundStmtAbbrev; }
893-
894809
bool hasChain() const { return Chain; }
895810
ASTReader *getChain() const { return Chain; }
896811

0 commit comments

Comments
 (0)