Skip to content

[SIL Opaque values] Add support for unchecked_bitwise_cast in AddressLowering #42064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions lib/SILOptimizer/Mandatory/AddressLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ static Operand *getProjectedDefOperand(SILValue value) {
/// is address-only, then the operand must be address-only and therefore must
/// mapped to ValueStorage.
///
/// If \p value is an unchecked_bitwise_cast, then return the cast operand.
///
/// open_existential_value must reuse storage because the boxed value is shared
/// with other instances of the existential. An explicit copy is needed to
/// obtain an owned value.
Expand All @@ -812,6 +814,7 @@ static Operand *getReusedStorageOperand(SILValue value) {
case ValueKind::OpenExistentialValueInst:
case ValueKind::OpenExistentialBoxValueInst:
case ValueKind::UncheckedEnumDataInst:
case ValueKind::UncheckedBitwiseCastInst:
return &cast<SingleValueInstruction>(value)->getOperandRef(0);

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

void visitUncheckedBitwiseCast(UncheckedBitwiseCastInst *uncheckedCastInst) {
// FIXME: Unimplemented
llvm::report_fatal_error("Unimplemented UncheckedBitwiseCast use.");
void
visitUncheckedBitwiseCastInst(UncheckedBitwiseCastInst *uncheckedCastInst) {
SILValue srcVal = uncheckedCastInst->getOperand();
SILValue srcAddr = pass.valueStorageMap.getStorage(srcVal).storageAddress;

auto destAddr = builder.createUncheckedAddrCast(
uncheckedCastInst->getLoc(), srcAddr,
uncheckedCastInst->getType().getAddressType());

markRewritten(uncheckedCastInst, destAddr);
}

void visitUncheckedEnumDataInst(UncheckedEnumDataInst *enumDataInst);
Expand Down
19 changes: 19 additions & 0 deletions test/SILOptimizer/address_lowering.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1169,3 +1169,22 @@ bb1(%3 : @owned $(T, Int, Int, T)):
bb2(%9 : $Error):
throw %9 : $Error
}

// CHECK-LABEL: sil hidden [ossa] @test_unchecked_bitwise_cast :
// CHECK: bb0(%0 : $*U, %1 : $*T, %2 : $@thick U.Type):
// CHECK: [[STK:%.*]] = alloc_stack $T
// CHECK: copy_addr %1 to [initialization] [[STK]] : $*T
// CHECK: [[CAST:%.*]] = unchecked_addr_cast [[STK]] : $*T to $*U
// CHECK: copy_addr [[CAST]] to [initialization] %0 : $*U
// CHECK: destroy_addr [[STK]] : $*T
// CHECK: dealloc_stack [[STK]] : $*T
// CHECK-LABEL: } // end sil function 'test_unchecked_bitwise_cast'
sil hidden [ossa] @test_unchecked_bitwise_cast : $@convention(thin) <T, U> (@in_guaranteed T, @thick U.Type) -> @out U {
bb0(%0 : @guaranteed $T, %1 : $@thick U.Type):
%4 = copy_value %0 : $T
%5 = unchecked_bitwise_cast %4 : $T to $U
%6 = copy_value %5 : $U
destroy_value %4 : $T
return %6 : $U
}