Skip to content

Commit c5e4afe

Browse files
authored
[C++20] [Modules] Support module level lookup (#122887) (#123281)
Close #90154 This patch is also an optimization to the lookup process to utilize the information provided by `export` keyword. Previously, in the lookup process, the `export` keyword only takes part in the check part, it doesn't get involved in the lookup process. That said, previously, in a name lookup for 'name', we would load all of declarations with the name 'name' and check if these declarations are valid or not. It works well. But it is inefficient since it may load declarations that may not be wanted. Note that this patch actually did a trick in the lookup process instead of bring module information to DeclarationName or considering module information when deciding if two declarations are the same. So it may not be a surprise to me if there are missing cases. But it is not a regression. It should be already the case. Issue reports are welcomed. In this patch, I tried to split the big lookup table into a lookup table as before and a module local lookup table, which takes a combination of the ID of the DeclContext and hash value of the primary module name as the key. And refactored `DeclContext::lookup()` method to take the module information. So that a lookup in a DeclContext won't load declarations that are local to **other** modules. And also I think it is already beneficial to split the big lookup table since it may reduce the conflicts during lookups in the hash table. BTW, this patch introduced a **regression** for a reachability rule in C++20 but it was false-negative. See 'clang/test/CXX/module/module.interface/p7.cpp' for details. This patch is not expected to introduce any other regressions for non-c++20-modules users since the module local lookup table should be empty for them.
1 parent 225fc4f commit c5e4afe

File tree

21 files changed

+684
-162
lines changed

21 files changed

+684
-162
lines changed

clang/docs/ReleaseNotes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ C++23 Feature Support
316316
C++20 Feature Support
317317
^^^^^^^^^^^^^^^^^^^^^
318318

319+
- Implemented module level lookup for C++20 modules. (#GH90154)
320+
319321

320322
Resolutions to C++ Defect Reports
321323
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/include/clang/AST/DeclBase.h

+4
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,10 @@ class alignas(8) Decl {
836836
return isFromASTFile() ? getImportedOwningModule() : getLocalOwningModule();
837837
}
838838

839+
/// Get the top level owning named module that owns this declaration if any.
840+
/// \returns nullptr if the declaration is not owned by a named module.
841+
Module *getTopLevelOwningNamedModule() const;
842+
839843
/// Get the module that owns this declaration for linkage purposes.
840844
/// There only ever is such a standard C++ module.
841845
Module *getOwningModuleForLinkage() const;

clang/include/clang/Serialization/ASTBitCodes.h

+6
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,8 @@ enum ASTRecordTypes {
738738
CXX_ADDED_TEMPLATE_SPECIALIZATION = 74,
739739

740740
CXX_ADDED_TEMPLATE_PARTIAL_SPECIALIZATION = 75,
741+
742+
UPDATE_MODULE_LOCAL_VISIBLE = 76,
741743
};
742744

743745
/// Record types used within a source manager block.
@@ -1334,6 +1336,10 @@ enum DeclCode {
13341336
/// into a DeclContext via DeclContext::lookup.
13351337
DECL_CONTEXT_VISIBLE,
13361338

1339+
/// A record containing the set of declarations that are
1340+
/// only visible from DeclContext in the same module.
1341+
DECL_CONTEXT_MODULE_LOCAL_VISIBLE,
1342+
13371343
/// A LabelDecl record.
13381344
DECL_LABEL,
13391345

clang/include/clang/Serialization/ASTReader.h

+26-3
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ class ASTIdentifierLookupTrait;
353353

354354
/// The on-disk hash table(s) used for DeclContext name lookup.
355355
struct DeclContextLookupTable;
356+
struct ModuleLocalLookupTable;
356357

357358
/// The on-disk hash table(s) used for specialization decls.
358359
struct LazySpecializationInfoLookupTable;
@@ -523,9 +524,14 @@ class ASTReader
523524
/// in the chain.
524525
DeclUpdateOffsetsMap DeclUpdateOffsets;
525526

527+
struct LookupBlockOffsets {
528+
uint64_t LexicalOffset;
529+
uint64_t VisibleOffset;
530+
uint64_t ModuleLocalOffset;
531+
};
532+
526533
using DelayedNamespaceOffsetMapTy =
527-
llvm::DenseMap<GlobalDeclID, std::pair</*LexicalOffset*/ uint64_t,
528-
/*VisibleOffset*/ uint64_t>>;
534+
llvm::DenseMap<GlobalDeclID, LookupBlockOffsets>;
529535

530536
/// Mapping from global declaration IDs to the lexical and visible block
531537
/// offset for delayed namespace in reduced BMI.
@@ -631,6 +637,9 @@ class ASTReader
631637
/// Map from a DeclContext to its lookup tables.
632638
llvm::DenseMap<const DeclContext *,
633639
serialization::reader::DeclContextLookupTable> Lookups;
640+
llvm::DenseMap<const DeclContext *,
641+
serialization::reader::ModuleLocalLookupTable>
642+
ModuleLocalLookups;
634643

635644
using SpecLookupTableTy =
636645
llvm::DenseMap<const Decl *,
@@ -659,6 +668,8 @@ class ASTReader
659668
/// Updates to the visible declarations of declaration contexts that
660669
/// haven't been loaded yet.
661670
llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates> PendingVisibleUpdates;
671+
llvm::DenseMap<GlobalDeclID, DeclContextVisibleUpdates>
672+
PendingModuleLocalVisibleUpdates;
662673

663674
using SpecializationsUpdate = SmallVector<UpdateData, 1>;
664675
using SpecializationsUpdateMap =
@@ -696,7 +707,8 @@ class ASTReader
696707
/// Read the record that describes the visible contents of a DC.
697708
bool ReadVisibleDeclContextStorage(ModuleFile &M,
698709
llvm::BitstreamCursor &Cursor,
699-
uint64_t Offset, GlobalDeclID ID);
710+
uint64_t Offset, GlobalDeclID ID,
711+
bool IsModuleLocal);
700712

701713
bool ReadSpecializations(ModuleFile &M, llvm::BitstreamCursor &Cursor,
702714
uint64_t Offset, Decl *D, bool IsPartial);
@@ -1132,6 +1144,10 @@ class ASTReader
11321144
/// Number of visible decl contexts read/total.
11331145
unsigned NumVisibleDeclContextsRead = 0, TotalVisibleDeclContexts = 0;
11341146

1147+
/// Number of module local visible decl contexts read/total.
1148+
unsigned NumModuleLocalVisibleDeclContexts = 0,
1149+
TotalModuleLocalVisibleDeclContexts = 0;
1150+
11351151
/// Total size of modules, in bits, currently loaded
11361152
uint64_t TotalModulesSizeInBits = 0;
11371153

@@ -1444,6 +1460,9 @@ class ASTReader
14441460
const serialization::reader::DeclContextLookupTable *
14451461
getLoadedLookupTables(DeclContext *Primary) const;
14461462

1463+
const serialization::reader::ModuleLocalLookupTable *
1464+
getModuleLocalLookupTables(DeclContext *Primary) const;
1465+
14471466
/// Get the loaded specializations lookup tables for \p D,
14481467
/// if any.
14491468
serialization::reader::LazySpecializationInfoLookupTable *
@@ -2608,6 +2627,10 @@ inline bool shouldSkipCheckingODR(const Decl *D) {
26082627
(D->isFromGlobalModule() || D->isFromHeaderUnit());
26092628
}
26102629

2630+
/// Calculate a hash value for the primary module name of the given module.
2631+
/// \returns std::nullopt if M is not a C++ standard module.
2632+
std::optional<unsigned> getPrimaryModuleHash(const Module *M);
2633+
26112634
} // namespace clang
26122635

26132636
#endif // LLVM_CLANG_SERIALIZATION_ASTREADER_H

clang/include/clang/Serialization/ASTWriter.h

+13-3
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,10 @@ class ASTWriter : public ASTDeserializationListener,
492492
/// file.
493493
unsigned NumVisibleDeclContexts = 0;
494494

495+
/// The number of module local visible declcontexts written to the AST
496+
/// file.
497+
unsigned NumModuleLocalDeclContexts = 0;
498+
495499
/// A mapping from each known submodule to its ID number, which will
496500
/// be a positive integer.
497501
llvm::DenseMap<const Module *, unsigned> SubmoduleIDs;
@@ -587,11 +591,15 @@ class ASTWriter : public ASTDeserializationListener,
587591
uint64_t WriteSpecializationInfoLookupTable(
588592
const NamedDecl *D, llvm::SmallVectorImpl<const Decl *> &Specializations,
589593
bool IsPartial);
590-
void GenerateNameLookupTable(ASTContext &Context, const DeclContext *DC,
591-
llvm::SmallVectorImpl<char> &LookupTable);
594+
void
595+
GenerateNameLookupTable(ASTContext &Context, const DeclContext *DC,
596+
llvm::SmallVectorImpl<char> &LookupTable,
597+
llvm::SmallVectorImpl<char> &ModuleLocalLookupTable);
592598
uint64_t WriteDeclContextLexicalBlock(ASTContext &Context,
593599
const DeclContext *DC);
594-
uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
600+
void WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC,
601+
uint64_t &VisibleBlockOffset,
602+
uint64_t &ModuleLocalBlockOffset);
595603
void WriteTypeDeclOffsets();
596604
void WriteFileDeclIDsMap();
597605
void WriteComments(ASTContext &Context);
@@ -624,7 +632,9 @@ class ASTWriter : public ASTDeserializationListener,
624632
unsigned DeclParmVarAbbrev = 0;
625633
unsigned DeclContextLexicalAbbrev = 0;
626634
unsigned DeclContextVisibleLookupAbbrev = 0;
635+
unsigned DeclModuleLocalVisibleLookupAbbrev = 0;
627636
unsigned UpdateVisibleAbbrev = 0;
637+
unsigned ModuleLocalUpdateVisibleAbbrev = 0;
628638
unsigned DeclRecordAbbrev = 0;
629639
unsigned DeclTypedefAbbrev = 0;
630640
unsigned DeclVarAbbrev = 0;

clang/lib/AST/DeclBase.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@ void Decl::setOwningModuleID(unsigned ID) {
130130
*IDAddress |= (uint64_t)ID << 48;
131131
}
132132

133+
Module *Decl::getTopLevelOwningNamedModule() const {
134+
if (getOwningModule() &&
135+
getOwningModule()->getTopLevelModule()->isNamedModule())
136+
return getOwningModule()->getTopLevelModule();
137+
138+
return nullptr;
139+
}
140+
133141
Module *Decl::getOwningModuleSlow() const {
134142
assert(isFromASTFile() && "Not from AST file?");
135143
return getASTContext().getExternalSource()->getModule(getOwningModuleID());

0 commit comments

Comments
 (0)