Skip to content

Commit 6a34f04

Browse files
Merge pull request #40473 from nate-chandler/lexical_lifetimes/enable-with-copy-propagation
Enabling copy propagation enables lexical lifetimes.
2 parents 7d2d2f6 + ea42e2f commit 6a34f04

26 files changed

+143
-67
lines changed

include/swift/AST/DiagnosticsFrontend.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ ERROR(error_unknown_arg,none,
8080
"unknown argument: '%0'", (StringRef))
8181
ERROR(error_invalid_arg_value,none,
8282
"invalid value '%1' in '%0'", (StringRef, StringRef))
83+
ERROR(error_invalid_arg_combination,none,
84+
"unsupported argument combination: '%0' and '%1'", (StringRef, StringRef))
8385
WARNING(warning_invalid_locale_code,none,
8486
"unsupported locale code; supported locale codes are: '%0'", (StringRef))
8587
WARNING(warning_locale_path_not_found,none,

include/swift/Option/FrontendOptions.td

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ def dependency_scan_cache_remarks : Flag<["-"], "Rdependency-scan-cache">,
213213
HelpText<"Emit remarks indicating use of the serialized module dependency scanning cache.">;
214214

215215
def enable_copy_propagation : Flag<["-"], "enable-copy-propagation">,
216-
HelpText<"Run SIL copy propagation to shorten object lifetime.">;
216+
HelpText<"Run SIL copy propagation with lexical lifetimes to shorten object "
217+
"lifetimes while preserving variable lifetimes.">;
217218
def disable_copy_propagation : Flag<["-"], "disable-copy-propagation">,
218219
HelpText<"Don't run SIL copy propagation to preserve object lifetime.">;
219220

@@ -259,15 +260,18 @@ def enable_experimental_concurrency :
259260
Flag<["-"], "enable-experimental-concurrency">,
260261
HelpText<"Enable experimental concurrency model">;
261262

262-
def disable_lexical_lifetimes :
263-
Flag<["-"], "disable-lexical-lifetimes">,
264-
HelpText<"Disables early lexical lifetimes. Mutually exclusive with "
265-
"-enable-lexical-lifetimes">;
266-
263+
def enable_lexical_borrow_scopes :
264+
Joined<["-"], "enable-lexical-borrow-scopes=">,
265+
HelpText<"Whether to emit lexical borrow scopes (default: true)">,
266+
MetaVarName<"true|false">;
267+
267268
def enable_lexical_lifetimes :
269+
Joined<["-"], "enable-lexical-lifetimes=">,
270+
HelpText<"Whether to enable lexical lifetimes">,
271+
MetaVarName<"true|false">;
272+
def enable_lexical_lifetimes_noArg :
268273
Flag<["-"], "enable-lexical-lifetimes">,
269-
HelpText<"Enable lexical lifetimes. Mutually exclusive with "
270-
"-disable-lexical-lifetimes">;
274+
HelpText<"Enable lexical lifetimes">;
271275

272276
def enable_experimental_move_only :
273277
Flag<["-"], "enable-experimental-move-only">,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,21 +1449,88 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
14491449
// -Ounchecked might also set removal of runtime asserts (cond_fail).
14501450
Opts.RemoveRuntimeAsserts |= Args.hasArg(OPT_RemoveRuntimeAsserts);
14511451

1452-
// If experimental move only is enabled, always enable lexical lifetime as
1453-
// well. Move only depends on lexical lifetimes.
1454-
bool enableExperimentalLexicalLifetimes =
1455-
Args.hasArg(OPT_enable_lexical_lifetimes) ||
1456-
Args.hasArg(OPT_enable_experimental_move_only);
1457-
// Error if both experimental lexical lifetimes and disable lexical lifetimes
1458-
// are both set.
1459-
if (enableExperimentalLexicalLifetimes &&
1460-
Args.hasArg(OPT_disable_lexical_lifetimes)) {
1452+
Optional<bool> enableLexicalBorrowScopesFlag;
1453+
if (Arg *A = Args.getLastArg(OPT_enable_lexical_borrow_scopes)) {
1454+
enableLexicalBorrowScopesFlag =
1455+
llvm::StringSwitch<Optional<bool>>(A->getValue())
1456+
.Case("true", true)
1457+
.Case("false", false)
1458+
.Default(None);
1459+
}
1460+
Optional<bool> enableLexicalLifetimesFlag;
1461+
if (Arg *A = Args.getLastArg(OPT_enable_lexical_lifetimes)) {
1462+
enableLexicalLifetimesFlag =
1463+
llvm::StringSwitch<Optional<bool>>(A->getValue())
1464+
.Case("true", true)
1465+
.Case("false", false)
1466+
.Default(None);
1467+
}
1468+
if (Args.getLastArg(OPT_enable_lexical_lifetimes_noArg)) {
1469+
if (!enableLexicalLifetimesFlag.getValueOr(true)) {
1470+
// Error if lexical lifetimes have been disabled via the meta-var form
1471+
// and enabled via the flag.
1472+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_combination,
1473+
"enable-lexical-lifetimes",
1474+
"enable-lexical-lifetimes=false");
1475+
return true;
1476+
} else {
1477+
enableLexicalLifetimesFlag = true;
1478+
}
1479+
}
1480+
1481+
if (enableLexicalLifetimesFlag.getValueOr(false) &&
1482+
!enableLexicalBorrowScopesFlag.getValueOr(true)) {
1483+
// Error if lexical lifetimes have been enabled but lexical borrow scopes--
1484+
// on which they are dependent--have been disabled.
1485+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_combination,
1486+
"enable-lexical-lifetimes=true",
1487+
"enable-lexical-borrow-scopes=false");
14611488
return true;
1462-
} else {
1463-
if (enableExperimentalLexicalLifetimes)
1489+
}
1490+
1491+
if (Args.hasArg(OPT_enable_experimental_move_only) &&
1492+
(enableLexicalBorrowScopesFlag.getValueOr(false))) {
1493+
// Error if move-only is enabled and lexical borrow scopes--on which it
1494+
// depends--has been disabled.
1495+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_combination,
1496+
"enable-experimental-move-only",
1497+
"enable-lexical-borrow-scopes=false");
1498+
return true;
1499+
}
1500+
1501+
if (Args.hasArg(OPT_enable_experimental_move_only) &&
1502+
(enableLexicalLifetimesFlag.getValueOr(false))) {
1503+
// Error if move-only is enabled and lexical lifetimes--on which it
1504+
// depends--has been disabled.
1505+
Diags.diagnose(SourceLoc(), diag::error_invalid_arg_combination,
1506+
"enable-experimental-move-only",
1507+
"enable-lexical-lifetimes=false");
1508+
return true;
1509+
}
1510+
1511+
// -enable-copy-propagation implies -enable-lexical-lifetimes unless
1512+
// otherwise specified.
1513+
if (Args.hasArg(OPT_enable_copy_propagation))
1514+
Opts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
1515+
1516+
// If move-only is enabled, always enable lexical lifetime as well. Move-only
1517+
// depends on lexical lifetimes.
1518+
if (Args.hasArg(OPT_enable_experimental_move_only))
1519+
Opts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
1520+
1521+
if (enableLexicalLifetimesFlag) {
1522+
if (*enableLexicalLifetimesFlag) {
14641523
Opts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
1465-
if (Args.hasArg(OPT_disable_lexical_lifetimes))
1524+
} else {
1525+
Opts.LexicalLifetimes = LexicalLifetimesOption::Early;
1526+
}
1527+
}
1528+
if (enableLexicalBorrowScopesFlag) {
1529+
if (*enableLexicalBorrowScopesFlag) {
1530+
Opts.LexicalLifetimes = LexicalLifetimesOption::Early;
1531+
} else {
14661532
Opts.LexicalLifetimes = LexicalLifetimesOption::Off;
1533+
}
14671534
}
14681535

