Skip to content

Commit 22f3abe

Browse files
authored
Revert "[rebranch] Temporarily revert "[Serialization] No transitive identifier change (llvm#92085)""
1 parent 99e3639 commit 22f3abe

File tree

9 files changed

+208
-82
lines changed

9 files changed

+208
-82
lines changed

clang/include/clang/Lex/ExternalPreprocessorSource.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class ExternalPreprocessorSource {
3939
/// Return the identifier associated with the given ID number.
4040
///
4141
/// The ID 0 is associated with the NULL identifier.
42-
virtual IdentifierInfo *GetIdentifier(unsigned ID) = 0;
42+
virtual IdentifierInfo *GetIdentifier(uint64_t ID) = 0;
4343

4444
/// Map a module ID to a module.
4545
virtual Module *getModule(unsigned ModuleID) = 0;

clang/include/clang/Serialization/ASTBitCodes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ const unsigned VERSION_MINOR = 1;
6060
///
6161
/// The ID numbers of identifiers are consecutive (in order of discovery)
6262
/// and start at 1. 0 is reserved for NULL.
63-
using IdentifierID = uint32_t;
63+
using IdentifierID = uint64_t;
6464

6565
/// The number of predefined identifier IDs.
6666
const unsigned int NUM_PREDEF_IDENT_IDS = 1;

clang/include/clang/Serialization/ASTReader.h

+8-11
Original file line numberDiff line numberDiff line change
@@ -677,14 +677,6 @@ class ASTReader
677677
/// been loaded.
678678
std::vector<IdentifierInfo *> IdentifiersLoaded;
679679

680-
using GlobalIdentifierMapType =
681-
ContinuousRangeMap<serialization::IdentifierID, ModuleFile *, 4>;
682-
683-
/// Mapping from global identifier IDs to the module in which the
684-
/// identifier resides along with the offset that should be added to the
685-
/// global identifier ID to produce a local ID.
686-
GlobalIdentifierMapType GlobalIdentifierMap;
687-
688680
/// A vector containing macros that have already been
689681
/// loaded.
690682
///
@@ -1556,6 +1548,11 @@ class ASTReader
15561548
/// Translate a \param GlobalDeclID to the index of DeclsLoaded array.
15571549
unsigned translateGlobalDeclIDToIndex(GlobalDeclID ID) const;
15581550

1551+
/// Translate an \param IdentifierID ID to the index of IdentifiersLoaded
1552+
/// array and the corresponding module file.
1553+
std::pair<ModuleFile *, unsigned>
1554+
translateIdentifierIDToIndex(serialization::IdentifierID ID) const;
1555+
15591556
/// Translate an \param TypeID ID to the index of TypesLoaded
15601557
/// array and the corresponding module file.
15611558
std::pair<ModuleFile *, unsigned>
@@ -2143,7 +2140,7 @@ class ASTReader
21432140
/// Load a selector from disk, registering its ID if it exists.
21442141
void LoadSelector(Selector Sel);
21452142

2146-
void SetIdentifierInfo(unsigned ID, IdentifierInfo *II);
2143+
void SetIdentifierInfo(serialization::IdentifierID ID, IdentifierInfo *II);
21472144
void SetGloballyVisibleDecls(IdentifierInfo *II,
21482145
const SmallVectorImpl<GlobalDeclID> &DeclIDs,
21492146
SmallVectorImpl<Decl *> *Decls = nullptr);
@@ -2170,10 +2167,10 @@ class ASTReader
21702167
return DecodeIdentifierInfo(ID);
21712168
}
21722169

2173-
IdentifierInfo *getLocalIdentifier(ModuleFile &M, unsigned LocalID);
2170+
IdentifierInfo *getLocalIdentifier(ModuleFile &M, uint64_t LocalID);
21742171

21752172
serialization::IdentifierID getGlobalIdentifierID(ModuleFile &M,
2176-
unsigned LocalID);
2173+
uint64_t LocalID);
21772174

21782175
void resolvePendingMacro(IdentifierInfo *II, const PendingMacroInfo &PMInfo);
21792176

clang/include/clang/Serialization/ModuleFile.h

