Skip to content

Frontend: teach -compile-module-from-interface action to emit ABI descriptor as byproduct #38979

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
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ ERROR(error_mode_cannot_emit_module_summary,none,
"this mode does not support emitting module summary files", ())
ERROR(error_mode_cannot_emit_symbol_graph,none,
"this mode does not support emitting symbol graph files", ())
ERROR(error_mode_cannot_emit_abi_descriptor,none,
"this mode does not support emitting ABI descriptor", ())
ERROR(cannot_emit_ir_skipping_function_bodies,none,
"the -experimental-skip-*-function-bodies* flags do not support "
"emitting IR", ())
Expand Down
7 changes: 6 additions & 1 deletion include/swift/Basic/SupplementaryOutputPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ struct SupplementaryOutputPaths {
/// The path to which we should emit module summary file.
std::string ModuleSummaryOutputPath;

/// The output path to generate ABI baseline.
std::string ABIDescriptorOutputPath;

SupplementaryOutputPaths() = default;
SupplementaryOutputPaths(const SupplementaryOutputPaths &) = default;

Expand Down Expand Up @@ -174,6 +177,8 @@ struct SupplementaryOutputPaths {
fn(PrivateModuleInterfaceOutputPath);
if (!ModuleSummaryOutputPath.empty())
fn(ModuleSummaryOutputPath);
if (!ABIDescriptorOutputPath.empty())
fn(ABIDescriptorOutputPath);
}

bool empty() const {
Expand All @@ -182,7 +187,7 @@ struct SupplementaryOutputPaths {
ReferenceDependenciesFilePath.empty() &&
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
TBDPath.empty() && ModuleInterfaceOutputPath.empty() &&
ModuleSourceInfoOutputPath.empty();
ModuleSourceInfoOutputPath.empty() && ABIDescriptorOutputPath.empty();
}
};
} // namespace swift
Expand Down
1 change: 1 addition & 0 deletions include/swift/Frontend/FrontendInputsAndOutputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class FrontendInputsAndOutputs {
bool hasModuleSourceInfoOutputPath() const;
bool hasModuleInterfaceOutputPath() const;
bool hasPrivateModuleInterfaceOutputPath() const;
bool hasABIDescriptorOutputPath() const;
bool hasModuleSummaryOutputPath() const;
bool hasTBDPath() const;

Expand Down
1 change: 1 addition & 0 deletions include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ class FrontendOptions {
static bool canActionEmitModuleDoc(ActionType);
static bool canActionEmitModuleSummary(ActionType);
static bool canActionEmitInterface(ActionType);
static bool canActionEmitABIDescriptor(ActionType);

public:
static bool doesActionGenerateSIL(ActionType);
Expand Down
3 changes: 2 additions & 1 deletion include/swift/Frontend/ModuleInterfaceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,8 @@ class ModuleInterfaceLoader : public SerializedModuleLoaderBase {
const ClangImporterOptions &ClangOpts, StringRef CacheDir,
StringRef PrebuiltCacheDir, StringRef BackupInterfaceDir,
StringRef ModuleName, StringRef InPath,
StringRef OutPath, bool SerializeDependencyHashes,
StringRef OutPath, StringRef ABIOutputPath,
bool SerializeDependencyHashes,
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions Opts,
RequireOSSAModules_t RequireOSSAModules);
};
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ def emit_fixits_path
: Separate<["-"], "emit-fixits-path">, MetaVarName<"<path>">,
HelpText<"Output compiler fixits as source edits to <path>">;

def emit_abi_descriptor_path
: Separate<["-"], "emit-abi-descriptor-path">, MetaVarName<"<path>">,
HelpText<"Output the ABI descriptor of current module to <path>">;

def serialize_module_interface_dependency_hashes
: Flag<["-"], "serialize-module-interface-dependency-hashes">,
Flags<[HelpHidden]>;
Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,11 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_doc);
return true;
}
if (!FrontendOptions::canActionEmitABIDescriptor(Opts.RequestedAction) &&
Opts.InputsAndOutputs.hasABIDescriptorOutputPath()) {
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_abi_descriptor);
return true;
}
// If we cannot emit module doc, we cannot emit source information file either.
if (!FrontendOptions::canActionEmitModuleDoc(Opts.RequestedAction) &&
Opts.InputsAndOutputs.hasModuleSourceInfoOutputPath()) {
Expand Down
8 changes: 7 additions & 1 deletion lib/Frontend/ArgsToFrontendOutputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,13 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
options::OPT_emit_module_source_info_path);
auto moduleSummaryOutput = getSupplementaryFilenamesFromArguments(
options::OPT_emit_module_summary_path);
auto abiDescriptorOutput = getSupplementaryFilenamesFromArguments(
options::OPT_emit_abi_descriptor_path);
if (!objCHeaderOutput || !moduleOutput || !moduleDocOutput ||
!dependenciesFile || !referenceDependenciesFile ||
!serializedDiagnostics || !fixItsOutput || !loadedModuleTrace || !TBD ||
!moduleInterfaceOutput || !privateModuleInterfaceOutput ||
!moduleSourceInfoOutput || !moduleSummaryOutput) {
!moduleSourceInfoOutput || !moduleSummaryOutput || !abiDescriptorOutput) {
return None;
}
std::vector<SupplementaryOutputPaths> result;
Expand All @@ -365,6 +367,7 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
sop.PrivateModuleInterfaceOutputPath = (*privateModuleInterfaceOutput)[i];
sop.ModuleSourceInfoOutputPath = (*moduleSourceInfoOutput)[i];
sop.ModuleSummaryOutputPath = (*moduleSummaryOutput)[i];
sop.ABIDescriptorOutputPath = (*abiDescriptorOutput)[i];
result.push_back(sop);
}
return result;
Expand Down Expand Up @@ -463,6 +466,8 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
auto PrivateModuleInterfaceOutputPath =
pathsFromArguments.PrivateModuleInterfaceOutputPath;

