Skip to content

Commit c9172d1

Browse files
authored
Merge pull request #64655 from rintaro/sourcekit-single-pluginregistry
[SourceKit] Use a single PluginRegistry in multiple ASTContexts
2 parents 76c5e4f + 59f744c commit c9172d1

File tree

15 files changed

+72
-31
lines changed

15 files changed

+72
-31
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,8 @@ class ASTContext final {
15031503
/// This should be called before any plugin is loaded.
15041504
void setPluginRegistry(PluginRegistry *newValue);
15051505

1506+
const llvm::StringSet<> &getLoadedPluginLibraryPaths() const;
1507+
15061508
private:
15071509
friend Decl;
15081510

include/swift/AST/PluginRegistry.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ class PluginRegistry {
139139
/// Flag to dump plugin messagings.
140140
bool dumpMessaging = false;
141141

142+
std::mutex mtx;
143+
142144
public:
143145
PluginRegistry();
144146

@@ -150,10 +152,6 @@ class PluginRegistry {
150152
/// If \p path plugin is already loaded, this returns the cached object.
151153
llvm::Expected<LoadedExecutablePlugin *>
152154
loadExecutablePlugin(llvm::StringRef path);
153-
154-
const llvm::StringMap<void *> &getLoadedLibraryPlugins() const {
155-
return LoadedPluginLibraries;
156-
}
157155
};
158156

159157
} // namespace swift

include/swift/IDETool/CompileInstance.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ namespace swift {
2525

2626
class CompilerInstance;
2727
class DiagnosticConsumer;
28+
class PluginRegistry;
2829

2930
namespace ide {
3031

3132
/// Manages \c CompilerInstance for completion like operations.
3233
class CompileInstance {
3334
const std::string &RuntimeResourcePath;
3435
const std::string &DiagnosticDocumentationPath;
36+
const std::shared_ptr<swift::PluginRegistry> Plugins;
3537

3638
struct Options {
3739
unsigned MaxASTReuseCount = 100;
@@ -66,10 +68,11 @@ class CompileInstance {
6668

6769
public:
6870
CompileInstance(const std::string &RuntimeResourcePath,
69-
const std::string &DiagnosticDocumentationPath)
71+
const std::string &DiagnosticDocumentationPath,
72+
std::shared_ptr<swift::PluginRegistry> Plugins = nullptr)
7073
: RuntimeResourcePath(RuntimeResourcePath),
7174
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
72-
CachedCIInvalidated(false), CachedReuseCount(0) {}
75+
Plugins(Plugins), CachedCIInvalidated(false), CachedReuseCount(0) {}
7376

7477
/// NOTE: \p Args is only used for checking the equaity of the invocation.
7578
/// Since this function assumes that it is already normalized, exact the same

include/swift/IDETool/IDEInspectionInstance.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ namespace swift {
3535
class CompilerInstance;
3636
class CompilerInvocation;
3737
class DiagnosticConsumer;
38+
class PluginRegistry;
3839

3940
namespace ide {
4041

@@ -96,6 +97,8 @@ class IDEInspectionInstance {
9697

9798
std::mutex mtx;
9899

100+
std::shared_ptr<PluginRegistry> Plugins;
101+
99102
std::shared_ptr<CompilerInstance> CachedCI;
100103
llvm::hash_code CachedArgHash;
101104
llvm::sys::TimePoint<> DependencyCheckedTimestamp;
@@ -167,7 +170,8 @@ class IDEInspectionInstance {
167170
Callback);
168171

169172
public:
170-
IDEInspectionInstance() : CachedCIShouldBeInvalidated(false) {}
173+
IDEInspectionInstance(std::shared_ptr<PluginRegistry> Plugins = nullptr)
174+
: Plugins(Plugins), CachedCIShouldBeInvalidated(false) {}
171175

172176
// Mark the cached compiler instance "should be invalidated". In the next
173177
// completion, new compiler instance will be used. (Thread safe.)

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ struct ASTContext::Implementation {
534534
/// Map a module name to an executable plugin path that provides the module.
535535
llvm::DenseMap<Identifier, StringRef> ExecutablePluginPaths;
536536

537+
llvm::StringSet<> LoadedPluginLibraryPaths;
538+
537539
/// The permanent arena.
538540
Arena Permanent;
539541

@@ -6395,6 +6397,9 @@ LoadedExecutablePlugin *ASTContext::loadExecutablePlugin(StringRef path) {
63956397
}
63966398

63976399
void *ASTContext::loadLibraryPlugin(StringRef path) {
6400+
// Remember the path (even if it fails to load.)
6401+
getImpl().LoadedPluginLibraryPaths.insert(path);
6402+
63986403
SmallString<128> resolvedPath;
63996404
auto fs = this->SourceMgr.getFileSystem();
64006405
if (auto err = fs->getRealPath(path, resolvedPath)) {
@@ -6413,6 +6418,10 @@ void *ASTContext::loadLibraryPlugin(StringRef path) {
64136418
return plugin.get();
64146419
}
64156420

6421+
const llvm::StringSet<> &ASTContext::getLoadedPluginLibraryPaths() const {
6422+
return getImpl().LoadedPluginLibraryPaths;
6423+
}
6424+
64166425
bool ASTContext::supportsMoveOnlyTypes() const {
64176426
// currently the only thing holding back whether the types can appear is this.
64186427
return SILOpts.LexicalLifetimes != LexicalLifetimesOption::Off;

lib/AST/PluginRegistry.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ PluginRegistry::PluginRegistry() {
4545
}
4646

4747
llvm::Expected<void *> PluginRegistry::loadLibraryPlugin(StringRef path) {
48+
std::lock_guard<std::mutex> lock(mtx);
4849
auto found = LoadedPluginLibraries.find(path);
4950
if (found != LoadedPluginLibraries.end()) {
5051
// Already loaded.
@@ -74,6 +75,8 @@ PluginRegistry::loadExecutablePlugin(StringRef path) {
7475
return llvm::errorCodeToError(err);
7576
}
7677

78+
std::lock_guard<std::mutex> lock(mtx);
79+
7780
// See if the plugin is already loaded.
7881
auto &storage = LoadedPluginExecutables[path];
7982
if (storage) {

lib/FrontendTool/LoadedModuleTrace.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,7 @@ bool swift::emitLoadedModuleTraceIfNeeded(ModuleDecl *mainModule,
760760
}
761761

762762
// Add compiler plugin libraries as dependencies.
763-
auto *pluginRegistry = ctxt.getPluginRegistry();
764-
for (auto &pluginEntry : pluginRegistry->getLoadedLibraryPlugins())
763+
for (auto &pluginEntry : ctxt.getLoadedPluginLibraryPaths())
765764
depTracker->addDependency(pluginEntry.getKey(), /*IsSystem*/ false);
766765

767766
std::vector<SwiftModuleTraceInfo> swiftModules;

lib/IDETool/CompileInstance.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ bool CompileInstance::setupCI(
299299
assert(Diags.hadAnyError());
300300
return false;
301301
}
302+
CI->getASTContext().setPluginRegistry(Plugins.get());
302303

303304
return true;
304305
}

lib/IDETool/IDEInspectionInstance.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ void IDEInspectionInstance::performNewOperation(
482482
InstanceSetupError));
483483
return;
484484
}
485+
CI->getASTContext().setPluginRegistry(Plugins.get());
485486
CI->getASTContext().CancellationFlag = CancellationFlag;
486487
registerIDERequestFunctions(CI->getASTContext().evaluator);
487488

tools/SourceKit/lib/SwiftLang/SwiftASTManager.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -553,10 +553,12 @@ struct SwiftASTManager::Implementation {
553553
std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocs,
554554
std::shared_ptr<GlobalConfig> Config,
555555
std::shared_ptr<SwiftStatistics> Stats,
556-
std::shared_ptr<RequestTracker> ReqTracker, StringRef SwiftExecutablePath,
556+
std::shared_ptr<RequestTracker> ReqTracker,
557+
std::shared_ptr<PluginRegistry> Plugins, StringRef SwiftExecutablePath,
557558
StringRef RuntimeResourcePath, StringRef DiagnosticDocumentationPath)
558559
: EditorDocs(EditorDocs), Config(Config), Stats(Stats),
559-
ReqTracker(ReqTracker), SwiftExecutablePath(SwiftExecutablePath),
560+
ReqTracker(ReqTracker), Plugins(Plugins),
561+
SwiftExecutablePath(SwiftExecutablePath),
560562
RuntimeResourcePath(RuntimeResourcePath),
561563
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
562564
SessionTimestamp(llvm::sys::toTimeT(std::chrono::system_clock::now())) {
@@ -566,6 +568,7 @@ struct SwiftASTManager::Implementation {
566568
std::shared_ptr<GlobalConfig> Config;
567569
std::shared_ptr<SwiftStatistics> Stats;
568570
std::shared_ptr<RequestTracker> ReqTracker;
571+
std::shared_ptr<PluginRegistry> Plugins;
569572
/// The path of the swift-frontend executable.
570573
/// Used to find clang relative to it.
571574
std::string SwiftExecutablePath;
@@ -638,9 +641,10 @@ SwiftASTManager::SwiftASTManager(
638641
std::shared_ptr<SwiftEditorDocumentFileMap> EditorDocs,
639642
std::shared_ptr<GlobalConfig> Config,
640643
std::shared_ptr<SwiftStatistics> Stats,
641-
std::shared_ptr<RequestTracker> ReqTracker, StringRef SwiftExecutablePath,
644+
std::shared_ptr<RequestTracker> ReqTracker,
645+
std::shared_ptr<PluginRegistry> Plugins, StringRef SwiftExecutablePath,
642646
StringRef RuntimeResourcePath, StringRef DiagnosticDocumentationPath)
643-
: Impl(*new Implementation(EditorDocs, Config, Stats, ReqTracker,
647+
: Impl(*new Implementation(EditorDocs, Config, Stats, ReqTracker, Plugins,
644648
SwiftExecutablePath, RuntimeResourcePath,
645649
DiagnosticDocumentationPath)) {}
646650

@@ -1073,6 +1077,7 @@ ASTUnitRef ASTBuildOperation::buildASTUnit(std::string &Error) {
10731077
}
10741078
return nullptr;
10751079
}
1080+
CompIns.getASTContext().setPluginRegistry(ASTManager->Impl.Plugins.get());
10761081
CompIns.getASTContext().CancellationFlag = CancellationFlag;
10771082
registerIDERequestFunctions(CompIns.getASTContext().evaluator);
10781083
if (TracedOp.enabled()) {

0 commit comments

Comments
 (0)