Skip to content

Commit 1e2b6a3

Browse files
committed
libswift: bridge more functions from SILBuilder
1 parent 211ac64 commit 1e2b6a3

File tree

14 files changed

+128
-11
lines changed

14 files changed

+128
-11
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

@@ -84,6 +85,14 @@ struct PassContext {
8485

8586
RefCountingInst_setIsAtomic(instruction.bridged, isAtomic)
8687
}
88+
89+
func getDeallocRef(for type: Type) -> Function? {
90+
PassContext_getDeallocRef(passContext, type.bridged).function
91+
}
92+
93+
func getContextSubstitutionMap(for type: Type) -> SubstitutionMap {
94+
SubstitutionMap(PassContext_getContextSubstitutionMap(passContext, type.bridged))
95+
}
8796
}
8897

8998
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.
@@ -67,4 +68,42 @@ public struct Builder {
6768
bridgedInsPoint, location.bridgedLocation, type.bridged, value)
6869
return literal.getAs(IntegerLiteralInst.self)
6970
}
71+
72+
public func createUncheckedRefCast(object: Value, type: Type) -> UncheckedRefCastInst {
73+
notifyInstructionsChanged()
74+
let object = SILBuilder_createUncheckedRefCast(
75+
bridgedInsPoint, location.bridgedLocation, object.bridged, type.bridged)
76+
return object.getAs(UncheckedRefCastInst.self)
77+
}
78+
79+
@discardableResult
80+
public func createSetDeallocating(operand: Value, isAtomic: Bool) -> SetDeallocatingInst {
81+
notifyInstructionsChanged()
82+
let setDeallocating = SILBuilder_createSetDeallocating(
83+
bridgedInsPoint, location.bridgedLocation, operand.bridged, isAtomic)
84+
return setDeallocating.getAs(SetDeallocatingInst.self)
85+
}
86+
87+
public func createFunctionRef(_ function: Function) -> FunctionRefInst {
88+
notifyInstructionsChanged()
89+
let functionRef = SILBuilder_createFunctionRef(
90+
bridgedInsPoint, location.bridgedLocation, function.bridged)
91+
return functionRef.getAs(FunctionRefInst.self)
92+
}
93+
94+
@discardableResult
95+
public func createApply(
96+
function: Value,
97+
_ substitutionMap: SubstitutionMap,
98+
arguments: [Value]
99+
) -> FunctionRefInst {
100+
notifyInstructionsChanged()
101+
let functionRef = arguments.withBridgedValues { valuesRef in
102+
SILBuilder_createApply(
103+
bridgedInsPoint, location.bridgedLocation, function.bridged,
104+
substitutionMap.bridged, valuesRef
105+
)
106+
}
107+
return functionRef.getAs(FunctionRefInst.self)
108+
}
70109
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,7 @@ final public class Function : CustomStringConvertible {
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/Instruction.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ final public class GlobalValueInst : GlobalAccessInst {}
330330

331331
final public class IntegerLiteralInst : SingleValueInstruction {}
332332

333+
final public class FunctionRefInst: SingleValueInstruction {}
334+
333335
final public class TupleInst : SingleValueInstruction {
334336
}
335337

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public func registerSILClasses() {
7979
register(GlobalAddrInst.self)
8080
register(GlobalValueInst.self)
8181
register(IntegerLiteralInst.self)
82+
register(FunctionRefInst.self)
8283
register(TupleInst.self)
8384
register(TupleExtractInst.self)
8485
register(TupleElementAddrInst.self)

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
@@ -131,4 +131,3 @@ extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
131131
return nil
132132
}
133133
}
134-

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,22 @@ BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
253253
BridgedLocation loc, BridgedValue condition, BridgedStringRef message);
254254
BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionPoint,
255255
BridgedLocation loc, BridgedType type, SwiftInt value);
256-
void SILBuilder_createDevirtualizedRelease(
257-
BridgedFunction dealloc, BridgedInstruction release, BridgedValue object);
256+
BridgedInstruction SILBuilder_createUncheckedRefCast(BridgedInstruction insertionPoint,
257+
BridgedLocation loc,
258+
BridgedValue op,
259+
BridgedType type);
260+
BridgedInstruction
261+
SILBuilder_createSetDeallocating(BridgedInstruction insertionPoint,
262+
BridgedLocation loc, BridgedValue op,
263+
bool isAtomic);
264+
BridgedInstruction
265+
SILBuilder_createFunctionRef(BridgedInstruction insertionPoint,
266+
BridgedLocation loc, BridgedFunction function);
267+
BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,
268+
BridgedLocation loc,
269+
BridgedValue function,
270+
BridgedSubstitutionMap subMap,
271+
BridgedValueArray arguments);
258272

