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()) {

tools/SourceKit/lib/SwiftLang/SwiftASTManager.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ namespace swift {
9595
class CompilerInstance;
9696
class CompilerInvocation;
9797
class DiagnosticEngine;
98+
class PluginRegistry;
9899
class SourceFile;
99100
class SourceManager;
100101
}
@@ -235,6 +236,7 @@ class SwiftASTManager : public std::enable_shared_from_this<SwiftASTManager> {
235236
std::shared_ptr<GlobalConfig> Config,
236237
std::shared_ptr<SwiftStatistics> Stats,
237238
std::shared_ptr<RequestTracker> ReqTracker,
239+
std::shared_ptr<swift::PluginRegistry> Plugins,
238240
StringRef SwiftExecutablePath,
239241
StringRef RuntimeResourcePath,
240242
StringRef DiagnosticDocumentationPath);

tools/SourceKit/lib/SwiftLang/SwiftCompile.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ compile::SessionManager::getSession(StringRef name) {
3131
}
3232

3333
bool inserted = false;
34-
std::tie(i, inserted) = sessions.try_emplace(name, std::make_shared<compile::Session>(RuntimeResourcePath, DiagnosticDocumentationPath));
34+
std::tie(i, inserted) = sessions.try_emplace(
35+
name, std::make_shared<compile::Session>(
36+
RuntimeResourcePath, DiagnosticDocumentationPath, Plugins));
3537
assert(inserted);
3638
return i->second;
3739
}
@@ -141,10 +143,10 @@ void SwiftLangSupport::performCompile(
141143
CancellationFlag->store(true, std::memory_order_relaxed);
142144
});
143145

144-
CompileManager.performCompileAsync(Name, Args, std::move(fileSystem),
145-
CancellationFlag, Receiver);
146+
CompileManager->performCompileAsync(Name, Args, std::move(fileSystem),
147+
CancellationFlag, Receiver);
146148
}
147149

