Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 13a29a0

Browse files
authoredJun 16, 2023
Merge pull request #66689 from rintaro/macros-serialization-pluginopts
[Macros] Update plugin search options serialization
2 parents 22348e6 + 6fa0c14 commit 13a29a0

File tree

10 files changed

+207
-170
lines changed

10 files changed

+207
-170
lines changed
 

‎include/swift/AST/SearchPathOptions.h

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#define SWIFT_AST_SEARCHPATHOPTIONS_H
1515

1616
#include "swift/Basic/ArrayRefView.h"
17+
#include "swift/Basic/ExternalUnion.h"
1718
#include "swift/Basic/PathRemapper.h"
18-
#include "swift/Basic/TaggedUnion.h"
1919
#include "llvm/ADT/Hashing.h"
2020
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2121
#include "llvm/ADT/StringMap.h"
@@ -187,25 +187,81 @@ struct ExternalPluginSearchPathAndServerPath {
187187
std::string ServerPath;
188188
};
189189

190-
namespace PluginSearchOption {
191-
struct LoadPluginLibrary {
192-
std::string LibraryPath;
193-
};
194-
struct LoadPluginExecutable {
195-
std::string ExecutablePath;
196-
std::vector<std::string> ModuleNames;
197-
};
198-
struct PluginPath {
199-
std::string SearchPath;
200-
};
201-
struct ExternalPluginPath {
202-
std::string SearchPath;
203-
std::string ServerPath;
204-
};
190+
class PluginSearchOption {
191+
public:
192+
struct LoadPluginLibrary {
193+
std::string LibraryPath;
194+
};
195+
struct LoadPluginExecutable {
196+
std::string ExecutablePath;
197+
std::vector<std::string> ModuleNames;
198+
};
199+
struct PluginPath {
200+
std::string SearchPath;
201+
};
202+
struct ExternalPluginPath {
203+
std::string SearchPath;
204+
std::string ServerPath;
205+
};
206+
207+
enum class Kind : uint8_t {
208+
LoadPluginLibrary,
209+
LoadPluginExecutable,
210+
PluginPath,
211+
ExternalPluginPath,
212+
};
205213

206-
using Value = TaggedUnion<LoadPluginLibrary, LoadPluginExecutable, PluginPath,
207-
ExternalPluginPath>;
208-
} // namespace PluginSearchOption
214+
private:
215+
using Members = ExternalUnionMembers<LoadPluginLibrary, LoadPluginExecutable,
216+
PluginPath, ExternalPluginPath>;
217+
static Members::Index getIndexForKind(Kind kind) {
218+
switch (kind) {
219+
case Kind::LoadPluginLibrary:
220+
return Members::indexOf<LoadPluginLibrary>();
221+
case Kind::LoadPluginExecutable:
222+
return Members::indexOf<LoadPluginExecutable>();
223+
case Kind::PluginPath:
224+
return Members::indexOf<PluginPath>();
225+
case Kind::ExternalPluginPath:
226+
return Members::indexOf<ExternalPluginPath>();
227+
}
228+
};
229+
using Storage = ExternalUnion<Kind, Members, getIndexForKind>;
230+
231+
Kind kind;
232+
Storage storage;
233+
234+
public:
235+
PluginSearchOption(const LoadPluginLibrary &v)
236+
: kind(Kind::LoadPluginLibrary) {
237+
storage.emplace<LoadPluginLibrary>(kind, v);
238+
}
239+
PluginSearchOption(const LoadPluginExecutable &v)
240+
: kind(Kind::LoadPluginExecutable) {
241+
storage.emplace<LoadPluginExecutable>(kind, v);
242+
}
243+
PluginSearchOption(const PluginPath &v) : kind(Kind::PluginPath) {
244+
storage.emplace<PluginPath>(kind, v);
245+
}
246+
PluginSearchOption(const ExternalPluginPath &v)
247+
: kind(Kind::ExternalPluginPath) {
248+
storage.emplace<ExternalPluginPath>(kind, v);
249+
}
250+
251+
Kind getKind() const { return kind; }
252+
253+
template <typename T>
254+
const T *dyn_cast() const {
255+
if (Members::indexOf<T>() != getIndexForKind(kind))
256+
return nullptr;
257+
return &storage.get<T>(kind);
258+
}
259+
260+
template <typename T>
261+
const T &get() const {
262+
return storage.get<T>(kind);
263+
}
264+
};
209265

