Skip to content

[SILOptimizer] Stop optimization after serialization if only emitting a module #20986

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
Dec 6, 2018
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
3 changes: 3 additions & 0 deletions include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ class SILOptions {
/// Whether to dump verbose SIL with scope and location information.
bool EmitVerboseSIL = false;

/// Whether to stop the optimization pipeline after serializing SIL.
bool StopOptimizationAfterSerialization = false;

/// Optimization mode being used.
OptimizationMode OptMode = OptimizationMode::NotSet;

Expand Down
5 changes: 5 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,11 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
}
}

// If we're only emitting a module, stop optimizations once we've serialized
// the SIL for the module.
if (FEOpts.RequestedAction == FrontendOptions::ActionType::EmitModuleOnly)
Opts.StopOptimizationAfterSerialization = true;

if (Args.hasArg(OPT_sil_merge_partial_modules))
Opts.MergePartialModules = true;

Expand Down
17 changes: 13 additions & 4 deletions lib/SILOptimizer/PassManager/PassPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ void addHighLevelLoopOptPasses(SILPassPipelinePlan &P) {
}

// Perform classic SSA optimizations.
void addSSAPasses(SILPassPipelinePlan &P, OptimizationLevelKind OpLevel) {
void addSSAPasses(SILPassPipelinePlan &P, OptimizationLevelKind OpLevel,
bool stopAfterSerialization = false) {
// Promote box allocations to stack allocations.
P.addAllocBoxToStack();

Expand Down Expand Up @@ -282,6 +283,9 @@ void addSSAPasses(SILPassPipelinePlan &P, OptimizationLevelKind OpLevel) {
// which reduces the ability of the compiler to optimize clients
// importing this module.
P.addSerializeSILPass();
if (stopAfterSerialization)
return;

// Does inline semantics-functions (except "availability"), but not
// global-init functions.
P.addPerfInliner();
Expand Down Expand Up @@ -384,9 +388,12 @@ static void addMidModulePassesStackPromotePassPipeline(SILPassPipelinePlan &P) {
P.addStackPromotion();
}

static void addMidLevelPassPipeline(SILPassPipelinePlan &P) {
static bool addMidLevelPassPipeline(SILPassPipelinePlan &P,
bool stopAfterSerialization) {
P.startPipeline("MidLevel");
addSSAPasses(P, OptimizationLevelKind::MidLevel);
addSSAPasses(P, OptimizationLevelKind::MidLevel, stopAfterSerialization);
if (stopAfterSerialization)
return true;

// Specialize partially applied functions with dead arguments as a preparation
// for CapturePropagation.
Expand All @@ -395,6 +402,7 @@ static void addMidLevelPassPipeline(SILPassPipelinePlan &P) {
// Run loop unrolling after inlining and constant propagation, because loop
// trip counts may have became constant.
P.addLoopUnroll();
return false;
}

static void addClosureSpecializePassPipeline(SILPassPipelinePlan &P) {
Expand Down Expand Up @@ -574,7 +582,8 @@ SILPassPipelinePlan::getPerformancePassPipeline(const SILOptions &Options) {
addMidModulePassesStackPromotePassPipeline(P);

// Run an iteration of the mid-level SSA passes.
addMidLevelPassPipeline(P);
if (addMidLevelPassPipeline(P, Options.StopOptimizationAfterSerialization))
return P;

// Perform optimizations that specialize.
addClosureSpecializePassPipeline(P);
Expand Down
23 changes: 23 additions & 0 deletions test/SILOptimizer/stop_after_module.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %target-swift-frontend -c -o /dev/null -O -Xllvm -sil-print-after=inline %s 2>&1 | %FileCheck %s --check-prefix NOTSKIPPING
// RUN: %target-swift-frontend -emit-sil -o /dev/null -O -Xllvm -sil-print-after=inline %s 2>&1 | %FileCheck %s --check-prefix NOTSKIPPING
// RUN: %target-swift-frontend -emit-module -o /dev/null -O -Xllvm -sil-print-after=inline %s 2>&1 | %FileCheck %s --check-prefix SKIPPING

// This test ensures that we don't run the Perf Inliner after serializing a
// module, if we're stopping optimizations after serializing. We want to also
// make sure we _do_ still run the Perf Inliner when we're doing a full
// compile or emitting SIL directly.

@inline(never)
func _blackHole(_ x: Int) {}

@inlinable
public func inlinableFunction(_ x: Int) -> Int {
return x + 1
}

public func caller() {
_blackHole(inlinableFunction(20))
}

// NOTSKIPPING: *** SIL function after {{.*}}, stage MidLevel, pass {{.*}}: PerfInliner (inline)
// SKIPPING-NOT: *** SIL function after {{.*}}, stage MidLevel, pass {{.*}}: PerfInliner (inline)