Skip to content

Commit e5d87f7

Browse files
committed
[SIL] Add source formal type to checked_cast_br.
It is necessary for opaque values where for casts that will newly start out as checked_cast_brs and be lowered to checked_cast_addr_brs, since the latter has the source formal type, IRGen relies on being able to access it, and there's no way in general to obtain the source formal type from the source lowered type.
1 parent b3cd553 commit e5d87f7

File tree

101 files changed

+431
-410
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

101 files changed

+431
-410
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyRefCasts.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ private extension UnaryInstruction {
3636
/// ```
3737
/// %2 = upcast %1 : $Derived to $Base
3838
/// %3 = init_existential_ref %2 : $Base : $Base, $AnyObject
39-
/// checked_cast_br %3 : $AnyObject to Derived, bb1, bb2
39+
/// checked_cast_br AnyObject in %3 : $AnyObject to Derived, bb1, bb2
4040
/// ```
4141
///
4242
/// This makes it more likely that the cast can be constant folded because the source
4343
/// operand's type is more accurate. In the example above, the cast reduces to
4444
/// ```
45-
/// checked_cast_br %1 : $Derived to Derived, bb1, bb2
45+
/// checked_cast_br Derived in %1 : $Derived to Derived, bb1, bb2
4646
/// ```
4747
/// which can be trivially folded to always-succeeds.
4848
///
@@ -97,13 +97,13 @@ private extension UnaryInstruction {
9797
/// For example:
9898
/// ```
9999
/// %inst = upcast %sourceValue : $Derived to $Base
100-
/// checked_cast_br %inst : $Base to Derived, success_block, failure_block
100+
/// checked_cast_br Base in %inst : $Base to Derived, success_block, failure_block
101101
/// ...
102102
/// failure_block(%oldArg : $Base):
103103
/// ```
104104
/// is converted to:
105105
/// ```
106-
/// checked_cast_br %sourceValue : $Derived to Derived, success_block, failure_block
106+
/// checked_cast_br Derived in %sourceValue : $Derived to Derived, success_block, failure_block
107107
/// ...
108108
/// failure_block(%newArg : $Derived):
109109
/// %3 = upcast %newArg : $Derived to $Base

docs/SIL.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ derived from the ARC object. As an example, consider the following Swift/SIL::
20822082

20832083
// Consume '%1'. This means '%1' can no longer be used after this point. We
20842084
// rebind '%1' in the destination blocks (bbYes, bbNo).
2085-
checked_cast_br %1 : $Klass to $OtherKlass, bbYes, bbNo
2085+
checked_cast_br Klass in %1 : $Klass to $OtherKlass, bbYes, bbNo
20862086

20872087
bbYes(%2 : @owned $OtherKlass): // On success, the checked_cast_br forwards
20882088
// '%1' into '%2' after casting to OtherKlass.
@@ -8097,9 +8097,9 @@ checked_cast_br
80978097
sil-identifier ',' sil-identifier
80988098
sil-checked-cast-exact ::= '[' 'exact' ']'
80998099

8100-
checked_cast_br %0 : $A to $B, bb1, bb2
8101-
checked_cast_br %0 : $*A to $*B, bb1, bb2
8102-
checked_cast_br [exact] %0 : $A to $A, bb1, bb2
8100+
checked_cast_br A in %0 : $A to $B, bb1, bb2
8101+
checked_cast_br *A in %0 : $*A to $*B, bb1, bb2
8102+
checked_cast_br [exact] A in %0 : $A to $A, bb1, bb2
81038103
// $A and $B must be both object types or both address types
81048104
// bb1 must take a single argument of type $B or $*B
81058105
// bb2 must take no arguments

include/swift/SIL/SILBuilder.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2575,17 +2575,18 @@ class SILBuilder {
25752575
}
25762576

25772577
CheckedCastBranchInst *
2578-
createCheckedCastBranch(SILLocation Loc, bool isExact, SILValue op,
2579-
SILType destLoweredTy, CanType destFormalTy,
2580-
SILBasicBlock *successBB,
2578+
createCheckedCastBranch(SILLocation Loc, bool isExact, SILValue op,
2579+
CanType srcFormalTy, SILType destLoweredTy,
2580+
CanType destFormalTy, SILBasicBlock *successBB,
25812581
SILBasicBlock *failureBB,
25822582
ProfileCounter Target1Count = ProfileCounter(),
25832583
ProfileCounter Target2Count = ProfileCounter());
25842584

25852585
CheckedCastBranchInst *
2586-
createCheckedCastBranch(SILLocation Loc, bool isExact, SILValue op,
2587-
SILType destLoweredTy, CanType destFormalTy,
2588-
SILBasicBlock *successBB, SILBasicBlock *failureBB,
2586+
createCheckedCastBranch(SILLocation Loc, bool isExact, SILValue op,
2587+
CanType srcFormalTy, SILType destLoweredTy,
2588+
CanType destFormalTy, SILBasicBlock *successBB,
2589+
SILBasicBlock *failureBB,
25892590
ValueOwnershipKind forwardingOwnershipKind,
25902591
ProfileCounter Target1Count = ProfileCounter(),
25912592
ProfileCounter Target2Count = ProfileCounter());

include/swift/SIL/SILCloner.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,6 +3128,7 @@ SILCloner<ImplClass>::visitCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
31283128
Inst, getBuilder().createCheckedCastBranch(
31293129
getOpLocation(Inst->getLoc()), Inst->isExact(),
31303130
getOpValue(Inst->getOperand()),
3131+
getOpASTType(Inst->getSourceFormalType()),
31313132
getOpType(Inst->getTargetLoweredType()),
31323133
getOpASTType(Inst->getTargetFormalType()), OpSuccBB, OpFailBB,
31333134
Inst->getForwardingOwnershipKind(), TrueCount, FalseCount));

include/swift/SIL/SILInstruction.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10024,12 +10024,13 @@ class CheckedCastBranchInst final
1002410024
CastBranchInstBase<OwnershipForwardingTermInst>> {
1002510025
friend SILBuilder;
1002610026

10027+
CanType SrcFormalTy;
1002710028
SILType DestLoweredTy;
1002810029
CanType DestFormalTy;
1002910030
bool IsExact;
1003010031

1003110032
CheckedCastBranchInst(SILDebugLocation DebugLoc, bool IsExact,
10032-
SILValue Operand,
10033+
SILValue Operand, CanType SrcFormalTy,
1003310034
ArrayRef<SILValue> TypeDependentOperands,
1003410035
SILType DestLoweredTy, CanType DestFormalTy,
1003510036
SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB,
@@ -10041,21 +10042,21 @@ class CheckedCastBranchInst final
1004110042
DebugLoc, Operand, TypeDependentOperands, SuccessBB, FailureBB,
1004210043
Target1Count, Target2Count, forwardingOwnershipKind,
1004310044
preservesOwnership),
10044-
DestLoweredTy(DestLoweredTy), DestFormalTy(DestFormalTy),
10045-
IsExact(IsExact) {}
10045+
SrcFormalTy(SrcFormalTy), DestLoweredTy(DestLoweredTy),
10046+
DestFormalTy(DestFormalTy), IsExact(IsExact) {}
1004610047

1004710048
static CheckedCastBranchInst *
1004810049
create(SILDebugLocation DebugLoc, bool IsExact, SILValue Operand,
10049-
SILType DestLoweredTy, CanType DestFormalTy, SILBasicBlock *SuccessBB,
10050-
SILBasicBlock *FailureBB, SILFunction &F,
10050+
CanType SrcFormalTy, SILType DestLoweredTy, CanType DestFormalTy,
10051+
SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB, SILFunction &F,
1005110052
ProfileCounter Target1Count, ProfileCounter Target2Count,
1005210053
ValueOwnershipKind forwardingOwnershipKind);
1005310054

1005410055
public:
1005510056
bool isExact() const { return IsExact; }
1005610057

1005710058
SILType getSourceLoweredType() const { return getOperand()->getType(); }
10058-
CanType getSourceFormalType() const { return getSourceLoweredType().getASTType(); }
10059+
CanType getSourceFormalType() const { return SrcFormalTy; }
1005910060

1006010061
SILType getTargetLoweredType() const { return DestLoweredTy; }
1006110062
CanType getTargetFormalType() const { return DestFormalTy; }

lib/IRGen/GenCast.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ FailableCastResult irgen::emitClassIdenticalCast(IRGenFunction &IGF,
125125
// of the metatype value to the subclass's static metatype instance.
126126
//
127127
// %1 = value_metatype $Super.Type, %0 : $A
128-
// checked_cast_br [exact] %1 : $Super.Type to $Sub.Type
128+
// checked_cast_br [exact] Super.Type in %1 : $Super.Type to $Sub.Type
129129
// =>
130130
// icmp eq %1, @metadata.Sub
131131
llvm::Value *objectMetadata = isMetatype ? from :

lib/SIL/IR/SILBuilder.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ void SILBuilder::emitScopedBorrowOperation(SILLocation loc, SILValue original,
675675
/// Example:
676676
///
677677
/// %mt = metatype $@thick C.Type
678-
/// checked_cast_br %mt : $@thick C.Type to AnyObject.Type, bb1, bb2,
678+
/// checked_cast_br C.Type in %mt : $@thick C.Type to AnyObject.Type, bb1, bb2,
679679
/// forwarding: @owned
680680
/// bb1(%arg : @owned AnyObject.Type):
681681
///
@@ -717,30 +717,30 @@ SwitchEnumInst *SILBuilder::createSwitchEnum(
717717
}
718718

719719
CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
720-
SILLocation Loc, bool isExact, SILValue op,
721-
SILType destLoweredTy, CanType destFormalTy,
722-
SILBasicBlock *successBB, SILBasicBlock *failureBB,
723-
ProfileCounter target1Count, ProfileCounter target2Count) {
720+
SILLocation Loc, bool isExact, SILValue op, CanType srcFormalTy,
721+
SILType destLoweredTy, CanType destFormalTy, SILBasicBlock *successBB,
722+
SILBasicBlock *failureBB, ProfileCounter target1Count,
723+
ProfileCounter target2Count) {
724724
auto forwardingOwnership =
725725
deriveForwardingOwnership(op, destLoweredTy, getFunction());
726-
return createCheckedCastBranch(Loc, isExact, op, destLoweredTy, destFormalTy,
727-
successBB, failureBB, forwardingOwnership,
728-
target1Count, target2Count);
726+
return createCheckedCastBranch(
727+
Loc, isExact, op, srcFormalTy, destLoweredTy, destFormalTy, successBB,
728+
failureBB, forwardingOwnership, target1Count, target2Count);
729729
}
730730

731731
CheckedCastBranchInst *SILBuilder::createCheckedCastBranch(
732-
SILLocation Loc, bool isExact, SILValue op, SILType destLoweredTy,
733-
CanType destFormalTy, SILBasicBlock *successBB, SILBasicBlock *failureBB,
734-
ValueOwnershipKind forwardingOwnershipKind, ProfileCounter target1Count,
735-
ProfileCounter target2Count) {
732+
SILLocation Loc, bool isExact, SILValue op, CanType srcFormalTy,
733+
SILType destLoweredTy, CanType destFormalTy, SILBasicBlock *successBB,
734+
SILBasicBlock *failureBB, ValueOwnershipKind forwardingOwnershipKind,
735+
ProfileCounter target1Count, ProfileCounter target2Count) {
736736
assert((!hasOwnership() || !failureBB->getNumArguments() ||
737737
failureBB->getArgument(0)->getType() == op->getType()) &&
738738
"failureBB's argument doesn't match incoming argument type");
739739

740740
return insertTerminator(CheckedCastBranchInst::create(
741-
getSILDebugLocation(Loc), isExact, op, destLoweredTy, destFormalTy,
742-
successBB, failureBB, getFunction(), target1Count, target2Count,
743-
forwardingOwnershipKind));
741+
getSILDebugLocation(Loc), isExact, op, srcFormalTy, destLoweredTy,
742+
destFormalTy, successBB, failureBB, getFunction(), target1Count,
743+
target2Count, forwardingOwnershipKind));
744744
}
745745

746746
void SILBuilderWithScope::insertAfter(SILInstruction *inst,

lib/SIL/IR/SILInstructions.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,8 +2551,8 @@ UnconditionalCheckedCastInst *UnconditionalCheckedCastInst::create(
25512551

25522552
CheckedCastBranchInst *CheckedCastBranchInst::create(
25532553
SILDebugLocation DebugLoc, bool IsExact, SILValue Operand,
2554-
SILType DestLoweredTy, CanType DestFormalTy, SILBasicBlock *SuccessBB,
2555-
SILBasicBlock *FailureBB, SILFunction &F,
2554+
CanType SrcFormalTy, SILType DestLoweredTy, CanType DestFormalTy,
2555+
SILBasicBlock *SuccessBB, SILBasicBlock *FailureBB, SILFunction &F,
25562556
ProfileCounter Target1Count, ProfileCounter Target2Count,
25572557
ValueOwnershipKind forwardingOwnershipKind) {
25582558
SILModule &module = F.getModule();
@@ -2561,12 +2561,12 @@ CheckedCastBranchInst *CheckedCastBranchInst::create(
25612561
SmallVector<SILValue, 8> TypeDependentOperands;
25622562
collectTypeDependentOperands(TypeDependentOperands, F, DestFormalTy);
25632563
unsigned size =
2564-
totalSizeToAlloc<swift::Operand>(1 + TypeDependentOperands.size());
2564+
totalSizeToAlloc<swift::Operand>(3 + TypeDependentOperands.size());
25652565
void *Buffer = module.allocateInst(size, alignof(CheckedCastBranchInst));
25662566
return ::new (Buffer) CheckedCastBranchInst(
2567-
DebugLoc, IsExact, Operand, TypeDependentOperands, DestLoweredTy,
2568-
DestFormalTy, SuccessBB, FailureBB, Target1Count, Target2Count,
2569-
forwardingOwnershipKind, preservesOwnership);
2567+
DebugLoc, IsExact, Operand, SrcFormalTy, TypeDependentOperands,
2568+
DestLoweredTy, DestFormalTy, SuccessBB, FailureBB, Target1Count,
2569+
Target2Count, forwardingOwnershipKind, preservesOwnership);
25702570
}
25712571

25722572
MetatypeInst *MetatypeInst::create(SILDebugLocation Loc, SILType Ty,

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,6 +1914,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
19141914
void visitCheckedCastBranchInst(CheckedCastBranchInst *CI) {
19151915
if (CI->isExact())
19161916
*this << "[exact] ";
1917+
*this << CI->getSourceFormalType() << " in ";
19171918
*this << getIDAndType(CI->getOperand()) << " to " << CI->getTargetFormalType()
19181919
<< ", " << Ctx.getID(CI->getSuccessBB()) << ", "
19191920
<< Ctx.getID(CI->getFailureBB());

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4489,6 +4489,9 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
44894489
parseSILOptional(isExact, *this, "exact"))
44904490
return true;
44914491

4492+
if (parseASTType(SourceType) || parseVerbatim("in"))
4493+
return true;
4494+
44924495
if (parseTypedValueRef(Val, B) || parseVerbatim("to") ||
44934496
parseASTType(TargetType) || parseConditionalBranchDestinations())
44944497
return true;
@@ -4501,8 +4504,9 @@ bool SILParser::parseSpecificSILInstruction(SILBuilder &B,
45014504

45024505
auto opaque = Lowering::AbstractionPattern::getOpaque();
45034506
ResultVal = B.createCheckedCastBranch(
4504-
InstLoc, isExact, Val, F->getLoweredType(opaque, TargetType),
4505-
TargetType, getBBForReference(SuccessBBName, SuccessBBLoc),
4507+
InstLoc, isExact, Val, SourceType,
4508+
F->getLoweredType(opaque, TargetType), TargetType,
4509+
getBBForReference(SuccessBBName, SuccessBBLoc),
45064510
getBBForReference(FailureBBName, FailureBBLoc), forwardingOwnership);
45074511
break;
45084512
}

0 commit comments

Comments
 (0)