Skip to content

Commit 308b5f9

Browse files
committed
Swift Optimizer: add bridging for dynamic cast utilities
* `func canDynamicallyCast` * `var CheckedCastAddrBranchInst.dynamicCastResult`
1 parent 6bb5ad6 commit 308b5f9

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,3 +695,23 @@ func getGlobalInitialization(
695695
}
696696
return nil
697697
}
698+
699+
func canDynamicallyCast(from sourceType: Type, to destType: Type, in function: Function, sourceTypeIsExact: Bool) -> Bool? {
700+
switch classifyDynamicCastBridged(sourceType.bridged, destType.bridged, function.bridged, sourceTypeIsExact) {
701+
case .willSucceed: return true
702+
case .maySucceed: return nil
703+
case .willFail: return false
704+
default: fatalError("unknown result from classifyDynamicCastBridged")
705+
}
706+
}
707+
708+
extension CheckedCastAddrBranchInst {
709+
var dynamicCastResult: Bool? {
710+
switch classifyDynamicCastBridged(bridged) {
711+
case .willSucceed: return true
712+
case .maySucceed: return nil
713+
case .willFail: return false
714+
default: fatalError("unknown result from classifyDynamicCastBridged")
715+
}
716+
}
717+
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,18 @@ struct BridgedPassContext {
331331

332332
bool FullApplySite_canInline(BridgedInstruction apply);
333333

334+
enum class BridgedDynamicCastResult {
335+
willSucceed,
336+
maySucceed,
337+
willFail
338+
};
339+
340+
BridgedDynamicCastResult classifyDynamicCastBridged(BridgedType sourceTy, BridgedType destTy,
341+
BridgedFunction function,
342+
bool sourceTypeIsExact);
343+
344+
BridgedDynamicCastResult classifyDynamicCastBridged(BridgedInstruction inst);
345+
334346
//===----------------------------------------------------------------------===//
335347
// Pass registration
336348
//===----------------------------------------------------------------------===//

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "swift/Demangling/Demangle.h"
1919
#include "../../IRGen/IRGenModule.h"
2020
#include "swift/SIL/ApplySite.h"
21+
#include "swift/SIL/DynamicCasts.h"
2122
#include "swift/SIL/SILCloner.h"
2223
#include "swift/SIL/SILFunction.h"
2324
#include "swift/SIL/SILModule.h"
@@ -1838,6 +1839,25 @@ bool FullApplySite_canInline(BridgedInstruction apply) {
18381839
swift::FullApplySite(apply.unbridged()));
18391840
}
18401841

1842+
BridgedDynamicCastResult classifyDynamicCastBridged(BridgedType sourceTy, BridgedType destTy,
1843+
BridgedFunction function,
1844+
bool sourceTypeIsExact) {
1845+
static_assert((int)DynamicCastFeasibility::WillSucceed == (int)BridgedDynamicCastResult::willSucceed);
1846+
static_assert((int)DynamicCastFeasibility::MaySucceed == (int)BridgedDynamicCastResult::maySucceed);
1847+
static_assert((int)DynamicCastFeasibility::WillFail == (int)BridgedDynamicCastResult::willFail);
1848+
1849+
return static_cast<BridgedDynamicCastResult>(
1850+
classifyDynamicCast(function.getFunction()->getModule().getSwiftModule(),
1851+
sourceTy.unbridged().getASTType(),
1852+
destTy.unbridged().getASTType(),
1853+
sourceTypeIsExact));
1854+
}
1855+
1856+
BridgedDynamicCastResult classifyDynamicCastBridged(BridgedInstruction inst) {
1857+
SILDynamicCastInst castInst(inst.unbridged());
1858+
return static_cast<BridgedDynamicCastResult>(castInst.classifyFeasibility(/*allowWholeModule=*/ false));
1859+
}
1860+
18411861
// TODO: can't be inlined to work around https://github.com/apple/swift/issues/64502
18421862
BridgedCalleeAnalysis::CalleeList BridgedCalleeAnalysis::getCallees(BridgedValue callee) const {
18431863
return ca->getCalleeListOfValue(callee.getSILValue());

0 commit comments

Comments
 (0)