Skip to content

Commit e7ddae6

Browse files
committed
TempRValueOptPass: accept try_apply as user of the temporary object
So far we only handled apply, but not try_apply.
1 parent aa0d60a commit e7ddae6

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 2 additions & 1 deletion
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.

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)