14691536
Opts.EnableCopyPropagation |= Args.hasArg(OPT_enable_copy_propagation);

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ broadenSingleElementStores(StoreInst *storeInst,
415415
/// borrow scope--copy/destroy is insufficient by itself.
416416
///
417417
/// FIXME: Technically this should be guarded by a compiler flag like
418-
/// -enable-copy-propagation until SILGen protects scoped variables by borrow
419-
/// scopes.
418+
/// -enable-copy-propagation until SILGen protects scoped variables by
419+
/// borrow scopes.
420420
static SILBasicBlock::iterator
421421
eliminateSimpleCopies(CopyValueInst *cvi, CanonicalizeInstruction &pass) {
422422
auto next = std::next(cvi->getIterator());

test/AutoDiff/validation-test/array.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -disable-lexical-lifetimes)
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-lexical-borrow-scopes=false)
22

33
// REQUIRES: executable_test
44

test/AutoDiff/validation-test/optional.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -disable-lexical-lifetimes)
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-lexical-borrow-scopes=false)
22

33
// REQUIRES: executable_test
44

test/AutoDiff/validation-test/optional_property.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %target-run-simple-swift(-Xfrontend -disable-lexical-lifetimes)
2-
// RUN: %target-swift-emit-sil -Xllvm -debug-only=differentiation -disable-lexical-lifetimes -module-name null -o /dev/null 2>&1 %s | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-Xfrontend -enable-lexical-borrow-scopes=false)
2+
// RUN: %target-swift-emit-sil -Xllvm -debug-only=differentiation -enable-lexical-borrow-scopes=false -module-name null -o /dev/null 2>&1 %s | %FileCheck %s
33

