Skip to content

Frontend: teach final module emitting jobs to dump a placeholder file for module semantic info #39689

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 1 commit into from
Oct 12, 2021
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 @@ -131,6 +131,8 @@ 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(error_mode_cannot_emit_module_semantic_info,none,
"this mode does not support emitting module semantic info", ())
ERROR(cannot_emit_ir_skipping_function_bodies,none,
"the -experimental-skip-*-function-bodies* flags do not support "
"emitting IR", ())
Expand Down
6 changes: 6 additions & 0 deletions include/swift/Basic/SupplementaryOutputPaths.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ struct SupplementaryOutputPaths {
/// The output path to generate ABI baseline.
std::string ABIDescriptorOutputPath;

/// The output path of Swift semantic info for this module.
std::string ModuleSemanticInfoOutputPath;

/// The output path for YAML optimization record file.
std::string YAMLOptRecordPath;

Expand Down Expand Up @@ -189,6 +192,8 @@ struct SupplementaryOutputPaths {
fn(YAMLOptRecordPath);
if (!BitstreamOptRecordPath.empty())
fn(BitstreamOptRecordPath);
if (!ModuleSemanticInfoOutputPath.empty())
fn(ModuleSemanticInfoOutputPath);
}

bool empty() const {
Expand All @@ -198,6 +203,7 @@ struct SupplementaryOutputPaths {
SerializedDiagnosticsPath.empty() && LoadedModuleTracePath.empty() &&
TBDPath.empty() && ModuleInterfaceOutputPath.empty() &&
ModuleSourceInfoOutputPath.empty() && ABIDescriptorOutputPath.empty() &&
ModuleSemanticInfoOutputPath.empty() &&
YAMLOptRecordPath.empty() && BitstreamOptRecordPath.empty();
}
};
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 @@ -257,6 +257,7 @@ class FrontendInputsAndOutputs {
bool hasModuleInterfaceOutputPath() const;
bool hasPrivateModuleInterfaceOutputPath() const;
bool hasABIDescriptorOutputPath() const;
bool hasModuleSemanticInfoOutputPath() const;
bool hasModuleSummaryOutputPath() const;
bool hasTBDPath() const;
bool hasYAMLOptRecordPath() 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 @@ -437,6 +437,7 @@ class FrontendOptions {
static bool canActionEmitModuleSummary(ActionType);
static bool canActionEmitInterface(ActionType);
static bool canActionEmitABIDescriptor(ActionType);
static bool canActionEmitModuleSemanticInfo(ActionType);

public:
static bool doesActionGenerateSIL(ActionType);
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 @@ -68,6 +68,10 @@ def emit_abi_descriptor_path
: Separate<["-"], "emit-abi-descriptor-path">, MetaVarName<"<path>">,
HelpText<"Output the ABI descriptor of current module to <path>">;

def emit_module_semantic_info_path
: Separate<["-"], "emit-module-semantic-info-path">, MetaVarName<"<path>">,
HelpText<"Output semantic info 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 @@ -680,6 +680,11 @@ bool ArgsToFrontendOptionsConverter::checkUnusedSupplementaryOutputPaths()
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_abi_descriptor);
return true;
}
if (!FrontendOptions::canActionEmitModuleSemanticInfo(Opts.RequestedAction) &&
Opts.InputsAndOutputs.hasModuleSemanticInfoOutputPath()) {
Diags.diagnose(SourceLoc(), diag::error_mode_cannot_emit_module_semantic_info);
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
7 changes: 6 additions & 1 deletion lib/Frontend/ArgsToFrontendOutputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,16 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
options::OPT_emit_module_summary_path);
auto abiDescriptorOutput = getSupplementaryFilenamesFromArguments(
options::OPT_emit_abi_descriptor_path);
auto moduleSemanticInfoOutput = getSupplementaryFilenamesFromArguments(
options::OPT_emit_module_semantic_info_path);
auto optRecordOutput = getSupplementaryFilenamesFromArguments(
options::OPT_save_optimization_record_path);
if (!objCHeaderOutput || !moduleOutput || !moduleDocOutput ||
!dependenciesFile || !referenceDependenciesFile ||
!serializedDiagnostics || !fixItsOutput || !loadedModuleTrace || !TBD ||
!moduleInterfaceOutput || !privateModuleInterfaceOutput ||
!moduleSourceInfoOutput || !moduleSummaryOutput || !abiDescriptorOutput ||
!optRecordOutput) {
!moduleSemanticInfoOutput || !optRecordOutput) {
return None;
}
std::vector<SupplementaryOutputPaths> result;
Expand All @@ -371,6 +373,7 @@ SupplementaryOutputPathsComputer::getSupplementaryOutputPathsFromArguments()
sop.ModuleSourceInfoOutputPath = (*moduleSourceInfoOutput)[i];
sop.ModuleSummaryOutputPath = (*moduleSummaryOutput)[i];
sop.ABIDescriptorOutputPath = (*abiDescriptorOutput)[i];
sop.ModuleSemanticInfoOutputPath = (*moduleSemanticInfoOutput)[i];
sop.YAMLOptRecordPath = (*optRecordOutput)[i];
sop.BitstreamOptRecordPath = (*optRecordOutput)[i];
result.push_back(sop);
Expand Down Expand Up @@ -473,6 +476,7 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(

// There is no non-path form of -emit-abi-descriptor-path
auto ABIDescriptorOutputPath = pathsFromArguments.ABIDescriptorOutputPath;
auto ModuleSemanticInfoOutputPath = pathsFromArguments.ModuleSemanticInfoOutputPath;
ID emitModuleOption;
std::string moduleExtension;
std::string mainOutputIfUsableForModule;
Expand Down Expand Up @@ -508,6 +512,7 @@ SupplementaryOutputPathsComputer::computeOutputPathsForOneInput(
sop.ModuleSourceInfoOutputPath = moduleSourceInfoOutputPath;
sop.ModuleSummaryOutputPath = moduleSummaryOutputPath;
sop.ABIDescriptorOutputPath = ABIDescriptorOutputPath;
sop.ModuleSemanticInfoOutputPath = ModuleSemanticInfoOutputPath;
sop.YAMLOptRecordPath = YAMLOptRecordPath;
sop.BitstreamOptRecordPath = bitstreamOptRecordPath;
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 @@ -509,6 +509,12 @@ bool FrontendInputsAndOutputs::hasABIDescriptorOutputPath() const {
return outs.ABIDescriptorOutputPath;
});
}
bool FrontendInputsAndOutputs::hasModuleSemanticInfoOutputPath() const {
return hasSupplementaryOutputPath(
[](const SupplementaryOutputPaths &outs) -> const std::string & {
return outs.ModuleSemanticInfoOutputPath;
});
}
bool FrontendInputsAndOutputs::hasModuleSummaryOutputPath() const {
return hasSupplementaryOutputPath(
[](const SupplementaryOutputPaths &outs) -> const std::string & {
Expand Down
42 changes: 42 additions & 0 deletions lib/Frontend/FrontendOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,48 @@ bool FrontendOptions::canActionEmitLoadedModuleTrace(ActionType action) {
}
llvm_unreachable("unhandled action");
}
bool FrontendOptions::canActionEmitModuleSemanticInfo(ActionType action) {
switch (action) {
case ActionType::MergeModules:
case ActionType::EmitModuleOnly:
case ActionType::CompileModuleFromInterface:
// For test
case ActionType::Typecheck:
return true;
case ActionType::NoneAction:
case ActionType::Parse:
case ActionType::ResolveImports:
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::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::canActionEmitABIDescriptor(ActionType action) {
switch (action) {
case ActionType::MergeModules:
Expand Down
20 changes: 20 additions & 0 deletions lib/FrontendTool/FrontendTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/FileSystem.h"

#if __has_include(<unistd.h>)
#include <unistd.h>
Expand Down Expand Up @@ -660,6 +661,22 @@ static void emitSwiftdepsForAllPrimaryInputsIfNeeded(
}
}

static bool writeModuleSemanticInfoIfNeeded(CompilerInstance &Instance) {
const auto &Invocation = Instance.getInvocation();
const auto &frontendOpts = Invocation.getFrontendOptions();
if (!frontendOpts.InputsAndOutputs.hasModuleSemanticInfoOutputPath())
return false;
std::error_code EC;
assert(frontendOpts.InputsAndOutputs.isWholeModule() &&
"TBDPath only makes sense when the whole module can be seen");
auto ModuleSemanticPath = frontendOpts.InputsAndOutputs
.getPrimarySpecificPathsForAtMostOnePrimary().SupplementaryOutputs
.ModuleSemanticInfoOutputPath;
llvm::raw_fd_ostream OS(ModuleSemanticPath, EC, llvm::sys::fs::OF_None);
OS << "{}\n";
return false;
}

static bool writeTBDIfNeeded(CompilerInstance &Instance) {
const auto &Invocation = Instance.getInvocation();
const auto &frontendOpts = Invocation.getFrontendOptions();
Expand Down Expand Up @@ -842,6 +859,9 @@ static bool emitAnyWholeModulePostTypeCheckSupplementaryOutputs(
hadAnyError |= writeTBDIfNeeded(Instance);
}

{
hadAnyError |= writeModuleSemanticInfoIfNeeded(Instance);
}
return hadAnyError;
}

Expand Down
7 changes: 7 additions & 0 deletions test/IDE/emit-module-semantic.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// REQUIRES: VENDOR=apple
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -emit-module %s -emit-module-path %t/Foo.swiftmodule -module-name Foo -emit-module-semantic-info-path %t/Foo.swiftsemanticinfo

// RUN: ls %t/Foo.swiftmodule
// RUN: ls %t/Foo.swiftsemanticinfo