Skip to content

Commit 8ec79e4

Browse files
committed
[serialization] no transitive decl change
1 parent 0bacffb commit 8ec79e4

File tree

12 files changed

+282
-147
lines changed

12 files changed

+282
-147
lines changed

clang/include/clang/AST/DeclBase.h

+3-14
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

+22-1
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,15 @@ 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+
// Implement it directly instead of calling `llvm::maskTrailingOnes` since
137+
// we don't want `MathExtras.h` to be inclued here.
138+
const unsigned Bits = CHAR_BIT * sizeof(DeclID);
139+
return ID & (DeclID(-1) >> (Bits - 32));
140+
}
141+
127142
friend bool operator==(const DeclIDBase &LHS, const DeclIDBase &RHS) {
128143
return LHS.ID == RHS.ID;
129144
}
@@ -156,6 +171,9 @@ class LocalDeclID : public DeclIDBase {
156171
LocalDeclID(PredefinedDeclIDs ID) : Base(ID) {}
157172
explicit LocalDeclID(DeclID ID) : Base(ID) {}
158173

174+
explicit LocalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
175+
: Base(LocalID, ModuleFileIndex) {}
176+
159177
LocalDeclID &operator++() {
160178
++ID;
161179
return *this;
@@ -175,6 +193,9 @@ class GlobalDeclID : public DeclIDBase {
175193
GlobalDeclID() : Base() {}
176194
explicit GlobalDeclID(DeclID ID) : Base(ID) {}
177195

196+
explicit GlobalDeclID(unsigned LocalID, unsigned ModuleFileIndex)
197+
: Base(LocalID, ModuleFileIndex) {}
198+
178199
// For DeclIDIterator<GlobalDeclID> to be able to convert a GlobalDeclID
179200
// to a LocalDeclID.
180201
explicit operator LocalDeclID() const { return LocalDeclID(this->ID); }

clang/include/clang/Serialization/ASTBitCodes.h

+6
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 unalighed_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

clang/include/clang/Serialization/ASTReader.h

+16-20
Original file line numberDiff line numberDiff line change
@@ -501,12 +501,6 @@ class ASTReader
501501
/// = I + 1 has already been loaded.
502502
llvm::PagedVector<Decl *> DeclsLoaded;
503503

504-
using GlobalDeclMapType = ContinuousRangeMap<GlobalDeclID, ModuleFile *, 4>;
505-
506-
/// Mapping from global declaration IDs to the module in which the
507-
/// declaration resides.
508-
GlobalDeclMapType GlobalDeclMap;
509-
510504
using FileOffset = std::pair<ModuleFile *, uint64_t>;
511505
using FileOffsetsTy = SmallVector<FileOffset, 2>;
512506
using DeclUpdateOffsetsMap = llvm::DenseMap<GlobalDeclID, FileOffsetsTy>;
@@ -589,10 +583,11 @@ class ASTReader
589583

590584
struct FileDeclsInfo {
591585
ModuleFile *Mod = nullptr;
592-
ArrayRef<LocalDeclID> Decls;
586+
ArrayRef<serialization::unalighed_decl_id_t> Decls;
593587

594588
FileDeclsInfo() = default;
595-
FileDeclsInfo(ModuleFile *Mod, ArrayRef<LocalDeclID> Decls)
589+
FileDeclsInfo(ModuleFile *Mod,
590+
ArrayRef<serialization::unalighed_decl_id_t> Decls)
596591
: Mod(Mod), Decls(Decls) {}
597592
};
598593

@@ -601,11 +596,7 @@ class ASTReader
601596

602597
/// An array of lexical contents of a declaration context, as a sequence of
603598
/// Decl::Kind, DeclID pairs.
604-
using unalighed_decl_id_t =
605-
llvm::support::detail::packed_endian_specific_integral<
606-
serialization::DeclID, llvm::endianness::native,
607-
llvm::support::unaligned>;
608-
using LexicalContents = ArrayRef<unalighed_decl_id_t>;
599+
using LexicalContents = ArrayRef<serialization::unalighed_decl_id_t>;
609600

610601
/// Map from a DeclContext to its lexical contents.
611602
llvm::DenseMap<const DeclContext*, std::pair<ModuleFile*, LexicalContents>>
@@ -1486,22 +1477,23 @@ class ASTReader
14861477
unsigned ClientLoadCapabilities);
14871478

14881479
public:
1489-
class ModuleDeclIterator : public llvm::iterator_adaptor_base<
1490-
ModuleDeclIterator, const LocalDeclID *,
1491-
std::random_access_iterator_tag, const Decl *,
1492-
ptrdiff_t, const Decl *, const Decl *> {
1480+
class ModuleDeclIterator
1481+
: public llvm::iterator_adaptor_base<
1482+
ModuleDeclIterator, const serialization::unalighed_decl_id_t *,
1483+
std::random_access_iterator_tag, const Decl *, ptrdiff_t,
1484+
const Decl *, const Decl *> {
14931485
ASTReader *Reader = nullptr;
14941486
ModuleFile *Mod = nullptr;
14951487

14961488
public:
14971489
ModuleDeclIterator() : iterator_adaptor_base(nullptr) {}
14981490

14991491
ModuleDeclIterator(ASTReader *Reader, ModuleFile *Mod,
1500-
const LocalDeclID *Pos)
1492+
const serialization::unalighed_decl_id_t *Pos)
15011493
: iterator_adaptor_base(Pos), Reader(Reader), Mod(Mod) {}
15021494

15031495
value_type operator*() const {
1504-
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, *I));
1496+
return Reader->GetDecl(Reader->getGlobalDeclID(*Mod, (LocalDeclID)*I));
15051497
}
15061498

15071499
value_type operator->() const { return **this; }
@@ -1541,6 +1533,9 @@ class ASTReader
15411533
StringRef Arg2 = StringRef(), StringRef Arg3 = StringRef()) const;
15421534
void Error(llvm::Error &&Err) const;
15431535

