Skip to content

Commit a0c1677

Browse files
authored
Turn off speculative devirtualization by default. (#29514)
This is a cherrypick of #29359 Explanation: Disable speculative devirtualization optimization. The optimization did not give significant performance wins. Disabling it improves code size. Risk: Low. No significant performance hit is expected. Issue: rdar://problem/58778959 Testing: Performance tests and regression tests Reviewer: Erik Eckstein
1 parent b0a329c commit a0c1677

17 files changed

+40
-24
lines changed

include/swift/AST/SILOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ class SILOptions {
5757
/// purposes.
5858
bool EnableOSSAOptimizations = true;
5959

60+
/// Controls whether to turn on speculative devirtualization.
61+
/// It is turned off by default.
62+
bool EnableSpeculativeDevirtualization = false;
63+
6064
/// Should we run any SIL performance optimizations
6165
///
6266
/// Useful when you want to enable -O LLVM opts but not -O SIL opts.

include/swift/Option/FrontendOptions.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,
278278
def disable_sil_partial_apply : Flag<["-"], "disable-sil-partial-apply">,
279279
HelpText<"Disable use of partial_apply in SIL generation">;
280280

281+
def enable_spec_devirt : Flag<["-"], "enable-spec-devirt">,
282+
HelpText<"Enable speculative devirtualization pass.">;
283+
281284
def enable_ownership_stripping_after_serialization
282285
: Flag<["-"], "enable-ownership-stripping-after-serialization">,
283286
HelpText<"Strip ownership after serialization">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
877877

878878
Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
879879
Opts.EnableOSSAOptimizations &= !Args.hasArg(OPT_disable_ossa_opts);
880+
Opts.EnableSpeculativeDevirtualization |= Args.hasArg(OPT_enable_spec_devirt);
880881
Opts.DisableSILPerfOptimizations |= Args.hasArg(OPT_disable_sil_perf_optzns);
881882
Opts.CrossModuleOptimization |= Args.hasArg(OPT_CrossModuleOptimization);
882883
Opts.VerifyAll |= Args.hasArg(OPT_sil_verify_all);

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,9 @@ static void addClosureSpecializePassPipeline(SILPassPipelinePlan &P) {
489489
P.addStackPromotion();
490490

491491
// Speculate virtual call targets.
492-
P.addSpeculativeDevirtualization();
492+
if (P.getOptions().EnableSpeculativeDevirtualization) {
493+
P.addSpeculativeDevirtualization();
494+
}
493495

494496
// There should be at least one SILCombine+SimplifyCFG between the
495497
// ClosureSpecializer, etc. and the last inliner. Cleaning up after these

lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ namespace {
596596
void run() override {
597597

598598
auto &CurFn = *getFunction();
599+
599600
// Don't perform speculative devirtualization at -Os.
600601
if (CurFn.optimizeForSize())
601602
return;

test/SILOptimizer/devirt_base_class.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -O -emit-sil %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -enable-spec-devirt -O -emit-sil %s | %FileCheck %s
22

33
public class Base1 { @inline(never) func f() -> Int { return 0 } }
44

test/SILOptimizer/devirt_covariant_return.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// RUN: %target-swift-frontend -module-name devirt_covariant_return -Xllvm -sil-full-demangle -O -Xllvm -disable-sil-cm-rr-cm=0 -Xllvm -sil-inline-generics=false -primary-file %s -emit-sil -sil-inline-threshold 1000 -Xllvm -sil-disable-pass=ObjectOutliner -sil-verify-all | %FileCheck %s
2+
// RUN: %target-swift-frontend -module-name devirt_covariant_return -Xllvm -sil-full-demangle -enable-spec-devirt -O -Xllvm -disable-sil-cm-rr-cm=0 -Xllvm -sil-inline-generics=false -primary-file %s -emit-sil -sil-inline-threshold 1000 -Xllvm -sil-disable-pass=ObjectOutliner -sil-verify-all | %FileCheck %s
33

44
// Make sure that we can dig all the way through the class hierarchy and
55
// protocol conformances with covariant return types correctly. The verifier

test/SILOptimizer/devirt_default_case.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
// RUN: %target-swift-frontend -O -module-name devirt_default_case -emit-sil %s | %FileCheck -check-prefix=CHECK -check-prefix=CHECK-NORMAL %s
3-
// RUN: %target-swift-frontend -O -module-name devirt_default_case -emit-sil -enable-testing %s | %FileCheck -check-prefix=CHECK -check-prefix=CHECK-TESTABLE %s
2+
// RUN: %target-swift-frontend -enable-spec-devirt -O -module-name devirt_default_case -emit-sil %s | %FileCheck -check-prefix=CHECK -check-prefix=CHECK-NORMAL %s
3+
// RUN: %target-swift-frontend -enable-spec-devirt -O -module-name devirt_default_case -emit-sil -enable-testing %s | %FileCheck -check-prefix=CHECK -check-prefix=CHECK-TESTABLE %s
44

55
@_silgen_name("action")
66
func action(_ n:Int) -> ()

test/SILOptimizer/devirt_protocol_method_invocations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
// RUN: %target-swift-frontend -module-name devirt_protocol_method_invocations -O -Xllvm -sil-disable-pass=ExistentialSpecializer -emit-sil %s | %FileCheck %s
2+
// RUN: %target-swift-frontend -module-name devirt_protocol_method_invocations -enable-spec-devirt -O -Xllvm -sil-disable-pass=ExistentialSpecializer -emit-sil %s | %FileCheck %s
33

44
protocol PPP {
55
func f()

test/SILOptimizer/devirt_speculate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend %/s -parse-as-library -O -emit-sil -save-optimization-record-path %t.opt.yaml | %FileCheck %s
1+
// RUN: %target-swift-frontend %/s -parse-as-library -enable-spec-devirt -O -emit-sil -save-optimization-record-path %t.opt.yaml | %FileCheck %s
22
// RUN: %FileCheck -check-prefix=YAML -input-file=%t.opt.yaml %s
33
// RUN: %target-swift-frontend %/s -parse-as-library -Osize -emit-sil | %FileCheck %s --check-prefix=OSIZE
44
//

0 commit comments

Comments
 (0)