Skip to content

Commit 0b7b4bf

Browse files
committed
libswift: bridge more functions from SILBuilder
1 parent 6ceb2fb commit 0b7b4bf

File tree

12 files changed

+128
-9
lines changed

12 files changed

+128
-9
lines changed

SwiftCompilerSources/Sources/AST/SubstitutionMap.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SILBridging
1414

1515
public struct SubstitutionMap {
16-
let bridged: BridgedSubstitutionMap
16+
public let bridged: BridgedSubstitutionMap
1717

1818
public init(_ bridged: BridgedSubstitutionMap) {
1919
self.bridged = bridged

SwiftCompilerSources/Sources/Optimizer/PassManager/PassUtils.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import AST
1314
import SIL
1415
import OptimizerBridging
1516

@@ -115,6 +116,14 @@ struct PassContext {
115116
func fixStackNesting(function: Function) {
116117
PassContext_fixStackNesting(_bridged, function.bridged)
117118
}
119+
120+
func getDeallocRef(for type: Type) -> Function? {
121+
PassContext_getDeallocRef(_bridged, type.bridged).function
122+
}
123+
124+
func getContextSubstitutionMap(for type: Type) -> SubstitutionMap {
125+
SubstitutionMap(PassContext_getContextSubstitutionMap(_bridged, type.bridged))
126+
}
118127
}
119128

120129
struct FunctionPass {

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13+
import AST
1314
import SILBridging
1415

1516
/// A utility to create new instructions at a given insertion point.
@@ -73,4 +74,42 @@ public struct Builder {
7374
let dr = SILBuilder_createDeallocStackRef(bridgedInsPoint, location.bridgedLocation, operand.bridged)
7475
return dr.getAs(DeallocStackRefInst.self)
7576
}
77+
78+
public func createUncheckedRefCast(object: Value, type: Type) -> UncheckedRefCastInst {
79+
notifyInstructionsChanged()
80+
let object = SILBuilder_createUncheckedRefCast(
81+
bridgedInsPoint, location.bridgedLocation, object.bridged, type.bridged)
82+
return object.getAs(UncheckedRefCastInst.self)
83+
}
84+
85+
@discardableResult
86+
public func createSetDeallocating(operand: Value, isAtomic: Bool) -> SetDeallocatingInst {
87+
notifyInstructionsChanged()
88+
let setDeallocating = SILBuilder_createSetDeallocating(
89+
bridgedInsPoint, location.bridgedLocation, operand.bridged, isAtomic)
90+
return setDeallocating.getAs(SetDeallocatingInst.self)
91+
}
92+
93+
public func createFunctionRef(_ function: Function) -> FunctionRefInst {
94+
notifyInstructionsChanged()
95+
let functionRef = SILBuilder_createFunctionRef(
96+
bridgedInsPoint, location.bridgedLocation, function.bridged)
97+
return functionRef.getAs(FunctionRefInst.self)
98+
}
99+
100+
@discardableResult
101+
public func createApply(
102+
function: Value,
103+
_ substitutionMap: SubstitutionMap,
104+
arguments: [Value]
105+
) -> FunctionRefInst {
106+
notifyInstructionsChanged()
107+
let functionRef = arguments.withBridgedValues { valuesRef in
108+
SILBuilder_createApply(
109+
bridgedInsPoint, location.bridgedLocation, function.bridged,
110+
substitutionMap.bridged, valuesRef
111+
)
112+
}
113+
return functionRef.getAs(FunctionRefInst.self)
114+
}
76115
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ public func != (lhs: Function, rhs: Function) -> Bool { lhs !== rhs }
5959
extension BridgedFunction {
6060
public var function: Function { obj.getAs(Function.self) }
6161
}
62+
63+
extension OptionalBridgedFunction {
64+
public var function: Function? { obj.getAs(Function.self) }
65+
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SILBridging
1414

1515
public struct Type {
16-
var bridged: BridgedType
16+
public let bridged: BridgedType
1717

1818
public var isAddress: Bool { SILType_isAddress(bridged) != 0 }
1919
public var isObject: Bool { !isAddress }

SwiftCompilerSources/Sources/SIL/Utils.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,3 @@ extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
219219
return nil
220220
}
221221
}
222-

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ extension Value {
2424
}
2525

2626
public var uses: UseList {
27-
return UseList(SILValue_firstUse(bridged))
27+
UseList(SILValue_firstUse(bridged))
2828
}
2929

3030
public var type: Type {
31-
return Type(bridged: SILValue_getType(bridged))
31+
Type(bridged: SILValue_getType(bridged))
3232
}
3333

3434
public var hashable: HashableValue { ObjectIdentifier(self) }
@@ -48,7 +48,7 @@ public func ==(_ lhs: Value, _ rhs: Value) -> Bool {
4848
}
4949

5050
extension BridgedValue {
51-
func getAs<T: AnyObject>(_ valueType: T.Type) -> T { obj.getAs(T.self) }
51+
public func getAs<T: AnyObject>(_ valueType: T.Type) -> T { obj.getAs(T.self) }
5252
}
5353

5454
final class Undef : Value {

include/swift/SIL/SILBridging.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,22 @@ BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionP
252252
BridgedLocation loc, BridgedType type, SwiftInt value);
253253
BridgedInstruction SILBuilder_createDeallocStackRef(BridgedInstruction insertionPoint,
254254
BridgedLocation loc, BridgedValue operand);
255+
BridgedInstruction SILBuilder_createUncheckedRefCast(BridgedInstruction insertionPoint,
256+
BridgedLocation loc,
257+
BridgedValue op,
258+
BridgedType type);
259+
BridgedInstruction
260+
SILBuilder_createSetDeallocating(BridgedInstruction insertionPoint,
261+
BridgedLocation loc, BridgedValue op,
262+
bool isAtomic);
263+
BridgedInstruction
264+
SILBuilder_createFunctionRef(BridgedInstruction insertionPoint,
265+
BridgedLocation loc, BridgedFunction function);
266+
BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,
267+
BridgedLocation loc,
268+
BridgedValue function,
269+
BridgedSubstitutionMap subMap,
270+
BridgedValueArray arguments);
255271

256272
SWIFT_END_NULLABILITY_ANNOTATIONS
257273

include/swift/SIL/SILBridgingUtils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ inline SILType castToSILType(BridgedType type) {
7474
return SILType::getFromOpaqueValue(type.typePtr);
7575
}
7676

77+
inline SubstitutionMap castToSubstitutionMap(BridgedSubstitutionMap subMap) {
78+
return SubstitutionMap::getFromOpaqueValue(subMap.op);
79+
}
80+
7781
template <class I = SILInstruction> I *castToInst(BridgedInstruction inst) {
7882
return cast<I>(static_cast<SILNode *>(inst.obj)->castToInstruction());
7983
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,13 @@ RCIdentityAnalysis_getFunctionInfo(BridgedRCIdentityAnalysis bridgedAnalysis,
151151
BridgedValue RCIdentityFunctionInfo_getRCIdentityRoot(
152152
BridgedRCIdentityFunctionInfo bridgedInfo, BridgedValue value);
153153

154-
OptionalBridgedFunction PassContext_getDestructor(BridgedPassContext context,
154+
OptionalBridgedFunction PassContext_getDeallocRef(BridgedPassContext context,
155155
BridgedType type);
156156

157+
BridgedSubstitutionMap
158+
PassContext_getContextSubstitutionMap(BridgedPassContext context,
159+
BridgedType bridgedType);
160+
157161
#ifdef __cplusplus
158162
} // extern "C"
159163
#endif

lib/SIL/Utils/SILBridging.cpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ void registerBridgedClass(BridgedStringRef className, SwiftMetatype metatype) {
9393
}
9494

9595
// Pre-populate the "unimplemented" ranges of metatypes.
96-
// If a specifc class is not implemented in Swift yet, it bridges to an
96+
// If a specific class is not implemented in Swift yet, it bridges to an
9797
// "unimplemented" class. This ensures that optimizations handle _all_ kind of
9898
// instructions gracefully, without the need to define the not-yet-used
9999
// classes in Swift.
@@ -541,3 +541,45 @@ BridgedInstruction SILBuilder_createDeallocStackRef(BridgedInstruction insertion
541541
return {builder.createDeallocStackRef(getRegularLocation(loc),
542542
castToSILValue(operand))};
543543
}
544+
545+
BridgedInstruction
546+
SILBuilder_createUncheckedRefCast(BridgedInstruction insertionPoint,
547+
BridgedLocation loc, BridgedValue op,
548+
BridgedType type) {
549+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
550+
return {builder.createUncheckedRefCast(getRegularLocation(loc),
551+
castToSILValue(op), getSILType(type))};
552+
}
553+
554+
BridgedInstruction
555+
SILBuilder_createSetDeallocating(BridgedInstruction insertionPoint,
556+
BridgedLocation loc, BridgedValue op,
557+
bool isAtomic) {
558+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
559+
return {builder.createSetDeallocating(
560+
getRegularLocation(loc), castToSILValue(op),
561+
isAtomic ? RefCountingInst::Atomicity::Atomic
562+
: RefCountingInst::Atomicity::NonAtomic)};
563+
}
564+
565+
BridgedInstruction
566+
SILBuilder_createFunctionRef(BridgedInstruction insertionPoint,
567+
BridgedLocation loc,
568+
BridgedFunction function) {
569+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
570+
return {builder.createFunctionRef(getRegularLocation(loc),
571+
castToFunction(function))};
572+
}
573+
574+
BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,
575+
BridgedLocation loc,
576+
BridgedValue function,
577+
BridgedSubstitutionMap subMap,
578+
BridgedValueArray arguments) {
579+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
580+
SmallVector<SILValue, 16> argValues;
581+
return {builder.createApply(getRegularLocation(loc), castToSILValue(function),
582+
castToSubstitutionMap(subMap),
583+
getSILValues(arguments, argValues))};
584+
}
585+

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,13 +1320,15 @@ BridgedFunction BasicBlockSet_getFunction(BridgedBasicBlockSet set) {
13201320

13211321
void AllocRefInstBase_setIsStackAllocatable(BridgedInstruction arb) {
13221322
castToInst<AllocRefInstBase>(arb)->setStackAllocatable();
1323+
}
1324+
13231325
BridgedRCIdentityAnalysis
13241326
PassContext_getRCIdentityAnalysis(BridgedPassContext context) {
13251327
SILPassManager *pm = castToPassInvocation(context)->getPassManager();
13261328
return {pm->getAnalysis<RCIdentityAnalysis>()};
13271329
}
13281330

1329-
OptionalBridgedFunction PassContext_getDestructor(BridgedPassContext context,
1331+
OptionalBridgedFunction PassContext_getDeallocRef(BridgedPassContext context,
13301332
BridgedType type) {
13311333
auto *cd = castToSILType(type).getClassOrBoundGenericClass();
13321334
assert(cd && "no class type allocated with alloc_ref");

0 commit comments

Comments
 (0)