-
Notifications
You must be signed in to change notification settings - Fork 13.3k
[clang] Enable making CompilerInstance
VFS thread-safe
#135737
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
Merged
Merged
Conversation
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
The `llvm::vfs::FileSystem` interface makes no promises around thread-safety. To enable making `CompilerInstance` thread-safe, this PR allows passing an explicit VFS to `cloneForModuleCompile()`. This will be used from the dependency scanner.
@llvm/pr-subscribers-clang Author: Jan Svoboda (jansvoboda11) ChangesThe Full diff: https://github.com/llvm/llvm-project/pull/135737.diff 2 Files Affected:
diff --git a/clang/include/clang/Frontend/CompilerInstance.h b/clang/include/clang/Frontend/CompilerInstance.h
index 41dc7f1fef461..d70f5c45b3d38 100644
--- a/clang/include/clang/Frontend/CompilerInstance.h
+++ b/clang/include/clang/Frontend/CompilerInstance.h
@@ -845,18 +845,25 @@ class CompilerInstance : public ModuleLoader {
/// Creates a \c CompilerInstance for compiling a module.
///
/// This expects a properly initialized \c FrontendInputFile.
+ ///
+ /// Explicitly-specified \c VFS takes precedence over the VFS of this instance
+ /// when creating the clone and also prevents \c FileManager sharing.
std::unique_ptr<CompilerInstance> cloneForModuleCompileImpl(
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
- StringRef OriginalModuleMapFile, StringRef ModuleFileName);
+ StringRef OriginalModuleMapFile, StringRef ModuleFileName,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
public:
/// Creates a new \c CompilerInstance for compiling a module.
///
/// This takes care of creating appropriate \c FrontendInputFile for
/// public/private frameworks, inferred modules and such.
- std::unique_ptr<CompilerInstance>
- cloneForModuleCompile(SourceLocation ImportLoc, Module *Module,
- StringRef ModuleFileName);
+ ///
+ /// Explicitly-specified \c VFS takes precedence over the VFS of this instance
+ /// when creating the clone and also prevents \c FileManager sharing.
+ std::unique_ptr<CompilerInstance> cloneForModuleCompile(
+ SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS = nullptr);
/// Compile a module file for the given module, using the options
/// provided by the importing compiler instance. Returns true if the module
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp
index 243e0a3c15b05..fa6b137338f26 100644
--- a/clang/lib/Frontend/CompilerInstance.cpp
+++ b/clang/lib/Frontend/CompilerInstance.cpp
@@ -1152,7 +1152,8 @@ static Language getLanguageFromOptions(const LangOptions &LangOpts) {
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
SourceLocation ImportLoc, StringRef ModuleName, FrontendInputFile Input,
- StringRef OriginalModuleMapFile, StringRef ModuleFileName) {
+ StringRef OriginalModuleMapFile, StringRef ModuleFileName,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
// Construct a compiler invocation for creating this module.
auto Invocation = std::make_shared<CompilerInvocation>(getInvocation());
@@ -1212,19 +1213,21 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompileImpl(
auto &Inv = *Invocation;
Instance.setInvocation(std::move(Invocation));
+ if (VFS) {
+ Instance.createFileManager(std::move(VFS));
+ } else if (FrontendOpts.ModulesShareFileManager) {
+ Instance.setFileManager(&getFileManager());
+ } else {
+ Instance.createFileManager(&getVirtualFileSystem());
+ }
+
Instance.createDiagnostics(
- getVirtualFileSystem(),
+ Instance.getVirtualFileSystem(),
new ForwardingDiagnosticConsumer(getDiagnosticClient()),
/*ShouldOwnClient=*/true);
-
if (llvm::is_contained(DiagOpts.SystemHeaderWarningsModules, ModuleName))
Instance.getDiagnostics().setSuppressSystemWarnings(false);
- if (FrontendOpts.ModulesShareFileManager) {
- Instance.setFileManager(&getFileManager());
- } else {
- Instance.createFileManager(&getVirtualFileSystem());
- }
Instance.createSourceManager(Instance.getFileManager());
SourceManager &SourceMgr = Instance.getSourceManager();
@@ -1318,7 +1321,8 @@ static OptionalFileEntryRef getPublicModuleMap(FileEntryRef File,
}
std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
- SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName) {
+ SourceLocation ImportLoc, Module *Module, StringRef ModuleFileName,
+ IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) {
StringRef ModuleName = Module->getTopLevelModuleName();
InputKind IK(getLanguageFromOptions(getLangOpts()), InputKind::ModuleMap);
@@ -1363,7 +1367,8 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
return cloneForModuleCompileImpl(
ImportLoc, ModuleName,
FrontendInputFile(ModuleMapFilePath, IK, IsSystem),
- ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
+ ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
+ std::move(VFS));
}
// FIXME: We only need to fake up an input file here as a way of
@@ -1380,7 +1385,8 @@ std::unique_ptr<CompilerInstance> CompilerInstance::cloneForModuleCompile(
auto Instance = cloneForModuleCompileImpl(
ImportLoc, ModuleName,
FrontendInputFile(FakeModuleMapFile, IK, +Module->IsSystem),
- ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName);
+ ModMap.getModuleMapFileForUniquing(Module)->getName(), ModuleFileName,
+ std::move(VFS));
std::unique_ptr<llvm::MemoryBuffer> ModuleMapBuffer =
llvm::MemoryBuffer::getMemBufferCopy(InferredModuleMapContent);
|
Ping. |
cyndyishida
approved these changes
Apr 21, 2025
jansvoboda11
added a commit
to jansvoboda11/llvm-project
that referenced
this pull request
Apr 24, 2025
…tor thread-safe The same principle as llvm#135473, llvm#135737, llvm#136178, llvm#136601 & llvm#137059.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The
llvm::vfs::FileSystem
interface makes no promises around thread-safety. To enable makingCompilerInstance
thread-safe, this PR allows passing an explicit VFS tocloneForModuleCompile()
. This will be used from the dependency scanner.