Skip to content

Commit 1a83e2b

Browse files
committed
[serialization] No transitive type change
1 parent a2fb7f5 commit 1a83e2b

File tree

11 files changed

+196
-119
lines changed

11 files changed

+196
-119
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "clang/Serialization/SourceLocationEncoding.h"
2727
#include "llvm/ADT/DenseMapInfo.h"
2828
#include "llvm/Bitstream/BitCodes.h"
29+
#include "llvm/Support/MathExtras.h"
2930
#include <cassert>
3031
#include <cstdint>
3132

@@ -70,38 +71,53 @@ using DeclID = DeclIDBase::DeclID;
7071

7172
/// An ID number that refers to a type in an AST file.
7273
///
73-
/// The ID of a type is partitioned into two parts: the lower
74+
/// The ID of a type is partitioned into three parts:
75+
/// - the lower
7476
/// three bits are used to store the const/volatile/restrict
75-
/// qualifiers (as with QualType) and the upper bits provide a
76-
/// type index. The type index values are partitioned into two
77+
/// qualifiers (as with QualType).
78+
/// - the upper 29 bits provide a type index in the corresponding
79+
/// module file.
80+
/// - the upper 32 bits provide a module file index.
81+
///
82+
/// The type index values are partitioned into two
7783
/// sets. The values below NUM_PREDEF_TYPE_IDs are predefined type
7884
/// IDs (based on the PREDEF_TYPE_*_ID constants), with 0 as a
7985
/// placeholder for "no type". Values from NUM_PREDEF_TYPE_IDs are
8086
/// other types that have serialized representations.
81-
using TypeID = uint32_t;
87+
using TypeID = uint64_t;
8288

8389
/// A type index; the type ID with the qualifier bits removed.
90+
/// Keep structure alignment 32-bit since the blob is assumed as 32-bit
91+
/// aligned.
8492
class TypeIdx {
93+
uint32_t ModuleFileIndex = 0;
8594
uint32_t Idx = 0;
8695

8796
public:
8897
TypeIdx() = default;
89-
explicit TypeIdx(uint32_t index) : Idx(index) {}
98+
explicit TypeIdx(uint32_t Idx) : ModuleFileIndex(0), Idx(Idx) {}
99+
100+
explicit TypeIdx(uint32_t ModuleFileIdx, uint32_t Idx)
101+
: ModuleFileIndex(ModuleFileIdx), Idx(Idx) {}
102+
103+
uint32_t getModuleFileIndex() const { return ModuleFileIndex; }
90104

91-
uint32_t getIndex() const { return Idx; }
105+
uint64_t getValue() const { return ((uint64_t)ModuleFileIndex << 32) | Idx; }
92106

93107
TypeID asTypeID(unsigned FastQuals) const {
94108
if (Idx == uint32_t(-1))
95109
return TypeID(-1);
96110

97-
return (Idx << Qualifiers::FastWidth) | FastQuals;
111+
unsigned Index = (Idx << Qualifiers::FastWidth) | FastQuals;
112+
return ((uint64_t)ModuleFileIndex << 32) | Index;
98113
}
99114

100115
static TypeIdx fromTypeID(TypeID ID) {
101116
if (ID == TypeID(-1))
102117
return TypeIdx(-1);
103118

104-
return TypeIdx(ID >> Qualifiers::FastWidth);
119+
return TypeIdx(ID >> 32, (ID & llvm::maskTrailingOnes<TypeID>(32)) >>
120+
Qualifiers::FastWidth);
105121
}
106122
};
107123

clang/include/clang/Serialization/ASTReader.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -487,14 +487,6 @@ class ASTReader
487487
/// ID = (I + 1) << FastQual::Width has already been loaded
488488
llvm::PagedVector<QualType> TypesLoaded;
489489

490-
using GlobalTypeMapType =
491-
ContinuousRangeMap<serialization::TypeID, ModuleFile *, 4>;
492-
493-
/// Mapping from global type IDs to the module in which the
494-
/// type resides along with the offset that should be added to the
495-
/// global type ID to produce a local ID.
496-
GlobalTypeMapType GlobalTypeMap;
497-
498490
/// Declarations that have already been loaded from the chain.
499491
///
500492
/// When the pointer at index I is non-NULL, the declaration with ID
@@ -1420,8 +1412,8 @@ class ASTReader
14201412
RecordLocation(ModuleFile *M, uint64_t O) : F(M), Offset(O) {}
14211413
};
14221414

