Skip to content

Commit c887056

Browse files
author
git apple-llvm automerger
committed
Merge commit 'd8ec452db016' from llvm.org/main into next
2 parents 3aae322 + d8ec452 commit c887056

File tree

12 files changed

+302
-154
lines changed

12 files changed

+302
-154
lines changed

clang/include/clang/AST/DeclBase.h

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -701,10 +701,7 @@ 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) {
705-
assert(isFromASTFile() && "Only works on a deserialized declaration");
706-
*((unsigned*)this - 2) = ID;
707-
}
704+
void setOwningModuleID(unsigned ID);
708705

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

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

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

794783
private:
795784
Module *getOwningModuleSlow() const;

clang/include/clang/AST/DeclID.h

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

22+
#include <climits>
23+
2224
namespace clang {
2325

2426
/// Predefined declaration IDs.
@@ -107,12 +109,16 @@ class DeclIDBase {
107109
///
108110
/// DeclID should only be used directly in serialization. All other users
109111
/// should use LocalDeclID or GlobalDeclID.
110-
using DeclID = uint32_t;
112+
using DeclID = uint64_t;
111113

112114
protected:
113115
DeclIDBase() : ID(PREDEF_DECL_NULL_ID) {}
114116
explicit DeclIDBase(DeclID ID) : ID(ID) {}
115117

118+
explicit DeclIDBase(unsigned LocalID, unsigned ModuleFileIndex) {
119+
ID = (DeclID)LocalID | ((DeclID)ModuleFileIndex << 32);
120+
}
121+
116122
public:
117123
DeclID get() const { return ID; }
118124

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

125131
bool isInvalid() const { return ID == PREDEF_DECL_NULL_ID; }
126132

133+
unsigned getModuleFileIndex() const { return ID >> 32; }
134+
135+
unsigned getLocalDeclIndex() const;
136+
127137
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
128138
return LHS.ID == RHS.ID;
129139
}
@@ -156,6 +166,9 @@ class LocalDeclID : public DeclIDBase {
156166
LocalDeclID(PredefinedDeclIDs ID) : Base(ID) {}
157167
explicit LocalDeclID(DeclID ID) : Base(ID) {}
158168

169+
explicit LocalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
170+
: Base(LocalID, ModuleFileIndex) {}
171+
159172
LocalDeclID &operator++() {
160173
++ID;
161174
return *this;
@@ -175,6 +188,9 @@ class GlobalDeclID : public DeclIDBase {
175188
GlobalDeclID() : Base() {}
176189
explicit GlobalDeclID(DeclID ID) : Base(ID) {}
177190

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

clang/include/clang/Serialization/ASTBitCodes.h

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ 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+
258264
/// The number of predefined preprocessed entity IDs.
259265
const unsigned int NUM_PREDEF_PP_ENTITY_IDS = 1;
260266

@@ -1990,33 +1996,46 @@ enum CleanupObjectKind { COK_Block, COK_CompoundLiteral };
19901996

19911997
/// Describes the categories of an Objective-C class.
19921998
struct ObjCCategoriesInfo {
1993-
// The ID of the definition
1994-
LocalDeclID DefinitionID;
1999+
// The ID of the definition. Use unaligned_decl_id_t to keep
2000+
// ObjCCategoriesInfo 32-bit aligned.
2001+
unaligned_decl_id_t DefinitionID;
19952002

19962003
// Offset into the array of category lists.
19972004
unsigned Offset;
19982005

2006+
ObjCCategoriesInfo() = default;
2007+
ObjCCategoriesInfo(LocalDeclID ID, unsigned Offset)
2008+
: DefinitionID(ID.get()), Offset(Offset) {}
2009+
2010+
LocalDeclID getDefinitionID() const {
2011+
return LocalDeclID(DefinitionID);
2012+
}
2013+
19992014
friend bool operator<(const ObjCCategoriesInfo &X,
20002015
const ObjCCategoriesInfo &Y) {
2001-
return X.DefinitionID < Y.DefinitionID;
2016+
return X.getDefinitionID() < Y.getDefinitionID();
20022017
}
20032018

20042019
friend bool operator>(const ObjCCategoriesInfo &X,
20052020
const ObjCCategoriesInfo &Y) {
2006-
return X.DefinitionID > Y.DefinitionID;
2021+
return X.getDefinitionID() > Y.getDefinitionID();
20072022
}
20082023

20092024
friend bool operator<=(const ObjCCategoriesInfo &X,
20102025
const ObjCCategoriesInfo &Y) {
2011-
return X.DefinitionID <= Y.DefinitionID;
2026+
return X.getDefinitionID() <= Y.getDefinitionID();
20122027
}
20132028

20142029
friend bool operator>=(const ObjCCategoriesInfo &X,
20152030
const ObjCCategoriesInfo &Y) {
2016-
return X.DefinitionID >= Y.DefinitionID;
2031+
return X.getDefinitionID() >= Y.getDefinitionID();
20172032
}
20182033
};
20192034

2035+
static_assert(alignof(ObjCCategoriesInfo) <= 4);
2036+
static_assert(std::is_standard_layout_v<ObjCCategoriesInfo> &&
2037+
std::is_trivial_v<ObjCCategoriesInfo>);
2038+
20202039
/// A key used when looking up entities by \ref DeclarationName.
20212040
///
20222041
/// Different \ref DeclarationNames are mapped to different keys, but the

clang/include/clang/Serialization/ASTReader.h

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

531-
using GlobalDeclMapType = ContinuousRangeMap<GlobalDeclID, ModuleFile *, 4>;
532-
533-
/// Mapping from global declaration IDs to the module in which the
534-
/// declaration resides.
535-
GlobalDeclMapType GlobalDeclMap;
536-
537531
using FileOffset = std::pair<ModuleFile *, uint64_t>;
538532
using FileOffsetsTy = SmallVector<FileOffset, 2>;
539533
using DeclUpdateOffsetsMap = llvm::DenseMap<GlobalDeclID, FileOffsetsTy>;
@@ -616,10 +610,11 @@ class ASTReader
616610

617611
struct FileDeclsInfo {
618612
ModuleFile *Mod = nullptr;
619-
ArrayRef<LocalDeclID> Decls;
613+
ArrayRef<serialization::unaligned_decl_id_t> Decls;
620614

621615
FileDeclsInfo() = default;
622-
FileDeclsInfo(ModuleFile *Mod, ArrayRef<LocalDeclID> Decls)
616+
FileDeclsInfo(ModuleFile *Mod,
617+
ArrayRef<serialization::unaligned_decl_id_t> Decls)
623618
: Mod(Mod), Decls(Decls) {}
624619
};
625620

@@ -628,11 +623,7 @@ class ASTReader
628623

629624
/// An array of lexical contents of a declaration context, as a sequence of
630625
/// Decl::Kind, DeclID pairs.
631-
using unaligned_decl_id_t =
632-
llvm::support::detail::packed_endian_specific_integral<
633-
serialization::DeclID, llvm::endianness::native,
634-
llvm::support::unaligned>;
635-
using LexicalContents = ArrayRef<unaligned_decl_id_t>;
626+
using LexicalContents = ArrayRef<serialization::unaligned_decl_id_t>;
636627

637628
/// Map from a DeclContext to its lexical contents.
638629
llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
@@ -1506,22 +1497,23 @@ class ASTReader
15061497
unsigned ClientLoadCapabilities);
15071498

15081499
public:
1509-
class ModuleDeclIterator : public llvm::iterator_adaptor_base<
1510-
ModuleDeclIterator, const LocalDeclID *,
1511-
std::random_access_iterator_tag, const Decl *,
1512-
ptrdiff_t, const Decl *, const Decl *> {
1500+
class ModuleDeclIterator
1501+
: public llvm::iterator_adaptor_base<
1502+
ModuleDeclIterator, const serialization::unaligned_decl_id_t *,
1503+
std::random_access_iterator_tag, const Decl *, ptrdiff_t,
1504+
const Decl *, const Decl *> {
15131505
ASTReader *Reader = nullptr;
15141506
ModuleFile *Mod = nullptr;
15151507

15161508
public:
15171509
ModuleDeclIterator() : iterator_adaptor_base(nullptr) {}
15181510

15191511
ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1520-
const LocalDeclID *Pos)
1512+
const serialization::unaligned_decl_id_t *Pos)
15211513
: iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
15221514

15231515
value_type operator*() const {
1524-
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *I));
1516+
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, (LocalDeclID)*I));
15251517
}
15261518

