Skip to content

Commit 2265f12

Browse files
committed
[serialization] No transitive type change
1 parent c612b56 commit 2265f12

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
@@ -3357,20 +3357,11 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
33573357
"duplicate TYPE_OFFSET record in AST file");
33583358
F.TypeOffsets = reinterpret_cast<const UnalignedUInt64 *>(Blob.data());
33593359
F.LocalNumTypes = Record[0];
3360-
unsigned LocalBaseTypeIndex = Record[1];
33613360
F.BaseTypeIndex = getTotalNumTypes();
33623361

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

@@ -4033,7 +4024,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40334024
RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
40344025
RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
40354026
RemapBuilder SelectorRemap(F.SelectorRemap);
4036-
RemapBuilder TypeRemap(F.TypeRemap);
40374027

40384028
auto &ImportedModuleVector = F.DependentModules;
40394029
assert(ImportedModuleVector.empty());
@@ -4069,8 +4059,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40694059
endian::readNext<uint32_t, llvm::endianness::little>(Data);
40704060
uint32_t SelectorIDOffset =
40714061
endian::readNext<uint32_t, llvm::endianness::little>(Data);
4072-
uint32_t TypeIndexOffset =
4073-
endian::readNext<uint32_t, llvm::endianness::little>(Data);
40744062

40754063
auto mapOffset = [&](uint32_t Offset, uint32_t BaseOffset,
40764064
RemapBuilder &Remap) {
@@ -4085,7 +4073,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
40854073
PreprocessedEntityRemap);
40864074
mapOffset(SubmoduleIDOffset, OM->BaseSubmoduleID, SubmoduleRemap);
40874075
mapOffset(SelectorIDOffset, OM->BaseSelectorID, SelectorRemap);
4088-
mapOffset(TypeIndexOffset, OM->BaseTypeIndex, TypeRemap);
40894076
}
40904077
}
40914078

