Skip to content

[Macros] Update plugin search options serialization #66689

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 2 commits into from
Jun 16, 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
96 changes: 76 additions & 20 deletions include/swift/AST/SearchPathOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#define SWIFT_AST_SEARCHPATHOPTIONS_H

#include "swift/Basic/ArrayRefView.h"
#include "swift/Basic/ExternalUnion.h"
#include "swift/Basic/PathRemapper.h"
#include "swift/Basic/TaggedUnion.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringMap.h"
Expand Down Expand Up @@ -187,25 +187,81 @@ struct ExternalPluginSearchPathAndServerPath {
std::string ServerPath;
};

namespace PluginSearchOption {
struct LoadPluginLibrary {
std::string LibraryPath;
};
struct LoadPluginExecutable {
std::string ExecutablePath;
std::vector<std::string> ModuleNames;
};
struct PluginPath {
std::string SearchPath;
};
struct ExternalPluginPath {
std::string SearchPath;
std::string ServerPath;
};
class PluginSearchOption {
public:
struct LoadPluginLibrary {
std::string LibraryPath;
};
struct LoadPluginExecutable {
std::string ExecutablePath;
std::vector<std::string> ModuleNames;
};
struct PluginPath {
std::string SearchPath;
};
struct ExternalPluginPath {
std::string SearchPath;
std::string ServerPath;
};

enum class Kind : uint8_t {
LoadPluginLibrary,
LoadPluginExecutable,
PluginPath,
ExternalPluginPath,
};

using Value = TaggedUnion<LoadPluginLibrary, LoadPluginExecutable, PluginPath,
ExternalPluginPath>;
} // namespace PluginSearchOption
private:
using Members = ExternalUnionMembers<LoadPluginLibrary, LoadPluginExecutable,
PluginPath, ExternalPluginPath>;
static Members::Index getIndexForKind(Kind kind) {
switch (kind) {
case Kind::LoadPluginLibrary:
return Members::indexOf<LoadPluginLibrary>();
case Kind::LoadPluginExecutable:
return Members::indexOf<LoadPluginExecutable>();
case Kind::PluginPath:
return Members::indexOf<PluginPath>();
case Kind::ExternalPluginPath:
return Members::indexOf<ExternalPluginPath>();
}
};
using Storage = ExternalUnion<Kind, Members, getIndexForKind>;

Kind kind;
Storage storage;

public:
PluginSearchOption(const LoadPluginLibrary &v)
: kind(Kind::LoadPluginLibrary) {
storage.emplace<LoadPluginLibrary>(kind, v);
}
PluginSearchOption(const LoadPluginExecutable &v)
: kind(Kind::LoadPluginExecutable) {
storage.emplace<LoadPluginExecutable>(kind, v);
}
PluginSearchOption(const PluginPath &v) : kind(Kind::PluginPath) {
storage.emplace<PluginPath>(kind, v);
}
PluginSearchOption(const ExternalPluginPath &v)
: kind(Kind::ExternalPluginPath) {
storage.emplace<ExternalPluginPath>(kind, v);
}

Kind getKind() const { return kind; }

template <typename T>
const T *dyn_cast() const {
if (Members::indexOf<T>() != getIndexForKind(kind))
return nullptr;
return &storage.get<T>(kind);
}

template <typename T>
const T &get() const {
return storage.get<T>(kind);
}
};