15271519
value_type operator->() const { return **this; }
@@ -1561,6 +1553,9 @@ class ASTReader
15611553
StringRef Arg2 = StringRef(), StringRef Arg3 = StringRef()) const;
15621554
void Error(llvm::Error &&Err) const;
15631555

1556+
/// Translate a \param GlobalDeclID to the index of DeclsLoaded array.
1557+
unsigned translateGlobalDeclIDToIndex(GlobalDeclID ID) const;
1558+
15641559
public:
15651560
/// Load the AST file and validate its contents against the given
15661561
/// Preprocessor.
@@ -1929,7 +1924,8 @@ class ASTReader
19291924

19301925
/// Retrieve the module file that owns the given declaration, or NULL
19311926
/// if the declaration is not from a module file.
1932-
ModuleFile *getOwningModuleFile(const Decl *D);
1927+
ModuleFile *getOwningModuleFile(const Decl *D) const;
1928+
ModuleFile *getOwningModuleFile(GlobalDeclID ID) const;
19331929

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

clang/include/clang/Serialization/ModuleFile.h

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

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

477465
/// Array of file-level DeclIDs sorted by file.
478-
const LocalDeclID *FileSortedDecls = nullptr;
466+
const serialization::unaligned_decl_id_t *FileSortedDecls = nullptr;
479467
unsigned NumFileSortedDecls = 0;
480468

481469
/// 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 (this order isn't really useful for anything).
48+
/// them.
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: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,17 @@ 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(unsigned) * 2 >= alignof(Decl),
78-
"Decl won't be misaligned");
77+
static_assert(sizeof(uint64_t) >= alignof(Decl), "Decl won't be misaligned");
7978
void *Start = Context.Allocate(Size + Extra + 8);
8079
void *Result = (char*)Start + 8;
8180

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

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

87-
// Store the global declaration ID in the second 4 bytes.
88-
PrefixPtr[1] = ID.get();
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));
8988

9089
return Result;
9190
}
@@ -111,6 +110,28 @@ void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
111110
return ::operator new(Size + Extra, Ctx);
112111
}
113112

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+
114135
Module *Decl::getOwningModuleSlow() const {
115136
assert(isFromASTFile() && "Not from AST file?");
116137
return getASTContext().getExternalSource()->getModule(getOwningModuleID());
@@ -2164,3 +2185,7 @@ DependentDiagnostic *DependentDiagnostic::Create(ASTContext &C,
21642185

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

0 commit comments

Comments
 (0)