148150
void SwiftLangSupport::closeCompile(StringRef Name) {
149-
CompileManager.clearSession(Name);
151+
CompileManager->clearSession(Name);
150152
}

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,24 +276,29 @@ static void configureIDEInspectionInstance(
276276
SwiftLangSupport::SwiftLangSupport(SourceKit::Context &SKCtx)
277277
: NotificationCtr(SKCtx.getNotificationCenter()),
278278
SwiftExecutablePath(SKCtx.getSwiftExecutablePath()),
279-
ReqTracker(SKCtx.getRequestTracker()), CCCache(new SwiftCompletionCache),
280-
CompileManager(RuntimeResourcePath, DiagnosticDocumentationPath) {
279+
ReqTracker(SKCtx.getRequestTracker()), CCCache(new SwiftCompletionCache) {
281280
llvm::SmallString<128> LibPath(SKCtx.getRuntimeLibPath());
282281
llvm::sys::path::append(LibPath, "swift");
283282
RuntimeResourcePath = std::string(LibPath.str());
284283
DiagnosticDocumentationPath = SKCtx.getDiagnosticDocumentationPath().str();
285284

286285
Stats = std::make_shared<SwiftStatistics>();
287286
EditorDocuments = std::make_shared<SwiftEditorDocumentFileMap>();
287+
288+
std::shared_ptr<PluginRegistry> Plugins = std::make_shared<PluginRegistry>();
289+
288290
ASTMgr = std::make_shared<SwiftASTManager>(
289291
EditorDocuments, SKCtx.getGlobalConfiguration(), Stats, ReqTracker,
290-
SwiftExecutablePath, RuntimeResourcePath, DiagnosticDocumentationPath);
291-
292-
IDEInspectionInst = std::make_shared<IDEInspectionInstance>();
292+
Plugins, SwiftExecutablePath, RuntimeResourcePath,
293+
DiagnosticDocumentationPath);
293294

295+
IDEInspectionInst = std::make_shared<IDEInspectionInstance>(Plugins);
294296
configureIDEInspectionInstance(IDEInspectionInst,
295297
SKCtx.getGlobalConfiguration());
296298

299+
CompileManager = std::make_shared<compile::SessionManager>(
300+
RuntimeResourcePath, DiagnosticDocumentationPath, Plugins);
301+
297302
// By default, just use the in-memory cache.
298303
CCCache->inMemory = std::make_unique<ide::CodeCompletionCache>();
299304

tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,9 @@ class Session {
293293

294294
public:
295295
Session(const std::string &RuntimeResourcePath,
296-
const std::string &DiagnosticDocumentationPath)
297-
: Compiler(RuntimeResourcePath, DiagnosticDocumentationPath) {}
296+
const std::string &DiagnosticDocumentationPath,
297+
std::shared_ptr<swift::PluginRegistry> Plugins)
298+
: Compiler(RuntimeResourcePath, DiagnosticDocumentationPath, Plugins) {}
298299

299300
bool
300301
performCompile(llvm::ArrayRef<const char *> Args,
@@ -308,6 +309,7 @@ class Session {
308309
class SessionManager {
309310
const std::string &RuntimeResourcePath;
310311
const std::string &DiagnosticDocumentationPath;
312+
const std::shared_ptr<swift::PluginRegistry> Plugins;
311313

312314
llvm::StringMap<std::shared_ptr<Session>> sessions;
313315
WorkQueue compileQueue{WorkQueue::Dequeuing::Concurrent,
@@ -316,9 +318,11 @@ class SessionManager {
316318

317319
public:
318320
SessionManager(std::string &RuntimeResourcePath,
319-
std::string &DiagnosticDocumentationPath)
321+
std::string &DiagnosticDocumentationPath,
322+
std::shared_ptr<swift::PluginRegistry> Plugins)
320323
: RuntimeResourcePath(RuntimeResourcePath),
321-
DiagnosticDocumentationPath(DiagnosticDocumentationPath) {}
324+
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
325+
Plugins(Plugins) {}
322326

323327
std::shared_ptr<Session> getSession(StringRef name);
324328

@@ -356,7 +360,7 @@ class SwiftLangSupport : public LangSupport {
356360
std::shared_ptr<SwiftStatistics> Stats;
357361
llvm::StringMap<std::unique_ptr<FileSystemProvider>> FileSystemProviders;
358362
std::shared_ptr<swift::ide::IDEInspectionInstance> IDEInspectionInst;
359-
compile::SessionManager CompileManager;
363+
std::shared_ptr<compile::SessionManager> CompileManager;
360364

361365
public:
362366
explicit SwiftLangSupport(SourceKit::Context &SKCtx);

tools/swift-ide-test/swift-ide-test.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "swift/AST/DiagnosticEngine.h"
2424
#include "swift/AST/ImportCache.h"
2525
#include "swift/AST/NameLookupRequests.h"
26+
#include "swift/AST/PluginRegistry.h"
2627
#include "swift/AST/PrintOptions.h"
2728
#include "swift/AST/RawComment.h"
2829
#include "swift/AST/USRGeneration.h"
@@ -1188,7 +1189,8 @@ static int doTypeContextInfo(const CompilerInvocation &InitInvok,
11881189
InitInvok, SourceFilename, SecondSourceFileName, CodeCompletionToken,
11891190
CodeCompletionDiagnostics,
11901191
[&](CompletionLikeOperationParams Params) -> bool {
1191-
IDEInspectionInstance IDEInspectionInst;
1192+
IDEInspectionInstance IDEInspectionInst(
1193+
std::make_shared<PluginRegistry>());
11921194
int ExitCode = 2;
11931195
IDEInspectionInst.typeContextInfo(
11941196
Params.Invocation, Params.Args, Params.FileSystem,
@@ -1260,7 +1262,8 @@ doConformingMethodList(const CompilerInvocation &InitInvok,
12601262
InitInvok, SourceFilename, SecondSourceFileName, CodeCompletionToken,
12611263
CodeCompletionDiagnostics,
12621264
[&](CompletionLikeOperationParams Params) -> bool {
1263-
IDEInspectionInstance IDEInspectionInst;
1265+
IDEInspectionInstance IDEInspectionInst(
1266+
std::make_shared<PluginRegistry>());
12641267
int ExitCode = 2;
12651268
IDEInspectionInst.conformingMethodList(
12661269
Params.Invocation, Params.Args, Params.FileSystem,
@@ -1410,7 +1413,7 @@ doCodeCompletion(const CompilerInvocation &InitInvok, StringRef SourceFilename,
14101413
InitInvok, SourceFilename, SecondSourceFileName, CodeCompletionToken,
14111414
CodeCompletionDiagnostics,
14121415
[&](CompletionLikeOperationParams Params) -> bool {
1413-
IDEInspectionInstance Inst;
1416+
IDEInspectionInstance Inst(std::make_shared<PluginRegistry>());
14141417
int ExitCode = 2;
14151418
Inst.codeComplete(
14161419
Params.Invocation, Params.Args, Params.FileSystem,
@@ -1504,7 +1507,7 @@ static int doBatchCodeCompletion(const CompilerInvocation &InitInvok,
15041507
CompilerInvocation Invocation(InitInvok);
15051508
auto FileSystem = llvm::vfs::getRealFileSystem();
15061509

1507-
IDEInspectionInstance IDEInspectionInst;
1510+
IDEInspectionInstance IDEInspectionInst(std::make_shared<PluginRegistry>());
15081511

15091512
std::unique_ptr<ide::OnDiskCodeCompletionCache> OnDiskCache;
15101513
if (!options::CompletionCachePath.empty())

0 commit comments

Comments
 (0)