Skip to content

Commit 3cda32d

Browse files
authored
[clang] [Serialization] No transitive change for MacroID and PreprocessedEntityID (#166346)
Similar to previous no transitive changes to decls, types, identifiers and source locations ( #92083 #92085 #92511 #86912 ) This patch does the same thing for MacroID and PreprocessedEntityID. --- ### Some background Previously we record different IDs linearly. That is, when writing a module, if we have 17 decls in imported modules, the ID of decls in the module will start from 18. This makes the contents of the BMI changes if the we add/remove any decls, types, identifiers and source locations in the imported modules. This makes it hard for us to reduce recompilations with modules. We want to skip recompilations as we think the modules can help us to remove fake dependencies. This can be done by split the ID into <ModuleIndex, LocalIndex> pairs. This is ALREADY done for several different ID above. We call it non-casacading changes (https://clang.llvm.org/docs/StandardCPlusPlusModules.html#experimental-non-cascading-changes). Our internal users have already used this feature and it works well for years. Now we want to extend this to MacroID and PreprocessedEntityID. This is helpful for us in the downstream as we allowed named modules to export macros. But I believe this is also helpful for header-like modules if you'd like to explore the area. And also I think this is a nice cleanup too. --- Given the use of MacroID and PreprocessedEntityID are not as complicated as other IDs in the above series, I feel the patch itself should be good. I hope the vendors can test the patch to make sure it won't affect existing users.
1 parent 6121826 commit 3cda32d

File tree

8 files changed

+158
-114
lines changed

8 files changed

+158
-114
lines changed

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,14 @@ struct UnsafeQualTypeDenseMapInfo {
151151
};
152152

153153
/// An ID number that refers to a macro in an AST file.
154-
using MacroID = uint32_t;
154+
using MacroID = uint64_t;
155155

156156
/// A global ID number that refers to a macro in an AST file.
157-
using GlobalMacroID = uint32_t;
157+
using GlobalMacroID = uint64_t;
158158

159159
/// A local to a module ID number that refers to a macro in an
160160
/// AST file.
161-
using LocalMacroID = uint32_t;
161+
using LocalMacroID = uint64_t;
162162

163163
/// The number of predefined macro IDs.
164164
const unsigned int NUM_PREDEF_MACRO_IDS = 1;
@@ -179,7 +179,7 @@ using CXXCtorInitializersID = uint32_t;
179179

180180
/// An ID number that refers to an entity in the detailed
181181
/// preprocessing record.
182-
using PreprocessedEntityID = uint32_t;
182+
using PreprocessedEntityID = uint64_t;
183183

184184
/// An ID number that refers to a submodule in a module file.
185185
using SubmoduleID = uint32_t;

clang/include/clang/Serialization/ASTReader.h

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -800,14 +800,6 @@ class ASTReader
800800
/// files.
801801
llvm::DenseSet<LoadedMacroInfo> LoadedUndefs;
802802

803-
using GlobalMacroMapType =
804-
ContinuousRangeMap<serialization::MacroID, ModuleFile *, 4>;
805-
806-
/// Mapping from global macro IDs to the module in which the
807-
/// macro resides along with the offset that should be added to the
808-
/// global macro ID to produce a local ID.
809-
GlobalMacroMapType GlobalMacroMap;
810-
811803
/// A vector containing submodules that have already been loaded.
812804
///
813805
/// This vector is indexed by the Submodule ID (-1). NULL submodule entries
@@ -1655,18 +1647,16 @@ class ASTReader
16551647

16561648
/// Returns the first preprocessed entity ID that begins or ends after
16571649
/// \arg Loc.
1658-
serialization::PreprocessedEntityID
1659-
findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
1650+
unsigned findPreprocessedEntity(SourceLocation Loc, bool EndsAfter) const;
16601651

16611652
/// Find the next module that contains entities and return the ID
16621653
/// of the first entry.
16631654
///
16641655
/// \param SLocMapI points at a chunk of a module that contains no
16651656
/// preprocessed entities or the entities it contains are not the
16661657
/// ones we are looking for.
1667-
serialization::PreprocessedEntityID
1668-
findNextPreprocessedEntity(
1669-
GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
1658+
unsigned findNextPreprocessedEntity(
1659+
GlobalSLocOffsetMapType::const_iterator SLocMapI) const;
16701660

16711661
/// Returns (ModuleFile, Local index) pair for \p GlobalIndex of a
16721662
/// preprocessed entity.
@@ -1748,6 +1738,14 @@ class ASTReader
17481738
std::pair<ModuleFile *, unsigned>
17491739
translateIdentifierIDToIndex(serialization::IdentifierID ID) const;
17501740

1741+
/// Translate an \param MacroID ID to the index of MacrosLoaded
1742+
/// array and the corresponding module file.
1743+
std::pair<ModuleFile *, unsigned>
1744+
translateMacroIDToIndex(serialization::MacroID ID) const;
1745+
1746+
unsigned translatePreprocessedEntityIDToIndex(
1747+
serialization::PreprocessedEntityID ID) const;
1748+
17511749
/// Translate an \param TypeID ID to the index of TypesLoaded
17521750
/// array and the corresponding module file.
17531751
std::pair<ModuleFile *, unsigned>
@@ -2163,6 +2161,14 @@ class ASTReader
21632161
LocalDeclID mapGlobalIDToModuleFileGlobalID(ModuleFile &M,
21642162
GlobalDeclID GlobalID);
21652163

2164+
/// Reads a macro ID from the given position in a record in the
2165+
/// given module.
2166+
///
2167+
/// \returns The declaration ID read from the record, adjusted to a global
2168+
/// Macro ID.
2169+
serialization::MacroID
2170+
ReadMacroID(ModuleFile &F, const RecordDataImpl &Record, unsigned &Idx);
2171+
21662172
/// Reads a declaration ID from the given position in a record in the
21672173
/// given module.
21682174
///
@@ -2388,7 +2394,8 @@ class ASTReader
23882394

23892395
/// Retrieve the global macro ID corresponding to the given local
23902396
/// ID within the given module file.
2391-
serialization::MacroID getGlobalMacroID(ModuleFile &M, unsigned LocalID);
2397+
serialization::MacroID getGlobalMacroID(ModuleFile &M,
2398+
serialization::MacroID LocalID);
23922399

23932400
/// Read the source location entry with index ID.
23942401
bool ReadSLocEntry(int ID) override;
@@ -2572,8 +2579,8 @@ class ASTReader
25722579

25732580
/// Determine the global preprocessed entity ID that corresponds to
25742581
/// the given local ID within the given module.
2575-
serialization::PreprocessedEntityID
2576-
getGlobalPreprocessedEntityID(ModuleFile &M, unsigned LocalID) const;
2582+
serialization::PreprocessedEntityID getGlobalPreprocessedEntityID(
2583+
ModuleFile &M, serialization::PreprocessedEntityID LocalID) const;
25772584

25782585
/// Add a macro to deserialize its macro directive history.
25792586
///

clang/include/clang/Serialization/ASTWriter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -782,6 +782,10 @@ class ASTWriter : public ASTDeserializationListener,
782782
void AddLookupOffsets(const LookupBlockOffsets &Offsets,
783783
RecordDataImpl &Record);
784784

785+
/// Emit a reference to a macro.
786+
void AddMacroRef(MacroInfo *MI, const IdentifierInfo *Name,
787+
RecordDataImpl &Record);
788+
785789
/// Emit a reference to a declaration.
786790
void AddDeclRef(const Decl *D, RecordDataImpl &Record);
787791
// Emit a reference to a declaration if the declaration was emitted.

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,6 @@ class ModuleFile {
353353
/// Base macro ID for macros local to this module.
354354
serialization::MacroID BaseMacroID = 0;
355355

356-
/// Remapping table for macro IDs in this module.
357-
ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
358-
359356
/// The offset of the start of the set of defined macros.
360357
uint64_t MacroStartOffset = 0;
361358

@@ -372,9 +369,6 @@ class ModuleFile {
372369
/// this module.
373370
serialization::PreprocessedEntityID BasePreprocessedEntityID = 0;
374371

375-
/// Remapping table for preprocessed entity IDs in this module.
376-
ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
377-
378372
const PPEntityOffset *PreprocessedEntityOffsets = nullptr;
379373
unsigned NumPreprocessedEntities = 0;
380374

0 commit comments

Comments
 (0)