Skip to content

Commit 5d3d934

Browse files
committed
Frontend: Inherit compiler debugging options during module interface actions.
When building a module interface for the -typecheck-module-from-interface or -compile-module-from-interface actions, inherit and honor compiler debugging options and emit debugging output at the end of the interface build.
1 parent 901e8be commit 5d3d934

File tree

9 files changed

+99
-49
lines changed

9 files changed

+99
-49
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,9 @@ class CompilerInstance {
798798
/// \returns true if any errors occurred.
799799
bool performSILProcessing(SILModule *silModule);
800800

801+
/// Dumps any debugging output for the compilation, if requested.
802+
void emitEndOfPipelineDebuggingOutput();
803+
801804
private:
802805
/// Creates a new source file for the main module.
803806
SourceFile *createSourceFileForMainModule(ModuleDecl *mod,

include/swift/Frontend/FrontendOptions.h

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ namespace llvm {
3535
namespace swift {
3636
enum class IntermoduleDepTrackingMode;
3737

38+
/// Options for debugging the behavior of the frontend.
39+
struct CompilerDebuggingOptions {
40+
/// Indicates whether or not the Clang importer should print statistics upon
41+
/// termination.
42+
bool PrintClangStats = false;
43+
44+
/// Indicates whether or not the availability scope trees built during
45+
/// compilation should be dumped upon termination.
46+
bool DumpAvailabilityScopes = false;
47+
48+
/// Indicates whether or not the Clang importer should dump lookup tables
49+
/// upon termination.
50+
bool DumpClangLookupTables = false;
51+
};
52+
3853
/// Options for controlling the behavior of the frontend.
3954
class FrontendOptions {
4055
friend class ArgsToFrontendOptionsConverter;
@@ -124,6 +139,9 @@ class FrontendOptions {
124139
/// A set of modules allowed to import this module.
125140
std::set<std::string> AllowableClients;
126141

142+
/// Options for debugging the compiler.
143+
CompilerDebuggingOptions CompilerDebuggingOpts;
144+
127145
/// Emit index data for imported serialized swift system modules.
128146
bool IndexSystemModules = false;
129147

@@ -302,18 +320,6 @@ class FrontendOptions {
302320
/// termination.
303321
bool PrintStats = false;
304322

305-
/// Indicates whether or not the Clang importer should print statistics upon
306-
/// termination.
307-
bool PrintClangStats = false;
308-
309-
/// Indicates whether or not the Clang importer should dump lookup tables
310-
/// upon termination.
311-
bool DumpClangLookupTables = false;
312-
313-
/// Indicates whether or not availability scopes should be dumped upon
314-
/// termination.
315-
bool DumpAvailabilityScopes = false;
316-
317323
/// Indicates whether standard help should be shown.
318324
bool PrintHelp = false;
319325

include/swift/Frontend/ModuleInterfaceLoader.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,8 @@ class ExplicitModuleMapParser {
455455
llvm::StringSaver Saver;
456456
};
457457

458-
struct ModuleInterfaceLoaderOptions {
458+
class ModuleInterfaceLoaderOptions {
459+
public:
459460
FrontendOptions::ActionType requestedAction =
460461
FrontendOptions::ActionType::EmitModuleOnly;
461462
bool remarkOnRebuildFromInterface = false;
@@ -464,28 +465,11 @@ struct ModuleInterfaceLoaderOptions {
464465
bool disableBuildingInterface = false;
465466
bool downgradeInterfaceVerificationError = false;
466467
bool strictImplicitModuleContext = false;
468+
CompilerDebuggingOptions compilerDebuggingOptions;
467469
std::string mainExecutablePath;
468-
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts):
469-
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
470-
disableInterfaceLock(Opts.DisableInterfaceFileLock),
471-
disableImplicitSwiftModule(Opts.DisableImplicitModules),
472-
disableBuildingInterface(Opts.DisableBuildingInterface),
473-
downgradeInterfaceVerificationError(Opts.DowngradeInterfaceVerificationError),
474-
strictImplicitModuleContext(Opts.StrictImplicitModuleContext),
475-
mainExecutablePath(Opts.MainExecutablePath)
476-
{
477-
switch (Opts.RequestedAction) {
478-
case FrontendOptions::ActionType::TypecheckModuleFromInterface:
479-
requestedAction = FrontendOptions::ActionType::Typecheck;
480-
break;
481-
case FrontendOptions::ActionType::ScanDependencies:
482-
requestedAction = Opts.RequestedAction;
483-
break;
484-
default:
485-
requestedAction = FrontendOptions::ActionType::EmitModuleOnly;
486-
break;
487-
}
488-
}
470+
471+
ModuleInterfaceLoaderOptions(const FrontendOptions &Opts,
472+
bool inheritDebuggingOpts = false);
489473
ModuleInterfaceLoaderOptions() = default;
490474
};
491475

lib/Frontend/ArgsToFrontendOptionsConverter.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,11 @@ bool ArgsToFrontendOptionsConverter::convert(
197197
computeDebugTimeOptions();
198198
computeTBDOptions();
199199

200-
Opts.DumpClangLookupTables |= Args.hasArg(OPT_dump_clang_lookup_tables);
201-
Opts.DumpAvailabilityScopes |= Args.hasArg(OPT_dump_availability_scopes);
200+
Opts.CompilerDebuggingOpts.DumpAvailabilityScopes |=
201+
Args.hasArg(OPT_dump_availability_scopes);
202+
203+
Opts.CompilerDebuggingOpts.DumpClangLookupTables |=
204+
Args.hasArg(OPT_dump_clang_lookup_tables);
202205

203206
Opts.CheckOnoneSupportCompleteness = Args.hasArg(OPT_check_onone_completeness);
204207

@@ -472,7 +475,8 @@ void ArgsToFrontendOptionsConverter::handleDebugCrashGroupArguments() {
472475
void ArgsToFrontendOptionsConverter::computePrintStatsOptions() {
473476
using namespace options;
474477
Opts.PrintStats |= Args.hasArg(OPT_print_stats);
475-
Opts.PrintClangStats |= Args.hasArg(OPT_print_clang_stats);
478+
Opts.CompilerDebuggingOpts.PrintClangStats |=
479+
Args.hasArg(OPT_print_clang_stats);
476480
Opts.PrintZeroStats |= Args.hasArg(OPT_print_zero_stats);
477481
#if defined(NDEBUG) && !LLVM_ENABLE_STATS
478482
if (Opts.PrintStats || Opts.PrintClangStats)

lib/Frontend/Frontend.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Frontend/Frontend.h"
1919
#include "swift/AST/ASTContext.h"
2020
#include "swift/AST/AvailabilityDomain.h"
21+
#include "swift/AST/AvailabilityScope.h"
2122
#include "swift/AST/DiagnosticsFrontend.h"
2223
#include "swift/AST/DiagnosticsSema.h"
2324
#include "swift/AST/FileSystem.h"
@@ -1867,6 +1868,23 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule) {
18671868
return false;
18681869
}
18691870

1871+
void CompilerInstance::emitEndOfPipelineDebuggingOutput() {
1872+
assert(hasASTContext());
1873+
auto &ctx = getASTContext();
1874+
const auto &Invocation = getInvocation();
1875+
const auto &opts = Invocation.getFrontendOptions().CompilerDebuggingOpts;
1876+
1877+
if (opts.PrintClangStats && ctx.getClangModuleLoader())
1878+
ctx.getClangModuleLoader()->printStatistics();
1879+
1880+
if (opts.DumpAvailabilityScopes)
1881+
getPrimaryOrMainSourceFile().getAvailabilityScope()->dump(llvm::errs(),
1882+
ctx.SourceMgr);
1883+
1884+
if (opts.DumpClangLookupTables && ctx.getClangModuleLoader())
1885+
ctx.getClangModuleLoader()->dumpSwiftLookupTables();
1886+
}
1887+
18701888
bool CompilerInstance::isCancellationRequested() const {
18711889
auto flag = getASTContext().CancellationFlag;
18721890
return flag && flag->load(std::memory_order_relaxed);

lib/Frontend/ModuleInterfaceBuilder.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ std::error_code ExplicitModuleInterfaceBuilder::buildSwiftModuleFromInterface(
203203
ArrayRef<std::string> CompiledCandidates,
204204
StringRef CompilerVersion) {
205205
auto Invocation = Instance.getInvocation();
206+
206207
// Try building forwarding module first. If succeed, return.
207208
if (Instance.getASTContext()
208209
.getModuleInterfaceChecker()
@@ -254,6 +255,9 @@ std::error_code ExplicitModuleInterfaceBuilder::buildSwiftModuleFromInterface(
254255
builtByCompiler);
255256
}
256257
}
258+
259+
// If requested, dump debugging output before exiting.
260+
Instance.emitEndOfPipelineDebuggingOutput();
257261
};
258262

259263
Instance.performSema();

lib/Frontend/ModuleInterfaceLoader.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,34 @@ bool ModuleInterfaceCheckerImpl::isCached(StringRef DepPath) {
12941294
return !PrebuiltCacheDir.empty() && DepPath.starts_with(PrebuiltCacheDir);
12951295
}
12961296

1297+
static FrontendOptions::ActionType
1298+
reqestedModuleLoaderActionForFrontendOpts(const FrontendOptions &Opts) {
1299+
switch (Opts.RequestedAction) {
1300+
case FrontendOptions::ActionType::TypecheckModuleFromInterface:
1301+
return FrontendOptions::ActionType::Typecheck;
1302+
case FrontendOptions::ActionType::ScanDependencies:
1303+
return Opts.RequestedAction;
1304+
default:
1305+
return FrontendOptions::ActionType::EmitModuleOnly;
1306+
}
1307+
}
1308+
1309+
ModuleInterfaceLoaderOptions::ModuleInterfaceLoaderOptions(
1310+
const FrontendOptions &Opts, bool inheritDebuggingOpts)
1311+
: requestedAction(reqestedModuleLoaderActionForFrontendOpts(Opts)),
1312+
remarkOnRebuildFromInterface(Opts.RemarkOnRebuildFromModuleInterface),
1313+
disableInterfaceLock(Opts.DisableInterfaceFileLock),
1314+
disableImplicitSwiftModule(Opts.DisableImplicitModules),
1315+
disableBuildingInterface(Opts.DisableBuildingInterface),
1316+
downgradeInterfaceVerificationError(
1317+
Opts.DowngradeInterfaceVerificationError),
1318+
strictImplicitModuleContext(Opts.StrictImplicitModuleContext),
1319+
mainExecutablePath(Opts.MainExecutablePath) {
1320+
if (inheritDebuggingOpts) {
1321+
compilerDebuggingOptions = Opts.CompilerDebuggingOpts;
1322+
}
1323+
}
1324+
12971325
bool ModuleInterfaceLoader::isCached(StringRef DepPath) {
12981326
return InterfaceChecker.isCached(DepPath);
12991327
}
@@ -1870,6 +1898,7 @@ InterfaceSubContextDelegateImpl::InterfaceSubContextDelegateImpl(
18701898
SubFEOpts.RequestedAction = LoaderOpts.requestedAction;
18711899
SubFEOpts.StrictImplicitModuleContext =
18721900
LoaderOpts.strictImplicitModuleContext;
1901+
SubFEOpts.CompilerDebuggingOpts = LoaderOpts.compilerDebuggingOptions;
18731902
if (!moduleCachePath.empty()) {
18741903
genericSubInvocation.setClangModuleCachePath(moduleCachePath);
18751904
}

lib/FrontendTool/FrontendTool.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ static bool buildModuleFromInterface(CompilerInstance &Instance) {
353353
assert(FEOpts.InputsAndOutputs.hasSingleInput());
354354
StringRef InputPath = FEOpts.InputsAndOutputs.getFilenameOfFirstInput();
355355
StringRef PrebuiltCachePath = FEOpts.PrebuiltModuleCachePath;
356-
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
356+
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts,
357+
/*inheritDebuggingOpts=*/true);
357358
StringRef ABIPath = Instance.getPrimarySpecificPathsForAtMostOnePrimary()
358359
.SupplementaryOutputs.ABIDescriptorOutputPath;
359360
bool IgnoreAdjacentModules = Instance.hasASTContext() &&
@@ -1021,25 +1022,16 @@ static void performEndOfPipelineActions(CompilerInstance &Instance) {
10211022
const auto &Invocation = Instance.getInvocation();
10221023
const auto &opts = Invocation.getFrontendOptions();
10231024

1024-
// If we were asked to print Clang stats, do so.
1025-
if (opts.PrintClangStats && ctx.getClangModuleLoader())
1026-
ctx.getClangModuleLoader()->printStatistics();
1027-
10281025
// Report AST stats if needed.
10291026
if (auto *stats = ctx.Stats)
10301027
countASTStats(*stats, Instance);
10311028

1032-
if (opts.DumpClangLookupTables && ctx.getClangModuleLoader())
1033-
ctx.getClangModuleLoader()->dumpSwiftLookupTables();
1034-
1035-
if (opts.DumpAvailabilityScopes)
1036-
Instance.getPrimaryOrMainSourceFile().getAvailabilityScope()->dump(
1037-
llvm::errs(), Instance.getASTContext().SourceMgr);
1038-
10391029
// Report mangling stats if there was no error.
10401030
if (!ctx.hadError())
10411031
Mangle::printManglingStats();
10421032

1033+
Instance.emitEndOfPipelineDebuggingOutput();
1034+
10431035
// Make sure we didn't load a module during a parse-only invocation, unless
10441036
// it's -emit-imported-modules, which can load modules.
10451037
auto action = opts.RequestedAction;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %empty-directory(%t)
2+
3+
// RUN: %target-swift-emit-module-interface(%t/Test.swiftinterface) %s -module-name Test
4+
// RUN: %target-swift-typecheck-module-from-interface(%t/Test.swiftinterface) -module-name Test -dump-availability-scopes 2>&1 | %FileCheck --strict-whitespace %s
5+
// RUN: %target-swift-frontend -compile-module-from-interface %t/Test.swiftinterface -o /dev/null -module-name Test -dump-availability-scopes 2>&1 | %FileCheck --strict-whitespace %s
6+
7+
// CHECK: {{^}}(root {{.*}} file={{.*}}{{/|\\}}availability-scopes.swift.tmp{{/|\\}}Test.swiftinterface
8+
// CHECK: {{^}} (decl {{.*}}unavailable=* decl=unavailable()
9+
@available(*, unavailable)
10+
public func unavailable() { }

0 commit comments

Comments
 (0)