Skip to content

Commit 5f7f6e4

Browse files
committed
Rename SILBuilder::createUncheckedBitCast
to SILBuilder::createUncheckedForwardingCast It would be disastrous to confuse this utility with a bit cast. A bit cast always produces an Unowned value which must immediately be copied to be used. This utility always forwards ownership. It cannot be used to truncate values. Also, be careful not to convert "reinterpret cast" (e.g. Builtin.reinterpretCast) into a "value cast" since ownership will be incorrect and the reinterpreted types might not have equivalent layout.
1 parent af276b7 commit 5f7f6e4

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

include/swift/SIL/SILBuilder.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,13 +2102,20 @@ class SILBuilder {
21022102
SingleValueInstruction *
21032103
createUncheckedReinterpretCast(SILLocation Loc, SILValue Op, SILType Ty);
21042104

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

21132120
//===--------------------------------------------------------------------===//
21142121
// Runtime failure

lib/SIL/IR/SILBuilder.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ SILBuilder::createUncheckedReinterpretCast(SILLocation Loc, SILValue Op,
174174

175175
// Create the appropriate cast instruction based on result type.
176176
SingleValueInstruction *
177-
SILBuilder::createUncheckedBitCast(SILLocation Loc, SILValue Op, SILType Ty) {
177+
SILBuilder::createUncheckedForwardingCast(SILLocation Loc, SILValue Op,
178+
SILType Ty) {
178179
// Without ownership, delegate to unchecked reinterpret cast.
179180
if (!hasOwnership())
180181
return createUncheckedReinterpretCast(Loc, Op, Ty);

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
169169
Args.push_back(UAC);
170170
} else if (OldOpType.getASTType() != NewOpType.getASTType()) {
171171
auto URC =
172-
Builder.createUncheckedBitCast(AI.getLoc(), Op, NewOpType);
172+
Builder.createUncheckedForwardingCast(AI.getLoc(), Op, NewOpType);
173173
Args.push_back(URC);
174174
} else {
175175
Args.push_back(Op);
@@ -221,8 +221,8 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
221221
for (auto e = newOpResultTypes.end(); newRetI != e;
222222
++oldRetI, ++newRetI, ++origArgI) {
223223
auto arg = normalBB->createPhiArgument(*newRetI, (*origArgI)->getOwnershipKind());
224-
auto converted = Builder.createUncheckedBitCast(AI.getLoc(),
225-
arg, *oldRetI);
224+
auto converted =
225+
Builder.createUncheckedForwardingCast(AI.getLoc(), arg, *oldRetI);
226226
branchArgs.push_back(converted);
227227
}
228228

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

248248
if (oldResultTy != newResultTy) {
249-
result = Builder.createUncheckedBitCast(AI.getLoc(), NAI, oldResultTy);
249+
result =
250+
Builder.createUncheckedForwardingCast(AI.getLoc(), NAI, oldResultTy);
250251
}
251252

252253
return result;

lib/SILOptimizer/Transforms/EagerSpecializer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ void EagerDispatch::emitDispatchTo(SILFunction *NewFunc) {
438438
auto GenResultTy = GenericFunc->mapTypeIntoContext(resultTy);
439439

440440
SILValue CastResult =
441-
Builder.createUncheckedBitCast(Loc, Result, GenResultTy);
441+
Builder.createUncheckedForwardingCast(Loc, Result, GenResultTy);
442442

443443
addReturnValue(Builder.getInsertionBB(), OldReturnBB, CastResult);
444444
}
@@ -640,7 +640,7 @@ SILValue EagerDispatch::emitArgumentCast(CanSILFunctionType CalleeSubstFnTy,
640640
if (CastTy.isAddress())
641641
return Builder.createUncheckedAddrCast(Loc, OrigArg, CastTy);
642642

643-
return Builder.createUncheckedBitCast(Loc, OrigArg, CastTy);
643+
return Builder.createUncheckedForwardingCast(Loc, OrigArg, CastTy);
644644
}
645645

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

0 commit comments

Comments
 (0)