-3
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,6 @@ class ModuleFile {
321321
/// Base identifier ID for identifiers local to this module.
322322
serialization::IdentifierID BaseIdentifierID = 0;
323323

324-
/// Remapping table for identifier IDs in this module.
325-
ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
326-
327324
/// Actual data for the on-disk hash table of identifiers.
328325
///
329326
/// This pointer points into a memory buffer, where the on-disk hash

clang/lib/Serialization/ASTReader.cpp

+51-45
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,7 @@ ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
969969
SelectorTable &SelTable = Reader.getContext().Selectors;
970970
unsigned N = endian::readNext<uint16_t, llvm::endianness::little>(d);
971971
const IdentifierInfo *FirstII = Reader.getLocalIdentifier(
972-
F, endian::readNext<uint32_t, llvm::endianness::little>(d));
972+
F, endian::readNext<IdentifierID, llvm::endianness::little>(d));
973973
if (N == 0)
974974
return SelTable.getNullarySelector(FirstII);
975975
else if (N == 1)
@@ -979,7 +979,7 @@ ASTSelectorLookupTrait::ReadKey(const unsigned char* d, unsigned) {
979979
Args.push_back(FirstII);
980980
for (unsigned I = 1; I != N; ++I)
981981
Args.push_back(Reader.getLocalIdentifier(
982-
F, endian::readNext<uint32_t, llvm::endianness::little>(d)));
982+
F, endian::readNext<IdentifierID, llvm::endianness::little>(d)));
983983

984984
return SelTable.getSelector(N, Args.data());
985985
}
@@ -1062,7 +1062,8 @@ static bool readBit(unsigned &Bits) {
10621062
IdentifierID ASTIdentifierLookupTrait::ReadIdentifierID(const unsigned char *d) {
10631063
using namespace llvm::support;
10641064

1065-
unsigned RawID = endian::readNext<uint32_t, llvm::endianness::little>(d);
1065+
IdentifierID RawID =
1066+
endian::readNext<IdentifierID, llvm::endianness::little>(d);
10661067
return Reader.getGlobalIdentifierID(F, RawID >> 1);
10671068
}
10681069

@@ -1080,9 +1081,12 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
10801081
unsigned DataLen) {
10811082
using namespace llvm::support;
10821083

1083-
unsigned RawID = endian::readNext<uint32_t, llvm::endianness::little>(d);
1084+
IdentifierID RawID =
1085+
endian::readNext<IdentifierID, llvm::endianness::little>(d);
10841086
bool IsInteresting = RawID & 0x01;
10851087

1088+
DataLen -= sizeof(IdentifierID);
1089+
10861090
// Wipe out the "is interesting" bit.
10871091
RawID = RawID >> 1;
10881092

@@ -1113,7 +1117,7 @@ IdentifierInfo *ASTIdentifierLookupTrait::ReadData(const internal_key_type& k,
11131117
bool HadMacroDefinition = readBit(Bits);
11141118

11151119
assert(Bits == 0 && "Extra bits in the identifier?");
1116-
DataLen -= 8;
1120+
DataLen -= sizeof(uint16_t) * 2;
11171121

11181122
// Set or check the various bits in the IdentifierInfo structure.
11191123
// Token IDs are read-only.
@@ -1240,7 +1244,7 @@ ASTDeclContextNameLookupTrait::ReadKey(const unsigned char *d, unsigned) {
12401244
case DeclarationName::CXXLiteralOperatorName:
12411245
case DeclarationName::CXXDeductionGuideName:
12421246
Data = (uint64_t)Reader.getLocalIdentifier(
1243-
F, endian::readNext<uint32_t, llvm::endianness::little>(d));
1247+
F, endian::readNext<IdentifierID, llvm::endianness::little>(d));
12441248
break;
12451249
case DeclarationName::ObjCZeroArgSelector:
12461250
case DeclarationName::ObjCOneArgSelector:
@@ -2111,7 +2115,7 @@ HeaderFileInfoTrait::ReadData(internal_key_ref key, const unsigned char *d,
21112115
HFI.DirInfo = (Flags >> 1) & 0x07;
21122116
HFI.IndexHeaderMapHeader = Flags & 0x01;
21132117
HFI.LazyControllingMacro = Reader.getGlobalIdentifierID(
2114-
M, endian::readNext<uint32_t, llvm::endianness::little>(d));
2118+
M, endian::readNext<IdentifierID, llvm::endianness::little>(d));
21152119
if (unsigned FrameworkOffset =
21162120
endian::readNext<uint32_t, llvm::endianness::little>(d)) {
21172121
// The framework offset is 1 greater than the actual offset,
@@ -3509,24 +3513,11 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F,
35093513
"duplicate IDENTIFIER_OFFSET record in AST file");
35103514
F.IdentifierOffsets = (const uint32_t *)Blob.data();
35113515
F.LocalNumIdentifiers = Record[0];
3512-
unsigned LocalBaseIdentifierID = Record[1];
35133516
F.BaseIdentifierID = getTotalNumIdentifiers();
35143517

3515-
if (F.LocalNumIdentifiers > 0) {
3516-
// Introduce the global -> local mapping for identifiers within this
3517-
// module.
3518-
GlobalIdentifierMap.insert(std::make_pair(getTotalNumIdentifiers() + 1,
3519-
&F));
3520-
3521-
// Introduce the local -> global mapping for identifiers within this
3522-
// module.
3523-
F.IdentifierRemap.insertOrReplace(
3524-
std::make_pair(LocalBaseIdentifierID,
3525-
F.BaseIdentifierID - LocalBaseIdentifierID));
3526-
3518+
if (F.LocalNumIdentifiers > 0)
35273519
IdentifiersLoaded.resize(IdentifiersLoaded.size()
35283520
+ F.LocalNumIdentifiers);
3529-
}
35303521
break;
35313522
}
35323523

@@ -4113,7 +4104,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
41134104
F.ModuleOffsetMap = StringRef();
41144105

41154106
using RemapBuilder = ContinuousRangeMap<uint32_t, int, 2>::Builder;
4116-
RemapBuilder IdentifierRemap(F.IdentifierRemap);
41174107
RemapBuilder MacroRemap(F.MacroRemap);
41184108
RemapBuilder PreprocessedEntityRemap(F.PreprocessedEntityRemap);
41194109
RemapBuilder SubmoduleRemap(F.SubmoduleRemap);
@@ -4145,8 +4135,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
41454135

41464136
ImportedModuleVector.push_back(OM);
41474137

4148-
uint32_t IdentifierIDOffset =
4149-
endian::readNext<uint32_t, llvm::endianness::little>(Data);
41504138
uint32_t MacroIDOffset =
41514139
endian::readNext<uint32_t, llvm::endianness::little>(Data);
41524140
uint32_t PreprocessedEntityIDOffset =
@@ -4164,7 +4152,6 @@ void ASTReader::ReadModuleOffsetMap(ModuleFile &F) const {
41644152
static_cast<int>(BaseOffset - Offset)));
41654153
};
41664154