1423-
QualType readTypeRecord(unsigned Index);
1424-
RecordLocation TypeCursorForIndex(unsigned Index);
1415+
QualType readTypeRecord(serialization::TypeID ID);
1416+
RecordLocation TypeCursorForIndex(serialization::TypeID ID);
14251417
void LoadedDecl(unsigned Index, Decl *D);
14261418
Decl *ReadDeclRecord(GlobalDeclID ID);
14271419
void markIncompleteDeclChain(Decl *D);
@@ -1533,6 +1525,11 @@ class ASTReader
15331525
std::pair<ModuleFile *, unsigned>
15341526
translateIdentifierIDToIndex(serialization::IdentifierID ID) const;
15351527

1528+
/// Translate an \param TypeID ID to the index of TypesLoaded
1529+
/// array and the corresponding module file.
1530+
std::pair<ModuleFile *, unsigned>
1531+
translateTypeIDToIndex(serialization::TypeID ID) const;
1532+
15361533
public:
15371534
/// Load the AST file and validate its contents against the given
15381535
/// Preprocessor.
@@ -1881,10 +1878,11 @@ class ASTReader
18811878
QualType GetType(serialization::TypeID ID);
18821879

18831880
/// Resolve a local type ID within a given AST file into a type.
1884-
QualType getLocalType(ModuleFile &F, unsigned LocalID);
1881+
QualType getLocalType(ModuleFile &F, serialization::TypeID LocalID);
18851882

18861883
/// Map a local type ID within a given AST file into a global type ID.
1887-
serialization::TypeID getGlobalTypeID(ModuleFile &F, unsigned LocalID) const;
1884+
serialization::TypeID getGlobalTypeID(ModuleFile &F,
1885+
serialization::TypeID LocalID) const;
18881886

18891887
/// Read a type from the current position in the given record, which
18901888
/// was read from the given AST file.
@@ -1906,6 +1904,7 @@ class ASTReader
19061904
/// if the declaration is not from a module file.
19071905
ModuleFile *getOwningModuleFile(const Decl *D) const;
19081906
ModuleFile *getOwningModuleFile(GlobalDeclID ID) const;
1907+
ModuleFile *getOwningModuleFile(serialization::TypeID ID) const;
19091908

19101909
/// Returns the source location for the decl \p ID.
19111910
SourceLocation getSourceLocationForDeclID(GlobalDeclID ID);

clang/include/clang/Serialization/ASTRecordReader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class ASTRecordReader
163163
void readTypeLoc(TypeLoc TL, LocSeq *Seq = nullptr);
164164

165165
/// Map a local type ID within a given AST file to a global type ID.
166-
serialization::TypeID getGlobalTypeID(unsigned LocalID) const {
166+
serialization::TypeID getGlobalTypeID(serialization::TypeID LocalID) const {
167167
return Reader->getGlobalTypeID(*F, LocalID);
168168
}
169169

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,6 @@ class ModuleFile {
482482
/// the global type ID space.
483483
serialization::TypeID BaseTypeIndex = 0;
484484

485-
/// Remapping table for type IDs in this module.
486-
ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
487-
488485
// === Miscellaneous ===
489486

490487
/// Diagnostic IDs and their mappings that the user changed.

clang/lib/Serialization/ASTReader.cpp

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3355,20 +3355,11 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
33553355
"duplicate TYPE_OFFSET record in AST file");
33563356
F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
33573357
F.LocalNumTypes = Record[0];
3358-
unsigned LocalBaseTypeIndex = Record[1];
33593358
F.BaseTypeIndex = getTotalNumTypes();
33603359

3361-
if (F.LocalNumTypes > 0) {
3362-
// Introduce the global -> local mapping for types within this module.
3363-
GlobalTypeMap.insert(std::make_pair(getTotalNumTypes(), &F));
3364-
3365-
// Introduce the local -> global mapping for types within this module.
3366-
F.TypeRemap.insertOrReplace(
3367-
std::make_pair(LocalBaseTypeIndex,
3368-
F.BaseTypeIndex - LocalBaseTypeIndex));
3369-
3360+
if (F.LocalNumTypes > 0)
33703361
TypesLoaded.resize(TypesLoaded.size() + F.LocalNumTypes);
3371-
}
3362+
33723363
break;
33733364
}
33743365

@@ -4031,7 +4022,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40314022
RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
40324023
RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
40334024
RemapBuilder SelectorRemap(F.SelectorRemap);
4034-
RemapBuilder TypeRemap(F.TypeRemap);
40354025

