Skip to content

Commit 5285bf7

Browse files
authored
Turn off speculative devirtualization by default. (#29359)
Turn off speculative devirtualization by default. Add a flag to support enabling the pass. Fixes rdar://58778959 and rdar://58429282
1 parent 5669985 commit 5285bf7

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
@@ -282,6 +282,9 @@ def disable_ossa_opts : Flag<["-"], "disable-ossa-opts">,
282282
def disable_sil_partial_apply : Flag<["-"], "disable-sil-partial-apply">,
283283
HelpText<"Disable use of partial_apply in SIL generation">;
284284

285+
def enable_spec_devirt : Flag<["-"], "enable-spec-devirt">,
286+
HelpText<"Enable speculative devirtualization pass.">;
287+
285288
def enable_ownership_stripping_after_serialization
286289
: Flag<["-"], "enable-ownership-stripping-after-serialization">,
287290
HelpText<"Strip ownership after serialization">;

lib/Frontend/CompilerInvocation.cpp

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

960960
Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
961961
Opts.EnableOSSAOptimizations &= !Args.hasArg(OPT_disable_ossa_opts);
962+
Opts.EnableSpeculativeDevirtualization |= Args.hasArg(OPT_enable_spec_devirt);
962963
Opts.DisableSILPerfOptimizations |= Args.hasArg(OPT_disable_sil_perf_optzns);
963964
Opts.CrossModuleOptimization |= Args.hasArg(OPT_CrossModuleOptimization);
964965
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
@@ -488,7 +488,9 @@ static void addClosureSpecializePassPipeline(SILPassPipelinePlan &P) {
488488
P.addStackPromotion();
489489

490490
// Speculate virtual call targets.
491-
P.addSpeculativeDevirtualization();
491+
if (P.getOptions().EnableSpeculativeDevirtualization) {
492+
P.addSpeculativeDevirtualization();
493+
}
492494

493495
// There should be at least one SILCombine+SimplifyCFG between the
494496
// 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)