1536+
/// Translate a \param GlobalDeclID to the index of DeclsLoaded array.
1537+
unsigned translateGlobalDeclIDToIndex(GlobalDeclID ID) const;
1538+
15441539
public:
15451540
/// Load the AST file and validate its contents against the given
15461541
/// Preprocessor.
@@ -1912,7 +1907,8 @@ class ASTReader
19121907

19131908
/// Retrieve the module file that owns the given declaration, or NULL
19141909
/// if the declaration is not from a module file.
1915-
ModuleFile *getOwningModuleFile(const Decl *D);
1910+
ModuleFile *getOwningModuleFile(const Decl *D) const;
1911+
ModuleFile *getOwningModuleFile(GlobalDeclID ID) const;
19161912

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

clang/include/clang/Serialization/ModuleFile.h

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

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;
457+
/// Base declaration index in ASTReader for declarations local to this module.
458+
unsigned BaseDeclIndex = 0;
471459

472460
/// Array of file-level DeclIDs sorted by file.
473-
const LocalDeclID *FileSortedDecls = nullptr;
461+
const serialization::unalighed_decl_id_t *FileSortedDecls = nullptr;
474462
unsigned NumFileSortedDecls = 0;
475463

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

clang/include/clang/Serialization/ModuleManager.h

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

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

clang/lib/AST/DeclBase.cpp

+27-7
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,16 @@ 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.
86+
assert(*PrefixPtr < llvm::maskTrailingOnes<uint64_t>(48));
8987

9088
return Result;
9189
}
@@ -111,6 +109,28 @@ void *Decl::operator new(std::size_t Size, const ASTContext &Ctx,
111109
return ::operator new(Size + Extra, Ctx);
112110
}
113111

112+
GlobalDeclID Decl::getGlobalID() const {
113+
if (!isFromASTFile())
114+
return GlobalDeclID();
115+
uint64_t ID = *((const uint64_t *)this - 1);
116+
return GlobalDeclID(ID & llvm::maskTrailingOnes<uint64_t>(48));
117+
}
118+
119+
unsigned Decl::getOwningModuleID() const {
120+
if (!isFromASTFile())
121+
return 0;
122+
123+
uint64_t ID = *((const uint64_t *)this - 1);
124+
return ID >> 48;
125+
}
126+
127+
void Decl::setOwningModuleID(unsigned ID) {
128+
assert(isFromASTFile() && "Only works on a deserialized declaration");
129+
uint64_t *IDAddress = (uint64_t *)this - 1;
130+
assert(!((*IDAddress) >> 48) && "We should only set the module ID once");
131+
*IDAddress |= (uint64_t)ID << 48;
132+
}
133+
114134
Module *Decl::getOwningModuleSlow() const {
115135
assert(isFromASTFile() && "Not from AST file?");
116136
return getASTContext().getExternalSource()->getModule(getOwningModuleID());

0 commit comments

Comments
 (0)