-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[WIP][C++20][Modules] Lazily, but fully load 'HeaderFileInfo' table into memory. #140867
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
mpark
wants to merge
1
commit into
llvm:main
Choose a base branch
from
mpark:full-hfi-load
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4155,6 +4155,7 @@ llvm::Error ASTReader::ReadASTBlock(ModuleFile &F, | |
if (Record[0]) { | ||
F.HeaderFileInfoTable = HeaderFileInfoLookupTable::Create( | ||
(const unsigned char *)F.HeaderFileInfoTableData + Record[0], | ||
(const unsigned char *)F.HeaderFileInfoTableData + sizeof(uint32_t), | ||
(const unsigned char *)F.HeaderFileInfoTableData, | ||
HeaderFileInfoTrait(*this, F)); | ||
|
||
|
@@ -6831,43 +6832,60 @@ std::optional<bool> ASTReader::isPreprocessedEntityInFileID(unsigned Index, | |
return false; | ||
} | ||
|
||
namespace { | ||
|
||
/// Visitor used to search for information about a header file. | ||
class HeaderFileInfoVisitor { | ||
FileEntryRef FE; | ||
std::optional<HeaderFileInfo> HFI; | ||
|
||
public: | ||
explicit HeaderFileInfoVisitor(FileEntryRef FE) : FE(FE) {} | ||
|
||
bool operator()(ModuleFile &M) { | ||
HeaderFileInfoLookupTable *Table | ||
= static_cast<HeaderFileInfoLookupTable *>(M.HeaderFileInfoTable); | ||
if (!Table) | ||
return false; | ||
static void mergeHeaderFileInfoModuleBits(HeaderFileInfo &HFI, | ||
bool isModuleHeader, | ||
bool isTextualModuleHeader) { | ||
HFI.isModuleHeader |= isModuleHeader; | ||
if (HFI.isModuleHeader) | ||
HFI.isTextualModuleHeader = false; | ||
else | ||
HFI.isTextualModuleHeader |= isTextualModuleHeader; | ||
} | ||
|
||
// Look in the on-disk hash table for an entry for this file name. | ||
HeaderFileInfoLookupTable::iterator Pos = Table->find(FE); | ||
if (Pos == Table->end()) | ||
return false; | ||
/// Merge the header file info provided by \p OtherHFI into the current | ||
/// header file info (\p HFI) | ||
static void mergeHeaderFileInfo(HeaderFileInfo &HFI, | ||
const HeaderFileInfo &OtherHFI) { | ||
assert(OtherHFI.External && "expected to merge external HFI"); | ||
|
||
HFI = *Pos; | ||
return true; | ||
} | ||
HFI.isImport |= OtherHFI.isImport; | ||
HFI.isPragmaOnce |= OtherHFI.isPragmaOnce; | ||
mergeHeaderFileInfoModuleBits(HFI, OtherHFI.isModuleHeader, | ||
OtherHFI.isTextualModuleHeader); | ||
|
||
std::optional<HeaderFileInfo> getHeaderFileInfo() const { return HFI; } | ||
}; | ||
if (!HFI.LazyControllingMacro.isValid()) | ||
HFI.LazyControllingMacro = OtherHFI.LazyControllingMacro; | ||
|
||
} // namespace | ||
HFI.DirInfo = OtherHFI.DirInfo; | ||
HFI.External = (!HFI.IsValid || HFI.External); | ||
HFI.IsValid = true; | ||
} | ||
|
||
HeaderFileInfo ASTReader::GetHeaderFileInfo(FileEntryRef FE) { | ||
HeaderFileInfoVisitor Visitor(FE); | ||
ModuleMgr.visit(Visitor); | ||
if (std::optional<HeaderFileInfo> HFI = Visitor.getHeaderFileInfo()) | ||
return *HFI; | ||
|
||
return HeaderFileInfo(); | ||
for (auto Iter = ModuleMgr.begin() + HeaderFileInfoIdx, End = ModuleMgr.end(); | ||
Iter != End; ++Iter) { | ||
if (auto *Table = static_cast<HeaderFileInfoLookupTable *>( | ||
Iter->HeaderFileInfoTable)) { | ||
auto &Info = Table->getInfoObj(); | ||
for (auto Iter = Table->data_begin(), End = Table->data_end(); | ||
Iter != End; ++Iter) { | ||
const auto *Item = Iter.getItem(); | ||
// Determine the length of the key and the data. | ||
const auto &[KeyLen, DataLen] = | ||
HeaderFileInfoTrait::ReadKeyDataLength(Item); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: maybe it is better to wrap such logics in ASTReaderInternals.h |
||
// Read the key. | ||
const auto &Key = Info.ReadKey(Item, KeyLen); | ||
if (auto EKey = Info.getFile(Key)) { | ||
auto Data = Info.ReadData(Key, Item + KeyLen, DataLen); | ||
auto [Iter, Inserted] = HeaderFileInfoLookup.try_emplace(*EKey, Data); | ||
if (!Inserted) | ||
mergeHeaderFileInfo(Iter->second, Data); | ||
} | ||
} | ||
} | ||
} | ||
HeaderFileInfoIdx = ModuleMgr.size(); | ||
return HeaderFileInfoLookup.lookup(FE); | ||
} | ||
|
||
void ASTReader::ReadPragmaDiagnosticMappings(DiagnosticsEngine &Diag) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Unloaded header file info idx.
And also I feel the name is odd. It is index for module files, not header file infos.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And also, this seems unsafe since ModuleManager can remove modules technically. Maybe it is better to insert a bool in ModuleFile to mark if its header info is loaded.