Skip to content

Commit cb60667

Browse files
committed
Revert "[serialization] no transitive decl change (#92083)"
This reverts commit d8ec452. This fails on LLDB macOS CI. See #92083 for details.
1 parent 8423337 commit cb60667

File tree

12 files changed

+154
-302
lines changed

12 files changed

+154
-302
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -701,7 +701,10 @@ class alignas(8) Decl {
701701

702702
/// Set the owning module ID. This may only be called for
703703
/// deserialized Decls.
704-
void setOwningModuleID(unsigned ID);
704+
void setOwningModuleID(unsigned ID) {
705+
assert(isFromASTFile() && "Only works on a deserialized declaration");
706+
*((unsigned*)this - 2) = ID;
707+
}
705708

706709
public:
707710
/// Determine the availability of the given declaration.
@@ -774,11 +777,19 @@ class alignas(8) Decl {
774777

775778
/// Retrieve the global declaration ID associated with this
776779
/// declaration, which specifies where this Decl was loaded from.
777-
GlobalDeclID getGlobalID() const;
780+
GlobalDeclID getGlobalID() const {
781+
if (isFromASTFile())
782+
return (*((const GlobalDeclID *)this - 1));
783+
return GlobalDeclID();
784+
}
778785

779786
/// Retrieve the global ID of the module that owns this particular
780787
/// declaration.
781-
unsigned getOwningModuleID() const;
788+
unsigned getOwningModuleID() const {
789+
if (isFromASTFile())
790+
return *((const unsigned*)this - 2);
791+
return 0;
792+
}
782793

783794
private:
784795
Module *getOwningModuleSlow() const;

clang/include/clang/AST/DeclID.h

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "llvm/ADT/DenseMapInfo.h"
2020
#include "llvm/ADT/iterator.h"
2121

22-
#include <climits>
23-
2422
namespace clang {
2523

2624
/// Predefined declaration IDs.
@@ -109,16 +107,12 @@ class DeclIDBase {
109107
///
110108
/// DeclID should only be used directly in serialization. All other users
111109
/// should use LocalDeclID or GlobalDeclID.
112-
using DeclID = uint64_t;
110+
using DeclID = uint32_t;
113111

114112
protected:
115113
DeclIDBase() : ID(PREDEF_DECL_NULL_ID) {}
116114
explicit DeclIDBase(DeclID ID) : ID(ID) {}
117115

118-
explicit DeclIDBase(unsigned LocalID, unsigned ModuleFileIndex) {
119-
ID = (DeclID)LocalID | ((DeclID)ModuleFileIndex << 32);
120-
}
121-
122116
public:
123117
DeclID get() const { return ID; }
124118

@@ -130,10 +124,6 @@ class DeclIDBase {
130124

131125
bool isInvalid() const { return ID == PREDEF_DECL_NULL_ID; }
132126

133-
unsigned getModuleFileIndex() const { return ID >> 32; }
134-
135-
unsigned getLocalDeclIndex() const;
136-
137127
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
138128
return LHS.ID == RHS.ID;
139129
}
@@ -166,9 +156,6 @@ class LocalDeclID : public DeclIDBase {
166156
LocalDeclID(PredefinedDeclIDs ID) : Base(ID) {}
167157
explicit LocalDeclID(DeclID ID) : Base(ID) {}
168158

169-
explicit LocalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
170-
: Base(LocalID, ModuleFileIndex) {}
171-
172159
LocalDeclID &operator++() {
173160
++ID;
174161
return *this;
@@ -188,9 +175,6 @@ class GlobalDeclID : public DeclIDBase {
188175
GlobalDeclID() : Base() {}
189176
explicit GlobalDeclID(DeclID ID) : Base(ID) {}
190177

191-
explicit GlobalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
192-
: Base(LocalID, ModuleFileIndex) {}
193-
194178
// For DeclIDIterator<GlobalDeclID> to be able to convert a GlobalDeclID
195179
// to a LocalDeclID.
196180
explicit operator LocalDeclID() const { return LocalDeclID(this->ID); }

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,6 @@ class DeclOffset {
255255
}
256256
};
257257

258-
// The unaligned decl ID used in the Blobs of bistreams.
259-
using unaligned_decl_id_t =
260-
llvm::support::detail::packed_endian_specific_integral<
261-
serialization::DeclID, llvm::endianness::native,
262-
llvm::support::unaligned>;
263-
264258
/// The number of predefined preprocessed entity IDs.
265259
const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1;
266260

@@ -1985,46 +1979,33 @@ enum CleanupObjectKind { COK_Block, COK_CompoundLiteral };
19851979

19861980
/// Describes the categories of an Objective-C class.
19871981
struct ObjCCategoriesInfo {
1988-
// The ID of the definition. Use unaligned_decl_id_t to keep
1989-
// ObjCCategoriesInfo 32-bit aligned.
1990-
unaligned_decl_id_t DefinitionID;
1982+
// The ID of the definition
1983+
LocalDeclID DefinitionID;
19911984

19921985
// Offset into the array of category lists.
19931986
unsigned Offset;
19941987

1995-
ObjCCategoriesInfo() = default;
1996-
ObjCCategoriesInfo(LocalDeclID ID, unsigned Offset)
1997-
: DefinitionID(ID.get()), Offset(Offset) {}
1998-
1999-
LocalDeclID getDefinitionID() const {
2000-
return LocalDeclID(DefinitionID);
2001-
}
2002-
20031988
friend bool operator<(const ObjCCategoriesInfo &X,
20041989
const ObjCCategoriesInfo &Y) {
2005-
return X.getDefinitionID() < Y.getDefinitionID();
1990+
return X.DefinitionID < Y.DefinitionID;
20061991
}
20071992

20081993
friend bool operator>(const ObjCCategoriesInfo &X,
20091994
const ObjCCategoriesInfo &Y) {
2010-
return X.getDefinitionID() > Y.getDefinitionID();
1995+
return X.DefinitionID > Y.DefinitionID;
20111996
}
20121997

20131998
friend bool operator<=(const ObjCCategoriesInfo &X,
20141999
const ObjCCategoriesInfo &Y) {
2015-
return X.getDefinitionID() <= Y.getDefinitionID();
2000+
return X.DefinitionID <= Y.DefinitionID;
20162001
}
20172002

20182003
friend bool operator>=(const ObjCCategoriesInfo &X,
20192004
const ObjCCategoriesInfo &Y) {
2020-
return X.getDefinitionID() >= Y.getDefinitionID();
2005+
return X.DefinitionID >= Y.DefinitionID;
20212006
}
20222007
};
20232008

2024-
static_assert(alignof(ObjCCategoriesInfo) <= 4);
2025-
static_assert(std::is_standard_layout_v<ObjCCategoriesInfo> &&
2026-
std::is_trivial_v<ObjCCategoriesInfo>);
2027-
20282009
/// A key used when looking up entities by \ref DeclarationName.
20292010
///
20302011
/// Different \ref DeclarationNames are mapped to different keys, but the

clang/include/clang/Serialization/ASTReader.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,12 @@ class ASTReader
504504
/// = I + 1 has already been loaded.
505505
llvm::PagedVector<Decl *> DeclsLoaded;
506506

507+
using GlobalDeclMapType = ContinuousRangeMap<GlobalDeclID, ModuleFile *, 4>;
508+
509+
/// Mapping from global declaration IDs to the module in which the
510+
/// declaration resides.
511+
GlobalDeclMapType GlobalDeclMap;
512+
507513
using FileOffset = std::pair<ModuleFile *, uint64_t>;
508514
using FileOffsetsTy = SmallVector<FileOffset, 2>;
509515
using DeclUpdateOffsetsMap = llvm::DenseMap<GlobalDeclID, FileOffsetsTy>;
@@ -586,11 +592,10 @@ class ASTReader
586592

587593
struct FileDeclsInfo {
588594
ModuleFile *Mod = nullptr;
589-
ArrayRef<serialization::unaligned_decl_id_t> Decls;
595+
ArrayRef<LocalDeclID> Decls;
590596

591597
FileDeclsInfo() = default;
592-
FileDeclsInfo(ModuleFile *Mod,
593-
ArrayRef<serialization::unaligned_decl_id_t> Decls)
598+
FileDeclsInfo(ModuleFile *Mod, ArrayRef<LocalDeclID> Decls)
594599
: Mod(Mod), Decls(Decls) {}
595600
};
596601

@@ -599,7 +604,11 @@ class ASTReader
599604

600605
/// An array of lexical contents of a declaration context, as a sequence of
601606
/// Decl::Kind, DeclID pairs.
602-
using LexicalContents = ArrayRef<serialization::unaligned_decl_id_t>;
607+
using unaligned_decl_id_t =
608+
llvm::support::detail::packed_endian_specific_integral<
609+
serialization::DeclID, llvm::endianness::native,
610+
llvm::support::unaligned>;
611+
using LexicalContents = ArrayRef<unaligned_decl_id_t>;
603612

604613
/// Map from a DeclContext to its lexical contents.
605614
llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
@@ -1480,23 +1489,22 @@ class ASTReader
14801489
unsigned ClientLoadCapabilities);
14811490

14821491
public:
1483-
class ModuleDeclIterator
1484-
: public llvm::iterator_adaptor_base<
1485-
ModuleDeclIterator, const serialization::unaligned_decl_id_t *,
1486-
std::random_access_iterator_tag, const Decl *, ptrdiff_t,
1487-
const Decl *, const Decl *> {
1492+
class ModuleDeclIterator : public llvm::iterator_adaptor_base<
1493+
ModuleDeclIterator, const LocalDeclID *,
1494+
std::random_access_iterator_tag, const Decl *,
1495+
ptrdiff_t, const Decl *, const Decl *> {
14881496
ASTReader *Reader = nullptr;
14891497
ModuleFile *Mod = nullptr;
14901498

14911499
public:
14921500
ModuleDeclIterator() : iterator_adaptor_base(nullptr) {}
14931501

14941502
ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1495-
const serialization::unaligned_decl_id_t *Pos)
1503+
const LocalDeclID *Pos)
14961504
: iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
14971505

14981506
value_type operator*() const {
1499-
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, (LocalDeclID)*I));
1507+
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *I));
15001508
}
15011509

15021510
value_type operator->() const { return **this; }
@@ -1536,9 +1544,6 @@ class ASTReader
15361544
StringRef Arg2 = StringRef(), StringRef Arg3 = StringRef()) const;
15371545
void Error(llvm::Error &&Err) const;
15381546

