Skip to content

Commit f63e8ed

Browse files
committed
Revert "[Modules] Delay deserialization of preferred_name attribute at r… (#122726)"
This reverts commit c3ba6f3. We are seeing performance regressions of up to 40% on some compilations with this patch, we will investigate and reland after fixing performance issues.
1 parent 9e6494c commit f63e8ed

File tree

9 files changed

+17
-156
lines changed

9 files changed

+17
-156
lines changed

clang/include/clang/AST/Attr.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ class Attr : public AttributeCommonInfo {
6060
unsigned IsLateParsed : 1;
6161
LLVM_PREFERRED_TYPE(bool)
6262
unsigned InheritEvenIfAlreadyPresent : 1;
63-
LLVM_PREFERRED_TYPE(bool)
64-
unsigned DeferDeserialization : 1;
6563

6664
void *operator new(size_t bytes) noexcept {
6765
llvm_unreachable("Attrs cannot be allocated with regular 'new'.");
@@ -82,11 +80,10 @@ class Attr : public AttributeCommonInfo {
8280

8381
protected:
8482
Attr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
85-
attr::Kind AK, bool IsLateParsed, bool DeferDeserialization = false)
83+
attr::Kind AK, bool IsLateParsed)
8684
: AttributeCommonInfo(CommonInfo), AttrKind(AK), Inherited(false),
8785
IsPackExpansion(false), Implicit(false), IsLateParsed(IsLateParsed),
88-
InheritEvenIfAlreadyPresent(false),
89-
DeferDeserialization(DeferDeserialization) {}
86+
InheritEvenIfAlreadyPresent(false) {}
9087

9188
public:
9289
attr::Kind getKind() const { return static_cast<attr::Kind>(AttrKind); }
@@ -108,8 +105,6 @@ class Attr : public AttributeCommonInfo {
108105
void setPackExpansion(bool PE) { IsPackExpansion = PE; }
109106
bool isPackExpansion() const { return IsPackExpansion; }
110107

111-
bool shouldDeferDeserialization() const { return DeferDeserialization; }
112-
113108
// Clone this attribute.
114109
Attr *clone(ASTContext &C) const;
115110

@@ -151,9 +146,8 @@ class InheritableAttr : public Attr {
151146
protected:
152147
InheritableAttr(ASTContext &Context, const AttributeCommonInfo &CommonInfo,
153148
attr::Kind AK, bool IsLateParsed,
154-
bool InheritEvenIfAlreadyPresent,
155-
bool DeferDeserialization = false)
156-
: Attr(Context, CommonInfo, AK, IsLateParsed, DeferDeserialization) {
149+
bool InheritEvenIfAlreadyPresent)
150+
: Attr(Context, CommonInfo, AK, IsLateParsed) {
157151
this->InheritEvenIfAlreadyPresent = InheritEvenIfAlreadyPresent;
158152
}
159153

clang/include/clang/Basic/Attr.td

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,6 @@ class Attr {
713713
// attribute may be documented under multiple categories, more than one
714714
// Documentation entry may be listed.
715715
list<Documentation> Documentation;
716-
// Set to true if deserialization of this attribute must be deferred until
717-
// the parent Decl is fully deserialized (during header module file
718-
// deserialization). E.g., this is the case for the preferred_name attribute,
719-
// since its type deserialization depends on its target Decl type.
720-
// (See https://github.com/llvm/llvm-project/issues/56490 for details).
721-
bit DeferDeserialization = 0;
722716
}
723717

724718
/// Used to define a set of mutually exclusive attributes.
@@ -3260,11 +3254,6 @@ def PreferredName : InheritableAttr {
32603254
let InheritEvenIfAlreadyPresent = 1;
32613255
let MeaningfulToClassTemplateDefinition = 1;
32623256
let TemplateDependent = 1;
3263-
// Type of this attribute depends on the target Decl type.
3264-
// Therefore, its deserialization must be deferred until
3265-
// deserialization of the target Decl is complete
3266-
// (for header modules).
3267-
let DeferDeserialization = 1;
32683257
}
32693258

32703259
def PreserveMost : DeclOrTypeAttr {

clang/include/clang/Serialization/ASTReader.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1236,24 +1236,6 @@ class ASTReader
12361236
/// been completed.
12371237
std::deque<PendingDeclContextInfo> PendingDeclContextInfos;
12381238

1239-
/// Deserialization of some attributes must be deferred since they refer
1240-
/// to themselves in their type (e.g., preferred_name attribute refers to the
1241-
/// typedef that refers back to the template specialization of the template
1242-
/// that the attribute is attached to).
1243-
/// More attributes that store TypeSourceInfo might be potentially affected,
1244-
/// see https://github.com/llvm/llvm-project/issues/56490 for details.
1245-
struct DeferredAttribute {
1246-
// Index of the deferred attribute in the Record of the TargetedDecl.
1247-
uint64_t RecordIdx;
1248-
// Decl to attach a deferred attribute to.
1249-
Decl *TargetedDecl;
1250-
};
1251-
1252-
/// The collection of Decls that have been loaded but some of their attributes
1253-
/// have been deferred, paired with the index inside the record pointing
1254-
/// at the skipped attribute.
1255-
SmallVector<DeferredAttribute> PendingDeferredAttributes;
1256-
12571239
template <typename DeclTy>
12581240
using DuplicateObjCDecls = std::pair<DeclTy *, DeclTy *>;
12591241

@@ -1606,7 +1588,6 @@ class ASTReader
16061588
void loadPendingDeclChain(Decl *D, uint64_t LocalOffset);
16071589
void loadObjCCategories(GlobalDeclID ID, ObjCInterfaceDecl *D,
16081590
unsigned PreviousGeneration = 0);
1609-
void loadDeferredAttribute(const DeferredAttribute &DA);
16101591

16111592
RecordLocation getLocalBitOffset(uint64_t GlobalOffset);
16121593
uint64_t getGlobalBitOffset(ModuleFile &M, uint64_t LocalOffset);

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,6 @@ class ASTRecordReader
8383
/// Returns the current value in this record, without advancing.
8484
uint64_t peekInt() { return Record[Idx]; }
8585

86-
/// Returns the next N values in this record, without advancing.
87-
uint64_t peekInts(unsigned N) { return Record[Idx + N]; }
88-
89-
/// Skips the current value.
90-
void skipInt() { Idx += 1; }
91-
9286
/// Skips the specified number of values.
9387
void skipInts(unsigned N) { Idx += N; }
9488

@@ -341,12 +335,7 @@ class ASTRecordReader
341335
Attr *readAttr();
342336

343337
/// Reads attributes from the current stream position, advancing Idx.
344-
/// For some attributes (where type depends on itself recursively), defer
345-
/// reading the attribute until the type has been read.
346-
void readAttributes(AttrVec &Attrs, Decl *D = nullptr);
347-
348-
/// Reads one attribute from the current stream position, advancing Idx.
349-
Attr *readOrDeferAttrFor(Decl *D);
338+
void readAttributes(AttrVec &Attrs);
350339

351340
/// Read an BTFTypeTagAttr object.
352341
BTFTypeTagAttr *readBTFTypeTagAttr() {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10239,11 +10239,6 @@ void ASTReader::finishPendingActions() {
1023910239
}
1024010240
PendingDeducedVarTypes.clear();
1024110241

10242-
// Load the delayed preferred name attributes.
10243-
for (unsigned I = 0; I != PendingDeferredAttributes.size(); ++I)
10244-
loadDeferredAttribute(PendingDeferredAttributes[I]);
10245-
PendingDeferredAttributes.clear();
10246-
1024710242
// For each decl chain that we wanted to complete while deserializing, mark
1024810243
// it as "still needs to be completed".
1024910244
for (unsigned I = 0; I != PendingIncompleteDeclChains.size(); ++I) {

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 4 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ void ASTDeclReader::VisitDecl(Decl *D) {
613613

614614
if (HasAttrs) {
615615
AttrVec Attrs;
616-
Record.readAttributes(Attrs, D);
616+
Record.readAttributes(Attrs);
617617
// Avoid calling setAttrs() directly because it uses Decl::getASTContext()
618618
// internally which is unsafe during derialization.
619619
D->setAttrsImpl(Attrs, Reader.getContext());
@@ -3098,8 +3098,6 @@ class AttrReader {
30983098
return Reader.readInt();
30993099
}
31003100

3101-
uint64_t peekInts(unsigned N) { return Reader.peekInts(N); }
3102-
31033101
bool readBool() { return Reader.readBool(); }
31043102

31053103
SourceRange readSourceRange() {
@@ -3130,29 +3128,18 @@ class AttrReader {
31303128
return Reader.readVersionTuple();
31313129
}
31323130

3133-
void skipInt() { Reader.skipInts(1); }
3134-
3135-
void skipInts(unsigned N) { Reader.skipInts(N); }
3136-
3137-
unsigned getCurrentIdx() { return Reader.getIdx(); }
3138-
31393131
OMPTraitInfo *readOMPTraitInfo() { return Reader.readOMPTraitInfo(); }
31403132

31413133
template <typename T> T *readDeclAs() { return Reader.readDeclAs<T>(); }
31423134
};
31433135
}
31443136

3145-
/// Reads one attribute from the current stream position, advancing Idx.
31463137
Attr *ASTRecordReader::readAttr() {
31473138
AttrReader Record(*this);
31483139
auto V = Record.readInt();
31493140
if (!V)
31503141
return nullptr;
31513142

3152-
// Read and ignore the skip count, since attribute deserialization is not
3153-
// deferred on this pass.
3154-
Record.skipInt();
3155-
31563143
Attr *New = nullptr;
31573144
// Kind is stored as a 1-based integer because 0 is used to indicate a null
31583145
// Attr pointer.
@@ -3182,28 +3169,13 @@ Attr *ASTRecordReader::readAttr() {
31823169
return New;
31833170
}
31843171

3185-
/// Reads attributes from the current stream position, advancing Idx.
3186-
/// For some attributes (where type depends on itself recursively), defer
3187-
/// reading the attribute until the type has been read.
3188-
void ASTRecordReader::readAttributes(AttrVec &Attrs, Decl *D) {
3172+
/// Reads attributes from the current stream position.
3173+
void ASTRecordReader::readAttributes(AttrVec &Attrs) {
31893174
for (unsigned I = 0, E = readInt(); I != E; ++I)
3190-
if (auto *A = readOrDeferAttrFor(D))
3175+
if (auto *A = readAttr())
31913176
Attrs.push_back(A);
31923177
}
31933178

3194-
/// Reads one attribute from the current stream position, advancing Idx.
3195-
/// For some attributes (where type depends on itself recursively), defer
3196-
/// reading the attribute until the type has been read.
3197-
Attr *ASTRecordReader::readOrDeferAttrFor(Decl *D) {
3198-
AttrReader Record(*this);
3199-
unsigned SkipCount = Record.peekInts(1);
3200-
if (!SkipCount)
3201-
return readAttr();
3202-
Reader->PendingDeferredAttributes.push_back({Record.getCurrentIdx(), D});
3203-
Record.skipInts(SkipCount);
3204-
return nullptr;
3205-
}
3206-
32073179
//===----------------------------------------------------------------------===//
32083180
// ASTReader Implementation
32093181
//===----------------------------------------------------------------------===//
@@ -4512,49 +4484,6 @@ void ASTReader::loadPendingDeclChain(Decl *FirstLocal, uint64_t LocalOffset) {
45124484
ASTDeclReader::attachLatestDecl(CanonDecl, MostRecent);
45134485
}
45144486

4515-
void ASTReader::loadDeferredAttribute(const DeferredAttribute &DA) {
4516-
Decl *D = DA.TargetedDecl;
4517-
ModuleFile *M = getOwningModuleFile(D);
4518-
4519-
unsigned LocalDeclIndex = D->getGlobalID().getLocalDeclIndex();
4520-
const DeclOffset &DOffs = M->DeclOffsets[LocalDeclIndex];
4521-
RecordLocation Loc(M, DOffs.getBitOffset(M->DeclsBlockStartOffset));
4522-
4523-
llvm::BitstreamCursor &Cursor = Loc.F->DeclsCursor;
4524-
SavedStreamPosition SavedPosition(Cursor);
4525-
if (llvm::Error Err = Cursor.JumpToBit(Loc.Offset)) {
4526-
Error(std::move(Err));
4527-
}
4528-
4529-
Expected<unsigned> MaybeCode = Cursor.ReadCode();
4530-
if (!MaybeCode) {
4531-
llvm::report_fatal_error(
4532-
Twine("ASTReader::loadPreferredNameAttribute failed reading code: ") +
4533-
toString(MaybeCode.takeError()));
4534-
}
4535-
unsigned Code = MaybeCode.get();
4536-
4537-
ASTRecordReader Record(*this, *Loc.F);
4538-
Expected<unsigned> MaybeRecCode = Record.readRecord(Cursor, Code);
4539-
if (!MaybeRecCode) {
4540-
llvm::report_fatal_error(
4541-
Twine(
4542-
"ASTReader::loadPreferredNameAttribute failed reading rec code: ") +
4543-
toString(MaybeCode.takeError()));
4544-
}
4545-
unsigned RecCode = MaybeRecCode.get();
4546-
if (RecCode < DECL_TYPEDEF || RecCode > DECL_LAST) {
4547-
llvm::report_fatal_error(
4548-
Twine("ASTReader::loadPreferredNameAttribute failed reading rec code: "
4549-
"expected valid DeclCode") +
4550-
toString(MaybeCode.takeError()));
4551-
}
4552-
4553-
Record.skipInts(DA.RecordIdx);
4554-
Attr *A = Record.readAttr();
4555-
getContext().getDeclAttrs(D).push_back(A);
4556-
}
4557-
45584487
namespace {
45594488

45604489
/// Given an ObjC interface, goes through the modules and links to the

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
#include "clang/AST/Type.h"
3838
#include "clang/AST/TypeLoc.h"
3939
#include "clang/AST/TypeLocVisitor.h"
40-
#include "clang/Basic/AttrKinds.h"
4140
#include "clang/Basic/Diagnostic.h"
4241
#include "clang/Basic/DiagnosticOptions.h"
4342
#include "clang/Basic/FileEntry.h"
@@ -5156,14 +5155,15 @@ void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
51565155

51575156
void ASTRecordWriter::AddAttr(const Attr *A) {
51585157
auto &Record = *this;
5159-
if (!A)
5158+
// FIXME: Clang can't handle the serialization/deserialization of
5159+
// preferred_name properly now. See
5160+
// https://github.com/llvm/llvm-project/issues/56490 for example.
5161+
if (!A || (isa<PreferredNameAttr>(A) &&
5162+
Writer->isWritingStdCXXNamedModules()))
51605163
return Record.push_back(0);
51615164

51625165
Record.push_back(A->getKind() + 1); // FIXME: stable encoding, target attrs
51635166

5164-
auto SkipIdx = Record.size();
5165-
// Add placeholder for the size of deferred attribute.
5166-
Record.push_back(0);
51675167
Record.AddIdentifierRef(A->getAttrName());
51685168
Record.AddIdentifierRef(A->getScopeName());
51695169
Record.AddSourceRange(A->getRange());
@@ -5174,12 +5174,6 @@ void ASTRecordWriter::AddAttr(const Attr *A) {
51745174
Record.push_back(A->isRegularKeywordAttribute());
51755175

51765176
#include "clang/Serialization/AttrPCHWrite.inc"
5177-
5178-
if (A->shouldDeferDeserialization()) {
5179-
// Record the actual size of deferred attribute (+ 1 to count the attribute
5180-
// kind).
5181-
Record[SkipIdx] = Record.size() - SkipIdx + 1;
5182-
}
51835177
}
51845178

51855179
/// Emit the list of attributes to the specified record.

clang/test/Modules/preferred_name.cppm

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,10 @@ import A;
5353
export using ::foo_templ;
5454

5555
//--- Use1.cpp
56-
// expected-no-diagnostics
57-
import A;
58-
#include "foo.h"
56+
import A; // expected-[email protected]:8 {{attribute declaration must precede definition}}
57+
#include "foo.h" // [email protected]:9 {{previous definition is here}}
58+
5959
//--- Use2.cpp
6060
// expected-no-diagnostics
6161
#include "foo.h"
6262
import A;
63-
64-
//--- Use3.cpp
65-
#include "foo.h"
66-
import A;
67-
foo test;
68-
int size = test.size(); // expected-error {{no member named 'size' in 'foo'}}

clang/utils/TableGen/ClangAttrEmitter.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,10 +3043,6 @@ static void emitAttributes(const RecordKeeper &Records, raw_ostream &OS,
30433043
<< (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true"
30443044
: "false");
30453045
}
3046-
if (R.getValueAsBit("DeferDeserialization")) {
3047-
OS << ", "
3048-
<< "/*DeferDeserialization=*/true";
3049-
}
30503046
OS << ")\n";
30513047

30523048
for (auto const &ai : Args) {

0 commit comments

Comments
 (0)