Skip to content

[SILOpt] Added flag to disable destroy hoisting. #42197

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
Apr 6, 2022
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: 13 additions & 0 deletions include/swift/AST/SILOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ enum class CopyPropagationOption : uint8_t {
On
};

enum class DestroyHoistingOption : uint8_t {
// Do not run SSADestroyHoisting.
Off = 0,

// Run SSADestroyHoisting pass after AllocBoxToStack in the function passes.
On = 1
};

class SILModule;

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

/// Whether to run the SSADestroyHoisting pass.
///
/// When this 'On' the pipeline has the default behavior.
DestroyHoistingOption DestroyHoisting = DestroyHoistingOption::On;

/// Controls whether the SIL ARC optimizations are run.
bool EnableARCOptimizations = true;

Expand Down
4 changes: 4 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ let Flags = [FrontendOption, NoDriverOption, HelpHidden, ModuleInterfaceOptionIg
def enable_lexical_lifetimes_noArg :
Flag<["-"], "enable-lexical-lifetimes">,
HelpText<"Enable lexical lifetimes">;
def enable_destroy_hoisting :
Joined<["-"], "enable-destroy-hoisting=">,
HelpText<"Whether to enable destroy hoisting">,
MetaVarName<"true|false">;
}

// Flags that are saved into module interfaces
Expand Down
19 changes: 17 additions & 2 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,15 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
// -Ounchecked might also set removal of runtime asserts (cond_fail).
Opts.RemoveRuntimeAsserts |= Args.hasArg(OPT_RemoveRuntimeAsserts);

Optional<DestroyHoistingOption> specifiedDestroyHoistingOption;
if (Arg *A = Args.getLastArg(OPT_enable_destroy_hoisting)) {
specifiedDestroyHoistingOption =
llvm::StringSwitch<Optional<DestroyHoistingOption>>(A->getValue())
.Case("true", DestroyHoistingOption::On)
.Case("false", DestroyHoistingOption::Off)
.Default(None);
}

Optional<CopyPropagationOption> specifiedCopyPropagationOption;
if (Arg *A = Args.getLastArg(OPT_copy_propagation_state_EQ)) {
specifiedCopyPropagationOption =
Expand Down Expand Up @@ -1654,13 +1663,17 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,

// Unless overridden below, enabling copy propagation means enabling lexical
// lifetimes.
if (Opts.CopyPropagation == CopyPropagationOption::On)
if (Opts.CopyPropagation == CopyPropagationOption::On) {
Opts.LexicalLifetimes = LexicalLifetimesOption::On;
Opts.DestroyHoisting = DestroyHoistingOption::On;
}

// Unless overridden below, disable copy propagation means disabling lexical
// lifetimes.
if (Opts.CopyPropagation == CopyPropagationOption::Off)
if (Opts.CopyPropagation == CopyPropagationOption::Off) {
Opts.LexicalLifetimes = LexicalLifetimesOption::DiagnosticMarkersOnly;
Opts.DestroyHoisting = DestroyHoistingOption::Off;
}

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

Opts.EnableARCOptimizations &= !Args.hasArg(OPT_disable_arc_opts);
Opts.EnableOSSAModules |= Args.hasArg(OPT_enable_ossa_modules);
Expand Down
4 changes: 3 additions & 1 deletion lib/SILOptimizer/PassManager/PassPipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ void addFunctionPasses(SILPassPipelinePlan &P,
// Promote box allocations to stack allocations.
P.addAllocBoxToStack();

P.addSSADestroyHoisting();
if (P.getOptions().DestroyHoisting == DestroyHoistingOption::On) {
P.addSSADestroyHoisting();
}

// Propagate copies through stack locations. Should run after
// box-to-stack promotion since it is limited to propagating through
Expand Down
103 changes: 103 additions & 0 deletions test/SILOptimizer/disable-copy-propagation-frontend-flag.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPUNSPEC-DHUNSPEC %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-destroy-hoisting=false \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPUNSPEC-DHOFF %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-destroy-hoisting=true \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPUNSPEC-DHON %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-copy-propagation=false \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPOFF-DHUNSPEC %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-copy-propagation=false \
// RUN: -enable-destroy-hoisting=false \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPOFF-DHOFF %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-copy-propagation=false \
// RUN: -enable-destroy-hoisting=true \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPOFF-DHON %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-copy-propagation=true \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPON-DHUNSPEC %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-copy-propagation=true \
// RUN: -enable-destroy-hoisting=false \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPON-DHOFF %s

// RUN: %target-swift-frontend %s \
// RUN: -O \
// RUN: -enable-copy-propagation=true \
// RUN: -enable-destroy-hoisting=true \
// RUN: -Xllvm -sil-print-pass-name \
// RUN: -emit-ir \
// RUN: -o /dev/null \
// RUN: 2>&1 | %FileCheck -check-prefix CHECK-CPON-DHON %s

// CHECK-CPUNSPEC-DHUNSPEC: copy-propagation
// CHECK-CPUNSPEC-DHUNSPEC: ssa-destroy-hoisting

// CHECK-CPUNSPEC-DHOFF: copy-propagation
// CHECK-CPUNSPEC-DHOFF-NOT: ssa-destroy-hoisting

// CHECK-CPUNSPEC-DHON: copy-propagation
// CHECK-CPUNSPEC-DHON: ssa-destroy-hoisting

// CHECK-CPOFF-DHUNSPEC-NOT: copy-propagation
// CHECK-CPOFF-DHUNSPEC-NOT: ssa-destroy-hoisting

// CHECK-CPOFF-DHOFF-NOT: copy-propagation
// CHECK-CPOFF-DHOFF-NOT: ssa-destroy-hoisting

// CHECK-CPOFF-DHON-NOT: copy-propagation
// CHECK-CPOFF-DHON: ssa-destroy-hoisting

// CHECK-CPON-DHUNSPEC: copy-propagation
// CHECK-CPON-DHUNSPEC: ssa-destroy-hoisting

// CHECK-CPON-DHOFF: copy-propagation
// CHECK-CPON-DHOFF-NOT: ssa-destroy-hoisting

// CHECK-CPON-DHON: copy-propagation
// CHECK-CPON-DHON: ssa-destroy-hoisting

func foo() {}