44
// REQUIRES: executable_test
55
// REQUIRES: asserts

test/Concurrency/Runtime/async_properties_actor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-run-simple-swift(-parse-as-library -Xfrontend -enable-copy-propagation -Xfrontend -disable-availability-checking %import-libdispatch) | %FileCheck %s
1+
// RUN: %target-run-simple-swift(-parse-as-library -Xfrontend -enable-copy-propagation -Xfrontend -enable-lexical-lifetimes=false -Xfrontend -disable-availability-checking %import-libdispatch) | %FileCheck %s
22

33
// REQUIRES: executable_test
44
// REQUIRES: concurrency

test/DebugInfo/if-branchlocations.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// RUN: %target-swift-frontend %s -emit-sil -disable-copy-propagation -emit-verbose-sil -g -o - | %FileCheck %s --check-prefixes=CHECK,CHECK-NCP
2-
// RUN: %target-swift-frontend %s -emit-sil -enable-copy-propagation -disable-lexical-lifetimes -emit-verbose-sil -g -o - | %FileCheck %s --check-prefixes=CHECK,CHECK-CP
2+
// RUN: %target-swift-frontend %s -emit-sil -enable-copy-propagation -enable-lexical-borrow-scopes=false -emit-verbose-sil -g -o - | %FileCheck %s --check-prefixes=CHECK,CHECK-CP
33

44
class NSURL {}
55

test/DebugInfo/linetable-do.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle %s -emit-ir -g -o - | %FileCheck %s
22
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -disable-copy-propagation %s -emit-sil -emit-verbose-sil -g -o - | %FileCheck --check-prefixes=CHECK-SIL,CHECK-NCP %s
3-
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -enable-copy-propagation -disable-lexical-lifetimes %s -emit-sil -emit-verbose-sil -g -o - | %FileCheck --check-prefixes=CHECK-SIL,CHECK-CP %s
3+
// RUN: %target-swift-frontend -Xllvm -sil-full-demangle -enable-copy-propagation -enable-lexical-borrow-scopes=false %s -emit-sil -emit-verbose-sil -g -o - | %FileCheck --check-prefixes=CHECK-SIL,CHECK-CP %s
44
import StdlibUnittest
55

66
class Obj {}

0 commit comments

Comments
 (0)