19#include "llvm/ADT/DenseMap.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/SmallString.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Bitstream/BitstreamReader.h"
24#include "llvm/Bitstream/BitstreamWriter.h"
25#include "llvm/Support/DJB.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/IOSandbox.h"
28#include "llvm/Support/LockFileManager.h"
29#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/OnDiskHashTable.h"
31#include "llvm/Support/Path.h"
32#include "llvm/Support/TimeProfiler.h"
33#include "llvm/Support/raw_ostream.h"
44 GLOBAL_INDEX_BLOCK_ID = llvm::bitc::FIRST_APPLICATION_BLOCKID
48 enum IndexRecordTypes {
73class IdentifierIndexReaderTrait {
75 typedef StringRef external_key_type;
76 typedef StringRef internal_key_type;
78 typedef unsigned hash_value_type;
79 typedef unsigned offset_type;
81 static bool EqualKey(
const internal_key_type& a,
const internal_key_type& b) {
85 static hash_value_type
ComputeHash(
const internal_key_type& a) {
86 return llvm::djbHash(a);
89 static std::pair<unsigned, unsigned>
90 ReadKeyDataLength(
const unsigned char*& d) {
91 using namespace llvm::support;
92 unsigned KeyLen = endian::readNext<uint16_t, llvm::endianness::little>(d);
93 unsigned DataLen = endian::readNext<uint16_t, llvm::endianness::little>(d);
94 return std::make_pair(KeyLen, DataLen);
97 static const internal_key_type&
98 GetInternalKey(
const external_key_type& x) {
return x; }
100 static const external_key_type&
101 GetExternalKey(
const internal_key_type& x) {
return x; }
103 static internal_key_type ReadKey(
const unsigned char* d,
unsigned n) {
104 return StringRef((
const char *)d, n);
107 static data_type ReadData(
const internal_key_type& k,
108 const unsigned char* d,
110 using namespace llvm::support;
113 while (DataLen > 0) {
114 unsigned ID = endian::readNext<uint32_t, llvm::endianness::little>(d);
123typedef llvm::OnDiskIterableChainedHashTable<IdentifierIndexReaderTrait>
124 IdentifierIndexTable;
128GlobalModuleIndex::GlobalModuleIndex(
129 std::unique_ptr<llvm::MemoryBuffer> IndexBuffer,
130 llvm::BitstreamCursor Cursor)
131 : Buffer(
std::move(IndexBuffer)), IdentifierIndex(), NumIdentifierLookups(),
132 NumIdentifierLookupHits() {
133 auto Fail = [&](llvm::Error &&Err) {
134 report_fatal_error(
"Module index '" + Buffer->getBufferIdentifier() +
135 "' failed: " +
toString(std::move(Err)));
138 llvm::TimeTraceScope TimeScope(
"Module LoadIndex");
140 bool InGlobalIndexBlock =
false;
143 llvm::BitstreamEntry Entry;
147 Fail(Res.takeError());
149 switch (Entry.Kind) {
150 case llvm::BitstreamEntry::Error:
153 case llvm::BitstreamEntry::EndBlock:
154 if (InGlobalIndexBlock) {
155 InGlobalIndexBlock =
false;
162 case llvm::BitstreamEntry::Record:
164 if (InGlobalIndexBlock)
169 case llvm::BitstreamEntry::SubBlock:
170 if (!InGlobalIndexBlock && Entry.ID == GLOBAL_INDEX_BLOCK_ID) {
171 if (llvm::Error Err =
Cursor.EnterSubBlock(GLOBAL_INDEX_BLOCK_ID))
172 Fail(std::move(Err));
173 InGlobalIndexBlock =
true;
174 }
else if (llvm::Error Err =
Cursor.SkipBlock())
175 Fail(std::move(Err));
183 if (!MaybeIndexRecord)
184 Fail(MaybeIndexRecord.takeError());
185 IndexRecordTypes IndexRecord =
186 static_cast<IndexRecordTypes
>(MaybeIndexRecord.get());
187 switch (IndexRecord) {
196 unsigned ID =
Record[Idx++];
199 if (ID == Modules.size())
200 Modules.push_back(ModuleInfo());
202 Modules.resize(ID + 1);
206 Modules[ID].Size =
Record[Idx++];
207 Modules[ID].ModTime =
Record[Idx++];
210 unsigned NameLen =
Record[Idx++];
211 Modules[ID].FileName.assign(
Record.begin() + Idx,
212 Record.begin() + Idx + NameLen);
216 unsigned NumDeps =
Record[Idx++];
217 Modules[ID].Dependencies.insert(Modules[ID].Dependencies.end(),
219 Record.begin() + Idx + NumDeps);
223 assert(Idx ==
Record.size() &&
"More module info?");
228 StringRef ModuleName = llvm::sys::path::stem(Modules[ID].
FileName);
230 ModuleName = ModuleName.rsplit(
'-').first;
231 UnresolvedModules[ModuleName] = ID;
235 case IDENTIFIER_INDEX:
238 IdentifierIndex = IdentifierIndexTable::Create(
239 (
const unsigned char *)Blob.data() +
Record[0],
240 (
const unsigned char *)Blob.data() +
sizeof(
uint32_t),
241 (
const unsigned char *)Blob.data(), IdentifierIndexReaderTrait());
249 delete static_cast<IdentifierIndexTable *
>(IdentifierIndex);
252std::pair<GlobalModuleIndex *, llvm::Error>
255 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
262 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> BufferOrErr =
263 llvm::MemoryBuffer::getFile(IndexPath.c_str());
265 return std::make_pair(
nullptr,
266 llvm::errorCodeToError(BufferOrErr.getError()));
267 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(BufferOrErr.get());
270 llvm::BitstreamCursor Cursor(*Buffer);
273 for (
unsigned char C : {
'B',
'C',
'G',
'I'}) {
276 return std::make_pair(
277 nullptr, llvm::createStringError(std::errc::illegal_byte_sequence,
278 "expected signature BCGI"));
280 return std::make_pair(
nullptr, Res.takeError());
283 return std::make_pair(
new GlobalModuleIndex(std::move(Buffer), std::move(Cursor)),
284 llvm::Error::success());
291 llvm::DenseMap<ModuleFile *, unsigned>::iterator
Known
292 = ModulesByFile.find(
File);
293 if (
Known == ModulesByFile.end())
297 Dependencies.clear();
299 for (
unsigned I = 0, N = StoredDependencies.size(); I != N; ++I) {
300 if (ModuleFile *MF = Modules[I].
File)
301 Dependencies.push_back(MF);
309 if (!IdentifierIndex)
313 ++NumIdentifierLookups;
314 IdentifierIndexTable &Table
315 = *
static_cast<IdentifierIndexTable *
>(IdentifierIndex);
316 IdentifierIndexTable::iterator
Known = Table.find(Name);
317 if (
Known == Table.end()) {
321 for (
unsigned ModuleID : *
Known) {
322 if (ModuleFile *MF = Modules[ModuleID].
File)
326 ++NumIdentifierLookupHits;
332 StringRef Name =
File->ModuleName;
333 llvm::StringMap<unsigned>::iterator
Known = UnresolvedModules.find(Name);
334 if (
Known == UnresolvedModules.end()) {
339 ModuleInfo &Info = Modules[
Known->second];
344 if (
File->Size == Info.Size &&
File->ModTime == Info.ModTime) {
352 UnresolvedModules.erase(
Known);
357 std::fprintf(
stderr,
"*** Global Module Index Statistics:\n");
358 if (NumIdentifierLookups) {
359 fprintf(
stderr,
" %u / %u identifier lookups succeeded (%f%%)\n",
360 NumIdentifierLookupHits, NumIdentifierLookups,
361 (
double)NumIdentifierLookupHits*100.0/NumIdentifierLookups);
363 std::fprintf(
stderr,
"\n");
367 llvm::errs() <<
"*** Global Module Index Dump:\n";
368 llvm::errs() <<
"Module files:\n";
369 for (
auto &MI : Modules) {
370 llvm::errs() <<
"** " << MI.FileName <<
"\n";
374 llvm::errs() <<
"\n";
376 llvm::errs() <<
"\n";
385 struct ModuleFileInfo {
395 struct ImportedModuleFileInfo {
397 time_t StoredModTime;
400 : StoredSize(Size), StoredModTime(ModTime), StoredSignature(Sig) {}
404 class GlobalModuleIndexBuilder {
405 FileManager &FileMgr;
406 const PCHContainerReader &PCHContainerRdr;
409 using ModuleFilesMap = llvm::MapVector<FileEntryRef, ModuleFileInfo>;
412 ModuleFilesMap ModuleFiles;
416 using ImportedModuleFilesMap =
417 std::multimap<FileEntryRef, ImportedModuleFileInfo>;
420 ImportedModuleFilesMap ImportedModuleFiles;
424 typedef llvm::StringMap<SmallVector<unsigned, 2> > InterestingIdentifierMap;
428 InterestingIdentifierMap InterestingIdentifiers;
431 void emitBlockInfoBlock(llvm::BitstreamWriter &Stream);
435 auto [It, Inserted] = ModuleFiles.try_emplace(
File);
437 unsigned NewID = ModuleFiles.size();
445 explicit GlobalModuleIndexBuilder(
446 FileManager &FileMgr,
const PCHContainerReader &PCHContainerRdr)
447 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr) {}
450 llvm::Error loadModuleFile(FileEntryRef
File);
454 bool writeIndex(llvm::BitstreamWriter &Stream);
459 llvm::BitstreamWriter &Stream,
463 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID,
Record);
466 if (!Name || Name[0] == 0)
return;
469 Record.push_back(*Name++);
470 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME,
Record);
474 llvm::BitstreamWriter &Stream,
479 Record.push_back(*Name++);
480 Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME,
Record);
484GlobalModuleIndexBuilder::emitBlockInfoBlock(llvm::BitstreamWriter &Stream) {
486 Stream.EnterBlockInfoBlock();
488#define BLOCK(X) emitBlockID(X ## _ID, #X, Stream, Record)
489#define RECORD(X) emitRecordID(X, #X, Stream, Record)
490 BLOCK(GLOBAL_INDEX_BLOCK);
501 class InterestingASTIdentifierLookupTrait
506 typedef std::pair<StringRef, bool> data_type;
508 data_type ReadData(
const internal_key_type& k,
509 const unsigned char* d,
513 using namespace llvm::support;
515 endian::readNext<IdentifierID, llvm::endianness::little>(d);
516 bool IsInteresting = RawID & 0x01;
517 return std::make_pair(k, IsInteresting);
525 auto Buffer =
FileMgr.getBufferForFile(
File,
true);
527 return llvm::createStringError(Buffer.getError(),
528 "failed getting buffer for module file");
531 std::unique_ptr<llvm::MemoryBuffer> ASTBuffer =
532 PCHContainerRdr.
ExtractPCH(std::move(*Buffer));
535 llvm::BitstreamCursor InStream(ASTBuffer->getBuffer());
538 for (
unsigned char C : {
'C',
'P',
'C',
'H'})
541 return llvm::createStringError(std::errc::illegal_byte_sequence,
542 "expected signature CPCH");
544 return Res.takeError();
548 unsigned ID = getModuleFileInfo(
File).ID;
551 enum {
Other, ControlBlock, ASTBlock, DiagnosticOptionsBlock } State =
Other;
556 return MaybeEntry.takeError();
557 llvm::BitstreamEntry Entry = MaybeEntry.get();
559 switch (Entry.Kind) {
560 case llvm::BitstreamEntry::Error:
564 case llvm::BitstreamEntry::Record:
566 if (State ==
Other) {
570 return Skipped.takeError();
576 case llvm::BitstreamEntry::SubBlock:
582 State = ControlBlock;
587 if (llvm::Error Err = InStream.EnterSubBlock(
AST_BLOCK_ID))
600 State = DiagnosticOptionsBlock;
604 if (llvm::Error Err = InStream.SkipBlock())
609 case llvm::BitstreamEntry::EndBlock:
619 return MaybeCode.takeError();
620 unsigned Code = MaybeCode.get();
623 if (State == ControlBlock && Code ==
IMPORT) {
635 Blob = Blob.substr(
Record[Idx++]);
638 bool IsCXX20ModuleOrPrebuiltHeaderUnit =
Record[Idx++];
641 if (IsCXX20ModuleOrPrebuiltHeaderUnit)
645 off_t StoredSize = (off_t)
Record[Idx++];
646 time_t StoredModTime = (time_t)
Record[Idx++];
653 SignatureBytes.end());
657 unsigned Length =
Record[Idx++];
658 StringRef ImportedFile = Blob.substr(0, Length);
659 Blob = Blob.substr(Length);
663 FileMgr.getOptionalFileRef(ImportedFile,
false,
667 return llvm::createStringError(std::errc::bad_file_descriptor,
668 "imported file \"%s\" not found",
669 std::string(ImportedFile).c_str());
673 ImportedModuleFiles.insert(std::make_pair(
674 *DependsOnFile, ImportedModuleFileInfo(StoredSize, StoredModTime,
678 unsigned DependsOnID = getModuleFileInfo(*DependsOnFile).ID;
679 getModuleFileInfo(
File).Dependencies.push_back(DependsOnID);
686 typedef llvm::OnDiskIterableChainedHashTable<
687 InterestingASTIdentifierLookupTrait> InterestingIdentifierTable;
688 std::unique_ptr<InterestingIdentifierTable> Table(
689 InterestingIdentifierTable::Create(
690 (
const unsigned char *)Blob.data() +
Record[0],
691 (
const unsigned char *)Blob.data() +
sizeof(
uint32_t),
692 (
const unsigned char *)Blob.data()));
693 for (InterestingIdentifierTable::data_iterator D = Table->data_begin(),
694 DEnd = Table->data_end();
696 std::pair<StringRef, bool> Ident = *D;
698 InterestingIdentifiers[Ident.first].push_back(ID);
700 (
void)InterestingIdentifiers[Ident.first];
705 if (State == DiagnosticOptionsBlock && Code ==
SIGNATURE) {
708 "Dummy AST file signature not backpatched in ASTWriter.");
709 getModuleFileInfo(
File).Signature = Signature;
715 return llvm::Error::success();
722class IdentifierIndexWriterTrait {
724 typedef StringRef key_type;
725 typedef StringRef key_type_ref;
726 typedef SmallVector<unsigned, 2> data_type;
727 typedef const SmallVector<unsigned, 2> &data_type_ref;
728 typedef unsigned hash_value_type;
729 typedef unsigned offset_type;
731 static hash_value_type
ComputeHash(key_type_ref Key) {
732 return llvm::djbHash(Key);
735 std::pair<unsigned,unsigned>
736 EmitKeyDataLength(raw_ostream& Out, key_type_ref Key, data_type_ref
Data) {
737 using namespace llvm::support;
738 endian::Writer
LE(Out, llvm::endianness::little);
739 unsigned KeyLen = Key.size();
740 unsigned DataLen =
Data.size() * 4;
743 return std::make_pair(KeyLen, DataLen);
746 void EmitKey(raw_ostream& Out, key_type_ref Key,
unsigned KeyLen) {
747 Out.write(Key.data(), KeyLen);
750 void EmitData(raw_ostream& Out, key_type_ref Key, data_type_ref
Data,
752 using namespace llvm::support;
753 for (
unsigned I = 0, N =
Data.size(); I != N; ++I)
754 endian::write<uint32_t>(Out,
Data[I], llvm::endianness::little);
760bool GlobalModuleIndexBuilder::writeIndex(llvm::BitstreamWriter &Stream) {
761 for (
auto MapEntry : ImportedModuleFiles) {
762 auto File = MapEntry.first;
763 ImportedModuleFileInfo &Info = MapEntry.second;
764 if (getModuleFileInfo(
File).Signature) {
765 if (getModuleFileInfo(
File).Signature != Info.StoredSignature)
768 }
else if (Info.StoredSize !=
File.getSize() ||
769 Info.StoredModTime !=
File.getModificationTime())
774 using namespace llvm;
775 llvm::TimeTraceScope TimeScope(
"Module WriteIndex");
778 Stream.Emit((
unsigned)
'B', 8);
779 Stream.Emit((
unsigned)
'C', 8);
780 Stream.Emit((
unsigned)
'G', 8);
781 Stream.Emit((
unsigned)
'I', 8);
785 emitBlockInfoBlock(Stream);
787 Stream.EnterSubblock(GLOBAL_INDEX_BLOCK_ID, 3);
792 Stream.EmitRecord(INDEX_METADATA,
Record);
795 for (ModuleFilesMap::iterator M = ModuleFiles.begin(),
796 MEnd = ModuleFiles.end();
799 Record.push_back(M->second.ID);
800 Record.push_back(M->first.getSize());
801 Record.push_back(M->first.getModificationTime());
804 StringRef Name(M->first.getName());
805 Record.push_back(Name.size());
806 Record.append(Name.begin(), Name.end());
809 Record.push_back(M->second.Dependencies.size());
810 Record.append(M->second.Dependencies.begin(), M->second.Dependencies.end());
811 Stream.EmitRecord(MODULE,
Record);
816 llvm::OnDiskChainedHashTableGenerator<IdentifierIndexWriterTrait>
Generator;
817 IdentifierIndexWriterTrait Trait;
820 for (InterestingIdentifierMap::iterator I = InterestingIdentifiers.begin(),
821 IEnd = InterestingIdentifiers.end();
823 Generator.insert(I->first(), I->second, Trait);
830 using namespace llvm::support;
833 endian::write<uint32_t>(Out, 0, llvm::endianness::little);
834 BucketOffset =
Generator.Emit(Out, Trait);
838 auto Abbrev = std::make_shared<BitCodeAbbrev>();
839 Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_INDEX));
840 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
841 Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
842 unsigned IDTableAbbrev = Stream.EmitAbbrev(std::move(Abbrev));
858 auto BypassSandbox = llvm::sys::sandbox::scopedDisable();
866 llvm::LockFileManager Lock(IndexPath);
868 if (llvm::Error Err = Lock.tryLock().moveInto(Owned)) {
869 llvm::consumeError(std::move(Err));
870 return llvm::createStringError(std::errc::io_error,
"LFS error");
875 return llvm::createStringError(std::errc::device_or_resource_busy,
876 "someone else is building the index");
882 GlobalModuleIndexBuilder Builder(
FileMgr, PCHContainerRdr);
886 for (llvm::sys::fs::directory_iterator D(Path, EC), DEnd;
890 if (llvm::sys::path::extension(D->path()) !=
".pcm") {
894 if (StringRef(D->path()).ends_with(
".pcm.lock"))
895 return llvm::createStringError(std::errc::device_or_resource_busy,
896 "someone else is building the index");
902 auto ModuleFile =
FileMgr.getOptionalFileRef(D->path());
907 if (llvm::Error Err = Builder.loadModuleFile(*ModuleFile))
914 llvm::BitstreamWriter OutputStream(OutputBuffer);
915 if (Builder.writeIndex(OutputStream))
916 return llvm::createStringError(std::errc::io_error,
917 "failed writing index");
920 return llvm::writeToOutput(IndexPath, [&OutputBuffer](llvm::raw_ostream &OS) {
922 return llvm::Error::success();
929 IdentifierIndexTable::key_iterator Current;
932 IdentifierIndexTable::key_iterator End;
935 explicit GlobalIndexIdentifierIterator(IdentifierIndexTable &Idx) {
936 Current = Idx.key_begin();
940 StringRef
Next()
override {
944 StringRef
Result = *Current;
952 IdentifierIndexTable &Table =
953 *
static_cast<IdentifierIndexTable *
>(IdentifierIndex);
954 return new GlobalIndexIdentifierIterator(Table);
#define RECORD(CLASS, BASE)
Defines the clang::FileManager interface and associated types.
static void emitRecordID(unsigned ID, const char *Name, llvm::BitstreamWriter &Stream, SmallVectorImpl< uint64_t > &Record)
static const unsigned CurrentVersion
The global index file version.
static const char *const IndexFileName
The name of the global index file.
static void emitBlockID(unsigned ID, const char *Name, llvm::BitstreamWriter &Stream, SmallVectorImpl< uint64_t > &Record)
Result
Implement __builtin_bit_cast and related operations.
llvm::MachO::Record Record
static std::string toString(const clang::SanitizerSet &Sanitizers)
Produce a string containing comma-separated names of sanitizers in Sanitizers set.
#define IMPORT(DERIVED, BASE)
#define BLOCK(DERIVED, BASE)
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Implements support for file system lookup, file system caching, and directory search management.
bool loadedModuleFile(ModuleFile *File)
Note that the given module file has been loaded.
void printStats()
Print statistics to standard error.
llvm::SmallPtrSet< ModuleFile *, 4 > HitSet
A set of module files in which we found a result.
bool lookupIdentifier(llvm::StringRef Name, HitSet &Hits)
Look for all of the module files with information about the given identifier, e.g....
void getModuleDependencies(ModuleFile *File, llvm::SmallVectorImpl< ModuleFile * > &Dependencies)
Retrieve the set of module files on which the given module file directly depends.
IdentifierIterator * createIdentifierIterator() const
Returns an iterator for identifiers stored in the index table.
static std::pair< GlobalModuleIndex *, llvm::Error > readIndex(llvm::StringRef Path)
Read a global index file for the given directory.
void dump()
Print debugging view to standard error.
static llvm::Error writeIndex(FileManager &FileMgr, const PCHContainerReader &PCHContainerRdr, llvm::StringRef Path)
Write a global index into the given.
An iterator that walks over all of the known identifiers in the lookup table.
Implements an efficient mapping from strings to IdentifierInfo nodes.
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
virtual std::unique_ptr< llvm::MemoryBuffer > ExtractPCH(std::unique_ptr< llvm::MemoryBuffer > Buffer) const =0
Returns a buffer with the serialized AST inside the PCH container Buffer.
Base class for the trait describing the on-disk hash table for the identifiers in an AST file.
@ ModuleFileInfo
Dump information about a module file.
bool LE(InterpState &S, CodePtr OpPC)
@ AST_BLOCK_ID
The AST block, which acts as a container around the full AST block.
@ CONTROL_BLOCK_ID
The control block, which contains all of the information that needs to be validated prior to committi...
@ UNHASHED_CONTROL_BLOCK_ID
A block with unhashed content.
@ SIGNATURE
Record code for the signature that identifiers this AST file.
@ IDENTIFIER_TABLE
Record code for the identifier table.
unsigned ComputeHash(Selector Sel)
uint64_t IdentifierID
An ID number that refers to an identifier in an AST file.
Top level wrappers for InstallAPI frontend operations.
int32_t uint32_t uint32_t __builtin_riscv_mhracc_i32 __builtin_riscv_mhraccu_u32 __builtin_riscv_mhraccsu_i32 __builtin_riscv_mhacc_h1_i32 __builtin_riscv_mhaccsu_h1_i32 __builtin_riscv_mulqr_i32 __builtin_riscv_mqrwacc_i64 __packed_splat4 __packed_splat2 __packed_splat8 __packed_splat4 __packed_splat2 __packed_splat4 uint16_t
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat uint32_t uint64_t __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
__LIBC_ATTRS FILE * stderr
The signature of a module, which is a hash of the AST content.
static constexpr size_t size
static ASTFileSignature create(std::array< uint8_t, 20 > Bytes)
static ASTFileSignature createDummy()