Skip to content

Commit 855e007

Browse files
committed
[AddressLowering] Handle try_apply results.
When a block argument is a terminator result from a try_apply, use the ApplyRewriter to convert the try_apply. In the case where the result is stored into an enum, with this change, the init_enum_data_addr instruction is created prior to the try_apply which is necessary in order for it to be passed as an argument to the try_apply.
1 parent fc6f1b8 commit 855e007

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3172,6 +3172,11 @@ class DefRewriter : SILInstructionVisitor<DefRewriter> {
31723172
protected:
31733173
// Set the storage address for an opaque block arg and mark it rewritten.
31743174
void rewriteArg(SILPhiArgument *arg) {
3175+
if (auto *tai =
3176+
dyn_cast_or_null<TryApplyInst>(arg->getTerminatorForResult())) {
3177+
ApplyRewriter(tai, pass).convertApplyWithIndirectResults();
3178+
return;
3179+
}
31753180
LLVM_DEBUG(llvm::dbgs() << "REWRITE ARG "; arg->dump());
31763181
if (storage.storageAddress)
31773182
LLVM_DEBUG(llvm::dbgs() << " STORAGE "; storage.storageAddress->dump());

test/SILOptimizer/address_lowering.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,25 @@ bb2(%9 : $Error):
12471247
throw %9 : $Error
12481248
}
12491249

1250+
// CHECK-LABEL: sil [ossa] @f261_testTryApplyIntoEnum : $@convention(thin) <T> () -> (@out Optional<T>, @error any Error) {
1251+
// CHECK: {{bb[0-9]+}}([[OUT_ADDR:%[^,]+]] : $*Optional<T>):
1252+
// CHECK: [[SOME_ADDR:%[^,]+]] = init_enum_data_addr %0 : $*Optional<T>, #Optional.some!enumelt
1253+
// CHECK: try_apply undef<T>([[SOME_ADDR]]) {{.*}}, normal [[NORMAL:bb[0-9]+]], error {{bb[0-9]+}}
1254+
// CHECK: [[NORMAL]]
1255+
// CHECK: inject_enum_addr [[OUT_ADDR]] : $*Optional<T>, #Optional.some!enumelt
1256+
// CHECK-LABEL: } // end sil function 'f261_testTryApplyIntoEnum'
1257+
sil [ossa] @f261_testTryApplyIntoEnum : $@convention(thin) <T> () -> (@out Optional<T>, @error any Error) {
1258+
entry:
1259+
try_apply undef<T>() : $@convention(thin) <T> () -> (@out T, @error any Error), normal okay, error nopers
1260+
1261+
okay(%instance : @owned $T):
1262+
%some = enum $Optional<T>, #Optional.some!enumelt, %instance : $T
1263+
return %some : $Optional<T>
1264+
1265+
nopers(%error : @owned $any Error):
1266+
throw %error : $any Error
1267+
}
1268+
12501269
// CHECK-LABEL: sil [ossa] @fixeeLifetime : {{.*}} {
12511270
// CHECK: {{bb[0-9]+}}([[ADDR:%[^,]+]] : $*T):
12521271
// CHECK: fix_lifetime [[ADDR]]

0 commit comments

Comments
 (0)