Skip to content

[SILGen] Add flag to skip typechecking and SIL gen for all function bodies #34489

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
Nov 6, 2020
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
13 changes: 7 additions & 6 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -5659,13 +5659,14 @@ class AbstractFunctionDecl : public GenericContext, public ValueDecl {
/// Set a new body for the function.
void setBody(BraceStmt *S, BodyKind NewBodyKind);

/// Note that the body was skipped for this function. Function body
/// Note that the body was skipped for this function. Function body
/// cannot be attached after this call.
void setBodySkipped(SourceRange bodyRange) {
// FIXME: Remove 'Parsed' from this once we can delay parsing function
// bodies. Right now -experimental-skip-non-inlinable-function-bodies
// requires being able to change the state from Parsed to Skipped,
// because we're still eagerly parsing function bodies.
// FIXME: Remove 'Parsed' from this list once we can always delay
// parsing bodies. The -experimental-skip-*-function-bodies options
// do currently skip parsing, unless disabled through other means in
// SourceFile::hasDelayedBodyParsing (eg. needing to build the full
// syntax tree due to -verify-syntax-tree).
assert(getBodyKind() == BodyKind::None ||
getBodyKind() == BodyKind::Unparsed ||
getBodyKind() == BodyKind::Parsed);
Expand Down
7 changes: 3 additions & 4 deletions include/swift/AST/DiagnosticsFrontend.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -128,8 +128,7 @@ ERROR(error_mode_cannot_emit_interface,none,
ERROR(error_mode_cannot_emit_module_summary,none,
"this mode does not support emitting module summary files", ())
ERROR(cannot_emit_ir_skipping_function_bodies,none,
"-experimental-skip-non-inlinable-function-bodies does not support "
"emitting IR", ())
"-experimental-skip-*-function-bodies do not support emitting IR", ())

WARNING(emit_reference_dependencies_without_primary_file,none,
"ignoring -emit-reference-dependencies (requires -primary-file)", ())
Expand Down Expand Up @@ -416,7 +415,7 @@ ERROR(expectation_missing_opening_braces,none,
ERROR(expectation_missing_closing_braces,none,
"didn't find '}}' to match '{{' in expectation", ())

WARNING(module_incompatible_with_skip_function_bodies,none,
WARNING(module_incompatible_with_skip_non_inlinable_function_bodies,none,
"module '%0' cannot be built with "
"-experimental-skip-non-inlinable-function-bodies; this option has "
"been automatically disabled", (StringRef))
Expand Down
13 changes: 7 additions & 6 deletions include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -18,14 +18,15 @@
#ifndef SWIFT_AST_SILOPTIONS_H
#define SWIFT_AST_SILOPTIONS_H

#include "swift/Basic/Sanitizers.h"
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/FunctionBodySkipping.h"
#include "swift/Basic/OptimizationMode.h"
#include "swift/Basic/OptionSet.h"
#include "swift/Basic/Sanitizers.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Remarks/RemarkFormat.h"
#include <string>
#include <climits>
#include <string>

namespace swift {

Expand Down Expand Up @@ -88,8 +89,8 @@ class SILOptions {
/// and go from OSSA to non-ownership SIL.
bool StopOptimizationBeforeLoweringOwnership = false;

/// Whether to skip emitting non-inlinable function bodies.
bool SkipNonInlinableFunctionBodies = false;
// The kind of function bodies to skip emitting.
FunctionBodySkipping SkipFunctionBodies = FunctionBodySkipping::None;

/// Optimization mode being used.
OptimizationMode OptMode = OptimizationMode::NotSet;
Expand Down
33 changes: 33 additions & 0 deletions include/swift/Basic/FunctionBodySkipping.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===------------------- FunctionBodySkipping.h -----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#ifndef SWIFT_BASIC_FUNCTIONBODYSKIPPING_H
#define SWIFT_BASIC_FUNCTIONBODYSKIPPING_H

#include "llvm/Support/DataTypes.h"

namespace swift {

/// Describes the function bodies that can be skipped in type-checking.
enum class FunctionBodySkipping : uint8_t {
/// Do not skip type-checking for any function bodies.
None,
/// Only non-inlinable function bodies should be skipped.
NonInlinable,
/// All function bodies should be skipped, where not otherwise required
/// for type inference.
All
};

} // end namespace swift

#endif // SWIFT_BASIC_FUNCTIONBODYSKIPPING_H
14 changes: 7 additions & 7 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand All @@ -18,18 +18,19 @@
#ifndef SWIFT_BASIC_LANGOPTIONS_H
#define SWIFT_BASIC_LANGOPTIONS_H

#include "swift/Config.h"
#include "swift/Basic/FunctionBodySkipping.h"
#include "swift/Basic/LLVM.h"
#include "swift/Basic/Version.h"
#include "swift/Config.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/Regex.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/VersionTuple.h"
#include "llvm/Support/raw_ostream.h"
#include <string>
#include <vector>

Expand Down Expand Up @@ -489,9 +490,8 @@ namespace swift {
/// dumped to llvm::errs().
bool DebugTimeExpressions = false;

/// Indicate that the type checker should skip type-checking non-inlinable
/// function bodies.
bool SkipNonInlinableFunctionBodies = false;
/// Controls the function bodies to skip during type-checking.
FunctionBodySkipping SkipFunctionBodies = FunctionBodySkipping::None;

///
/// Flags for developers
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -762,4 +762,9 @@ def use_static_resource_dir
def disable_building_interface
: Flag<["-"], "disable-building-interface">,
HelpText<"Disallow building binary module from textual interface">;

def experimental_skip_all_function_bodies:
Flag<["-"], "experimental-skip-all-function-bodies">,
HelpText<"Skip type-checking function bodies and all SIL generation">;

} // end let Flags = [FrontendOption, NoDriverOption, HelpHidden]
2 changes: 1 addition & 1 deletion include/swift/Option/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down
8 changes: 4 additions & 4 deletions include/swift/SILOptimizer/PassManager/Passes.def
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -191,6 +191,9 @@ PASS(GenericSpecializer, "generic-specializer",
"Generic Function Specialization on Static Types")
PASS(ExistentialSpecializer, "existential-specializer",
"Existential Specializer")
PASS(SILSkippingChecker, "check-sil-skipping",
"Utility pass to ensure -experimental-skip-*-function-bodies skip "
"SIL generation of not-to-be-serialized functions entirely")
PASS(GlobalOpt, "global-opt",
"SIL Global Optimization")
PASS(GlobalPropertyOpt, "global-property-opt",
Expand Down Expand Up @@ -345,9 +348,6 @@ PASS(SerializeSILPass, "serialize-sil",
"Utility pass. Serializes the current SILModule")
PASS(CMOSerializeSILPass, "cmo-serialize-sil",
"Utility pass. Serializes the current SILModule for cross-module-optimization")
PASS(NonInlinableFunctionSkippingChecker, "check-non-inlinable-function-skipping",
"Utility pass to ensure -experimental-skip-non-inlinable-function-bodies "
"skips everything it should")
PASS(YieldOnceCheck, "yield-once-check",
"Check correct usage of yields in yield-once coroutines")
PASS(OSLogOptimization, "os-log-optimization", "Optimize os log calls")
Expand Down
2 changes: 1 addition & 1 deletion lib/Frontend/ArgsToFrontendInputsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down
5 changes: 3 additions & 2 deletions lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@ bool ArgsToFrontendOptionsConverter::convert(
if (checkUnusedSupplementaryOutputPaths())
return true;

if (FrontendOptions::doesActionGenerateIR(Opts.RequestedAction)
&& Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies)) {
if (FrontendOptions::doesActionGenerateIR(Opts.RequestedAction) &&
(Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies) ||
Args.hasArg(OPT_experimental_skip_all_function_bodies))) {
Diags.diagnose(SourceLoc(), diag::cannot_emit_ir_skipping_function_bodies);
return true;
}
Expand Down
26 changes: 15 additions & 11 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -715,22 +715,26 @@ static bool ParseTypeCheckerArgs(TypeCheckerOptions &Opts, ArgList &Args,
Opts.DebugTimeFunctionBodies |= Args.hasArg(OPT_debug_time_function_bodies);
Opts.DebugTimeExpressions |=
Args.hasArg(OPT_debug_time_expression_type_checking);
Opts.SkipNonInlinableFunctionBodies |=
Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies);

// If asked to perform InstallAPI, go ahead and enable non-inlinable function
// body skipping.
Opts.SkipNonInlinableFunctionBodies |= Args.hasArg(OPT_tbd_is_installapi);
if (Args.hasArg(OPT_experimental_skip_non_inlinable_function_bodies) ||
Args.hasArg(OPT_tbd_is_installapi))
Opts.SkipFunctionBodies = FunctionBodySkipping::NonInlinable;

if (Opts.SkipNonInlinableFunctionBodies &&
if (Args.hasArg(OPT_experimental_skip_all_function_bodies))
Opts.SkipFunctionBodies = FunctionBodySkipping::All;

if (Opts.SkipFunctionBodies == FunctionBodySkipping::NonInlinable &&
FrontendOpts.ModuleName == SWIFT_ONONE_SUPPORT) {
// Disable this optimization if we're compiling SwiftOnoneSupport, because
// we _definitely_ need to look inside every declaration to figure out
// what gets prespecialized.
Opts.SkipNonInlinableFunctionBodies = false;
Diags.diagnose(SourceLoc(),
diag::module_incompatible_with_skip_function_bodies,
SWIFT_ONONE_SUPPORT);
Opts.SkipFunctionBodies = FunctionBodySkipping::None;
Diags.diagnose(
SourceLoc(),
diag::module_incompatible_with_skip_non_inlinable_function_bodies,
SWIFT_ONONE_SUPPORT);
}

Opts.DisableConstraintSolverPerformanceHacks |=
Expand Down Expand Up @@ -1060,8 +1064,8 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
Opts.StopOptimizationAfterSerialization = true;

// Propagate the typechecker's understanding of
// -experimental-skip-non-inlinable-function-bodies to SIL.
Opts.SkipNonInlinableFunctionBodies = TCOpts.SkipNonInlinableFunctionBodies;
// -experimental-skip-*-function-bodies to SIL.
Opts.SkipFunctionBodies = TCOpts.SkipFunctionBodies;

// Parse the optimization level.
// Default to Onone settings if no option is passed.
Expand Down
9 changes: 6 additions & 3 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -1039,8 +1039,11 @@ CompilerInstance::getSourceFileParsingOptions(bool forPrimary) const {
}

if (forPrimary || isWholeModuleCompilation()) {
// Disable delayed body parsing for primaries and in WMO.
opts |= SourceFile::ParsingFlags::DisableDelayedBodies;
// Disable delayed body parsing for primaries and in WMO, unless
// forcefully skipping function bodies
auto typeOpts = getASTContext().TypeCheckerOpts;
if (typeOpts.SkipFunctionBodies == FunctionBodySkipping::None)
opts |= SourceFile::ParsingFlags::DisableDelayedBodies;
} else {
// Suppress parse warnings for non-primaries, as they'll get parsed multiple
// times.
Expand Down
8 changes: 7 additions & 1 deletion lib/SILGen/SILGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -1946,6 +1946,12 @@ ASTLoweringRequest::evaluate(Evaluator &evaluator,

auto silMod = SILModule::createEmptyModule(desc.context, desc.conv,
desc.opts);

// If all function bodies are being skipped there's no reason to do any
// SIL generation.
if (desc.opts.SkipFunctionBodies == FunctionBodySkipping::All)
return silMod;

SILGenModuleRAII scope(*silMod);

// Emit a specific set of SILDeclRefs if needed.
Expand Down
8 changes: 4 additions & 4 deletions lib/SILOptimizer/PassManager/PassPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -116,10 +116,10 @@ static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) {
P.addClosureLifetimeFixup();

#ifndef NDEBUG
// Add a verification pass to check our work when skipping non-inlinable
// Add a verification pass to check our work when skipping
// function bodies.
if (Options.SkipNonInlinableFunctionBodies)
P.addNonInlinableFunctionSkippingChecker();
if (Options.SkipFunctionBodies != FunctionBodySkipping::None)
P.addSILSkippingChecker();
#endif

if (Options.shouldOptimize()) {
Expand Down
2 changes: 1 addition & 1 deletion lib/SILOptimizer/UtilityPasses/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ target_sources(swiftSILOptimizer PRIVATE
BugReducerTester.cpp
CFGPrinter.cpp
CallerAnalysisPrinter.cpp
NonInlinableFunctionSkippingChecker.cpp
ComputeDominanceInfo.cpp
ComputeLoopInfo.cpp
ConstantEvaluatorTester.cpp
Expand All @@ -29,6 +28,7 @@ target_sources(swiftSILOptimizer PRIVATE
RCIdentityDumper.cpp
SerializeSILPass.cpp
SILDebugInfoGenerator.cpp
SILSkippingChecker.cpp
SideEffectsDumper.cpp
SimplifyUnreachableContainingBlocks.cpp
StripDebugInfo.cpp
Expand Down
Loading