Skip to content

Commit d996b98

Browse files
authored
Merge pull request #42064 from meg-gupta/opaquevaluework
[SIL Opaque values] Add support for unchecked_bitwise_cast in AddressLowering
2 parents a49548c + 425d457 commit d996b98

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,8 @@ static Operand *getProjectedDefOperand(SILValue value) {
798798
/// is address-only, then the operand must be address-only and therefore must
799799
/// mapped to ValueStorage.
800800
///
801+
/// If \p value is an unchecked_bitwise_cast, then return the cast operand.
802+
///
801803
/// open_existential_value must reuse storage because the boxed value is shared
802804
/// with other instances of the existential. An explicit copy is needed to
803805
/// obtain an owned value.
@@ -812,6 +814,7 @@ static Operand *getReusedStorageOperand(SILValue value) {
812814
case ValueKind::OpenExistentialValueInst:
813815
case ValueKind::OpenExistentialBoxValueInst:
814816
case ValueKind::UncheckedEnumDataInst:
817+
case ValueKind::UncheckedBitwiseCastInst:
815818
return &cast<SingleValueInstruction>(value)->getOperandRef(0);
816819

817820
case ValueKind::SILPhiArgument: {
@@ -2551,9 +2554,16 @@ class UseRewriter : SILInstructionVisitor<UseRewriter> {
25512554
// Extract from an opaque tuple.
25522555
void visitTupleExtractInst(TupleExtractInst *extractInst);
25532556

2554-
void visitUncheckedBitwiseCast(UncheckedBitwiseCastInst *uncheckedCastInst) {
2555-
// FIXME: Unimplemented
2556-
llvm::report_fatal_error("Unimplemented UncheckedBitwiseCast use.");
2557+
void
2558+
visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *uncheckedCastInst) {
2559+
SILValue srcVal = uncheckedCastInst->getOperand();
2560+
SILValue srcAddr = pass.valueStorageMap.getStorage(srcVal).storageAddress;
2561+
2562+
auto destAddr = builder.createUncheckedAddrCast(
2563+
uncheckedCastInst->getLoc(), srcAddr,
2564+
uncheckedCastInst->getType().getAddressType());
2565+
2566+
markRewritten(uncheckedCastInst, destAddr);
25572567
}
25582568

25592569
void visitUncheckedEnumDataInst(UncheckedEnumDataInst *enumDataInst);

test/SILOptimizer/address_lowering.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,3 +1169,22 @@ bb1(%3 : @owned $(T, Int, Int, T)):
11691169
bb2(%9 : $Error):
11701170
throw %9 : $Error
11711171
}
1172+
1173+
// CHECK-LABEL: sil hidden [ossa] @test_unchecked_bitwise_cast :
1174+
// CHECK: bb0(%0 : $*U, %1 : $*T, %2 : $@thick U.Type):
1175+
// CHECK: [[STK:%.*]] = alloc_stack $T
1176+
// CHECK: copy_addr %1 to [initialization] [[STK]] : $*T
1177+
// CHECK: [[CAST:%.*]] = unchecked_addr_cast [[STK]] : $*T to $*U
1178+
// CHECK: copy_addr [[CAST]] to [initialization] %0 : $*U
1179+
// CHECK: destroy_addr [[STK]] : $*T
1180+
// CHECK: dealloc_stack [[STK]] : $*T
1181+
// CHECK-LABEL: } // end sil function 'test_unchecked_bitwise_cast'
1182+
sil hidden [ossa] @test_unchecked_bitwise_cast : $@convention(thin) <T, U> (@in_guaranteed T, @thick U.Type) -> @out U {
1183+
bb0(%0 : @guaranteed $T, %1 : $@thick U.Type):
1184+
%4 = copy_value %0 : $T
1185+
%5 = unchecked_bitwise_cast %4 : $T to $U
1186+
%6 = copy_value %5 : $U
1187+
destroy_value %4 : $T
1188+
return %6 : $U
1189+
}
1190+

0 commit comments

Comments
 (0)