// There is no non-path form of -emit-abi-descriptor-path
auto ABIDescriptorOutputPath = pathsFromArguments.ABIDescriptorOutputPath;
ID emitModuleOption;
std::string moduleExtension;
std::string mainOutputIfUsableForModule;
Expand All @@ -488,6 +493,7 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
sop.PrivateModuleInterfaceOutputPath = PrivateModuleInterfaceOutputPath;
sop.ModuleSourceInfoOutputPath = moduleSourceInfoOutputPath;
sop.ModuleSummaryOutputPath = moduleSummaryOutputPath;
sop.ABIDescriptorOutputPath = ABIDescriptorOutputPath;
return sop;
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Frontend/FrontendInputsAndOutputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,12 @@ bool FrontendInputsAndOutputs::hasPrivateModuleInterfaceOutputPath() const {
return outs.PrivateModuleInterfaceOutputPath;
});
}
bool FrontendInputsAndOutputs::hasABIDescriptorOutputPath() const {
return hasSupplementaryOutputPath(
[](const SupplementaryOutputPaths &outs) -> const std::string & {
return outs.ABIDescriptorOutputPath;
});
}
bool FrontendInputsAndOutputs::hasModuleSummaryOutputPath() const {
return hasSupplementaryOutputPath(
[](const SupplementaryOutputPaths &outs) -> const std::string & {
Expand Down
42 changes: 41 additions & 1 deletion lib/Frontend/FrontendOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,47 @@ bool FrontendOptions::canActionEmitLoadedModuleTrace(ActionType action) {
}
llvm_unreachable("unhandled action");
}

bool FrontendOptions::canActionEmitABIDescriptor(ActionType action) {
switch (action) {
case ActionType::CompileModuleFromInterface:
return true;
case ActionType::NoneAction:
case ActionType::Parse:
case ActionType::ResolveImports:
case ActionType::Typecheck:
case ActionType::DumpParse:
case ActionType::DumpInterfaceHash:
case ActionType::DumpAST:
case ActionType::EmitSyntax:
case ActionType::PrintAST:
case ActionType::EmitPCH:
case ActionType::DumpScopeMaps:
case ActionType::DumpTypeRefinementContexts:
case ActionType::DumpTypeInfo:
case ActionType::EmitSILGen:
case ActionType::TypecheckModuleFromInterface:
case ActionType::Immediate:
case ActionType::REPL:
case ActionType::EmitPCM:
case ActionType::DumpPCM:
case ActionType::ScanDependencies:
case ActionType::PrintVersion:
case ActionType::PrintFeature:
case ActionType::MergeModules:
case ActionType::EmitModuleOnly:
case ActionType::EmitSIL:
case ActionType::EmitSIBGen:
case ActionType::EmitSIB:
case ActionType::EmitIRGen:
case ActionType::EmitIR:
case ActionType::EmitBC:
case ActionType::EmitAssembly:
case ActionType::EmitObject:
case ActionType::EmitImportedModules:
return false;
}
llvm_unreachable("unhandled action");
}
bool FrontendOptions::canActionEmitModule(ActionType action) {
switch (action) {
case ActionType::NoneAction:
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/ModuleInterfaceBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ bool ModuleInterfaceBuilder::buildSwiftModuleInternal(
if (SubInstance.getDiags().hadAnyError()) {
return std::make_error_code(std::errc::not_supported);
}
if (!ABIDescriptorPath.empty()) {
swift::ide::api::dumpModuleContent(Mod, ABIDescriptorPath, true);
}
return std::error_code();
});
}, ThreadStackSize);
Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/ModuleInterfaceBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ModuleInterfaceBuilder {
const StringRef moduleCachePath;
const StringRef prebuiltCachePath;
const StringRef backupInterfaceDir;
const StringRef ABIDescriptorPath;
const bool disableInterfaceFileLock;
const SourceLoc diagnosticLoc;
DependencyTracker *const dependencyTracker;
Expand Down Expand Up @@ -96,6 +97,7 @@ class ModuleInterfaceBuilder {
StringRef moduleCachePath,
StringRef backupInterfaceDir,
StringRef prebuiltCachePath,
StringRef ABIDescriptorPath,
bool disableInterfaceFileLock = false,
SourceLoc diagnosticLoc = SourceLoc(),
DependencyTracker *tracker = nullptr)
Expand All @@ -104,6 +106,7 @@ class ModuleInterfaceBuilder {
interfacePath(interfacePath), moduleName(moduleName),
moduleCachePath(moduleCachePath), prebuiltCachePath(prebuiltCachePath),
backupInterfaceDir(backupInterfaceDir),
ABIDescriptorPath(ABIDescriptorPath),
disableInterfaceFileLock(disableInterfaceFileLock),
diagnosticLoc(diagnosticLoc), dependencyTracker(tracker) {}

Expand Down
11 changes: 6 additions & 5 deletions lib/Frontend/ModuleInterfaceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1007,7 +1007,7 @@ class ModuleInterfaceLoaderImpl {
ModuleInterfaceBuilder builder(
ctx.SourceMgr, diagsToUse,
astDelegate, interfacePath, moduleName, cacheDir,
prebuiltCacheDir, backupInterfaceDir,
prebuiltCacheDir, backupInterfaceDir, StringRef(),
Opts.disableInterfaceLock, diagnosticLoc,
dependencyTracker);
// If we found an out-of-date .swiftmodule, we still want to add it as
Expand Down Expand Up @@ -1039,7 +1039,7 @@ class ModuleInterfaceLoaderImpl {
// the genericSubInvocation we'll need to use to compute the cache paths.
ModuleInterfaceBuilder fallbackBuilder(
ctx.SourceMgr, &ctx.Diags, astDelegate, backupPath, moduleName, cacheDir,
prebuiltCacheDir, backupInterfaceDir,
prebuiltCacheDir, backupInterfaceDir, StringRef(),
Opts.disableInterfaceLock, diagnosticLoc,
dependencyTracker);
if (rebuildInfo.sawOutOfDateModule(modulePath))
Expand Down Expand Up @@ -1208,7 +1208,8 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
const ClangImporterOptions &ClangOpts, StringRef CacheDir,
StringRef PrebuiltCacheDir, StringRef BackupInterfaceDir,
StringRef ModuleName, StringRef InPath,
StringRef OutPath, bool SerializeDependencyHashes,
StringRef OutPath, StringRef ABIOutputPath,
bool SerializeDependencyHashes,
bool TrackSystemDependencies, ModuleInterfaceLoaderOptions LoaderOpts,
RequireOSSAModules_t RequireOSSAModules) {
InterfaceSubContextDelegateImpl astDelegate(
Expand All @@ -1218,7 +1219,7 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
SerializeDependencyHashes, TrackSystemDependencies, RequireOSSAModules);
ModuleInterfaceBuilder builder(SourceMgr, &Diags, astDelegate, InPath,
ModuleName, CacheDir, PrebuiltCacheDir,
BackupInterfaceDir,
BackupInterfaceDir, ABIOutputPath,
LoaderOpts.disableInterfaceLock);
// FIXME: We really only want to serialize 'important' dependencies here, if
// we want to ship the built swiftmodules to another machine.
Expand All @@ -1236,7 +1237,7 @@ bool ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
assert(!backInPath.empty());
ModuleInterfaceBuilder backupBuilder(SourceMgr, &Diags, astDelegate, backInPath,
ModuleName, CacheDir, PrebuiltCacheDir,
BackupInterfaceDir,
BackupInterfaceDir, ABIOutputPath,
LoaderOpts.disableInterfaceLock);
// Ensure we can rebuild module after user changed the original interface file.
backupBuilder.addExtraDependency(InPath);
Expand Down
4 changes: 3 additions & 1 deletion lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,15 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
StringRef InputPath = FEOpts.InputsAndOutputs.getFilenameOfFirstInput();
StringRef PrebuiltCachePath = FEOpts.PrebuiltModuleCachePath;
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
StringRef ABIPath = Instance.getPrimarySpecificPathsForAtMostOnePrimary()
.SupplementaryOutputs.ABIDescriptorOutputPath;
return ModuleInterfaceLoader::buildSwiftModuleFromSwiftInterface(
Instance.getSourceMgr(), Instance.getDiags(),
Invocation.getSearchPathOptions(), Invocation.getLangOptions(),
Invocation.getClangImporterOptions(),
Invocation.getClangModuleCachePath(), PrebuiltCachePath,
FEOpts.BackupModuleInterfaceDir,
Invocation.getModuleName(), InputPath, Invocation.getOutputFilename(),
Invocation.getModuleName(), InputPath, Invocation.getOutputFilename(), ABIPath,
FEOpts.SerializeModuleInterfaceDependencyHashes,
FEOpts.shouldTrackSystemDependencies(), LoaderOpts,
RequireOSSAModules_t(Invocation.getSILOptions()));
Expand Down
13 changes: 13 additions & 0 deletions test/ModuleInterface/emit-abi-descriptor.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %empty-directory(%t)
// RUN: %empty-directory(%t/Foo.swiftmodule)
// RUN: %empty-directory(%t/ResourceDir/%target-sdk-name/prebuilt-modules/Foo.swiftmodule)
// RUN: echo "public func foo() {}" > %t/Foo.swift

// RUN: %target-swift-frontend -emit-module %t/Foo.swift -module-name Foo -emit-module-interface-path %t/Foo.swiftinterface
// RUN: %target-swift-frontend -compile-module-from-interface %t/Foo.swiftinterface -o %t/Foo.swiftmodule -module-name Foo -emit-abi-descriptor-path %t/Foo.json

// RUN: %FileCheck %s < %t/Foo.json

// CHECK: "kind": "Root"
// CHECK-NEXT: "name": "TopLevel"
// CHECK-NEXT: "printedName": "TopLevel"