40364026
auto &ImportedModuleVector = F.TransitiveImports;
40374027
assert(ImportedModuleVector.empty());
@@ -4067,8 +4057,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40674057
endian::readNext<uint32_t, llvm::endianness::little>(Data);
40684058
uint32_t SelectorIDOffset =
40694059
endian::readNext<uint32_t, llvm::endianness::little>(Data);
4070-
uint32_t TypeIndexOffset =
4071-
endian::readNext<uint32_t, llvm::endianness::little>(Data);
40724060

40734061
auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
40744062
RemapBuilder &Remap) {
@@ -4083,7 +4071,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40834071
PreprocessedEntityRemap);
40844072
mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
40854073
mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4086-
mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
40874074
}
40884075
}
40894076

@@ -5062,12 +5049,12 @@ void ASTReader::InitializeContext() {
50625049

50635050
// Load the special types.
50645051
if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5065-
if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5052+
if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
50665053
if (!Context.CFConstantStringTypeDecl)
50675054
Context.setCFConstantStringType(GetType(String));
50685055
}
50695056

5070-
if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5057+
if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
50715058
QualType FileType = GetType(File);
50725059
if (FileType.isNull()) {
50735060
Error("FILE type is NULL");
@@ -5088,7 +5075,7 @@ void ASTReader::InitializeContext() {
50885075
}
50895076
}
50905077

5091-
if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5078+
if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
50925079
QualType Jmp_bufType = GetType(Jmp_buf);
50935080
if (Jmp_bufType.isNull()) {
50945081
Error("jmp_buf type is NULL");
@@ -5109,7 +5096,7 @@ void ASTReader::InitializeContext() {
51095096
}
51105097
}
51115098

5112-
if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5099+
if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
51135100
QualType Sigjmp_bufType = GetType(Sigjmp_buf);
51145101
if (Sigjmp_bufType.isNull()) {
51155102
Error("sigjmp_buf type is NULL");
@@ -5127,25 +5114,24 @@ void ASTReader::InitializeContext() {
51275114
}
51285115
}
51295116

5130-
if (unsigned ObjCIdRedef
5131-
= SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5117+
if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
51325118
if (Context.ObjCIdRedefinitionType.isNull())
51335119
Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
51345120
}
51355121