210266
/// Options for controlling search path behavior.
211267
class SearchPathOptions {
@@ -383,7 +439,7 @@ class SearchPathOptions {
383439
std::vector<std::string> RuntimeLibraryPaths;
384440

385441
/// Plugin search path options.
386-
std::vector<PluginSearchOption::Value> PluginSearchOpts;
442+
std::vector<PluginSearchOption> PluginSearchOpts;
387443

388444
/// Don't look in for compiler-provided modules.
389445
bool SkipRuntimeLibraryImportPaths = false;

‎include/swift/Serialization/SerializationOptions.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H
1414
#define SWIFT_SERIALIZATION_SERIALIZATIONOPTIONS_H
1515

16+
#include "swift/AST/SearchPathOptions.h"
1617
#include "swift/Basic/LLVM.h"
1718
#include "swift/Basic/PathRemapper.h"
1819
#include "llvm/Support/VersionTuple.h"
@@ -43,10 +44,7 @@ namespace swift {
4344
StringRef ModuleLinkName;
4445
StringRef ModuleInterface;
4546
std::vector<std::string> ExtraClangOptions;
46-
std::vector<std::string> PluginSearchPaths;
47-
std::vector<std::string> ExternalPluginSearchPaths;
48-
std::vector<std::string> CompilerPluginLibraryPaths;
49-
std::vector<std::string> CompilerPluginExecutablePaths;
47+
std::vector<swift::PluginSearchOption> PluginSearchOptions;
5048

5149
/// Path prefixes that should be rewritten in debug info.
5250
PathRemapper DebuggingOptionsPrefixMap;

‎include/swift/Serialization/Validation.h

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,8 @@ struct ValidationInfo {
111111
class ExtendedValidationInfo {
112112
SmallVector<StringRef, 4> ExtraClangImporterOpts;
113113

114-
SmallVector<StringRef, 1> PluginSearchPaths;
115-
SmallVector<StringRef, 1> ExternalPluginSearchPaths;
116-
SmallVector<StringRef, 1> CompilerPluginLibraryPaths;
117-
SmallVector<StringRef, 1> CompilerPluginExecutablePaths;
114+
SmallVector<std::pair<PluginSearchOption::Kind, StringRef>, 2>
115+
PluginSearchOptions;
118116

119117
std::string SDKPath;
120118
StringRef ModuleABIName;
@@ -149,32 +147,13 @@ class ExtendedValidationInfo {
149147
ExtraClangImporterOpts.push_back(option);
150148
}
151149

152-
ArrayRef<StringRef> getPluginSearchPaths() const {
153-
return PluginSearchPaths;
150+
ArrayRef<std::pair<PluginSearchOption::Kind, StringRef>>
151+
getPluginSearchOptions() const {
152+
return PluginSearchOptions;
154153
}
155-
void addPluginSearchPath(StringRef path) {
156-
PluginSearchPaths.push_back(path);
157-
}
158-
159-
ArrayRef<StringRef> getExternalPluginSearchPaths() const {
160-
return ExternalPluginSearchPaths;
161-
}
162-
void addExternalPluginSearchPath(StringRef path) {
163-
ExternalPluginSearchPaths.push_back(path);
164-
}
165-
166-
ArrayRef<StringRef> getCompilerPluginLibraryPaths() const {
167-
return CompilerPluginLibraryPaths;
168-
}
169-
void addCompilerPluginLibraryPath(StringRef path) {
170-
CompilerPluginLibraryPaths.push_back(path);
171-
}
172-
173-
ArrayRef<StringRef> getCompilerPluginExecutablePaths() const {
174-
return CompilerPluginExecutablePaths;
175-
}
176-
void addCompilerPluginExecutablePath(StringRef path) {
177-
CompilerPluginExecutablePaths.push_back(path);
154+
void addPluginSearchOption(
155+
const std::pair<PluginSearchOption::Kind, StringRef> &opt) {
156+
PluginSearchOptions.push_back(opt);
178157
}
179158

180159
bool isSIB() const { return Bits.IsSIB; }

‎lib/AST/PluginLoader.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,31 @@ PluginLoader::lookupPluginByModuleName(Identifier moduleName) {
6161

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

7374
// Try '-load-plugin-executable'.
74-
if (auto *v = entry.dyn_cast<LoadPluginExecutable>()) {
75+
case PluginSearchOption::Kind::LoadPluginExecutable: {
76+
auto &val = entry.get<PluginSearchOption::LoadPluginExecutable>();
7577
auto found = ExecutablePluginPaths.find(moduleName);
76-
if (found != ExecutablePluginPaths.end()) {
77-
return {"", std::string(found->second)};
78+
if (found != ExecutablePluginPaths.end() &&
79+
found->second == val.ExecutablePath) {
80+
return {"", val.ExecutablePath};
7881
}
7982
continue;
8083
}
8184

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

9296
// Try '-external-plugin-path'.
93-
if (auto *v = entry.dyn_cast<ExternalPluginPath>()) {
94-
SmallString<128> fullPath(v->SearchPath);
97+
case PluginSearchOption::Kind::ExternalPluginPath: {
98+
auto &val = entry.get<PluginSearchOption::ExternalPluginPath>();
99+
SmallString<128> fullPath(val.SearchPath);
95100
llvm::sys::path::append(fullPath, pluginLibBasename);
96101
if (fs->exists(fullPath)) {
97-
return {std::string(fullPath), v->ServerPath};
102+
return {std::string(fullPath), val.ServerPath};
98103
}
99104
continue;
100105
}
106+
}
101107
}
102108

103109
return {};

‎lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -209,36 +209,8 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
209209
serializationOpts.ExtraClangOptions = getClangImporterOptions().ExtraArgs;
210210
}
211211

212-
// FIXME: Preserve the order of these options.
213-
for (auto &elem : getSearchPathOptions().PluginSearchOpts) {
214-
// '-plugin-path' options.
215-
if (auto *arg = elem.dyn_cast<PluginSearchOption::PluginPath>()) {
216-
serializationOpts.PluginSearchPaths.push_back(arg->SearchPath);
217-
continue;
218-
}
219-
220-
// '-external-plugin-path' options.
221-
if (auto *arg = elem.dyn_cast<PluginSearchOption::ExternalPluginPath>()) {
222-
serializationOpts.ExternalPluginSearchPaths.push_back(
223-
arg->SearchPath + "#" + arg->ServerPath);
224-
continue;
225-
}
226-
227-
// '-load-plugin-library' options.
228-
if (auto *arg = elem.dyn_cast<PluginSearchOption::LoadPluginLibrary>()) {
229-
serializationOpts.CompilerPluginLibraryPaths.push_back(arg->LibraryPath);
230-
continue;
231-
}
232-
233-
// '-load-plugin-executable' options.
234-
if (auto *arg = elem.dyn_cast<PluginSearchOption::LoadPluginExecutable>()) {
235-
std::string optStr = arg->ExecutablePath + "#";
236-
llvm::interleave(
237-
arg->ModuleNames, [&](auto &name) { optStr += name; },
238-
[&]() { optStr += ","; });
239-
serializationOpts.CompilerPluginExecutablePaths.push_back(optStr);
240-
}
241-
}
212+
serializationOpts.PluginSearchOptions =
213+
getSearchPathOptions().PluginSearchOpts;
242214

243215
serializationOpts.DisableCrossModuleIncrementalInfo =
244216
opts.DisableCrossModuleIncrementalBuild;

‎lib/Serialization/ModuleFileSharedCore.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,27 @@ static bool readOptionsBlock(llvm::BitstreamCursor &cursor,
126126
case options_block::XCC:
127127
extendedInfo.addExtraClangImporterOption(blobData);
128128
break;
129-
case options_block::PLUGIN_SEARCH_PATH:
130-
extendedInfo.addPluginSearchPath(blobData);
131-
break;
132-
case options_block::EXTERNAL_SEARCH_PLUGIN_PATH:
133-
extendedInfo.addExternalPluginSearchPath(blobData);
134-
break;
135-
case options_block::COMPILER_PLUGIN_LIBRARY_PATH:
136-
extendedInfo.addCompilerPluginLibraryPath(blobData);
137-
break;
138-
case options_block::COMPILER_PLUGIN_EXECUTABLE_PATH:
139-
extendedInfo.addCompilerPluginExecutablePath(blobData);
129+
case options_block::PLUGIN_SEARCH_OPTION: {
130+
unsigned kind;
131+
options_block::ResilienceStrategyLayout::readRecord(scratch, kind);
132+
PluginSearchOption::Kind optKind;
133+
switch (PluginSearchOptionKind(kind)) {
134+
case PluginSearchOptionKind::PluginPath:
135+
optKind = PluginSearchOption::Kind::PluginPath;
136+
break;
137+
case PluginSearchOptionKind::ExternalPluginPath:
138+
optKind = PluginSearchOption::Kind::ExternalPluginPath;
139+
break;
140+
case PluginSearchOptionKind::LoadPluginLibrary:
141+
optKind = PluginSearchOption::Kind::LoadPluginLibrary;
142+
break;
143+
case PluginSearchOptionKind::LoadPluginExecutable:
144+
optKind = PluginSearchOption::Kind::LoadPluginExecutable;
145+
break;
146+
}
147+
extendedInfo.addPluginSearchOption({optKind, blobData});
140148
break;
149+
}
141150
case options_block::IS_SIB:
142151
bool IsSIB;
143152
options_block::IsSIBLayout::readRecord(scratch, IsSIB);

‎lib/Serialization/ModuleFormat.h

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 792; // removed select_value
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 793; // PluginSearchOption
6262

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

654+
// These IDs must \em not be renumbered or reordered without incrementing
655+
// the module version.
656+
enum class PluginSearchOptionKind : uint8_t {
657+
PluginPath,
658+
ExternalPluginPath,
659+
LoadPluginLibrary,
660+
LoadPluginExecutable,
661+
};
662+
using PluginSearchOptionKindField = BCFixed<3>;
663+
654664
// Encodes a VersionTuple:
655665
//
656666
// Major
@@ -884,10 +894,7 @@ namespace options_block {
884894
IS_CONCURRENCY_CHECKED,
885895
MODULE_PACKAGE_NAME,
886896
MODULE_EXPORT_AS_NAME,
887-
PLUGIN_SEARCH_PATH,
888-
EXTERNAL_SEARCH_PLUGIN_PATH,
889-
COMPILER_PLUGIN_LIBRARY_PATH,
890-
COMPILER_PLUGIN_EXECUTABLE_PATH,
897+
PLUGIN_SEARCH_OPTION,
891898
HAS_CXX_INTEROPERABILITY_ENABLED,
892899
};
893900

@@ -901,24 +908,10 @@ namespace options_block {
901908
BCBlob // -Xcc flag, as string
902909
>;
903910

904-
using PluginSearchPathLayout = BCRecordLayout<
905-
PLUGIN_SEARCH_PATH,
906-
BCBlob // -plugin-path value
907-
>;
908-
909-
using ExternalPluginSearchPathLayout = BCRecordLayout<
910-
EXTERNAL_SEARCH_PLUGIN_PATH,
911-
BCBlob // -external-plugin-path value
912-
>;
913-
914-
using CompilerPluginLibraryPathLayout = BCRecordLayout<
915-
COMPILER_PLUGIN_LIBRARY_PATH,
916-
BCBlob // -load-plugin-library value
917-
>;
918-
919-
using CompilerPluginExecutablePathLayout = BCRecordLayout<
920-
COMPILER_PLUGIN_EXECUTABLE_PATH,
921-
BCBlob // -load-plugin-executable value
911+
using PluginSearchOptionLayout = BCRecordLayout<
912+
PLUGIN_SEARCH_OPTION,
913+
PluginSearchOptionKindField, // kind
914+
BCBlob // option value string
922915
>;
923916

924917
using IsSIBLayout = BCRecordLayout<

0 commit comments

Comments
 (0)