Skip to content

Commit 67b5556

Browse files
authored
Merge pull request #82005 from DougGregor/generalize-checked-cast-options
[SIL] Generalize CastingIsolatedConformances to CheckedCastInstOptions
2 parents 89b09a6 + bc4cf12 commit 67b5556

31 files changed

+253
-247
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyAllocStack.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,14 +213,14 @@ private extension AllocStackInst {
213213
builder.createCheckedCastAddrBranch(
214214
source: newAlloc, sourceFormalType: concreteFormalType,
215215
destination: cab.destination, targetFormalType: cab.targetFormalType,
216-
isolatedConformances: cab.isolatedConformances,
216+
options: cab.checkedCastOptions,
217217
consumptionKind: cab.consumptionKind,
218218
successBlock: cab.successBlock, failureBlock: cab.failureBlock)
219219
context.erase(instruction: cab)
220220
case let ucca as UnconditionalCheckedCastAddrInst:
221221
let builder = Builder(before: ucca, context)
222222
builder.createUnconditionalCheckedCastAddr(
223-
isolatedConformances: ucca.isolatedConformances,
223+
options: ucca.checkedCastOptions,
224224
source: newAlloc, sourceFormalType: concreteFormalType,
225225
destination: ucca.destination, targetFormalType: ucca.targetFormalType)
226226
context.erase(instruction: ucca)

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public struct Builder {
180180
public func createCheckedCastAddrBranch(
181181
source: Value, sourceFormalType: CanonicalType,
182182
destination: Value, targetFormalType: CanonicalType,
183-
isolatedConformances: CastingIsolatedConformances,
183+
options: CheckedCastInstOptions,
184184
consumptionKind: CheckedCastAddrBranchInst.CastConsumptionKind,
185185
successBlock: BasicBlock,
186186
failureBlock: BasicBlock
@@ -195,21 +195,20 @@ public struct Builder {
195195

196196
let cast = bridged.createCheckedCastAddrBranch(source.bridged, sourceFormalType.bridged,
197197
destination.bridged, targetFormalType.bridged,
198-
isolatedConformances.bridged,
198+
options.bridged,
199199
bridgedConsumption,
200200
successBlock.bridged, failureBlock.bridged)
201201
return notifyNew(cast.getAs(CheckedCastAddrBranchInst.self))
202202
}
203203

204204
@discardableResult
205205
public func createUnconditionalCheckedCastAddr(
206-
isolatedConformances: CastingIsolatedConformances,
206+
options: CheckedCastInstOptions,
207207
source: Value, sourceFormalType: CanonicalType,
208208
destination: Value, targetFormalType: CanonicalType
209209
) -> UnconditionalCheckedCastAddrInst {
210210
let cast = bridged.createUnconditionalCheckedCastAddr(
211-
isolatedConformances.bridged, source.bridged,
212-
sourceFormalType.bridged,
211+
options.bridged, source.bridged, sourceFormalType.bridged,
213212
destination.bridged, targetFormalType.bridged
214213
)
215214
return notifyNew(cast.getAs(UnconditionalCheckedCastAddrInst.self))

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,8 @@ final public class UnconditionalCheckedCastAddrInst : Instruction, SourceDestAdd
546546
public var isInitializationOfDestination: Bool { true }
547547
public override var mayTrap: Bool { true }
548548

549-
public var isolatedConformances: CastingIsolatedConformances {
550-
switch bridged.UnconditionalCheckedCastAddr_getIsolatedConformances() {
551-
case .Allow: .allow
552-
case .Prohibit: .prohibit
553-
@unknown default: fatalError("Unhandled CastingIsolatedConformances")
554-
}
549+
public var checkedCastOptions: CheckedCastInstOptions {
550+
.init(storage: bridged.UnconditionalCheckedCastAddr_getCheckedCastOptions().storage)
555551
}
556552
}
557553

@@ -1060,12 +1056,8 @@ class UnconditionalCheckedCastInst : SingleValueInstruction, UnaryInstruction {
10601056
CanonicalType(bridged: bridged.UnconditionalCheckedCast_getTargetFormalType())
10611057
}
10621058

1063-
public var isolatedConformances: CastingIsolatedConformances {
1064-
switch bridged.UnconditionalCheckedCast_getIsolatedConformances() {
1065-
case .Allow: .allow
1066-
case .Prohibit: .prohibit
1067-
@unknown default: fatalError("Unhandled CastingIsolatedConformances")
1068-
}
1059+
public var checkedCastOptions: CheckedCastInstOptions {
1060+
.init(storage: bridged.UnconditionalCheckedCast_getCheckedCastOptions().storage)
10691061
}
10701062
}
10711063

@@ -1793,16 +1785,21 @@ final public class DynamicMethodBranchInst : TermInst {
17931785
final public class AwaitAsyncContinuationInst : TermInst, UnaryInstruction {
17941786
}
17951787

1788+
public struct CheckedCastInstOptions {
1789+
var storage: UInt8 = 0
1790+
1791+
var bridged: BridgedInstruction.CheckedCastInstOptions {
1792+
.init(storage: storage)
1793+
}
1794+
1795+
var isolatedConformances: CastingIsolatedConformances {
1796+
return (storage & 0x01) != 0 ? .prohibit : .allow
1797+
}
1798+
}
1799+
17961800
public enum CastingIsolatedConformances {
17971801
case allow
17981802
case prohibit
1799-
1800-
var bridged: BridgedInstruction.CastingIsolatedConformances {
1801-
switch self {
1802-
case .allow: return .Allow
1803-
case .prohibit: return .Prohibit
1804-
}
1805-
}
18061803
}
18071804

18081805
final public class CheckedCastBranchInst : TermInst, UnaryInstruction {
@@ -1814,12 +1811,8 @@ final public class CheckedCastBranchInst : TermInst, UnaryInstruction {
18141811
bridged.CheckedCastBranch_updateSourceFormalTypeFromOperandLoweredType()
18151812
}
18161813

1817-
public var isolatedConformances: CastingIsolatedConformances {
1818-
switch bridged.CheckedCastBranch_getIsolatedConformances() {
1819-
case .Allow: return .allow
1820-
case .Prohibit: return .prohibit
1821-
default: fatalError("Bad CastingIsolatedConformances value")
1822-
}
1814+
public var checkedCastOptions: CheckedCastInstOptions {
1815+
.init(storage: bridged.CheckedCastBranch_getCheckedCastOptions().storage)
18231816
}
18241817
}
18251818

@@ -1862,12 +1855,8 @@ final public class CheckedCastAddrBranchInst : TermInst {
18621855
}
18631856
}
18641857

1865-
public var isolatedConformances: CastingIsolatedConformances {
1866-
switch bridged.CheckedCastAddrBranch_getIsolatedConformances() {
1867-
case .Allow: .allow
1868-
case .Prohibit: .prohibit
1869-
@unknown default: fatalError("Unhandled CastingIsolatedConformances")
1870-
}
1858+
public var checkedCastOptions: CheckedCastInstOptions {
1859+
.init(storage: bridged.CheckedCastAddrBranch_getCheckedCastOptions().storage)
18711860
}
18721861
}
18731862

include/swift/SIL/DynamicCasts.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ bool canIRGenUseScalarCheckedCastInstructions(SILModule &M,
107107
/// using a scalar cast operation.
108108
void emitIndirectConditionalCastWithScalar(
109109
SILBuilder &B, ModuleDecl *M, SILLocation loc,
110-
CastingIsolatedConformances isolatedConformances,
110+
CheckedCastInstOptions options,
111111
CastConsumptionKind consumption, SILValue src, CanType sourceType,
112112
SILValue dest, CanType targetType, SILBasicBlock *trueBB,
113113
SILBasicBlock *falseBB, ProfileCounter TrueCount = ProfileCounter(),
@@ -458,18 +458,18 @@ struct SILDynamicCastInst {
458458
getModule(), getSourceFormalType(), getTargetFormalType());
459459
}
460460

461-
CastingIsolatedConformances getIsolatedConformances() const {
461+
CheckedCastInstOptions getCheckedCastOptions() const {
462462
switch (getKind()) {
463463
case SILDynamicCastKind::CheckedCastAddrBranchInst:
464-
return cast<CheckedCastAddrBranchInst>(inst)->getIsolatedConformances();
464+
return cast<CheckedCastAddrBranchInst>(inst)->getCheckedCastOptions();
465465
case SILDynamicCastKind::CheckedCastBranchInst:
466-
return cast<CheckedCastBranchInst>(inst)->getIsolatedConformances();
466+
return cast<CheckedCastBranchInst>(inst)->getCheckedCastOptions();
467467
case SILDynamicCastKind::UnconditionalCheckedCastAddrInst:
468468
return cast<UnconditionalCheckedCastAddrInst>(inst)
469-
->getIsolatedConformances();
469+
->getCheckedCastOptions();
470470
case SILDynamicCastKind::UnconditionalCheckedCastInst:
471471
return cast<UnconditionalCheckedCastInst>(inst)
472-
->getIsolatedConformances();
472+
->getCheckedCastOptions();
473473
}
474474
}
475475
};

include/swift/SIL/SILBridging.h

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -716,9 +716,8 @@ struct BridgedInstruction {
716716
CopyOnSuccess
717717
};
718718

719-
enum class CastingIsolatedConformances {
720-
Allow,
721-
Prohibit
719+
struct CheckedCastInstOptions {
720+
uint8_t storage;
722721
};
723722

724723
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef CondFailInst_getMessage() const;
@@ -831,22 +830,22 @@ struct BridgedInstruction {
831830
BRIDGED_INLINE void LoadInst_setOwnership(SwiftInt ownership) const;
832831
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType UnconditionalCheckedCast_getSourceFormalType() const;
833832
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType UnconditionalCheckedCast_getTargetFormalType() const;
834-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CastingIsolatedConformances
835-
UnconditionalCheckedCast_getIsolatedConformances() const;
833+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CheckedCastInstOptions
834+
UnconditionalCheckedCast_getCheckedCastOptions() const;
836835
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType UnconditionalCheckedCastAddr_getSourceFormalType() const;
837836
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType UnconditionalCheckedCastAddr_getTargetFormalType() const;
838-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CastingIsolatedConformances
839-
UnconditionalCheckedCastAddr_getIsolatedConformances() const;
837+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CheckedCastInstOptions
838+
UnconditionalCheckedCastAddr_getCheckedCastOptions() const;
840839
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock CheckedCastBranch_getSuccessBlock() const;
841840
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock CheckedCastBranch_getFailureBlock() const;
842-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CastingIsolatedConformances
843-
CheckedCastBranch_getIsolatedConformances() const;
841+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CheckedCastInstOptions
842+
CheckedCastBranch_getCheckedCastOptions() const;
844843
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType CheckedCastAddrBranch_getSourceFormalType() const;
845844
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType CheckedCastAddrBranch_getTargetFormalType() const;
846845
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock CheckedCastAddrBranch_getSuccessBlock() const;
847846
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedBasicBlock CheckedCastAddrBranch_getFailureBlock() const;
848-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CastingIsolatedConformances
849-
CheckedCastAddrBranch_getIsolatedConformances() const;
847+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE CheckedCastInstOptions
848+
CheckedCastAddrBranch_getCheckedCastOptions() const;
850849
BRIDGED_INLINE void CheckedCastBranch_updateSourceFormalTypeFromOperandLoweredType() const;
851850
BRIDGED_INLINE CastConsumptionKind CheckedCastAddrBranch_getConsumptionKind() const;
852851
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedSubstitutionMap ApplySite_getSubstitutionMap() const;
@@ -1176,11 +1175,11 @@ struct BridgedBuilder{
11761175
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createCheckedCastAddrBranch(
11771176
BridgedValue source, BridgedCanType sourceFormalType,
11781177
BridgedValue destination, BridgedCanType targetFormalType,
1179-
BridgedInstruction::CastingIsolatedConformances isolatedConformances,
1178+
BridgedInstruction::CheckedCastInstOptions options,
11801179
BridgedInstruction::CastConsumptionKind consumptionKind,
11811180
BridgedBasicBlock successBlock, BridgedBasicBlock failureBlock) const;
11821181
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUnconditionalCheckedCastAddr(
1183-
BridgedInstruction::CastingIsolatedConformances isolatedConformances,
1182+
BridgedInstruction::CheckedCastInstOptions options,
11841183
BridgedValue source, BridgedCanType sourceFormalType,
11851184
BridgedValue destination, BridgedCanType targetFormalType) const;
11861185
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction createUncheckedOwnershipConversion(

include/swift/SIL/SILBridgingImpl.h

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1564,10 +1564,11 @@ BridgedCanType BridgedInstruction::UnconditionalCheckedCast_getTargetFormalType(
15641564
return {getAs<swift::UnconditionalCheckedCastInst>()->getTargetFormalType()};
15651565
}
15661566

1567-
BridgedInstruction::CastingIsolatedConformances
1568-
BridgedInstruction::UnconditionalCheckedCast_getIsolatedConformances() const {
1569-
return static_cast<BridgedInstruction::CastingIsolatedConformances>(
1570-
getAs<swift::UnconditionalCheckedCastInst>()->getIsolatedConformances());
1567+
BridgedInstruction::CheckedCastInstOptions
1568+
BridgedInstruction::UnconditionalCheckedCast_getCheckedCastOptions() const {
1569+
return BridgedInstruction::CheckedCastInstOptions{
1570+
getAs<swift::UnconditionalCheckedCastInst>()->getCheckedCastOptions()
1571+
.getStorage()};
15711572
}
15721573

15731574
BridgedCanType BridgedInstruction::UnconditionalCheckedCastAddr_getSourceFormalType() const {
@@ -1578,10 +1579,11 @@ BridgedCanType BridgedInstruction::UnconditionalCheckedCastAddr_getTargetFormalT
15781579
return {getAs<swift::UnconditionalCheckedCastAddrInst>()->getTargetFormalType()};
15791580
}
15801581

1581-
BridgedInstruction::CastingIsolatedConformances
1582-
BridgedInstruction::UnconditionalCheckedCastAddr_getIsolatedConformances() const {
1583-
return static_cast<BridgedInstruction::CastingIsolatedConformances>(
1584-
getAs<swift::UnconditionalCheckedCastAddrInst>()->getIsolatedConformances());
1582+
BridgedInstruction::CheckedCastInstOptions
1583+
BridgedInstruction::UnconditionalCheckedCastAddr_getCheckedCastOptions() const {
1584+
return BridgedInstruction::CheckedCastInstOptions{
1585+
getAs<swift::UnconditionalCheckedCastAddrInst>()->getCheckedCastOptions()
1586+
.getStorage()};
15851587
}
15861588

15871589
BridgedBasicBlock BridgedInstruction::CheckedCastBranch_getSuccessBlock() const {
@@ -1592,10 +1594,11 @@ BridgedBasicBlock BridgedInstruction::CheckedCastBranch_getFailureBlock() const
15921594
return {getAs<swift::CheckedCastBranchInst>()->getFailureBB()};
15931595
}
15941596

1595-
BridgedInstruction::CastingIsolatedConformances
1596-
BridgedInstruction::CheckedCastBranch_getIsolatedConformances() const {
1597-
return static_cast<BridgedInstruction::CastingIsolatedConformances>(
1598-
getAs<swift::CheckedCastBranchInst>()->getIsolatedConformances());
1597+
BridgedInstruction::CheckedCastInstOptions
1598+
BridgedInstruction::CheckedCastBranch_getCheckedCastOptions() const {
1599+
return BridgedInstruction::CheckedCastInstOptions{
1600+
getAs<swift::CheckedCastBranchInst>()->getCheckedCastOptions()
1601+
.getStorage()};
15991602
}
16001603

16011604
BridgedCanType BridgedInstruction::CheckedCastAddrBranch_getSourceFormalType() const {
@@ -1626,10 +1629,11 @@ BridgedInstruction::CastConsumptionKind BridgedInstruction::CheckedCastAddrBranc
16261629
getAs<swift::CheckedCastAddrBranchInst>()->getConsumptionKind());
16271630
}
16281631

1629-
BridgedInstruction::CastingIsolatedConformances
1630-
BridgedInstruction::CheckedCastAddrBranch_getIsolatedConformances() const {
1631-
return static_cast<BridgedInstruction::CastingIsolatedConformances>(
1632-
getAs<swift::CheckedCastAddrBranchInst>()->getIsolatedConformances());
1632+
BridgedInstruction::CheckedCastInstOptions
1633+
BridgedInstruction::CheckedCastAddrBranch_getCheckedCastOptions() const {
1634+
return BridgedInstruction::CheckedCastInstOptions{
1635+
getAs<swift::CheckedCastAddrBranchInst>()->getCheckedCastOptions()
1636+
.getStorage()};
16331637
}
16341638

16351639
BridgedSubstitutionMap BridgedInstruction::ApplySite_getSubstitutionMap() const {
@@ -2190,27 +2194,27 @@ BridgedInstruction BridgedBuilder::createUpcast(BridgedValue op, BridgedType typ
21902194
BridgedInstruction BridgedBuilder::createCheckedCastAddrBranch(
21912195
BridgedValue source, BridgedCanType sourceFormalType,
21922196
BridgedValue destination, BridgedCanType targetFormalType,
2193-
BridgedInstruction::CastingIsolatedConformances isolatedConformances,
2197+
BridgedInstruction::CheckedCastInstOptions options,
21942198
BridgedInstruction::CastConsumptionKind consumptionKind,
21952199
BridgedBasicBlock successBlock, BridgedBasicBlock failureBlock) const
21962200
{
21972201
return {unbridged().createCheckedCastAddrBranch(
21982202
regularLoc(),
2199-
(swift::CastingIsolatedConformances)isolatedConformances,
2203+
swift::CheckedCastInstOptions(options.storage),
22002204
(swift::CastConsumptionKind)consumptionKind,
22012205
source.getSILValue(), sourceFormalType.unbridged(),
22022206
destination.getSILValue(), targetFormalType.unbridged(),
22032207
successBlock.unbridged(), failureBlock.unbridged())};
22042208
}
22052209

22062210
BridgedInstruction BridgedBuilder::createUnconditionalCheckedCastAddr(
2207-
BridgedInstruction::CastingIsolatedConformances isolatedConformances,
2211+
BridgedInstruction::CheckedCastInstOptions options,
22082212
BridgedValue source, BridgedCanType sourceFormalType,
22092213
BridgedValue destination, BridgedCanType targetFormalType) const
22102214
{
22112215
return {unbridged().createUnconditionalCheckedCastAddr(
22122216
regularLoc(),
2213-
(swift::CastingIsolatedConformances)isolatedConformances,
2217+
swift::CheckedCastInstOptions(options.storage),
22142218
source.getSILValue(), sourceFormalType.unbridged(),
22152219
destination.getSILValue(), targetFormalType.unbridged())};
22162220
}

0 commit comments

Comments
 (0)