Skip to content

Commit 65a5147

Browse files
Merge pull request #61992 from nate-chandler/opaque-values/1/20221108
[AddressLowering] Rewrite checked_casts as seen.
2 parents 429f15f + ed193d6 commit 65a5147

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,6 @@ struct AddressLoweringState {
419419
// parameters are rewritten.
420420
SmallBlotSetVector<FullApplySite, 16> indirectApplies;
421421

422-
// checked_cast_br instructions with loadable source type and opaque target
423-
// type need to be rewritten in a post-pass, once all the uses of the opaque
424-
// target value are rewritten to their address forms.
425-
SmallVector<CheckedCastBranchInst *, 8> opaqueResultCCBs;
426-
427422
// All function-exiting terminators (return or throw instructions).
428423
SmallVector<TermInst *, 8> exitingInsts;
429424

@@ -611,15 +606,6 @@ void OpaqueValueVisitor::mapValueStorage() {
611606
if (auto apply = FullApplySite::isa(&inst))
612607
checkForIndirectApply(apply);
613608

614-
// Collect all checked_cast_br instructions that have a loadable source
615-
// type and opaque target type
616-
if (auto *ccb = dyn_cast<CheckedCastBranchInst>(&inst)) {
617-
if (!ccb->getSourceLoweredType().isAddressOnly(*ccb->getFunction()) &&
618-
ccb->getTargetLoweredType().isAddressOnly(*ccb->getFunction())) {
619-
pass.opaqueResultCCBs.push_back(ccb);
620-
}
621-
}
622-
623609
for (auto result : inst.getResults()) {
624610
if (isPseudoCallResult(result) || isPseudoReturnValue(result))
625611
continue;
@@ -3177,6 +3163,10 @@ class DefRewriter : SILInstructionVisitor<DefRewriter> {
31773163
CallArgRewriter(tai, pass).rewriteArguments();
31783164
ApplyRewriter(tai, pass).convertApplyWithIndirectResults();
31793165
return;
3166+
} else if (auto *ccbi = dyn_cast_or_null<CheckedCastBranchInst>(
3167+
arg->getTerminatorForResult())) {
3168+
CheckedCastBrRewriter(ccbi, pass).rewrite();
3169+
return;
31803170
}
31813171
LLVM_DEBUG(llvm::dbgs() << "REWRITE ARG "; arg->dump());
31823172
if (storage.storageAddress)
@@ -3412,12 +3402,6 @@ static void rewriteFunction(AddressLoweringState &pass) {
34123402
}
34133403
}
34143404

3415-
// Rewrite all checked_cast_br instructions with loadable source type and
3416-
// opaque target type now
3417-
for (auto *ccb : pass.opaqueResultCCBs) {
3418-
CheckedCastBrRewriter(ccb, pass).rewrite();
3419-
}
3420-
34213405
// Rewrite this function's return value now that all opaque values within the
34223406
// function are rewritten. This still depends on a valid ValueStorage
34233407
// projection operands.

test/SILOptimizer/address_lowering.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,6 +1613,36 @@ bb3:
16131613
return %31 : $()
16141614
}
16151615

1616+
// Test the result being stored into an @out enum.
1617+
// CHECK-LABEL: sil [ossa] @test_checked_cast_br4 : $@convention(method) <Element><T> (@owned TestGeneric<Element>) -> @out Optional<T> {
1618+
// CHECK: {{bb[0-9]+}}([[OUT_ADDR:%[^,]+]] : $*Optional<T>, [[INSTANCE:%[^,]+]] : @owned $TestGeneric<Element>):
1619+
// CHECK: [[TEMP:%[^,]+]] = alloc_stack $Optional<T>
1620+
// CHECK: [[CAST_DEST_ADDR:%[^,]+]] = init_enum_data_addr [[TEMP]] : $*Optional<T>, #Optional.some!enumelt
1621+
// CHECK: [[CAST_SOURCE_ADDR:%[^,]+]] = alloc_stack $TestGeneric<Element>
1622+
// CHECK: store [[INSTANCE]] to [init] [[CAST_SOURCE_ADDR]]
1623+
// CHECK: checked_cast_addr_br take_on_success TestGeneric<Element> in [[CAST_SOURCE_ADDR]] : $*TestGeneric<Element> to T in [[CAST_DEST_ADDR]] : $*T, [[SUCCESS:bb[0-9]+]]
1624+
// CHECK: [[SUCCESS]]:
1625+
// CHECK: dealloc_stack [[CAST_SOURCE_ADDR]]
1626+
// CHECK: inject_enum_addr [[TEMP]] : $*Optional<T>, #Optional.some!enumelt
1627+
// CHECK: copy_addr [take] [[TEMP]] to [init] [[OUT_ADDR]]
1628+
// CHECK-LABEL: } // end sil function 'test_checked_cast_br4'
1629+
sil [ossa] @test_checked_cast_br4 : $@convention(method) <Element><T> (@owned TestGeneric<Element>) -> @out Optional<T> {
1630+
bb0(%3 : @owned $TestGeneric<Element>):
1631+
checked_cast_br %3 : $TestGeneric<Element> to T, bb1, bb2
1632+
1633+
bb1(%5 : @owned $T):
1634+
%6 = enum $Optional<T>, #Optional.some!enumelt, %5 : $T
1635+
br bb3(%6 : $Optional<T>)
1636+
1637+
bb2(%8 : @owned $TestGeneric<Element>):
1638+
destroy_value %8 : $TestGeneric<Element>
1639+
%10 = enum $Optional<T>, #Optional.none!enumelt
1640+
br bb3(%10 : $Optional<T>)
1641+
1642+
bb3(%12 : @owned $Optional<T>):
1643+
return %12 : $Optional<T>
1644+
}
1645+
16161646
// CHECK-LABEL: sil hidden [ossa] @test_unchecked_bitwise_cast :
16171647
// CHECK: bb0(%0 : $*U, %1 : $*T, %2 : $@thick U.Type):
16181648
// CHECK: [[STK:%.*]] = alloc_stack $T

0 commit comments

Comments
 (0)