259273
SWIFT_END_NULLABILITY_ANNOTATIONS
260274

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
@@ -89,9 +89,13 @@ RCIdentityAnalysis_getFunctionInfo(BridgedRCIdentityAnalysis bridgedAnalysis,
8989
BridgedValue RCIdentityFunctionInfo_getRCIdentityRoot(
9090
BridgedRCIdentityFunctionInfo bridgedInfo, BridgedValue value);
9191

92-
OptionalBridgedFunction PassContext_getDestructor(BridgedPassContext context,
92+
OptionalBridgedFunction PassContext_getDeallocRef(BridgedPassContext context,
9393
BridgedType type);
9494

95+
BridgedSubstitutionMap
96+
PassContext_getContextSubstitutionMap(BridgedPassContext context,
97+
BridgedType bridgedType);
98+
9599
#ifdef __cplusplus
96100
} // extern "C"
97101
#endif

lib/SIL/Utils/SILBridging.cpp

Lines changed: 42 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.
@@ -518,3 +518,44 @@ BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionP
518518
return {builder.createIntegerLiteral(getRegularLocation(loc),
519519
getSILType(type), value)};
520520
}
521+
522+
BridgedInstruction
523+
SILBuilder_createUncheckedRefCast(BridgedInstruction insertionPoint,
524+
BridgedLocation loc, BridgedValue op,
525+
BridgedType type) {
526+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
527+
return {builder.createUncheckedRefCast(getRegularLocation(loc),
528+
castToSILValue(op), getSILType(type))};
529+
}
530+
531+
BridgedInstruction
532+
SILBuilder_createSetDeallocating(BridgedInstruction insertionPoint,
533+
BridgedLocation loc, BridgedValue op,
534+
bool isAtomic) {
535+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
536+
return {builder.createSetDeallocating(
537+
getRegularLocation(loc), castToSILValue(op),
538+
isAtomic ? RefCountingInst::Atomicity::Atomic
539+
: RefCountingInst::Atomicity::NonAtomic)};
540+
}
541+
542+
BridgedInstruction
543+
SILBuilder_createFunctionRef(BridgedInstruction insertionPoint,
544+
BridgedLocation loc,
545+
BridgedFunction function) {
546+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
547+
return {builder.createFunctionRef(getRegularLocation(loc),
548+
castToFunction(function))};
549+
}
550+
551+
BridgedInstruction SILBuilder_createApply(BridgedInstruction insertionPoint,
552+
BridgedLocation loc,
553+
BridgedValue function,
554+
BridgedSubstitutionMap subMap,
555+
BridgedValueArray arguments) {
556+
SILBuilder builder(castToInst(insertionPoint), getSILDebugScope(loc));
557+
SmallVector<SILValue, 16> argValues;
558+
return {builder.createApply(getRegularLocation(loc), castToSILValue(function),
559+
castToSubstitutionMap(subMap),
560+
getSILValues(arguments, argValues))};
561+
}

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,7 @@ PassContext_getRCIdentityAnalysis(BridgedPassContext context) {
12171217
return {pm->getAnalysis<RCIdentityAnalysis>()};
12181218
}
12191219

1220-
OptionalBridgedFunction PassContext_getDestructor(BridgedPassContext context,
1220+
OptionalBridgedFunction PassContext_getDeallocRef(BridgedPassContext context,
12211221
BridgedType type) {
12221222
auto *cd = castToSILType(type).getClassOrBoundGenericClass();
12231223
assert(cd && "no class type allocated with alloc_ref");

0 commit comments

Comments
 (0)