Skip to content

Commit 4f70c5e

Browse files
committed
Revert "[serialization] no transitive decl change (#92083)"
This reverts commit 5c10487. The ArmV7 bot is complaining the change breaks the alignment.
1 parent 222e0a0 commit 4f70c5e

File tree

16 files changed

+167
-338
lines changed

16 files changed

+167
-338
lines changed

clang/include/clang/AST/ASTUnresolvedSet.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
#include "clang/AST/ASTVector.h"
1818
#include "clang/AST/DeclAccessPair.h"
19-
#include "clang/AST/DeclID.h"
2019
#include "clang/AST/UnresolvedSet.h"
2120
#include "clang/Basic/Specifiers.h"
2221
#include <cassert>
@@ -57,10 +56,6 @@ class ASTUnresolvedSet {
5756
Decls.push_back(DeclAccessPair::make(D, AS), C);
5857
}
5958

60-
void addLazyDecl(ASTContext &C, GlobalDeclID ID, AccessSpecifier AS) {
61-
Decls.push_back(DeclAccessPair::makeLazy(ID.get(), AS), C);
62-
}
63-
6459
/// Replaces the given declaration with the new one, once.
6560
///
6661
/// \return true if the set changed
@@ -114,10 +109,10 @@ class LazyASTUnresolvedSet {
114109

115110
void reserve(ASTContext &C, unsigned N) { Impl.reserve(C, N); }
116111

117-
void addLazyDecl(ASTContext &C, GlobalDeclID ID, AccessSpecifier AS) {
112+
void addLazyDecl(ASTContext &C, uintptr_t ID, AccessSpecifier AS) {
118113
assert(Impl.empty() || Impl.Decls.isLazy());
119114
Impl.Decls.setLazy(true);
120-
Impl.addLazyDecl(C, ID, AS);
115+
Impl.addDecl(C, reinterpret_cast<NamedDecl *>(ID << 2), AS);
121116
}
122117
};
123118

clang/include/clang/AST/DeclAccessPair.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,9 @@ class NamedDecl;
2727
/// A POD class for pairing a NamedDecl* with an access specifier.
2828
/// Can be put into unions.
2929
class DeclAccessPair {
30-
/// Use the lower 2 bit to store AccessSpecifier. Use the higher
31-
/// 61 bit to store the pointer to a NamedDecl or the DeclID to
32-
/// a NamedDecl. If the 3rd bit is set, storing the DeclID, otherwise
33-
/// storing the pointer.
34-
//
35-
// we'd use llvm::PointerUnion, but it isn't trivial
36-
uint64_t Ptr;
30+
uintptr_t Ptr; // we'd use llvm::PointerUnion, but it isn't trivial
3731

38-
enum { ASMask = 0x3, Mask = 0x7 };
39-
40-
bool isDeclID() const { return (Ptr >> 2) & 0x1; }
32+
enum { Mask = 0x3 };
4133

4234
public:
4335
static DeclAccessPair make(NamedDecl *D, AccessSpecifier AS) {
@@ -46,22 +38,12 @@ class DeclAccessPair {
4638
return p;
4739
}
4840

49-
static DeclAccessPair makeLazy(uint64_t ID, AccessSpecifier AS) {
50-
DeclAccessPair p;
51-
p.Ptr = (ID << 3) | (0x1 << 2) | uint64_t(AS);
52-
return p;
53-
}
54-
55-
uint64_t getDeclID() const {
56-
assert(isDeclID());
57-
return (~Mask & Ptr) >> 3;
58-
}
59-
6041
NamedDecl *getDecl() const {
61-
assert(!isDeclID());
6242
return reinterpret_cast<NamedDecl*>(~Mask & Ptr);
6343
}
64-
AccessSpecifier getAccess() const { return AccessSpecifier(ASMask & Ptr); }
44+
AccessSpecifier getAccess() const {
45+
return AccessSpecifier(Mask & Ptr);
46+
}
6547

6648
void setDecl(NamedDecl *D) {
6749
set(D, getAccess());
@@ -70,7 +52,7 @@ class DeclAccessPair {
7052
set(getDecl(), AS);
7153
}
7254
void set(NamedDecl *D, AccessSpecifier AS) {
73-
Ptr = uint64_t(AS) | reinterpret_cast<uint64_t>(D);
55+
Ptr = uintptr_t(AS) | reinterpret_cast<uintptr_t>(D);
7456
}
7557

7658
operator NamedDecl*() const { return getDecl(); }

clang/include/clang/AST/DeclBase.h

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

709709
/// Set the owning module ID. This may only be called for
710710
/// deserialized Decls.
711-
void setOwningModuleID(unsigned ID);
711+
void setOwningModuleID(unsigned ID) {
712+
assert(isFromASTFile() && "Only works on a deserialized declaration");
713+
*((unsigned*)this - 2) = ID;
714+
}
712715

713716
public:
714717
/// Determine the availability of the given declaration.
@@ -781,11 +784,19 @@ class alignas(8) Decl {
781784

782785
/// Retrieve the global declaration ID associated with this
783786
/// declaration, which specifies where this Decl was loaded from.
784-
GlobalDeclID getGlobalID() const;
787+
GlobalDeclID getGlobalID() const {
788+
if (isFromASTFile())
789+
return (*((const GlobalDeclID *)this - 1));
790+
return GlobalDeclID();
791+
}
785792

786793
/// Retrieve the global ID of the module that owns this particular
787794
/// declaration.
788-
unsigned getOwningModuleID() const;
795+
unsigned getOwningModuleID() const {
796+
if (isFromASTFile())
797+
return *((const unsigned*)this - 2);
798+
return 0;
799+
}
789800

790801
private:
791802
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/AST/UnresolvedSet.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class UnresolvedSetIterator : public llvm::iterator_adaptor_base<
4747
// temporaries with defaulted ctors are not zero initialized.
4848
UnresolvedSetIterator() : iterator_adaptor_base(nullptr) {}
4949

50-
uint64_t getDeclID() const { return I->getDeclID(); }
5150
NamedDecl *getDecl() const { return I->getDecl(); }
5251
void setDecl(NamedDecl *ND) const { return I->setDecl(ND); }
5352
AccessSpecifier getAccess() const { return I->getAccess(); }

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 6 additions & 23 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

@@ -1986,44 +1980,33 @@ enum CleanupObjectKind { COK_Block, COK_CompoundLiteral };
19861980

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

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

1996-
ObjCCategoriesInfo() = default;
1997-
ObjCCategoriesInfo(LocalDeclID ID, unsigned Offset)
1998-
: DefinitionID(ID.get()), Offset(Offset) {}
1999-
2000-
LocalDeclID getDefinitionID() const { return LocalDeclID(DefinitionID); }
2001-
20021989
friend bool operator<(const ObjCCategoriesInfo &X,
20031990
const ObjCCategoriesInfo &Y) {
2004-
return X.getDefinitionID() < Y.getDefinitionID();
1991+
return X.DefinitionID < Y.DefinitionID;
20051992
}
20061993

20071994
friend bool operator>(const ObjCCategoriesInfo &X,
20081995
const ObjCCategoriesInfo &Y) {
2009-
return X.getDefinitionID() > Y.getDefinitionID();
1996+
return X.DefinitionID > Y.DefinitionID;
20101997
}
20111998

20121999
friend bool operator<=(const ObjCCategoriesInfo &X,
20132000
const ObjCCategoriesInfo &Y) {
2014-
return X.getDefinitionID() <= Y.getDefinitionID();
2001+
return X.DefinitionID <= Y.DefinitionID;
20152002
}
20162003

20172004
friend bool operator>=(const ObjCCategoriesInfo &X,
20182005
const ObjCCategoriesInfo &Y) {
2019-
return X.getDefinitionID() >= Y.getDefinitionID();
2006+
return X.DefinitionID >= Y.DefinitionID;
20202007
}
20212008
};
20222009

2023-
static_assert(alignof(ObjCCategoriesInfo) <= 4);
2024-
static_assert(std::is_standard_layout_v<ObjCCategoriesInfo> &&
2025-
std::is_trivial_v<ObjCCategoriesInfo>);
2026-
20272010
/// A key used when looking up entities by \ref DeclarationName.
20282011
///
20292012
/// 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

0 commit comments

Comments
 (0)