Skip to content
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
19 changes: 13 additions & 6 deletions include/swift/SIL/SILBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -2102,13 +2102,20 @@ class SILBuilder {
SingleValueInstruction *
createUncheckedReinterpretCast(SILLocation Loc, SILValue Op, SILType Ty);

/// Create an appropriate cast instruction based on result type.
/// Create an appropriate cast instruction based on result type. This cast
/// forwards ownership from the operand to the result.
///
/// NOTE: This assumes that the input and the result cast are layout
/// compatible. Reduces to createUncheckedReinterpretCast when ownership is
/// disabled.
SingleValueInstruction *createUncheckedBitCast(SILLocation Loc, SILValue Op,
SILType Ty);
/// WARNING: Because it forwards ownership, this cast is only valid with the
/// source and destination types are layout equivalent. The destination type
/// must include all the same references in the same positions.
///
/// Note: Forwarding casts do not exist outside of OSSA. When ownership is
/// disabled, this reduces to createUncheckedReinterpretCast, which may
/// fall-back to unchecked_bitwise_cast. It is the caller's responsibility to
/// emit the correct retains and releases.
SingleValueInstruction *createUncheckedForwardingCast(SILLocation Loc,
SILValue Op,
SILType Ty);

//===--------------------------------------------------------------------===//
// Runtime failure
Expand Down
3 changes: 2 additions & 1 deletion lib/SIL/IR/SILBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ SILBuilder::createUncheckedReinterpretCast(SILLocation Loc, SILValue Op,

// Create the appropriate cast instruction based on result type.
SingleValueInstruction *
SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
SILBuilder::createUncheckedForwardingCast(SILLocation Loc, SILValue Op,
SILType Ty) {
// Without ownership, delegate to unchecked reinterpret cast.
if (!hasOwnership())
return createUncheckedReinterpretCast(Loc, Op, Ty);
Copy link
Contributor

@meg-gupta meg-gupta Sep 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

createUncheckedReinterpretCast can call UncheckedBitWiseCast::create, so not really forwarding cast ? : )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without ownership, the forwarding cast degenerates into a bit-wise cast. I can explain that in comments.

Expand Down
9 changes: 5 additions & 4 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
Args.push_back(UAC);
} else if (OldOpType.getASTType() != NewOpType.getASTType()) {
auto URC =
Builder.createUncheckedBitCast(AI.getLoc(), Op, NewOpType);
Builder.createUncheckedForwardingCast(AI.getLoc(), Op, NewOpType);
Args.push_back(URC);
} else {
Args.push_back(Op);
Expand Down Expand Up @@ -221,8 +221,8 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
for (auto e = newOpResultTypes.end(); newRetI != e;
++oldRetI, ++newRetI, ++origArgI) {
auto arg = normalBB->createPhiArgument(*newRetI, (*origArgI)->getOwnershipKind());
auto converted = Builder.createUncheckedBitCast(AI.getLoc(),
arg, *oldRetI);
auto converted =
Builder.createUncheckedForwardingCast(AI.getLoc(), arg, *oldRetI);
branchArgs.push_back(converted);
}

Expand All @@ -246,7 +246,8 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
SILInstruction *result = NAI;

if (oldResultTy != newResultTy) {
result = Builder.createUncheckedBitCast(AI.getLoc(), NAI, oldResultTy);
result =
Builder.createUncheckedForwardingCast(AI.getLoc(), NAI, oldResultTy);
}

return result;
Expand Down
4 changes: 2 additions & 2 deletions lib/SILOptimizer/Transforms/EagerSpecializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ void EagerDispatch::emitDispatchTo(SILFunction *NewFunc) {
auto GenResultTy = GenericFunc->mapTypeIntoContext(resultTy);

SILValue CastResult =
Builder.createUncheckedBitCast(Loc, Result, GenResultTy);
Builder.createUncheckedForwardingCast(Loc, Result, GenResultTy);

addReturnValue(Builder.getInsertionBB(), OldReturnBB, CastResult);
}
Expand Down Expand Up @@ -640,7 +640,7 @@ SILValue EagerDispatch::emitArgumentCast(CanSILFunctionType CalleeSubstFnTy,
if (CastTy.isAddress())
return Builder.createUncheckedAddrCast(Loc, OrigArg, CastTy);

return Builder.createUncheckedBitCast(Loc, OrigArg, CastTy);
return Builder.createUncheckedForwardingCast(Loc, OrigArg, CastTy);
}

/// Converts each generic function argument into a SILValue that can be passed
Expand Down