4167-
mapOffset(IdentifierIDOffset, OM->BaseIdentifierID, IdentifierRemap);
41684155
mapOffset(MacroIDOffset, OM->BaseMacroID, MacroRemap);
41694156
mapOffset(PreprocessedEntityIDOffset, OM->BasePreprocessedEntityID,
41704157
PreprocessedEntityRemap);
@@ -8302,7 +8289,6 @@ LLVM_DUMP_METHOD void ASTReader::dump() {
83028289
llvm::errs() << "*** PCH/ModuleFile Remappings:\n";
83038290
dumpModuleIDMap("Global bit offset map", GlobalBitOffsetsMap);
83048291
dumpModuleIDMap("Global source location entry map", GlobalSLocEntryMap);
8305-
dumpModuleIDMap("Global identifier map", GlobalIdentifierMap);
83068292
dumpModuleIDMap("Global macro map", GlobalMacroMap);
83078293
dumpModuleIDMap("Global submodule map", GlobalSubmoduleMap);
83088294
dumpModuleIDMap("Global selector map", GlobalSelectorMap);
@@ -8942,8 +8928,9 @@ void ASTReader::LoadSelector(Selector Sel) {
89428928

89438929
void ASTReader::SetIdentifierInfo(IdentifierID ID, IdentifierInfo *II) {
89448930
assert(ID && "Non-zero identifier ID required");
8945-
assert(ID <= IdentifiersLoaded.size() && "identifier ID out of range");
8946-
IdentifiersLoaded[ID - 1] = II;
8931+
unsigned Index = translateIdentifierIDToIndex(ID).second;
8932+
assert(Index < IdentifiersLoaded.size() && "identifier ID out of range");
8933+
IdentifiersLoaded[Index] = II;
89478934
if (DeserializationListener)
89488935
DeserializationListener->IdentifierRead(ID, II);
89498936
}
@@ -8996,6 +8983,22 @@ void ASTReader::SetGloballyVisibleDecls(
89968983
}
89978984
}
89988985

8986+
std::pair<ModuleFile *, unsigned>
8987+
ASTReader::translateIdentifierIDToIndex(IdentifierID ID) const {
8988+
if (ID == 0)
8989+
return {nullptr, 0};
8990+
8991+
unsigned ModuleFileIndex = ID >> 32;
8992+
unsigned LocalID = ID & llvm::maskTrailingOnes<IdentifierID>(32);
8993+
8994+
assert(ModuleFileIndex && "not translating loaded IdentifierID?");
8995+
assert(getModuleManager().size() > ModuleFileIndex - 1);
8996+
8997+
ModuleFile &MF = getModuleManager()[ModuleFileIndex - 1];
8998+
assert(LocalID < MF.LocalNumIdentifiers);
8999+
return {&MF, MF.BaseIdentifierID + LocalID};
9000+
}
9001+
89999002
IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
90009003
if (ID == 0)
90019004
return nullptr;
@@ -9005,45 +9008,48 @@ IdentifierInfo *ASTReader::DecodeIdentifierInfo(IdentifierID ID) {
90059008
return nullptr;
90069009
}
90079010

9008-
ID -= 1;
9009-
if (!IdentifiersLoaded[ID]) {
9010-
GlobalIdentifierMapType::iterator I = GlobalIdentifierMap.find(ID + 1);
9011-
assert(I != GlobalIdentifierMap.end() && "Corrupted global identifier map");
9012-
ModuleFile *M = I->second;
9013-
unsigned Index = ID - M->BaseIdentifierID;
9011+
auto [M, Index] = translateIdentifierIDToIndex(ID);
9012+
if (!IdentifiersLoaded[Index]) {
9013+
assert(M != nullptr && "Untranslated Identifier ID?");
9014+
assert(Index >= M->BaseIdentifierID);
9015+
unsigned LocalIndex = Index - M->BaseIdentifierID;
90149016
const unsigned char *Data =
9015-
M->IdentifierTableData + M->IdentifierOffsets[Index];
9017+
M->IdentifierTableData + M->IdentifierOffsets[LocalIndex];
90169018

90179019
ASTIdentifierLookupTrait Trait(*this, *M);
90189020
auto KeyDataLen = Trait.ReadKeyDataLength(Data);
90199021
auto Key = Trait.ReadKey(Data, KeyDataLen.first);
90209022
auto &II = PP.getIdentifierTable().get(Key);
9021-
IdentifiersLoaded[ID] = &II;
9023+
IdentifiersLoaded[Index] = &II;
90229024
markIdentifierFromAST(*this, II);
90239025
if (DeserializationListener)
9024-
DeserializationListener->IdentifierRead(ID + 1, &II);
9026+
DeserializationListener->IdentifierRead(ID, &II);
90259027
}
90269028

9027-
return IdentifiersLoaded[ID];
9029+
return IdentifiersLoaded[Index];
90289030
}
90299031

9030-
IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, unsigned LocalID) {
9032+
IdentifierInfo *ASTReader::getLocalIdentifier(ModuleFile &M, uint64_t LocalID) {
90319033
return DecodeIdentifierInfo(getGlobalIdentifierID(M, LocalID));
90329034
}
90339035

9034-
IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, unsigned LocalID) {
9036+
IdentifierID ASTReader::getGlobalIdentifierID(ModuleFile &M, uint64_t LocalID) {
90359037
if (LocalID < NUM_PREDEF_IDENT_IDS)
90369038
return LocalID;
90379039

90389040
if (!M.ModuleOffsetMap.empty())
90399041
ReadModuleOffsetMap(M);
90409042

9041-
ContinuousRangeMap<uint32_t, int, 2>::iterator I
9042-
= M.IdentifierRemap.find(LocalID - NUM_PREDEF_IDENT_IDS);
9043-
assert(I != M.IdentifierRemap.end()
9044-
&& "Invalid index into identifier index remap");
9043+
unsigned ModuleFileIndex = LocalID >> 32;
9044+
LocalID &= llvm::maskTrailingOnes<IdentifierID>(32);
9045+
ModuleFile *MF =
9046+
ModuleFileIndex ? M.TransitiveImports[ModuleFileIndex - 1] : &M;
9047+
assert(MF && "malformed identifier ID encoding?");
90459048

9046-
return LocalID + I->second;
9049+
if (!ModuleFileIndex)
9050+
LocalID -= NUM_PREDEF_IDENT_IDS;
9051+
9052+
return ((IdentifierID)(MF->Index + 1) << 32) | LocalID;
90479053
}
90489054

90499055
MacroInfo *ASTReader::getMacro(MacroID ID) {

0 commit comments

Comments
 (0)