@@ -5064,12 +5051,12 @@ void ASTReader::InitializeContext() {
50645051

50655052
// Load the special types.
50665053
if (SpecialTypes.size() >= NumSpecialTypeIDs) {
5067-
if (unsigned String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
5054+
if (TypeID String = SpecialTypes[SPECIAL_TYPE_CF_CONSTANT_STRING]) {
50685055
if (!Context.CFConstantStringTypeDecl)
50695056
Context.setCFConstantStringType(GetType(String));
50705057
}
50715058

5072-
if (unsigned File = SpecialTypes[SPECIAL_TYPE_FILE]) {
5059+
if (TypeID File = SpecialTypes[SPECIAL_TYPE_FILE]) {
50735060
QualType FileType = GetType(File);
50745061
if (FileType.isNull()) {
50755062
Error("FILE type is NULL");
@@ -5090,7 +5077,7 @@ void ASTReader::InitializeContext() {
50905077
}
50915078
}
50925079

5093-
if (unsigned Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
5080+
if (TypeID Jmp_buf = SpecialTypes[SPECIAL_TYPE_JMP_BUF]) {
50945081
QualType Jmp_bufType = GetType(Jmp_buf);
50955082
if (Jmp_bufType.isNull()) {
50965083
Error("jmp_buf type is NULL");
@@ -5111,7 +5098,7 @@ void ASTReader::InitializeContext() {
51115098
}
51125099
}
51135100

5114-
if (unsigned Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
5101+
if (TypeID Sigjmp_buf = SpecialTypes[SPECIAL_TYPE_SIGJMP_BUF]) {
51155102
QualType Sigjmp_bufType = GetType(Sigjmp_buf);
51165103
if (Sigjmp_bufType.isNull()) {
51175104
Error("sigjmp_buf type is NULL");
@@ -5129,25 +5116,24 @@ void ASTReader::InitializeContext() {
51295116
}
51305117
}
51315118

5132-
if (unsigned ObjCIdRedef
5133-
= SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
5119+
if (TypeID ObjCIdRedef = SpecialTypes[SPECIAL_TYPE_OBJC_ID_REDEFINITION]) {
51345120
if (Context.ObjCIdRedefinitionType.isNull())
51355121
Context.ObjCIdRedefinitionType = GetType(ObjCIdRedef);
51365122
}
51375123

5138-
if (unsigned ObjCClassRedef
5139-
= SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
5124+
if (TypeID ObjCClassRedef =
5125+
SpecialTypes[SPECIAL_TYPE_OBJC_CLASS_REDEFINITION]) {
51405126
if (Context.ObjCClassRedefinitionType.isNull())
51415127
Context.ObjCClassRedefinitionType = GetType(ObjCClassRedef);
51425128
}
51435129

5144-
if (unsigned ObjCSelRedef
5145-
= SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
5130+
if (TypeID ObjCSelRedef =
5131+
SpecialTypes[SPECIAL_TYPE_OBJC_SEL_REDEFINITION]) {
51465132
if (Context.ObjCSelRedefinitionType.isNull())
51475133
Context.ObjCSelRedefinitionType = GetType(ObjCSelRedef);
51485134
}
51495135

5150-
if (unsigned Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
5136+
if (TypeID Ucontext_t = SpecialTypes[SPECIAL_TYPE_UCONTEXT_T]) {
51515137
QualType Ucontext_tType = GetType(Ucontext_t);
51525138
if (Ucontext_tType.isNull()) {
51535139
Error("ucontext_t type is NULL");
@@ -6632,10 +6618,8 @@ void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) {
66326618
}
66336619

66346620
/// Get the correct cursor and offset for loading a type.
6635-
ASTReader::RecordLocation ASTReader::TypeCursorForIndex(unsigned Index) {
6636-
GlobalTypeMapType::iterator I = GlobalTypeMap.find(Index);
6637-
assert(I != GlobalTypeMap.end() && "Corrupted global type map");
6638-
ModuleFile *M = I->second;
6621+
ASTReader::RecordLocation ASTReader::TypeCursorForIndex(TypeID ID) {
6622+
auto [M, Index] = translateTypeIDToIndex(ID);
66396623
return RecordLocation(M, M->TypeOffsets[Index - M->BaseTypeIndex].get() +
66406624
M->DeclsBlockStartOffset);
66416625
}
@@ -6656,10 +6640,10 @@ static std::optional<Type::TypeClass> getTypeClassForCode(TypeCode code) {
66566640
/// routine actually reads the record corresponding to the type at the given
66576641
/// location. It is a helper routine for GetType, which deals with reading type
66586642
/// IDs.
6659-
QualType ASTReader::readTypeRecord(unsigned Index) {
6643+
QualType ASTReader::readTypeRecord(TypeID ID) {
66606644
assert(ContextObj && "reading type with no AST context");
66616645
ASTContext &Context = *ContextObj;
6662-
RecordLocation Loc = TypeCursorForIndex(Index);
6646+
RecordLocation Loc = TypeCursorForIndex(ID);
66636647
BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
66646648

66656649
// Keep track of where we are in the stream, then jump back there
@@ -7100,14 +7084,25 @@ TypeSourceInfo *ASTRecordReader::readTypeSourceInfo() {
71007084
return TInfo;
71017085
}
71027086

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

71077102
unsigned FastQuals = ID & Qualifiers::FastMask;
7108-
unsigned Index = ID >> Qualifiers::FastWidth;
71097103

7110-
if (Index < NUM_PREDEF_TYPE_IDS) {
7104+
if (uint64_t Index = ID >> Qualifiers::FastWidth;
7105+
Index < NUM_PREDEF_TYPE_IDS) {
71117106
QualType T;
71127107
switch ((PredefinedTypeIDs)Index) {
71137108
case PREDEF_TYPE_LAST_ID:
@@ -7376,10 +7371,11 @@ QualType ASTReader::GetType(TypeID ID) {
73767371
return T.withFastQualifiers(FastQuals);
73777372
}
73787373

7379-
Index -= NUM_PREDEF_TYPE_IDS;
7374+
unsigned Index = translateTypeIDToIndex(ID).second;
7375+
73807376
assert(Index < TypesLoaded.size() && "Type index out-of-range");
73817377
if (TypesLoaded[Index].isNull()) {
7382-
TypesLoaded[Index] = readTypeRecord(Index);
7378+
TypesLoaded[Index] = readTypeRecord(ID);
73837379
if (TypesLoaded[Index].isNull())
73847380
return QualType();
73857381

@@ -7392,27 +7388,28 @@ QualType ASTReader::GetType(TypeID ID) {
73927388
return TypesLoaded[Index].withFastQualifiers(FastQuals);
73937389
}
73947390

7395-
QualType ASTReader::getLocalType(ModuleFile &F, unsigned LocalID) {
7391+
QualType ASTReader::getLocalType(ModuleFile &F, TypeID LocalID) {
73967392
return GetType(getGlobalTypeID(F, LocalID));
73977393
}
73987394

7399-
serialization::TypeID
7400-
ASTReader::getGlobalTypeID(ModuleFile &F, unsigned LocalID) const {
7401-
unsigned FastQuals = LocalID & Qualifiers::FastMask;
7402-
unsigned LocalIndex = LocalID >> Qualifiers::FastWidth;
7403-
7404-
if (LocalIndex < NUM_PREDEF_TYPE_IDS)
7395+
serialization::TypeID ASTReader::getGlobalTypeID(ModuleFile &F,
7396+
TypeID LocalID) const {
7397+
if ((LocalID >> Qualifiers::FastWidth) < NUM_PREDEF_TYPE_IDS)
74057398
return LocalID;
74067399

74077400
if (!F.ModuleOffsetMap.empty())
74087401
ReadModuleOffsetMap(F);
74097402

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

7414-
unsigned GlobalIndex = LocalIndex + I->second;
7415-
return (GlobalIndex << Qualifiers::FastWidth) | FastQuals;
7406+
if (ModuleFileIndex == 0)
7407+
LocalID -= NUM_PREDEF_TYPE_IDS << Qualifiers::FastWidth;
7408+
7409+
ModuleFile &MF =
7410+
ModuleFileIndex ? *F.DependentModules[ModuleFileIndex - 1] : F;
7411+
ModuleFileIndex = MF.Index + 1;
7412+
return ((uint64_t)ModuleFileIndex << 32) | LocalID;
74167413
}
74177414

74187415
TemplateArgumentLocInfo
@@ -7650,6 +7647,16 @@ ModuleFile *ASTReader::getOwningModuleFile(GlobalDeclID ID) const {
76507647
return &getModuleManager()[ModuleFileIndex - 1];
76517648
}
76527649

7650+
ModuleFile *ASTReader::getOwningModuleFile(TypeID ID) const {
7651+
if (ID < NUM_PREDEF_TYPE_IDS)
7652+
return nullptr;
7653+
7654+
uint64_t ModuleFileIndex = ID >> 32;
7655+
assert(ModuleFileIndex && "Untranslated Local Decl?");
7656+
7657+
return &getModuleManager()[ModuleFileIndex - 1];
7658+
}
7659+
76537660
ModuleFile *ASTReader::getOwningModuleFile(const Decl *D) const {
76547661
if (!D->isFromASTFile())
76557662
return nullptr;
@@ -8167,7 +8174,6 @@ LLVM_DUMP_METHOD void ASTReader::dump() {
81678174
llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
81688175
dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
81698176
dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8170-
dumpModuleIDMap("Global type map", GlobalTypeMap);
81718177
dumpModuleIDMap("Global macro map", GlobalMacroMap);
81728178
dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
81738179
dumpModuleIDMap("Global selector map", GlobalSelectorMap);

0 commit comments

Comments
 (0)