1539-
/// Translate a \param GlobalDeclID to the index of DeclsLoaded array.
1540-
unsigned translateGlobalDeclIDToIndex(GlobalDeclID ID) const;
1541-
15421547
public:
15431548
/// Load the AST file and validate its contents against the given
15441549
/// Preprocessor.
@@ -1910,8 +1915,7 @@ class ASTReader
19101915

19111916
/// Retrieve the module file that owns the given declaration, or NULL
19121917
/// if the declaration is not from a module file.
1913-
ModuleFile *getOwningModuleFile(const Decl *D) const;
1914-
ModuleFile *getOwningModuleFile(GlobalDeclID ID) const;
1918+
ModuleFile *getOwningModuleFile(const Decl *D);
19151919

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

clang/include/clang/Serialization/ModuleFile.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,23 @@ class ModuleFile {
454454
/// by the declaration ID (-1).
455455
const DeclOffset *DeclOffsets = nullptr;
456456

457-
/// Base declaration index in ASTReader for declarations local to this module.
458-
unsigned BaseDeclIndex = 0;
457+
/// Base declaration ID for declarations local to this module.
458+
serialization::DeclID BaseDeclID = 0;
459+
460+
/// Remapping table for declaration IDs in this module.
461+
ContinuousRangeMap<serialization::DeclID, int, 2> DeclRemap;
462+
463+
/// Mapping from the module files that this module file depends on
464+
/// to the base declaration ID for that module as it is understood within this
465+
/// module.
466+
///
467+
/// This is effectively a reverse global-to-local mapping for declaration
468+
/// IDs, so that we can interpret a true global ID (for this translation unit)
469+
/// as a local ID (for this module file).
470+
llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
459471

460472
/// Array of file-level DeclIDs sorted by file.
461-
const serialization::unaligned_decl_id_t *FileSortedDecls = nullptr;
473+
const LocalDeclID *FileSortedDecls = nullptr;
462474
unsigned NumFileSortedDecls = 0;
463475

464476
/// Array of category list location information within this

clang/include/clang/Serialization/ModuleManager.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace serialization {
4545
/// Manages the set of modules loaded by an AST reader.
4646
class ModuleManager {
4747
/// The chain of AST files, in the order in which we started to load
48-
/// them.
48+
/// them (this order isn't really useful for anything).
4949
SmallVector<std::unique_ptr<ModuleFile>, 2> Chain;
5050

5151
/// The chain of non-module PCH files. The first entry is the one named

clang/lib/AST/DeclBase.cpp

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,18 @@ void *Decl::operator new(std::size_t Size, const ASTContext &Context,
7474
GlobalDeclID ID, std::size_t Extra) {
7575
// Allocate an extra 8 bytes worth of storage, which ensures that the
7676
// resulting pointer will still be 8-byte aligned.
77-
static_assert(sizeof(uint64_t) >= alignof(Decl), "Decl won't be misaligned");
77+
static_assert(sizeof(unsigned) * 2 >= alignof(Decl),
78+
"Decl won't be misaligned");
7879
void *Start = Context.Allocate(Size + Extra + 8);
7980
void *Result = (char*)Start + 8;
8081

81-
uint64_t *PrefixPtr = (uint64_t *)Result - 1;
82+
unsigned *PrefixPtr = (unsigned *)Result - 2;
8283

83-
*PrefixPtr = ID.get();
84+
// Zero out the first 4 bytes; this is used to store the owning module ID.
85+
PrefixPtr[0] = 0;
8486

85-
// We leave the upper 16 bits to store the module IDs. 48 bits should be
86-
// sufficient to store a declaration ID.
87-
assert(*PrefixPtr < llvm::maskTrailingOnes<uint64_t>(48));
87+
// Store the global declaration ID in the second 4 bytes.
88+
PrefixPtr[1] = ID.get();
8889

8990
return Result;
9091
}
@@ -110,28 +111,6 @@ void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
110111
return ::operator new(Size + Extra, Ctx);
111112
}
112113

113-
GlobalDeclID Decl::getGlobalID() const {
114-
if (!isFromASTFile())
115-
return GlobalDeclID();
116-
// See the comments in `Decl::operator new` for details.
117-
uint64_t ID = *((const uint64_t *)this - 1);
118-
return GlobalDeclID(ID & llvm::maskTrailingOnes<uint64_t>(48));
119-
}
120-
121-
unsigned Decl::getOwningModuleID() const {
122-
if (!isFromASTFile())
123-
return 0;
124-
125-
uint64_t ID = *((const uint64_t *)this - 1);
126-
return ID >> 48;
127-
}
128-
129-
void Decl::setOwningModuleID(unsigned ID) {
130-
assert(isFromASTFile() && "Only works on a deserialized declaration");
131-
uint64_t *IDAddress = (uint64_t *)this - 1;
132-
*IDAddress |= (uint64_t)ID << 48;
133-
}
134-
135114
Module *Decl::getOwningModuleSlow() const {
136115
assert(isFromASTFile() && "Not from AST file?");
137116
return getASTContext().getExternalSource()->getModule(getOwningModuleID());
@@ -2185,7 +2164,3 @@ DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
21852164

21862165
return DD;
21872166
}
2188-
2189-
unsigned DeclIDBase::getLocalDeclIndex() const {
2190-
return ID & llvm::maskTrailingOnes<DeclID>(32);
2191-
}

0 commit comments

Comments
 (0)