Skip to content

Commit f95a68a

Browse files
authored
Merge pull request swiftlang#27721 from eeckstein/temprvalueopt
TempRValueOptPass: accept try_apply as user of the temporary object
2 parents 8dc43e6 + e7ddae6 commit f95a68a

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,7 +1645,8 @@ bool TempRValueOptPass::collectLoads(
16451645
<< *user);
16461646
return false;
16471647

1648-
case SILInstructionKind::ApplyInst: {
1648+
case SILInstructionKind::ApplyInst:
1649+
case SILInstructionKind::TryApplyInst: {
16491650
ApplySite apply(user);
16501651

16511652
// Check if the function can just read from userOp.
@@ -1714,12 +1715,10 @@ bool TempRValueOptPass::collectLoads(
17141715

17151716
case SILInstructionKind::LoadInst:
17161717
case SILInstructionKind::LoadBorrowInst:
1717-
case SILInstructionKind::WitnessMethodInst: {
17181718
// Loads are the end of the data flow chain. The users of the load can't
17191719
// access the temporary storage.
17201720
loadInsts.insert(user);
17211721
return true;
1722-
}
17231722

17241723
case SILInstructionKind::CopyAddrInst: {
17251724
// copy_addr which read from the temporary are like loads.
@@ -1834,17 +1833,9 @@ bool TempRValueOptPass::tryOptimizeCopyIntoTemp(CopyAddrInst *copyInst) {
18341833
use->set(copyInst->getSrc());
18351834
break;
18361835
}
1837-
case SILInstructionKind::StructElementAddrInst:
1838-
case SILInstructionKind::TupleElementAddrInst:
1839-
case SILInstructionKind::LoadInst:
1840-
case SILInstructionKind::LoadBorrowInst:
1841-
case SILInstructionKind::ApplyInst:
1842-
case SILInstructionKind::OpenExistentialAddrInst:
1836+
default:
18431837
use->set(copyInst->getSrc());
18441838
break;
1845-
1846-
default:
1847-
llvm_unreachable("unhandled instruction");
18481839
}
18491840
}
18501841
tempObj->eraseFromParent();

test/SILOptimizer/temp_rvalue_opt.sil

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ bb0(%0 : $*Klass, %1 : $*Klass):
3131
return %9999 : $()
3232
}
3333

34+
sil @throwing_function : $@convention(thin) (@in_guaranteed Klass) -> ((), @error Error)
35+
3436
///////////
3537
// Tests //
3638
///////////
@@ -383,6 +385,26 @@ bb0(%0 : $*Klass):
383385
return %9 : $()
384386
}
385387

388+
// CHECK-LABEL: sil @try_apply_argument : $@convention(thin) (@inout Klass) -> () {
389+
// CHECK-NOT: copy_addr
390+
// CHECK: try_apply {{%[0-9]+}}(%0)
391+
// CHECK: } // end sil function 'try_apply_argument'
392+
sil @try_apply_argument : $@convention(thin) (@inout Klass) -> () {
393+
bb0(%0 : $*Klass):
394+
%1 = alloc_stack $Klass
395+
copy_addr %0 to [initialization] %1 : $*Klass
396+
%5 = function_ref @throwing_function : $@convention(thin) (@in_guaranteed Klass) -> ((), @error Error)
397+
try_apply %5(%1) : $@convention(thin) (@in_guaranteed Klass) -> ((), @error Error), normal bb1, error bb2
398+
bb1(%r : $()):
399+
br bb3
400+
bb2(%e : $Error):
401+
br bb3
402+
bb3:
403+
destroy_addr %1 : $*Klass
404+
dealloc_stack %1 : $*Klass
405+
%9 = tuple ()
406+
return %9 : $()
407+
}
386408

387409
// Make sure that we can eliminate temporaries passed via a temporary rvalue to
388410
// an @in_guaranteed function.

0 commit comments

Comments
 (0)