Skip to content

[5.9] Bundle macro plugin changes #64793

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 6 commits into from
Mar 31, 2023
Merged
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
21 changes: 16 additions & 5 deletions include/swift/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -1461,12 +1461,21 @@ class ASTContext final {
/// The declared interface type of Builtin.TheTupleType.
BuiltinTupleType *getBuiltinTupleType();

/// Finds the address of the given symbol. If `libraryHandleHint` is non-null,
/// search within the library.
void *getAddressOfSymbol(const char *name, void *libraryHandleHint = nullptr);

Type getNamedSwiftType(ModuleDecl *module, StringRef name);

/// Lookup a library plugin that can handle \p moduleName and return the path
/// to it.
/// The path is valid within the VFS, use `FS.getRealPath()` for the
/// underlying path.
Optional<std::string> lookupLibraryPluginByModuleName(Identifier moduleName);

/// Load the specified dylib plugin path resolving the path with the
/// current VFS. If it fails to load the plugin, a diagnostic is emitted, and
/// returns a nullptr.
/// NOTE: This method is idempotent. If the plugin is already loaded, the same
/// instance is simply returned.
void *loadLibraryPlugin(StringRef path);

/// Lookup an executable plugin that is declared to handle \p moduleName
/// module by '-load-plugin-executable'.
/// The path is valid within the VFS, use `FS.getRealPath()` for the
Expand Down Expand Up @@ -1494,6 +1503,8 @@ class ASTContext final {
/// This should be called before any plugin is loaded.
void setPluginRegistry(PluginRegistry *newValue);

const llvm::StringSet<> &getLoadedPluginLibraryPaths() const;

private:
friend Decl;

Expand All @@ -1506,7 +1517,7 @@ class ASTContext final {
Optional<StringRef> getBriefComment(const Decl *D);
void setBriefComment(const Decl *D, StringRef Comment);

void loadCompilerPlugins();
void createModuleToExecutablePluginMap();

friend TypeBase;
friend ArchetypeType;
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ void Plugin_lock(PluginHandle handle);
/// Unlock the plugin.
void Plugin_unlock(PluginHandle handle);

/// Launch the plugin if it's not running.
_Bool Plugin_spawnIfNeeded(PluginHandle handle);

/// Sends the message to the plugin, returns true if there was an error.
/// Clients should receive the response by \c Plugin_waitForNextMessage .
_Bool Plugin_sendMessage(PluginHandle handle, const BridgedData data);
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ ERROR(error_invalid_source_location_str,none,
ERROR(error_no_source_location_scope_map,none,
"-dump-scope-maps argument must be 'expanded' or a list of "
"source locations", ())
ERROR(error_load_plugin_executable,none,
"invalid value '%0' in '-load-plugin-executable'; "
"make sure to use format '<plugin path>#<module names>'", (StringRef))

NOTE(note_valid_swift_versions, none,
"valid arguments to '-swift-version' are %0", (StringRef))
Expand Down
95 changes: 81 additions & 14 deletions include/swift/AST/PluginRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,82 @@

namespace swift {

/// Represent a "resolved" exectuable plugin.
///
/// Plugin clients usually deal with this object to communicate with the actual
/// plugin implementation.
/// This object has a file path of the plugin executable, and is responsible to
/// launch it and manages the process. When the plugin process crashes, this
/// should automatically relaunch the process so the clients can keep using this
/// object as the interface.
class LoadedExecutablePlugin {
const llvm::sys::procid_t pid;

/// Represents the current process of the executable plugin.
struct PluginProcess {
const llvm::sys::procid_t pid;
const int inputFileDescriptor;
const int outputFileDescriptor;
bool isStale = false;

PluginProcess(llvm::sys::procid_t pid, int inputFileDescriptor,
int outputFileDescriptor);

~PluginProcess();

ssize_t write(const void *buf, size_t nbyte) const;
ssize_t read(void *buf, size_t nbyte) const;
};

/// Launched current process.
std::unique_ptr<PluginProcess> Process;

/// Path to the plugin executable.
const std::string ExecutablePath;

/// Last modification time of the `ExecutablePath` when this is initialized.
const llvm::sys::TimePoint<> LastModificationTime;
const int inputFileDescriptor;
const int outputFileDescriptor;

/// Opaque value of the protocol capability of the pluugin. This is a
/// value from ASTGen.
const void *capability = nullptr;

/// Callbacks to be called when the connection is restored.
llvm::SmallVector<std::function<void(void)> *, 0> onReconnect;

/// Flag to dump plugin messagings.
bool dumpMessaging = false;

/// Cleanup function to call ASTGen.
std::function<void(void)> cleanup;

std::mutex mtx;

ssize_t write(const void *buf, size_t nbyte) const;
ssize_t read(void *buf, size_t nbyte) const;

public:
LoadedExecutablePlugin(llvm::sys::procid_t pid,
llvm::sys::TimePoint<> LastModificationTime,
int inputFileDescriptor, int outputFileDescriptor);
LoadedExecutablePlugin(llvm::StringRef ExecutablePath,
llvm::sys::TimePoint<> LastModificationTime)
: ExecutablePath(ExecutablePath),
LastModificationTime(LastModificationTime){};
~LoadedExecutablePlugin();

/// The last modification time of 'ExecutablePath' when this object is
/// created.
llvm::sys::TimePoint<> getLastModificationTime() const {
return LastModificationTime;
}

/// Indicates that the current process is usable.
bool isAlive() const { return Process != nullptr && !Process->isStale; }

/// Mark the current process "stale".
void setStale() const { Process->isStale = true; }

void lock() { mtx.lock(); }
void unlock() { mtx.unlock(); }

// Launch the plugin if it's not already running, or it's stale. Return an
// error if it's fails to execute it.
llvm::Error spawnIfNeeded();

/// Send a message to the plugin.
llvm::Error sendMessage(llvm::StringRef message) const;

Expand All @@ -63,10 +109,23 @@ class LoadedExecutablePlugin {
this->cleanup = cleanup;
}

llvm::sys::procid_t getPid() { return pid; }
/// Add "on reconnect" callback.
/// These callbacks are called when `spawnIfNeeded()` relaunched the plugin.
void addOnReconnect(std::function<void(void)> *fn) {
onReconnect.push_back(fn);
}

/// Remove "on reconnect" callback.
void removeOnReconnect(std::function<void(void)> *fn) {
llvm::erase_value(onReconnect, fn);
}

llvm::sys::procid_t getPid() { return Process->pid; }

const void *getCapability() { return capability; };
void setCapability(const void *newValue) { capability = newValue; };

void setDumpMessaging(bool flag) { dumpMessaging = flag; }
};

class PluginRegistry {
Expand All @@ -77,14 +136,22 @@ class PluginRegistry {
llvm::StringMap<std::unique_ptr<LoadedExecutablePlugin>>
LoadedPluginExecutables;

/// Flag to dump plugin messagings.
bool dumpMessaging = false;

std::mutex mtx;

public:
PluginRegistry();

/// Load a dynamic link library specified by \p path.
/// If \p path plugin is already loaded, this returns the cached object.
llvm::Expected<void *> loadLibraryPlugin(llvm::StringRef path);

/// Load an executable plugin specified by \p path .
/// If \p path plugin is already loaded, this returns the cached object.
llvm::Expected<LoadedExecutablePlugin *>
loadExecutablePlugin(llvm::StringRef path);

const llvm::StringMap<void *> &getLoadedLibraryPlugins() const {
return LoadedPluginLibraries;
}
};

} // namespace swift
16 changes: 11 additions & 5 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,12 @@ class ModuleSearchPathLookup {
llvm::vfs::FileSystem *FS, bool IsOSDarwin);
};

/// Pair of a plugin path and the module name that the plugin provides.
struct PluginExecutablePathAndModuleNames {
std::string ExecutablePath;
std::vector<std::string> ModuleNames;
};

/// Pair of a plugin search path and the corresponding plugin server executable
/// path.
struct ExternalPluginSearchPathAndServerPath {
Expand Down Expand Up @@ -242,8 +248,7 @@ class SearchPathOptions {
std::vector<std::string> CompilerPluginLibraryPaths;

/// Compiler plugin executable paths and providing module names.
/// Format: '<path>#<module names>'
std::vector<std::string> CompilerPluginExecutablePaths;
std::vector<PluginExecutablePathAndModuleNames> CompilerPluginExecutablePaths;

/// Add a single import search path. Must only be called from
/// \c ASTContext::addSearchPath.
Expand Down Expand Up @@ -361,12 +366,13 @@ class SearchPathOptions {
}

void setCompilerPluginExecutablePaths(
std::vector<std::string> NewCompilerPluginExecutablePaths) {
CompilerPluginExecutablePaths = NewCompilerPluginExecutablePaths;
std::vector<PluginExecutablePathAndModuleNames> &&newValue) {
CompilerPluginExecutablePaths = std::move(newValue);
Lookup.searchPathsDidChange();
}

ArrayRef<std::string> getCompilerPluginExecutablePaths() const {
ArrayRef<PluginExecutablePathAndModuleNames>
getCompilerPluginExecutablePaths() const {
return CompilerPluginExecutablePaths;
}

Expand Down
7 changes: 5 additions & 2 deletions include/swift/IDETool/CompileInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ namespace swift {

class CompilerInstance;
class DiagnosticConsumer;
class PluginRegistry;

namespace ide {

/// Manages \c CompilerInstance for completion like operations.
class CompileInstance {
const std::string &RuntimeResourcePath;
const std::string &DiagnosticDocumentationPath;
const std::shared_ptr<swift::PluginRegistry> Plugins;

struct Options {
unsigned MaxASTReuseCount = 100;
Expand Down Expand Up @@ -66,10 +68,11 @@ class CompileInstance {

public:
CompileInstance(const std::string &RuntimeResourcePath,
const std::string &DiagnosticDocumentationPath)
const std::string &DiagnosticDocumentationPath,
std::shared_ptr<swift::PluginRegistry> Plugins = nullptr)
: RuntimeResourcePath(RuntimeResourcePath),
DiagnosticDocumentationPath(DiagnosticDocumentationPath),
CachedCIInvalidated(false), CachedReuseCount(0) {}
Plugins(Plugins), CachedCIInvalidated(false), CachedReuseCount(0) {}

/// NOTE: \p Args is only used for checking the equaity of the invocation.
/// Since this function assumes that it is already normalized, exact the same
Expand Down
6 changes: 5 additions & 1 deletion include/swift/IDETool/IDEInspectionInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace swift {
class CompilerInstance;
class CompilerInvocation;
class DiagnosticConsumer;
class PluginRegistry;

namespace ide {

Expand Down Expand Up @@ -96,6 +97,8 @@ class IDEInspectionInstance {

std::mutex mtx;

std::shared_ptr<PluginRegistry> Plugins;

std::shared_ptr<CompilerInstance> CachedCI;
llvm::hash_code CachedArgHash;
llvm::sys::TimePoint<> DependencyCheckedTimestamp;
Expand Down Expand Up @@ -167,7 +170,8 @@ class IDEInspectionInstance {
Callback);

public:
IDEInspectionInstance() : CachedCIShouldBeInvalidated(false) {}
IDEInspectionInstance(std::shared_ptr<PluginRegistry> Plugins = nullptr)
: Plugins(Plugins), CachedCIShouldBeInvalidated(false) {}

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