5136-
if (unsigned ObjCClassRedef
5137-
= SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5122+
if (TypeID ObjCClassRedef =
5123+
SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
51385124
if (Context.ObjCClassRedefinitionType.isNull())
51395125
Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
51405126
}
51415127

5142-
if (unsigned ObjCSelRedef
5143-
= SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5128+
if (TypeID ObjCSelRedef =
5129+
SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
51445130
if (Context.ObjCSelRedefinitionType.isNull())
51455131
Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
51465132
}
51475133

5148-
if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5134+
if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
51495135
QualType Ucontext_tType = GetType(Ucontext_t);
51505136
if (Ucontext_tType.isNull()) {
51515137
Error("ucontext_t type is NULL");
@@ -6630,10 +6616,8 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
66306616
}
66316617

66326618
/// Get the correct cursor and offset for loading a type.
6633-
ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6634-
GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6635-
assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6636-
ModuleFile *M = I->second;
6619+
ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
6620+
auto [M, Index] = translateTypeIDToIndex(ID);
66376621
return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
66386622
M->DeclsBlockStartOffset);
66396623
}
@@ -6654,10 +6638,10 @@ static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
66546638
/// routine actually reads the record corresponding to the type at the given
66556639
/// location. It is a helper routine for GetType, which deals with reading type
66566640
/// IDs.
6657-
QualType ASTReader::readTypeRecord(unsigned Index) {
6641+
QualType ASTReader::readTypeRecord(TypeID ID) {
66586642
assert(ContextObj && "reading type with no AST context");
66596643
ASTContext &Context = *ContextObj;
6660-
RecordLocation Loc = TypeCursorForIndex(Index);
6644+
RecordLocation Loc = TypeCursorForIndex(ID);
66616645
BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
66626646

66636647
// Keep track of where we are in the stream, then jump back there
@@ -7098,14 +7082,25 @@ TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
70987082
return TInfo;
70997083
}
71007084

7085+
std::pair<ModuleFile *, unsigned>
7086+
ASTReader::translateTypeIDToIndex(serialization::TypeID ID) const {
7087+
unsigned Index =
7088+
(ID & llvm::maskTrailingOnes<TypeID>(32)) >> Qualifiers::FastWidth;
7089+
7090+
ModuleFile *OwningModuleFile = getOwningModuleFile(ID);
7091+
assert(OwningModuleFile &&
7092+
"untranslated type ID or local type ID shouldn't be in TypesLoaded");
7093+
return {OwningModuleFile, OwningModuleFile->BaseTypeIndex + Index};
7094+
}
7095+
71017096
QualType ASTReader::GetType(TypeID ID) {
71027097
assert(ContextObj && "reading type with no AST context");
71037098
ASTContext &Context = *ContextObj;
71047099

71057100
unsigned FastQuals = ID & Qualifiers::FastMask;
7106-
unsigned Index = ID >> Qualifiers::FastWidth;
71077101

7108-
if (Index < NUM_PREDEF_TYPE_IDS) {
7102+
if (uint64_t Index = ID >> Qualifiers::FastWidth;
7103+
Index < NUM_PREDEF_TYPE_IDS) {
71097104
QualType T;
71107105
switch ((PredefinedTypeIDs)Index) {
71117106
case PREDEF_TYPE_LAST_ID:
@@ -7374,10 +7369,11 @@ QualType ASTReader::GetType(TypeID ID) {
73747369
return T.withFastQualifiers(FastQuals);
73757370
}
73767371

7377-
Index -= NUM_PREDEF_TYPE_IDS;
7372+
unsigned Index = translateTypeIDToIndex(ID).second;
7373+
73787374
assert(Index < TypesLoaded.size() && "Type index out-of-range");
73797375
if (TypesLoaded[Index].isNull()) {
7380-
TypesLoaded[Index] = readTypeRecord(Index);
7376+
TypesLoaded[Index] = readTypeRecord(ID);
73817377
if (TypesLoaded[Index].isNull())
73827378
return QualType();
73837379

@@ -7390,27 +7386,28 @@ QualType ASTReader::GetType(TypeID ID) {
73907386
return TypesLoaded[Index].withFastQualifiers(FastQuals);
73917387
}
73927388

7393-
QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7389+
QualType ASTReader::getLocalType(ModuleFile &F, TypeID LocalID) {
73947390
return GetType(getGlobalTypeID(F, LocalID));
73957391
}
73967392

7397-
serialization::TypeID
7398-
ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7399-
unsigned FastQuals = LocalID & Qualifiers::FastMask;
7400-
unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7401-
7402-
if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7393+
serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
7394+
TypeID LocalID) const {
7395+
if ((LocalID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS)
74037396
return LocalID;
74047397

74057398
if (!F.ModuleOffsetMap.empty())
74067399
ReadModuleOffsetMap(F);
74077400

7408-
ContinuousRangeMap<uint32_t, int, 2>::iterator I
7409-
= F.TypeRemap.find(LocalIndex - NUM_PREDEF_TYPE_IDS);
7410-
assert(I != F.TypeRemap.end() && "Invalid index into type index remap");
7401+
unsigned ModuleFileIndex = LocalID >> 32;
7402+
LocalID &= llvm::maskTrailingOnes<TypeID>(32);
74117403

7412-
unsigned GlobalIndex = LocalIndex + I->second;
7413-
return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7404+
if (ModuleFileIndex == 0)
7405+
LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
7406+
7407+
ModuleFile &MF =
7408+
ModuleFileIndex ? *F.TransitiveImports[ModuleFileIndex - 1] : F;
7409+
ModuleFileIndex = MF.Index + 1;
7410+
return ((uint64_t)ModuleFileIndex << 32) | LocalID;
74147411
}
74157412

74167413
TemplateArgumentLocInfo
@@ -7648,6 +7645,16 @@ ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
76487645
return &getModuleManager()[ModuleFileIndex - 1];
76497646
}
76507647

7648+
ModuleFile *ASTReader::getOwningModuleFile(TypeID ID) const {
7649+
if (ID < NUM_PREDEF_TYPE_IDS)
7650+
return nullptr;
7651+
7652+
uint64_t ModuleFileIndex = ID >> 32;
7653+
assert(ModuleFileIndex && "Untranslated Local Decl?");
7654+
7655+
return &getModuleManager()[ModuleFileIndex - 1];
7656+
}
7657+
76517658
ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
76527659
if (!D->isFromASTFile())
76537660
return nullptr;
@@ -8165,7 +8172,6 @@ LLVM_DUMP_METHOD void ASTReader::dump() {
81658172
llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
81668173
dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
81678174
dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8168-
dumpModuleIDMap("Global type map", GlobalTypeMap);
81698175
dumpModuleIDMap("Global macro map", GlobalMacroMap);
81708176
dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
81718177
dumpModuleIDMap("Global selector map", GlobalSelectorMap);

0 commit comments

Comments
 (0)