Skip to content

Commit 11e5c97

Browse files
committed
[SILOpt] Added flag to disable destroy hoisting.
It is on by default. The default changes with the value of the CopyPropagation setting. But it can still be overridden.
1 parent cd4bc88 commit 11e5c97

File tree

5 files changed

+140
-3
lines changed

5 files changed

+140
-3
lines changed

include/swift/AST/SILOptions.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,14 @@ enum class CopyPropagationOption : uint8_t {
5858
On
5959
};
6060

61+
enum class DestroyHoistingOption : uint8_t {
62+
// Do not run SSADestroyHoisting.
63+
Off = 0,
64+
65+
// Run SSADestroyHoisting pass after AllocBoxToStack in the function passes.
66+
On = 1
67+
};
68+
6169
class SILModule;
6270

6371
class SILOptions {
@@ -84,6 +92,11 @@ class SILOptions {
8492
/// When this is 'On' the pipeline has default behavior.
8593
CopyPropagationOption CopyPropagation = CopyPropagationOption::On;
8694

95+
/// Whether to run the SSADestroyHoisting pass.
96+
///
97+
/// When this 'On' the pipeline has the default behavior.
98+
DestroyHoistingOption DestroyHoisting = DestroyHoistingOption::On;
99+
87100
/// Controls whether the SIL ARC optimizations are run.
88101
bool EnableARCOptimizations = true;
89102

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ let Flags = [FrontendOption, NoDriverOption, HelpHidden, ModuleInterfaceOptionIg
248248
def enable_lexical_lifetimes_noArg :
249249
Flag<["-"], "enable-lexical-lifetimes">,
250250
HelpText<"Enable lexical lifetimes">;
251+
def enable_destroy_hoisting :
252+
Joined<["-"], "enable-destroy-hoisting=">,
253+
HelpText<"Whether to enable destroy hoisting">,
254+
MetaVarName<"true|false">;
251255
}
252256

253257
// Flags that are saved into module interfaces

lib/Frontend/CompilerInvocation.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,15 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
15531553
// -Ounchecked might also set removal of runtime asserts (cond_fail).
15541554
Opts.RemoveRuntimeAsserts |= Args.hasArg(OPT_RemoveRuntimeAsserts);
15551555

1556+
Optional<DestroyHoistingOption> specifiedDestroyHoistingOption;
1557+
if (Arg *A = Args.getLastArg(OPT_enable_destroy_hoisting)) {
1558+
specifiedDestroyHoistingOption =
1559+
llvm::StringSwitch<Optional<DestroyHoistingOption>>(A->getValue())
1560+
.Case("true", DestroyHoistingOption::On)
1561+
.Case("false", DestroyHoistingOption::Off)
1562+
.Default(None);
1563+
}
1564+
15561565
Optional<CopyPropagationOption> specifiedCopyPropagationOption;
15571566
if (Arg *A = Args.getLastArg(OPT_copy_propagation_state_EQ)) {
15581567
specifiedCopyPropagationOption =
@@ -1654,13 +1663,17 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
16541663

16551664
// Unless overridden below, enabling copy propagation means enabling lexical
16561665
// lifetimes.
1657-
if (Opts.CopyPropagation == CopyPropagationOption::On)
1666+
if (Opts.CopyPropagation == CopyPropagationOption::On) {
16581667
Opts.LexicalLifetimes = LexicalLifetimesOption::On;
1668+
Opts.DestroyHoisting = DestroyHoistingOption::On;
1669+
}
16591670

16601671
// Unless overridden below, disable copy propagation means disabling lexical
16611672
// lifetimes.
1662-
if (Opts.CopyPropagation == CopyPropagationOption::Off)
1673+
if (Opts.CopyPropagation == CopyPropagationOption::Off) {
16631674
Opts.LexicalLifetimes = LexicalLifetimesOption::DiagnosticMarkersOnly;
1675+
Opts.DestroyHoisting = DestroyHoistingOption::Off;
1676+
}
16641677

16651678
// If move-only is enabled, always enable lexical lifetime as well. Move-only
16661679
// depends on lexical lifetimes.
@@ -1681,6 +1694,8 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
16811694
Opts.LexicalLifetimes = LexicalLifetimesOption::Off;
16821695
}
16831696
}
1697+
if (specifiedDestroyHoistingOption)
1698+
Opts.DestroyHoisting = *specifiedDestroyHoistingOption;
16841699

16851700
Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
16861701
Opts.EnableOSSAModules |= Args.hasArg(OPT_enable_ossa_modules);

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ void addFunctionPasses(SILPassPipelinePlan &P,
354354
// Promote box allocations to stack allocations.
355355
P.addAllocBoxToStack();
356356

357-
P.addSSADestroyHoisting();
357+
if (P.getOptions().DestroyHoisting == DestroyHoistingOption::On) {
358+
P.addSSADestroyHoisting();
359+
}
358360

359361
// Propagate copies through stack locations. Should run after
360362
// box-to-stack promotion since it is limited to propagating through
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// RUN: %target-swift-frontend %s \
2+
// RUN: -O \
3+
// RUN: -Xllvm -sil-print-pass-name \
4+
// RUN: -emit-ir \
5+
// RUN: -o /dev/null \
6+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPUNSPEC-DHUNSPEC %s
7+
8+
// RUN: %target-swift-frontend %s \
9+
// RUN: -O \
10+
// RUN: -enable-destroy-hoisting=false \
11+
// RUN: -Xllvm -sil-print-pass-name \
12+
// RUN: -emit-ir \
13+
// RUN: -o /dev/null \
14+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPUNSPEC-DHOFF %s
15+
16+
// RUN: %target-swift-frontend %s \
17+
// RUN: -O \
18+
// RUN: -enable-destroy-hoisting=true \
19+
// RUN: -Xllvm -sil-print-pass-name \
20+
// RUN: -emit-ir \
21+
// RUN: -o /dev/null \
22+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPUNSPEC-DHON %s
23+
24+
// RUN: %target-swift-frontend %s \
25+
// RUN: -O \
26+
// RUN: -enable-copy-propagation=false \
27+
// RUN: -Xllvm -sil-print-pass-name \
28+
// RUN: -emit-ir \
29+
// RUN: -o /dev/null \
30+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPOFF-DHUNSPEC %s
31+
32+
// RUN: %target-swift-frontend %s \
33+
// RUN: -O \
34+
// RUN: -enable-copy-propagation=false \
35+
// RUN: -enable-destroy-hoisting=false \
36+
// RUN: -Xllvm -sil-print-pass-name \
37+
// RUN: -emit-ir \
38+
// RUN: -o /dev/null \
39+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPOFF-DHOFF %s
40+
41+
// RUN: %target-swift-frontend %s \
42+
// RUN: -O \
43+
// RUN: -enable-copy-propagation=false \
44+
// RUN: -enable-destroy-hoisting=true \
45+
// RUN: -Xllvm -sil-print-pass-name \
46+
// RUN: -emit-ir \
47+
// RUN: -o /dev/null \
48+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPOFF-DHON %s
49+
50+
// RUN: %target-swift-frontend %s \
51+
// RUN: -O \
52+
// RUN: -enable-copy-propagation=true \
53+
// RUN: -Xllvm -sil-print-pass-name \
54+
// RUN: -emit-ir \
55+
// RUN: -o /dev/null \
56+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPON-DHUNSPEC %s
57+
58+
// RUN: %target-swift-frontend %s \
59+
// RUN: -O \
60+
// RUN: -enable-copy-propagation=true \
61+
// RUN: -enable-destroy-hoisting=false \
62+
// RUN: -Xllvm -sil-print-pass-name \
63+
// RUN: -emit-ir \
64+
// RUN: -o /dev/null \
65+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPON-DHOFF %s
66+
67+
// RUN: %target-swift-frontend %s \
68+
// RUN: -O \
69+
// RUN: -enable-copy-propagation=true \
70+
// RUN: -enable-destroy-hoisting=true \
71+
// RUN: -Xllvm -sil-print-pass-name \
72+
// RUN: -emit-ir \
73+
// RUN: -o /dev/null \
74+
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPON-DHON %s
75+
76+
// CHECK-CPUNSPEC-DHUNSPEC: copy-propagation
77+
// CHECK-CPUNSPEC-DHUNSPEC: ssa-destroy-hoisting
78+
79+
// CHECK-CPUNSPEC-DHOFF: copy-propagation
80+
// CHECK-CPUNSPEC-DHOFF-NOT: ssa-destroy-hoisting
81+
82+
// CHECK-CPUNSPEC-DHON: copy-propagation
83+
// CHECK-CPUNSPEC-DHON: ssa-destroy-hoisting
84+
85+
// CHECK-CPOFF-DHUNSPEC-NOT: copy-propagation
86+
// CHECK-CPOFF-DHUNSPEC-NOT: ssa-destroy-hoisting
87+
88+
// CHECK-CPOFF-DHOFF-NOT: copy-propagation
89+
// CHECK-CPOFF-DHOFF-NOT: ssa-destroy-hoisting
90+
91+
// CHECK-CPOFF-DHON-NOT: copy-propagation
92+
// CHECK-CPOFF-DHON: ssa-destroy-hoisting
93+
94+
// CHECK-CPON-DHUNSPEC: copy-propagation
95+
// CHECK-CPON-DHUNSPEC: ssa-destroy-hoisting
96+
97+
// CHECK-CPON-DHOFF: copy-propagation
98+
// CHECK-CPON-DHOFF-NOT: ssa-destroy-hoisting
99+
100+
// CHECK-CPON-DHON: copy-propagation
101+
// CHECK-CPON-DHON: ssa-destroy-hoisting
102+
103+
func foo() {}

0 commit comments

Comments
 (0)