2019-08-07 04:43:25 +08:00
|
|
|
//===- DependencyScanningFilesystem.cpp - clang-scan-deps fs --------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Tooling/DependencyScanning/DependencyScanningFilesystem.h"
|
|
|
|
#include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/Threading.h"
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
using namespace tooling;
|
|
|
|
using namespace dependencies;
|
|
|
|
|
|
|
|
CachedFileSystemEntry CachedFileSystemEntry::createFileEntry(
|
|
|
|
StringRef Filename, llvm::vfs::FileSystem &FS, bool Minimize) {
|
|
|
|
// Load the file and its content from the file system.
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> MaybeFile =
|
|
|
|
FS.openFileForRead(Filename);
|
|
|
|
if (!MaybeFile)
|
|
|
|
return MaybeFile.getError();
|
|
|
|
llvm::ErrorOr<llvm::vfs::Status> Stat = (*MaybeFile)->status();
|
|
|
|
if (!Stat)
|
|
|
|
return Stat.getError();
|
|
|
|
|
|
|
|
llvm::vfs::File &F = **MaybeFile;
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MaybeBuffer =
|
|
|
|
F.getBuffer(Stat->getName());
|
|
|
|
if (!MaybeBuffer)
|
|
|
|
return MaybeBuffer.getError();
|
|
|
|
|
|
|
|
llvm::SmallString<1024> MinimizedFileContents;
|
|
|
|
// Minimize the file down to directives that might affect the dependencies.
|
|
|
|
const auto &Buffer = *MaybeBuffer;
|
|
|
|
SmallVector<minimize_source_to_dependency_directives::Token, 64> Tokens;
|
|
|
|
if (!Minimize || minimizeSourceToDependencyDirectives(
|
|
|
|
Buffer->getBuffer(), MinimizedFileContents, Tokens)) {
|
|
|
|
// Use the original file unless requested otherwise, or
|
|
|
|
// if the minimization failed.
|
|
|
|
// FIXME: Propage the diagnostic if desired by the client.
|
|
|
|
CachedFileSystemEntry Result;
|
|
|
|
Result.MaybeStat = std::move(*Stat);
|
|
|
|
Result.Contents.reserve(Buffer->getBufferSize() + 1);
|
|
|
|
Result.Contents.append(Buffer->getBufferStart(), Buffer->getBufferEnd());
|
|
|
|
// Implicitly null terminate the contents for Clang's lexer.
|
|
|
|
Result.Contents.push_back('\0');
|
|
|
|
Result.Contents.pop_back();
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedFileSystemEntry Result;
|
|
|
|
size_t Size = MinimizedFileContents.size();
|
|
|
|
Result.MaybeStat = llvm::vfs::Status(Stat->getName(), Stat->getUniqueID(),
|
|
|
|
Stat->getLastModificationTime(),
|
|
|
|
Stat->getUser(), Stat->getGroup(), Size,
|
|
|
|
Stat->getType(), Stat->getPermissions());
|
|
|
|
// The contents produced by the minimizer must be null terminated.
|
|
|
|
assert(MinimizedFileContents.data()[MinimizedFileContents.size()] == '\0' &&
|
|
|
|
"not null terminated contents");
|
|
|
|
// Even though there's an implicit null terminator in the minimized contents,
|
|
|
|
// we want to temporarily make it explicit. This will ensure that the
|
|
|
|
// std::move will preserve it even if it needs to do a copy if the
|
|
|
|
// SmallString still has the small capacity.
|
|
|
|
MinimizedFileContents.push_back('\0');
|
|
|
|
Result.Contents = std::move(MinimizedFileContents);
|
|
|
|
// Now make the null terminator implicit again, so that Clang's lexer can find
|
|
|
|
// it right where the buffer ends.
|
|
|
|
Result.Contents.pop_back();
|
2019-09-12 04:40:31 +08:00
|
|
|
|
|
|
|
// Compute the skipped PP ranges that speedup skipping over inactive
|
|
|
|
// preprocessor blocks.
|
|
|
|
llvm::SmallVector<minimize_source_to_dependency_directives::SkippedRange, 32>
|
|
|
|
SkippedRanges;
|
|
|
|
minimize_source_to_dependency_directives::computeSkippedRanges(Tokens,
|
|
|
|
SkippedRanges);
|
|
|
|
PreprocessorSkippedRangeMapping Mapping;
|
|
|
|
for (const auto &Range : SkippedRanges) {
|
|
|
|
if (Range.Length < 16) {
|
|
|
|
// Ignore small ranges as non-profitable.
|
|
|
|
// FIXME: This is a heuristic, its worth investigating the tradeoffs
|
|
|
|
// when it should be applied.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Mapping[Range.Offset] = Range.Length;
|
|
|
|
}
|
|
|
|
Result.PPSkippedRangeMapping = std::move(Mapping);
|
|
|
|
|
2019-08-07 04:43:25 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CachedFileSystemEntry
|
|
|
|
CachedFileSystemEntry::createDirectoryEntry(llvm::vfs::Status &&Stat) {
|
|
|
|
assert(Stat.isDirectory() && "not a directory!");
|
|
|
|
auto Result = CachedFileSystemEntry();
|
|
|
|
Result.MaybeStat = std::move(Stat);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2021-07-20 17:42:34 +08:00
|
|
|
DependencyScanningFilesystemSharedCache::SingleCache::SingleCache() {
|
2019-08-07 04:43:25 +08:00
|
|
|
// This heuristic was chosen using a empirical testing on a
|
|
|
|
// reasonably high core machine (iMacPro 18 cores / 36 threads). The cache
|
|
|
|
// sharding gives a performance edge by reducing the lock contention.
|
|
|
|
// FIXME: A better heuristic might also consider the OS to account for
|
|
|
|
// the different cost of lock contention on different OSes.
|
[Support] On Windows, ensure hardware_concurrency() extends to all CPU sockets and all NUMA groups
The goal of this patch is to maximize CPU utilization on multi-socket or high core count systems, so that parallel computations such as LLD/ThinLTO can use all hardware threads in the system. Before this patch, on Windows, a maximum of 64 hardware threads could be used at most, in some cases dispatched only on one CPU socket.
== Background ==
Windows doesn't have a flat cpu_set_t like Linux. Instead, it projects hardware CPUs (or NUMA nodes) to applications through a concept of "processor groups". A "processor" is the smallest unit of execution on a CPU, that is, an hyper-thread if SMT is active; a core otherwise. There's a limit of 32-bit processors on older 32-bit versions of Windows, which later was raised to 64-processors with 64-bit versions of Windows. This limit comes from the affinity mask, which historically is represented by the sizeof(void*). Consequently, the concept of "processor groups" was introduced for dealing with systems with more than 64 hyper-threads.
By default, the Windows OS assigns only one "processor group" to each starting application, in a round-robin manner. If the application wants to use more processors, it needs to programmatically enable it, by assigning threads to other "processor groups". This also means that affinity cannot cross "processor group" boundaries; one can only specify a "preferred" group on start-up, but the application is free to allocate more groups if it wants to.
This creates a peculiar situation, where newer CPUs like the AMD EPYC 7702P (64-cores, 128-hyperthreads) are projected by the OS as two (2) "processor groups". This means that by default, an application can only use half of the cores. This situation could only get worse in the years to come, as dies with more cores will appear on the market.
== The problem ==
The heavyweight_hardware_concurrency() API was introduced so that only *one hardware thread per core* was used. Once that API returns, that original intention is lost, only the number of threads is retained. Consider a situation, on Windows, where the system has 2 CPU sockets, 18 cores each, each core having 2 hyper-threads, for a total of 72 hyper-threads. Both heavyweight_hardware_concurrency() and hardware_concurrency() currently return 36, because on Windows they are simply wrappers over std::thread::hardware_concurrency() -- which can only return processors from the current "processor group".
== The changes in this patch ==
To solve this situation, we capture (and retain) the initial intention until the point of usage, through a new ThreadPoolStrategy class. The number of threads to use is deferred as late as possible, until the moment where the std::threads are created (ThreadPool in the case of ThinLTO).
When using hardware_concurrency(), setting ThreadCount to 0 now means to use all the possible hardware CPU (SMT) threads. Providing a ThreadCount above to the maximum number of threads will have no effect, the maximum will be used instead.
The heavyweight_hardware_concurrency() is similar to hardware_concurrency(), except that only one thread per hardware *core* will be used.
When LLVM_ENABLE_THREADS is OFF, the threading APIs will always return 1, to ensure any caller loops will be exercised at least once.
Differential Revision: https://reviews.llvm.org/D71775
2020-02-14 11:49:57 +08:00
|
|
|
NumShards =
|
|
|
|
std::max(2u, llvm::hardware_concurrency().compute_thread_count() / 4);
|
2019-08-15 07:04:18 +08:00
|
|
|
CacheShards = std::make_unique<CacheShard[]>(NumShards);
|
2019-08-07 04:43:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
DependencyScanningFilesystemSharedCache::SharedFileSystemEntry &
|
2021-07-20 17:42:34 +08:00
|
|
|
DependencyScanningFilesystemSharedCache::SingleCache::get(StringRef Key) {
|
2019-08-07 04:43:25 +08:00
|
|
|
CacheShard &Shard = CacheShards[llvm::hash_value(Key) % NumShards];
|
|
|
|
std::unique_lock<std::mutex> LockGuard(Shard.CacheLock);
|
|
|
|
auto It = Shard.Cache.try_emplace(Key);
|
|
|
|
return It.first->getValue();
|
|
|
|
}
|
|
|
|
|
2021-07-20 17:42:34 +08:00
|
|
|
DependencyScanningFilesystemSharedCache::SharedFileSystemEntry &
|
|
|
|
DependencyScanningFilesystemSharedCache::get(StringRef Key, bool Minimized) {
|
|
|
|
SingleCache &Cache = Minimized ? CacheMinimized : CacheOriginal;
|
|
|
|
return Cache.get(Key);
|
|
|
|
}
|
|
|
|
|
2019-10-11 05:40:17 +08:00
|
|
|
/// Whitelist file extensions that should be minimized, treating no extension as
|
|
|
|
/// a source file that should be minimized.
|
|
|
|
///
|
|
|
|
/// This is kinda hacky, it would be better if we knew what kind of file Clang
|
|
|
|
/// was expecting instead.
|
|
|
|
static bool shouldMinimize(StringRef Filename) {
|
|
|
|
StringRef Ext = llvm::sys::path::extension(Filename);
|
|
|
|
if (Ext.empty())
|
|
|
|
return true; // C++ standard library
|
|
|
|
return llvm::StringSwitch<bool>(Ext)
|
|
|
|
.CasesLower(".c", ".cc", ".cpp", ".c++", ".cxx", true)
|
|
|
|
.CasesLower(".h", ".hh", ".hpp", ".h++", ".hxx", true)
|
|
|
|
.CasesLower(".m", ".mm", true)
|
|
|
|
.CasesLower(".i", ".ii", ".mi", ".mmi", true)
|
|
|
|
.CasesLower(".def", ".inc", true)
|
|
|
|
.Default(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool shouldCacheStatFailures(StringRef Filename) {
|
|
|
|
StringRef Ext = llvm::sys::path::extension(Filename);
|
|
|
|
if (Ext.empty())
|
|
|
|
return false; // This may be the module cache directory.
|
|
|
|
return shouldMinimize(Filename); // Only cache stat failures on source files.
|
|
|
|
}
|
|
|
|
|
2021-07-20 16:57:12 +08:00
|
|
|
void DependencyScanningWorkerFilesystem::ignoreFile(StringRef RawFilename) {
|
|
|
|
llvm::SmallString<256> Filename;
|
|
|
|
llvm::sys::path::native(RawFilename, Filename);
|
|
|
|
IgnoredFiles.insert(Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DependencyScanningWorkerFilesystem::shouldIgnoreFile(
|
|
|
|
StringRef RawFilename) {
|
|
|
|
llvm::SmallString<256> Filename;
|
|
|
|
llvm::sys::path::native(RawFilename, Filename);
|
|
|
|
return IgnoredFiles.contains(Filename);
|
|
|
|
}
|
|
|
|
|
2019-10-10 23:29:01 +08:00
|
|
|
llvm::ErrorOr<const CachedFileSystemEntry *>
|
2019-10-11 04:19:02 +08:00
|
|
|
DependencyScanningWorkerFilesystem::getOrCreateFileSystemEntry(
|
|
|
|
const StringRef Filename) {
|
2021-07-20 19:17:45 +08:00
|
|
|
bool ShouldMinimize = !shouldIgnoreFile(Filename) && shouldMinimize(Filename);
|
2021-07-20 17:42:34 +08:00
|
|
|
|
|
|
|
if (const auto *Entry = Cache.getCachedEntry(Filename, ShouldMinimize))
|
2019-10-10 23:29:01 +08:00
|
|
|
return Entry;
|
2019-08-07 04:43:25 +08:00
|
|
|
|
|
|
|
// FIXME: Handle PCM/PCH files.
|
|
|
|
// FIXME: Handle module map files.
|
|
|
|
|
|
|
|
DependencyScanningFilesystemSharedCache::SharedFileSystemEntry
|
2021-07-20 17:42:34 +08:00
|
|
|
&SharedCacheEntry = SharedCache.get(Filename, ShouldMinimize);
|
2019-08-07 04:43:25 +08:00
|
|
|
const CachedFileSystemEntry *Result;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> LockGuard(SharedCacheEntry.ValueLock);
|
|
|
|
CachedFileSystemEntry &CacheEntry = SharedCacheEntry.Value;
|
|
|
|
|
|
|
|
if (!CacheEntry.isValid()) {
|
|
|
|
llvm::vfs::FileSystem &FS = getUnderlyingFS();
|
|
|
|
auto MaybeStatus = FS.status(Filename);
|
2019-10-11 05:40:17 +08:00
|
|
|
if (!MaybeStatus) {
|
|
|
|
if (!shouldCacheStatFailures(Filename))
|
|
|
|
// HACK: We need to always restat non source files if the stat fails.
|
|
|
|
// This is because Clang first looks up the module cache and module
|
|
|
|
// files before building them, and then looks for them again. If we
|
|
|
|
// cache the stat failure, it won't see them the second time.
|
|
|
|
return MaybeStatus.getError();
|
|
|
|
else
|
|
|
|
CacheEntry = CachedFileSystemEntry(MaybeStatus.getError());
|
|
|
|
} else if (MaybeStatus->isDirectory())
|
2019-08-07 04:43:25 +08:00
|
|
|
CacheEntry = CachedFileSystemEntry::createDirectoryEntry(
|
|
|
|
std::move(*MaybeStatus));
|
|
|
|
else
|
2021-07-20 17:42:34 +08:00
|
|
|
CacheEntry = CachedFileSystemEntry::createFileEntry(Filename, FS,
|
|
|
|
ShouldMinimize);
|
2019-08-07 04:43:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Result = &CacheEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the result in the local cache.
|
2021-07-20 17:42:34 +08:00
|
|
|
Cache.setCachedEntry(Filename, ShouldMinimize, Result);
|
2019-10-10 23:29:01 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<llvm::vfs::Status>
|
|
|
|
DependencyScanningWorkerFilesystem::status(const Twine &Path) {
|
|
|
|
SmallString<256> OwnedFilename;
|
|
|
|
StringRef Filename = Path.toStringRef(OwnedFilename);
|
2019-10-11 04:19:02 +08:00
|
|
|
const llvm::ErrorOr<const CachedFileSystemEntry *> Result =
|
|
|
|
getOrCreateFileSystemEntry(Filename);
|
2019-10-10 23:29:01 +08:00
|
|
|
if (!Result)
|
|
|
|
return Result.getError();
|
|
|
|
return (*Result)->getStatus();
|
2019-08-07 04:43:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/// The VFS that is used by clang consumes the \c CachedFileSystemEntry using
|
|
|
|
/// this subclass.
|
|
|
|
class MinimizedVFSFile final : public llvm::vfs::File {
|
|
|
|
public:
|
|
|
|
MinimizedVFSFile(std::unique_ptr<llvm::MemoryBuffer> Buffer,
|
|
|
|
llvm::vfs::Status Stat)
|
|
|
|
: Buffer(std::move(Buffer)), Stat(std::move(Stat)) {}
|
|
|
|
|
2020-10-13 04:01:07 +08:00
|
|
|
static llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
|
|
|
|
create(const CachedFileSystemEntry *Entry,
|
|
|
|
ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings);
|
2019-08-07 04:43:25 +08:00
|
|
|
|
2020-10-13 04:01:07 +08:00
|
|
|
llvm::ErrorOr<llvm::vfs::Status> status() override { return Stat; }
|
2019-08-07 04:43:25 +08:00
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>>
|
|
|
|
getBuffer(const Twine &Name, int64_t FileSize, bool RequiresNullTerminator,
|
|
|
|
bool IsVolatile) override {
|
|
|
|
return std::move(Buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code close() override { return {}; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> Buffer;
|
|
|
|
llvm::vfs::Status Stat;
|
|
|
|
};
|
|
|
|
|
2020-10-13 04:01:07 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>> MinimizedVFSFile::create(
|
|
|
|
const CachedFileSystemEntry *Entry,
|
|
|
|
ExcludedPreprocessorDirectiveSkipMapping *PPSkipMappings) {
|
2019-09-14 06:12:02 +08:00
|
|
|
if (Entry->isDirectory())
|
|
|
|
return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(
|
|
|
|
std::make_error_code(std::errc::is_a_directory));
|
2019-08-07 04:43:25 +08:00
|
|
|
llvm::ErrorOr<StringRef> Contents = Entry->getContents();
|
|
|
|
if (!Contents)
|
|
|
|
return Contents.getError();
|
2019-09-12 04:40:31 +08:00
|
|
|
auto Result = std::make_unique<MinimizedVFSFile>(
|
2019-08-07 04:43:25 +08:00
|
|
|
llvm::MemoryBuffer::getMemBuffer(*Contents, Entry->getName(),
|
|
|
|
/*RequiresNullTerminator=*/false),
|
|
|
|
*Entry->getStatus());
|
2019-09-12 04:40:31 +08:00
|
|
|
if (!Entry->getPPSkippedRangeMapping().empty() && PPSkipMappings)
|
2020-10-09 06:27:47 +08:00
|
|
|
(*PPSkipMappings)[Result->Buffer->getBufferStart()] =
|
2019-09-12 04:40:31 +08:00
|
|
|
&Entry->getPPSkippedRangeMapping();
|
2019-09-12 05:00:13 +08:00
|
|
|
return llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>(
|
|
|
|
std::unique_ptr<llvm::vfs::File>(std::move(Result)));
|
2019-08-07 04:43:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
|
|
|
|
DependencyScanningWorkerFilesystem::openFileForRead(const Twine &Path) {
|
|
|
|
SmallString<256> OwnedFilename;
|
|
|
|
StringRef Filename = Path.toStringRef(OwnedFilename);
|
|
|
|
|
2019-10-11 04:19:02 +08:00
|
|
|
const llvm::ErrorOr<const CachedFileSystemEntry *> Result =
|
|
|
|
getOrCreateFileSystemEntry(Filename);
|
2019-10-10 23:29:01 +08:00
|
|
|
if (!Result)
|
|
|
|
return Result.getError();
|
2020-10-13 04:01:07 +08:00
|
|
|
return MinimizedVFSFile::create(Result.get(), PPSkipMappings);
|
2019-08-07 04:43:25 +08:00
|
|
|
}
|