/// Options for controlling search path behavior.
class SearchPathOptions {
Expand Down Expand Up @@ -383,7 +439,7 @@ class SearchPathOptions {
std::vector<std::string> RuntimeLibraryPaths;

/// Plugin search path options.
std::vector<PluginSearchOption::Value> PluginSearchOpts;
std::vector<PluginSearchOption> PluginSearchOpts;

/// Don't look in for compiler-provided modules.
bool SkipRuntimeLibraryImportPaths = false;
Expand Down
6 changes: 2 additions & 4 deletions include/swift/Serialization/SerializationOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#ifndef SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H
#define SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H

#include "swift/AST/SearchPathOptions.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/PathRemapper.h"
#include "llvm/Support/VersionTuple.h"
Expand Down Expand Up @@ -43,10 +44,7 @@ namespace swift {
StringRef ModuleLinkName;
StringRef ModuleInterface;
std::vector<std::string> ExtraClangOptions;
std::vector<std::string> PluginSearchPaths;
std::vector<std::string> ExternalPluginSearchPaths;
std::vector<std::string> CompilerPluginLibraryPaths;
std::vector<std::string> CompilerPluginExecutablePaths;
std::vector<swift::PluginSearchOption> PluginSearchOptions;

/// Path prefixes that should be rewritten in debug info.
PathRemapper DebuggingOptionsPrefixMap;
Expand Down
37 changes: 8 additions & 29 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ struct ValidationInfo {
class ExtendedValidationInfo {
SmallVector<StringRef, 4> ExtraClangImporterOpts;

SmallVector<StringRef, 1> PluginSearchPaths;
SmallVector<StringRef, 1> ExternalPluginSearchPaths;
SmallVector<StringRef, 1> CompilerPluginLibraryPaths;
SmallVector<StringRef, 1> CompilerPluginExecutablePaths;
SmallVector<std::pair<PluginSearchOption::Kind, StringRef>, 2>
PluginSearchOptions;

std::string SDKPath;
StringRef ModuleABIName;
Expand Down Expand Up @@ -149,32 +147,13 @@ class ExtendedValidationInfo {
ExtraClangImporterOpts.push_back(option);
}

ArrayRef<StringRef> getPluginSearchPaths() const {
return PluginSearchPaths;
ArrayRef<std::pair<PluginSearchOption::Kind, StringRef>>
getPluginSearchOptions() const {
return PluginSearchOptions;
}
void addPluginSearchPath(StringRef path) {
PluginSearchPaths.push_back(path);
}

ArrayRef<StringRef> getExternalPluginSearchPaths() const {
return ExternalPluginSearchPaths;
}
void addExternalPluginSearchPath(StringRef path) {
ExternalPluginSearchPaths.push_back(path);
}

ArrayRef<StringRef> getCompilerPluginLibraryPaths() const {
return CompilerPluginLibraryPaths;
}
void addCompilerPluginLibraryPath(StringRef path) {
CompilerPluginLibraryPaths.push_back(path);
}

ArrayRef<StringRef> getCompilerPluginExecutablePaths() const {
return CompilerPluginExecutablePaths;
}
void addCompilerPluginExecutablePath(StringRef path) {
CompilerPluginExecutablePaths.push_back(path);
void addPluginSearchOption(
const std::pair<PluginSearchOption::Kind, StringRef> &opt) {
PluginSearchOptions.push_back(opt);
}

bool isSIB() const { return Bits.IsSIB; }
Expand Down
30 changes: 18 additions & 12 deletions lib/AST/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,31 @@ PluginLoader::lookupPluginByModuleName(Identifier moduleName) {

// FIXME: Should we create a lookup table keyed by module name?
for (auto &entry : Ctx.SearchPathOpts.PluginSearchOpts) {
using namespace PluginSearchOption;
switch (entry.getKind()) {
// Try '-load-plugin-library'.
if (auto *val = entry.dyn_cast<LoadPluginLibrary>()) {
if (llvm::sys::path::filename(val->LibraryPath) == pluginLibBasename) {
return {val->LibraryPath, ""};
case PluginSearchOption::Kind::LoadPluginLibrary: {
auto &val = entry.get<PluginSearchOption::LoadPluginLibrary>();
if (llvm::sys::path::filename(val.LibraryPath) == pluginLibBasename) {
return {val.LibraryPath, ""};
}
continue;
}

// Try '-load-plugin-executable'.
if (auto *v = entry.dyn_cast<LoadPluginExecutable>()) {
case PluginSearchOption::Kind::LoadPluginExecutable: {
auto &val = entry.get<PluginSearchOption::LoadPluginExecutable>();
auto found = ExecutablePluginPaths.find(moduleName);
if (found != ExecutablePluginPaths.end()) {
return {"", std::string(found->second)};
if (found != ExecutablePluginPaths.end() &&
found->second == val.ExecutablePath) {
return {"", val.ExecutablePath};
}
continue;
}

// Try '-plugin-path'.
if (auto *v = entry.dyn_cast<PluginPath>()) {
SmallString<128> fullPath(v->SearchPath);
case PluginSearchOption::Kind::PluginPath: {
auto &val = entry.get<PluginSearchOption::PluginPath>();
SmallString<128> fullPath(val.SearchPath);
llvm::sys::path::append(fullPath, pluginLibBasename);
if (fs->exists(fullPath)) {
return {std::string(fullPath), ""};
Expand All @@ -90,14 +94,16 @@ PluginLoader::lookupPluginByModuleName(Identifier moduleName) {
}

// Try '-external-plugin-path'.
if (auto *v = entry.dyn_cast<ExternalPluginPath>()) {
SmallString<128> fullPath(v->SearchPath);
case PluginSearchOption::Kind::ExternalPluginPath: {
auto &val = entry.get<PluginSearchOption::ExternalPluginPath>();
SmallString<128> fullPath(val.SearchPath);
llvm::sys::path::append(fullPath, pluginLibBasename);
if (fs->exists(fullPath)) {
return {std::string(fullPath), v->ServerPath};
return {std::string(fullPath), val.ServerPath};
}
continue;
}
}
}

return {};
Expand Down
32 changes: 2 additions & 30 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,36 +209,8 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;
}

// FIXME: Preserve the order of these options.
for (auto &elem : getSearchPathOptions().PluginSearchOpts) {
// '-plugin-path' options.
if (auto *arg = elem.dyn_cast<PluginSearchOption::PluginPath>()) {
serializationOpts.PluginSearchPaths.push_back(arg->SearchPath);
continue;
}

// '-external-plugin-path' options.
if (auto *arg = elem.dyn_cast<PluginSearchOption::ExternalPluginPath>()) {
serializationOpts.ExternalPluginSearchPaths.push_back(
arg->SearchPath + "#" + arg->ServerPath);
continue;
}

// '-load-plugin-library' options.
if (auto *arg = elem.dyn_cast<PluginSearchOption::LoadPluginLibrary>()) {
serializationOpts.CompilerPluginLibraryPaths.push_back(arg->LibraryPath);
continue;
}

// '-load-plugin-executable' options.
if (auto *arg = elem.dyn_cast<PluginSearchOption::LoadPluginExecutable>()) {
std::string optStr = arg->ExecutablePath + "#";
llvm::interleave(
arg->ModuleNames, [&](auto &name) { optStr += name; },
[&]() { optStr += ","; });
serializationOpts.CompilerPluginExecutablePaths.push_back(optStr);
}
}
serializationOpts.PluginSearchOptions =
getSearchPathOptions().PluginSearchOpts;

serializationOpts.DisableCrossModuleIncrementalInfo =
opts.DisableCrossModuleIncrementalBuild;
Expand Down
31 changes: 20 additions & 11 deletions lib/Serialization/ModuleFileSharedCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,27 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
case options_block::XCC:
extendedInfo.addExtraClangImporterOption(blobData);
break;
case options_block::PLUGIN_SEARCH_PATH:
extendedInfo.addPluginSearchPath(blobData);
break;
case options_block::EXTERNAL_SEARCH_PLUGIN_PATH:
extendedInfo.addExternalPluginSearchPath(blobData);
break;
case options_block::COMPILER_PLUGIN_LIBRARY_PATH:
extendedInfo.addCompilerPluginLibraryPath(blobData);
break;
case options_block::COMPILER_PLUGIN_EXECUTABLE_PATH:
extendedInfo.addCompilerPluginExecutablePath(blobData);
case options_block::PLUGIN_SEARCH_OPTION: {
unsigned kind;
options_block::ResilienceStrategyLayout::readRecord(scratch, kind);
PluginSearchOption::Kind optKind;
switch (PluginSearchOptionKind(kind)) {
case PluginSearchOptionKind::PluginPath:
optKind = PluginSearchOption::Kind::PluginPath;
break;
case PluginSearchOptionKind::ExternalPluginPath:
optKind = PluginSearchOption::Kind::ExternalPluginPath;
break;
case PluginSearchOptionKind::LoadPluginLibrary:
optKind = PluginSearchOption::Kind::LoadPluginLibrary;
break;
case PluginSearchOptionKind::LoadPluginExecutable:
optKind = PluginSearchOption::Kind::LoadPluginExecutable;
break;
}
extendedInfo.addPluginSearchOption({optKind, blobData});
break;
}
case options_block::IS_SIB:
bool IsSIB;
options_block::IsSIBLayout::readRecord(scratch, IsSIB);
Expand Down
39 changes: 16 additions & 23 deletions lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 792; // removed select_value
const uint16_t SWIFTMODULE_VERSION_MINOR = 793; // PluginSearchOption

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -651,6 +651,16 @@ enum class MacroIntroducedDeclNameKind : uint8_t {
};
using MacroIntroducedDeclNameKindField = BCFixed<4>;

// These IDs must \em not be renumbered or reordered without incrementing
// the module version.
enum class PluginSearchOptionKind : uint8_t {
PluginPath,
ExternalPluginPath,
LoadPluginLibrary,
LoadPluginExecutable,
};
using PluginSearchOptionKindField = BCFixed<3>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like a BCFixed<2> should be enough for the current enum size.


// Encodes a VersionTuple:
//
// Major
Expand Down Expand Up @@ -884,10 +894,7 @@ namespace options_block {
IS_CONCURRENCY_CHECKED,
MODULE_PACKAGE_NAME,
MODULE_EXPORT_AS_NAME,
PLUGIN_SEARCH_PATH,
EXTERNAL_SEARCH_PLUGIN_PATH,
COMPILER_PLUGIN_LIBRARY_PATH,
COMPILER_PLUGIN_EXECUTABLE_PATH,
PLUGIN_SEARCH_OPTION,
HAS_CXX_INTEROPERABILITY_ENABLED,
};

Expand All @@ -901,24 +908,10 @@ namespace options_block {
BCBlob // -Xcc flag, as string
>;

using PluginSearchPathLayout = BCRecordLayout<
PLUGIN_SEARCH_PATH,
BCBlob // -plugin-path value
>;

using ExternalPluginSearchPathLayout = BCRecordLayout<
EXTERNAL_SEARCH_PLUGIN_PATH,
BCBlob // -external-plugin-path value
>;

using CompilerPluginLibraryPathLayout = BCRecordLayout<
COMPILER_PLUGIN_LIBRARY_PATH,
BCBlob // -load-plugin-library value
>;

using CompilerPluginExecutablePathLayout = BCRecordLayout<
COMPILER_PLUGIN_EXECUTABLE_PATH,
BCBlob // -load-plugin-executable value
using PluginSearchOptionLayout = BCRecordLayout<
PLUGIN_SEARCH_OPTION,
PluginSearchOptionKindField, // kind
BCBlob // option value string
>;

using IsSIBLayout = BCRecordLayout<
Expand Down
Loading