mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-20 12:05:48 -04:00

Clang's internal build system for implicit modules uses lock files to ensure that after a process writes a PCM it will read the same one back in (without contention from other -cc1 commands). Since PCMs are read from disk repeatedly while invalidating, building, and importing, the lock is not released quickly. Furthermore, the LockFileManager is not robust in every environment. Other -cc1 commands can stall until timeout (after about eight minutes). This commit changes the lock file from being necessary for correctness to a (possibly dubious) performance hack. The remaining benefit is to reduce duplicate work in competing -cc1 commands which depend on the same module. Follow-up commits will change the internal build system to continue after a timeout, and reduce the timeout. Perhaps we should reconsider blocking at all. This also fixes a use-after-free, when one part of a compilation validates a PCM and starts using it, and another tries to swap out the PCM for something new. The PCMCache is a new type called MemoryBufferCache, which saves memory buffers based on their filename. Its ownership is shared by the CompilerInstance and ModuleManager. - The ModuleManager stores PCMs there that it loads from disk, never touching the disk if the cache is hot. - When modules fail to validate, they're removed from the cache. - When a CompilerInstance is spawned to build a new module, each already-loaded PCM is assumed to be valid, and is frozen to avoid the use-after-free. - Any newly-built module is written directly to the cache to avoid the round-trip to the filesystem, making lock files unnecessary for correctness. Original patch by Manman Ren; most testcases by Adrian Prantl! llvm-svn: 298165
77 lines
2.5 KiB
C++
77 lines
2.5 KiB
C++
//===--- GeneratePCH.cpp - Sema Consumer for PCH Generation -----*- C++ -*-===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file defines the PCHGenerator, which as a SemaConsumer that generates
|
|
// a PCH file.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
#include "clang/Lex/Preprocessor.h"
|
|
#include "clang/Sema/SemaConsumer.h"
|
|
#include "clang/Serialization/ASTWriter.h"
|
|
#include "llvm/Bitcode/BitstreamWriter.h"
|
|
|
|
using namespace clang;
|
|
|
|
PCHGenerator::PCHGenerator(
|
|
const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
|
|
std::shared_ptr<PCHBuffer> Buffer,
|
|
ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
|
|
bool AllowASTWithErrors, bool IncludeTimestamps)
|
|
: PP(PP), OutputFile(OutputFile), isysroot(isysroot.str()),
|
|
SemaPtr(nullptr), Buffer(Buffer), Stream(Buffer->Data),
|
|
Writer(Stream, Buffer->Data, PP.getPCMCache(), Extensions,
|
|
IncludeTimestamps),
|
|
AllowASTWithErrors(AllowASTWithErrors) {
|
|
Buffer->IsComplete = false;
|
|
}
|
|
|
|
PCHGenerator::~PCHGenerator() {
|
|
}
|
|
|
|
void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
|
|
// Don't create a PCH if there were fatal failures during module loading.
|
|
if (PP.getModuleLoader().HadFatalFailure)
|
|
return;
|
|
|
|
bool hasErrors = PP.getDiagnostics().hasErrorOccurred();
|
|
if (hasErrors && !AllowASTWithErrors)
|
|
return;
|
|
|
|
Module *Module = nullptr;
|
|
if (PP.getLangOpts().isCompilingModule()) {
|
|
Module = PP.getHeaderSearchInfo().lookupModule(
|
|
PP.getLangOpts().CurrentModule, /*AllowSearch*/ false);
|
|
if (!Module) {
|
|
assert(hasErrors && "emitting module but current module doesn't exist");
|
|
return;
|
|
}
|
|
}
|
|
|
|
// Emit the PCH file to the Buffer.
|
|
assert(SemaPtr && "No Sema?");
|
|
Buffer->Signature =
|
|
Writer.WriteAST(*SemaPtr, OutputFile, Module, isysroot,
|
|
// For serialization we are lenient if the errors were
|
|
// only warn-as-error kind.
|
|
PP.getDiagnostics().hasUncompilableErrorOccurred());
|
|
|
|
Buffer->IsComplete = true;
|
|
}
|
|
|
|
ASTMutationListener *PCHGenerator::GetASTMutationListener() {
|
|
return &Writer;
|
|
}
|
|
|
|
ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
|
|
return &Writer;
|
|
}
|