Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class DependencyScanningCASFilesystem
public:
static const char ID;

DependencyScanningCASFilesystem(
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> WorkerFS,
llvm::cas::ActionCache &Cache);
DependencyScanningCASFilesystem(DependencyScanningService &Service,
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> WorkerFS);

~DependencyScanningCASFilesystem();

Expand Down Expand Up @@ -109,6 +108,8 @@ class DependencyScanningCASFilesystem

llvm::cas::ObjectStore &CAS;
llvm::cas::ActionCache &Cache;
/// The service associated with this VFS.
DependencyScanningService &Service;
std::optional<llvm::cas::ObjectRef> ClangFullVersionID;
std::optional<llvm::cas::ObjectRef> DepDirectivesID;
std::optional<llvm::cas::ObjectRef> EmptyBlobID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace clang {
namespace tooling {
namespace dependencies {

class DependencyScanningService;

using DependencyDirectivesTy =
SmallVector<dependency_directives_scan::Directive, 20>;

Expand Down Expand Up @@ -387,7 +389,7 @@ class DependencyScanningWorkerFilesystem
static const char ID;

DependencyScanningWorkerFilesystem(
DependencyScanningFilesystemSharedCache &SharedCache,
DependencyScanningService &Service,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS);

llvm::ErrorOr<llvm::vfs::Status> status(const Twine &Path) override;
Expand Down Expand Up @@ -476,10 +478,7 @@ class DependencyScanningWorkerFilesystem
/// Returns entry associated with the unique ID in the shared cache or nullptr
/// if none is found.
const CachedFileSystemEntry *
findSharedEntryByUID(llvm::vfs::Status Stat) const {
return SharedCache.getShardForUID(Stat.getUniqueID())
.findEntryByUID(Stat.getUniqueID());
}
findSharedEntryByUID(llvm::vfs::Status Stat) const;

/// Associates the given entry with the filename in the local cache and
/// returns it.
Expand All @@ -493,20 +492,14 @@ class DependencyScanningWorkerFilesystem
/// some. Otherwise, constructs new one with the given error code, associates
/// it with the filename and returns the result.
const CachedFileSystemEntry &
getOrEmplaceSharedEntryForFilename(StringRef Filename, std::error_code EC) {
return SharedCache.getShardForFilename(Filename)
.getOrEmplaceEntryForFilename(Filename, EC);
}
getOrEmplaceSharedEntryForFilename(StringRef Filename, std::error_code EC);

/// Returns entry associated with the filename in the shared cache if there is
/// some. Otherwise, associates the given entry with the filename and returns
/// it.
const CachedFileSystemEntry &
getOrInsertSharedEntryForFilename(StringRef Filename,
const CachedFileSystemEntry &Entry) {
return SharedCache.getShardForFilename(Filename)
.getOrInsertEntryForFilename(Filename, Entry);
}
const CachedFileSystemEntry &Entry);

void printImpl(raw_ostream &OS, PrintType Type,
unsigned IndentLevel) const override {
Expand All @@ -519,8 +512,9 @@ class DependencyScanningWorkerFilesystem
/// VFS.
bool shouldBypass(StringRef Path) const;

/// The global cache shared between worker threads.
DependencyScanningFilesystemSharedCache &SharedCache;
/// The service associated with this VFS.
DependencyScanningService &Service;

/// The local cache is used by the worker thread to cache file system queries
/// locally instead of querying the global cache every time.
DependencyScanningFilesystemLocalCache LocalCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ enum class ScanningOptimizations {

#undef DSS_LAST_BITMASK_ENUM

bool shouldNegativeStatCacheDefault();

/// The dependency scanning service contains shared configuration and state that
/// is used by the individual dependency scanning workers.
class DependencyScanningService {
Expand All @@ -109,7 +111,8 @@ class DependencyScanningService {
ScanningOptimizations OptimizeArgs = ScanningOptimizations::Default,
bool EagerLoadModules = false, bool TraceVFS = false,
std::time_t BuildSessionTimestamp =
llvm::sys::toTimeT(std::chrono::system_clock::now()));
llvm::sys::toTimeT(std::chrono::system_clock::now()),
bool CacheNegativeStats = shouldNegativeStatCacheDefault());

ScanningMode getMode() const { return Mode; }

Expand All @@ -121,6 +124,8 @@ class DependencyScanningService {

bool shouldTraceVFS() const { return TraceVFS; }

bool shouldCacheNegativeStats() const { return CacheNegativeStats; }

DependencyScanningFilesystemSharedCache &getSharedCache() {
assert(!SharedFS && "Expected not to have a CASFS");
assert(SharedCache && "Expected a shared cache");
Expand Down Expand Up @@ -152,6 +157,7 @@ class DependencyScanningService {
const bool EagerLoadModules;
/// Whether to trace VFS accesses.
const bool TraceVFS;
const bool CacheNegativeStats;
/// Shared CachingOnDiskFileSystem. Set to nullptr to not use CAS dependency
/// scanning.
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> SharedFS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "clang/Tooling/DependencyScanning/DependencyScanningCASFilesystem.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
#include "clang/Basic/Version.h"
#include "clang/Lex/DependencyDirectivesScanner.h"
#include "llvm/CAS/ActionCache.h"
Expand Down Expand Up @@ -35,10 +36,10 @@ static void reportAsFatalIfError(llvm::Error E) {
using llvm::Error;

DependencyScanningCASFilesystem::DependencyScanningCASFilesystem(
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> WorkerFS,
llvm::cas::ActionCache &Cache)
: FS(WorkerFS), Entries(EntryAlloc), CAS(WorkerFS->getCAS()), Cache(Cache) {
}
DependencyScanningService &Service,
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> WorkerFS)
: FS(WorkerFS), Entries(EntryAlloc), CAS(WorkerFS->getCAS()),
Cache(*Service.getCache()), Service(Service) {}

const char DependencyScanningCASFilesystem::ID = 0;
DependencyScanningCASFilesystem::~DependencyScanningCASFilesystem() = default;
Expand Down Expand Up @@ -233,7 +234,7 @@ DependencyScanningCASFilesystem::lookupPath(const Twine &Path) {
llvm::ErrorOr<llvm::vfs::Status> MaybeStatus =
getCachingFS().statusAndFileID(PathRef, FileID);
if (!MaybeStatus) {
if (shouldCacheStatFailures(PathRef))
if (Service.shouldCacheNegativeStats() && shouldCacheStatFailures(PathRef))
Entries[PathRef].EC = MaybeStatus.getError();
return LookupPathResult{nullptr, MaybeStatus.getError()};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
#include "llvm/CAS/CASFileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Threading.h"
Expand Down Expand Up @@ -255,19 +256,19 @@ bool DependencyScanningWorkerFilesystem::shouldBypass(StringRef Path) const {
}

DependencyScanningWorkerFilesystem::DependencyScanningWorkerFilesystem(
DependencyScanningFilesystemSharedCache &SharedCache,
DependencyScanningService &Service,
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS)
: llvm::RTTIExtends<DependencyScanningWorkerFilesystem,
llvm::vfs::ProxyFileSystem>(std::move(FS)),
SharedCache(SharedCache),
WorkingDirForCacheLookup(llvm::errc::invalid_argument) {
Service(Service), WorkingDirForCacheLookup(llvm::errc::invalid_argument) {
updateWorkingDirForCacheLookup();
}

const CachedFileSystemEntry &
DependencyScanningWorkerFilesystem::getOrEmplaceSharedEntryForUID(
TentativeEntry TEntry) {
auto &Shard = SharedCache.getShardForUID(TEntry.Status.getUniqueID());
auto &Shard =
Service.getSharedCache().getShardForUID(TEntry.Status.getUniqueID());
return Shard.getOrEmplaceEntryForUID(
TEntry.Status.getUniqueID(), std::move(TEntry.Status),
std::move(TEntry.Contents), std::move(TEntry.CASContents));
Expand All @@ -278,18 +279,50 @@ DependencyScanningWorkerFilesystem::findEntryByFilenameWithWriteThrough(
StringRef Filename) {
if (const auto *Entry = LocalCache.findEntryByFilename(Filename))
return Entry;
auto &Shard = SharedCache.getShardForFilename(Filename);
auto &Shard = Service.getSharedCache().getShardForFilename(Filename);
if (const auto *Entry = Shard.findEntryByFilename(Filename))
return &LocalCache.insertEntryForFilename(Filename, *Entry);
return nullptr;
}

const CachedFileSystemEntry *
DependencyScanningWorkerFilesystem::findSharedEntryByUID(
llvm::vfs::Status Stat) const {
return Service.getSharedCache()
.getShardForUID(Stat.getUniqueID())
.findEntryByUID(Stat.getUniqueID());
}

const CachedFileSystemEntry &
DependencyScanningWorkerFilesystem::getOrEmplaceSharedEntryForFilename(
StringRef Filename, std::error_code EC) {
return Service.getSharedCache()
.getShardForFilename(Filename)
.getOrEmplaceEntryForFilename(Filename, EC);
}

const CachedFileSystemEntry &
DependencyScanningWorkerFilesystem::getOrInsertSharedEntryForFilename(
StringRef Filename, const CachedFileSystemEntry &Entry) {
return Service.getSharedCache()
.getShardForFilename(Filename)
.getOrInsertEntryForFilename(Filename, Entry);
}

llvm::ErrorOr<const CachedFileSystemEntry &>
DependencyScanningWorkerFilesystem::computeAndStoreResult(
StringRef OriginalFilename, StringRef FilenameForLookup) {
llvm::ErrorOr<llvm::vfs::Status> Stat =
getUnderlyingFS().status(OriginalFilename);
if (!Stat) {
// rdar://148027982
// rdar://127079541
// Negative caching directories can cause build failures due to incorrectly
// configured projects.
StringRef Ext = llvm::sys::path::extension(OriginalFilename);
if (!Service.shouldCacheNegativeStats() || Ext.empty() || Ext == ".framework")
return Stat.getError();

const auto &Entry =
getOrEmplaceSharedEntryForFilename(FilenameForLookup, Stat.getError());
return insertLocalEntryForFilename(FilenameForLookup, Entry);
Expand Down Expand Up @@ -478,7 +511,8 @@ DependencyScanningWorkerFilesystem::getRealPath(const Twine &Path,
return HandleCachedRealPath(*RealPath);

// If we have the result in the shared cache, cache it locally.
auto &Shard = SharedCache.getShardForFilename(*FilenameForLookup);
auto &Shard =
Service.getSharedCache().getShardForFilename(*FilenameForLookup);
if (const auto *ShardRealPath =
Shard.findRealPathByFilename(*FilenameForLookup)) {
const auto &RealPath = LocalCache.insertRealPathForFilename(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,36 @@
#include "llvm/CAS/ActionCache.h"
#include "llvm/CAS/CachingOnDiskFileSystem.h"
#include "llvm/CAS/ObjectStore.h"
#include "llvm/Support/Process.h"

using namespace clang;
using namespace tooling;
using namespace dependencies;

bool clang::tooling::dependencies::shouldNegativeStatCacheDefault() {
if (std::optional<std::string> MaybeNegStats =
llvm::sys::Process::GetEnv("CLANG_SCAN_CACHE_NEGATIVE_STATS")) {
if (MaybeNegStats->empty())
return true;
return llvm::StringSwitch<bool>(*MaybeNegStats)
.Case("1", true)
.Case("0", false)
.Default(false);
}
return false;
}

DependencyScanningService::DependencyScanningService(
ScanningMode Mode, ScanningOutputFormat Format, CASOptions CASOpts,
std::shared_ptr<llvm::cas::ObjectStore> CAS,
std::shared_ptr<llvm::cas::ActionCache> Cache,
IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> SharedFS,
ScanningOptimizations OptimizeArgs, bool EagerLoadModules, bool TraceVFS,
std::time_t BuildSessionTimestamp)
std::time_t BuildSessionTimestamp, bool CacheNegativeStats)
: Mode(Mode), Format(Format), CASOpts(std::move(CASOpts)),
CAS(std::move(CAS)), Cache(std::move(Cache)), OptimizeArgs(OptimizeArgs),
EagerLoadModules(EagerLoadModules), TraceVFS(TraceVFS),
CacheNegativeStats(CacheNegativeStats),
SharedFS(std::move(SharedFS)),
BuildSessionTimestamp(BuildSessionTimestamp) {
if (!this->SharedFS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ DependencyScanningWorker::DependencyScanningWorker(

if (Service.useCASFS()) {
CacheFS = Service.getSharedFS().createProxyFS();
DepCASFS = new DependencyScanningCASFilesystem(CacheFS, *Service.getCache());
DepCASFS = new DependencyScanningCASFilesystem(Service, CacheFS);
BaseFS = DepCASFS;
return;
}

switch (Service.getMode()) {
case ScanningMode::DependencyDirectivesScan:
DepFS = llvm::makeIntrusiveRefCnt<DependencyScanningWorkerFilesystem>(
Service.getSharedCache(), FS);
Service, FS);
BaseFS = DepFS;
break;
case ScanningMode::CanonicalPreprocessing:
Expand Down
10 changes: 7 additions & 3 deletions clang/tools/clang-scan-deps/ClangScanDeps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ static ScanningOutputFormat Format = ScanningOutputFormat::Make;
static ScanningOptimizations OptimizeArgs;
static std::string ModuleFilesDir;
static bool EagerLoadModules;
static bool CacheNegativeStats = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value here is always overwritten in ParseArgs().

static unsigned NumThreads = 0;
static std::string CompilationDB;
static std::optional<std::string> ModuleName;
Expand Down Expand Up @@ -212,6 +213,8 @@ static void ParseArgs(int argc, char **argv) {

EagerLoadModules = Args.hasArg(OPT_eager_load_pcm);

CacheNegativeStats = !Args.hasArg(OPT_no_cache_negative_stats);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means clang-scan-deps has a different default to other clients, why is that?


if (const llvm::opt::Arg *A = Args.getLastArg(OPT_j)) {
StringRef S{A->getValue()};
if (!llvm::to_integer(S, NumThreads, 0)) {
Expand Down Expand Up @@ -1496,9 +1499,10 @@ int clang_scan_deps_main(int argc, char **argv, const llvm::ToolContext &) {
});
};

DependencyScanningService Service(ScanMode, Format, CASOpts, CAS, Cache, FS, OptimizeArgs,
EagerLoadModules,
/*TraceVFS=*/Verbose);
DependencyScanningService Service(
ScanMode, Format, CASOpts, CAS, Cache, FS, OptimizeArgs, EagerLoadModules,
/*TraceVFS=*/Verbose,
llvm::sys::toTimeT(std::chrono::system_clock::now()), CacheNegativeStats);

llvm::Timer T;
T.startTimer();
Expand Down
1 change: 1 addition & 0 deletions clang/tools/clang-scan-deps/Opts.td
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defm module_files_dir : Eq<"module-files-dir",

def optimize_args_EQ : CommaJoined<["-", "--"], "optimize-args=">, HelpText<"Which command-line arguments of modules to optimize">;
def eager_load_pcm : F<"eager-load-pcm", "Load PCM files eagerly (instead of lazily on import)">;
def no_cache_negative_stats : F<"no-cache-negative-stats", "Don't cache stat failures">;

def j : Arg<"j", "Number of worker threads to use (default: use all concurrent threads)">;

Expand Down
Loading