Skip to content

Commit 68f5d8d

Browse files
Merge pull request #40519 from nate-chandler/lexical_lifetimes/disable-with-copy-propagation
Disable lexical-lifetimes with copy-propagation.
2 parents a77f161 + 08a3d3c commit 68f5d8d

File tree

7 files changed

+35
-24
lines changed

7 files changed

+35
-24
lines changed

include/swift/AST/SILOptions.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@
3131
namespace swift {
3232

3333
enum class LexicalLifetimesOption : uint8_t {
34-
// Do not insert any lexical lifetimes.
34+
// Do not insert lexical markers.
3535
Off = 0,
3636

37-
// Insert lexical lifetimes in SILGen, but remove them before leaving Raw SIL.
38-
Early,
37+
// Insert lexical markers via lexical borrow scopes and the lexical flag on
38+
// alloc_stacks produced from alloc_boxes, but strip them when lowering out of
39+
// Raw SIL.
40+
DiagnosticMarkersOnly,
3941

40-
// Insert lexical lifetimes and do not remove them until OSSA is lowered. This
41-
// is experimental.
42-
ExperimentalLate,
42+
// Insert lexical markers and use them to lengthen object lifetime based on
43+
// lexical scope.
44+
On,
4345
};
4446

4547
class SILModule;
@@ -59,7 +61,8 @@ class SILOptions {
5961
bool RemoveRuntimeAsserts = false;
6062

6163
/// Enable experimental support for emitting defined borrow scopes.
62-
LexicalLifetimesOption LexicalLifetimes = LexicalLifetimesOption::Early;
64+
LexicalLifetimesOption LexicalLifetimes =
65+
LexicalLifetimesOption::DiagnosticMarkersOnly;
6366

6467
/// Force-run SIL copy propagation to shorten object lifetime in whatever
6568
/// optimization pipeline is currently used.

include/swift/SIL/SILModule.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,14 +911,17 @@ inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SILModule &M){
911911
inline bool SILOptions::supportsLexicalLifetimes(const SILModule &mod) const {
912912
switch (mod.getStage()) {
913913
case SILStage::Raw:
914-
// In Raw SIL, we support lexical lifetimes as long as lexical lifetimes is
915-
// not turned off all the way. This means lexical lifetimes is set to either
916-
// early or experimental late.
914+
// In raw SIL, lexical markers are used for diagnostics. These markers are
915+
// present as long as the lexical lifetimes feature is not disabled
916+
// entirely.
917917
return LexicalLifetimes != LexicalLifetimesOption::Off;
918918
case SILStage::Canonical:
919-
// In Canonical SIL, we only support lexical lifetimes when in experimental
920-
// late mode.
921-
return LexicalLifetimes == LexicalLifetimesOption::ExperimentalLate;
919+
// In Canonical SIL, lexical markers are used to ensure that object
920+
// lifetimes do not get observably shortened from the end of a lexical
921+
// scope. That behavior only occurs when lexical lifetimes is (fully)
922+
// enabled. (When only diagnostic markers are enabled, the markers are
923+
// stripped as part of lowering from raw to canonical SIL.)
924+
return LexicalLifetimes == LexicalLifetimesOption::On;
922925
case SILStage::Lowered:
923926
// We do not support OSSA in Lowered SIL, so this is always false.
924927
return false;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,23 +1525,27 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
15251525
// -enable-copy-propagation implies -enable-lexical-lifetimes unless
15261526
// otherwise specified.
15271527
if (Args.hasArg(OPT_enable_copy_propagation))
1528-
Opts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
1528+
Opts.LexicalLifetimes = LexicalLifetimesOption::On;
1529+
1530+
// -disable-copy-propagation implies -enable-lexical-lifetimes=false
1531+
if (Args.hasArg(OPT_disable_copy_propagation))
1532+
Opts.LexicalLifetimes = LexicalLifetimesOption::DiagnosticMarkersOnly;
15291533

15301534
// If move-only is enabled, always enable lexical lifetime as well. Move-only
15311535
// depends on lexical lifetimes.
15321536
if (Args.hasArg(OPT_enable_experimental_move_only))
1533-
Opts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
1537+
Opts.LexicalLifetimes = LexicalLifetimesOption::On;
15341538

15351539
if (enableLexicalLifetimesFlag) {
15361540
if (*enableLexicalLifetimesFlag) {
1537-
Opts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
1541+
Opts.LexicalLifetimes = LexicalLifetimesOption::On;
15381542
} else {
1539-
Opts.LexicalLifetimes = LexicalLifetimesOption::Early;
1543+
Opts.LexicalLifetimes = LexicalLifetimesOption::DiagnosticMarkersOnly;
15401544
}
15411545
}
15421546
if (enableLexicalBorrowScopesFlag) {
15431547
if (*enableLexicalBorrowScopesFlag) {
1544-
Opts.LexicalLifetimes = LexicalLifetimesOption::Early;
1548+
Opts.LexicalLifetimes = LexicalLifetimesOption::DiagnosticMarkersOnly;
15451549
} else {
15461550
Opts.LexicalLifetimes = LexicalLifetimesOption::Off;
15471551
}

lib/SILOptimizer/Mandatory/LexicalLifetimeEliminator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LexicalLifetimeEliminatorPass : public SILFunctionTransform {
3030
// run this pass since we want lexical lifetimes to exist later in the
3131
// pipeline.
3232
if (fn->getModule().getOptions().LexicalLifetimes ==
33-
LexicalLifetimesOption::ExperimentalLate)
33+
LexicalLifetimesOption::On)
3434
return;
3535

3636
bool madeChange = false;

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) {
180180

181181
// Now that we have finished performing diagnostics that rely on lexical
182182
// scopes, if lexical lifetimes are not enabled, eliminate lexical lfietimes.
183-
if (Options.LexicalLifetimes != LexicalLifetimesOption::ExperimentalLate) {
183+
if (Options.LexicalLifetimes != LexicalLifetimesOption::On) {
184184
P.addLexicalLifetimeEliminator();
185185
}
186186

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,7 @@ static bool shouldAddLexicalLifetime(AllocStackInst *asi) {
316316
asi->getFunction()
317317
->getModule()
318318
.getASTContext()
319-
.SILOpts.LexicalLifetimes ==
320-
LexicalLifetimesOption::ExperimentalLate &&
319+
.SILOpts.LexicalLifetimes == LexicalLifetimesOption::On &&
321320
asi->isLexical() &&
322321
!asi->getElementType().isTrivial(*asi->getFunction());
323322
}

tools/sil-opt/SILOpt.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,9 @@ int main(int argc, char **argv) {
528528
SILOpts.DisableCopyPropagation = DisableCopyPropagation;
529529

530530
if (EnableCopyPropagation)
531-
SILOpts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
531+
SILOpts.LexicalLifetimes = LexicalLifetimesOption::On;
532+
if (DisableCopyPropagation)
533+
SILOpts.LexicalLifetimes = LexicalLifetimesOption::DiagnosticMarkersOnly;
532534

533535
// Enable lexical lifetimes if it is set or if experimental move only is
534536
// enabled. This is because move only depends on lexical lifetimes being
@@ -541,7 +543,7 @@ int main(int argc, char **argv) {
541543
exit(-1);
542544
}
543545
if (enableLexicalLifetimes)
544-
SILOpts.LexicalLifetimes = LexicalLifetimesOption::ExperimentalLate;
546+
SILOpts.LexicalLifetimes = LexicalLifetimesOption::On;
545547
if (!EnableLexicalBorrowScopes)
546548
SILOpts.LexicalLifetimes = LexicalLifetimesOption::Off;
547549

0 commit comments

Comments
 (0)