Skip to content

Commit 5325a4f

Browse files
committed
Swift SIL: add some instruction classes and APIs
* add `UnownedRetainInst` and `UnownedReleaseInst` * add `var value` to `RetainValueInst` and `ReleaseValueInst` * make the protocol `UnaryInstruction` be an `Instruction` * add `var Type.isValueTypeWithDeinit` * add `var Type.isUnownedStorageType` * add `var OperandArray.values`
1 parent ae9a0c1 commit 5325a4f

File tree

9 files changed

+65
-2
lines changed

9 files changed

+65
-2
lines changed

SwiftCompilerSources/Sources/SIL/ApplySite.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extension ApplySite {
8080
///
8181
/// This does not include the callee function operand.
8282
public var arguments: LazyMapSequence<OperandArray, Value> {
83-
argumentOperands.lazy.map { $0.value }
83+
argumentOperands.values
8484
}
8585

8686
public var substitutionMap: SubstitutionMap {

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,18 @@ public struct Builder {
136136
return notifyNew(release.getAs(StrongReleaseInst.self))
137137
}
138138

139+
@discardableResult
140+
public func createUnownedRetain(operand: Value) -> UnownedRetainInst {
141+
let retain = bridged.createUnownedRetain(operand.bridged)
142+
return notifyNew(retain.getAs(UnownedRetainInst.self))
143+
}
144+
145+
@discardableResult
146+
public func createUnownedRelease(operand: Value) -> UnownedReleaseInst {
147+
let release = bridged.createUnownedRelease(operand.bridged)
148+
return notifyNew(release.getAs(UnownedReleaseInst.self))
149+
}
150+
139151
public func createFunctionRef(_ function: Function) -> FunctionRefInst {
140152
let functionRef = bridged.createFunctionRef(function.bridged)
141153
return notifyNew(functionRef.getAs(FunctionRefInst.self))

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public class MultipleValueInstruction : Instruction {
197197
}
198198

199199
/// Instructions, which have a single operand.
200-
public protocol UnaryInstruction : AnyObject {
200+
public protocol UnaryInstruction : Instruction {
201201
var operands: OperandArray { get }
202202
var operand: Operand { get }
203203
}
@@ -306,14 +306,24 @@ final public class StrongRetainInst : RefCountingInst {
306306
public var instance: Value { operand.value }
307307
}
308308

309+
final public class UnownedRetainInst : RefCountingInst {
310+
public var instance: Value { operand.value }
311+
}
312+
309313
final public class RetainValueInst : RefCountingInst {
314+
public var value: Value { return operand.value }
310315
}
311316

312317
final public class StrongReleaseInst : RefCountingInst {
313318
public var instance: Value { operand.value }
314319
}
315320

321+
final public class UnownedReleaseInst : RefCountingInst {
322+
public var instance: Value { operand.value }
323+
}
324+
316325
final public class ReleaseValueInst : RefCountingInst {
326+
public var value: Value { return operand.value }
317327
}
318328

319329
final public class DestroyValueInst : Instruction, UnaryInstruction {

SwiftCompilerSources/Sources/SIL/Operand.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ public struct OperandArray : RandomAccessCollection, CustomReflectable {
7676
base: OptionalBridgedOperand(op: base.advancedBy(bounds.lowerBound).op),
7777
count: bounds.upperBound - bounds.lowerBound)
7878
}
79+
80+
public var values: LazyMapSequence<LazySequence<OperandArray>.Elements, Value> {
81+
self.lazy.map { $0.value }
82+
}
7983
}
8084

8185
public struct UseList : CollectionLikeSequence {

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ public func registerSILClasses() {
5454
register(EndApplyInst.self)
5555
register(AbortApplyInst.self)
5656
register(StrongRetainInst.self)
57+
register(UnownedRetainInst.self)
5758
register(RetainValueInst.self)
5859
register(StrongReleaseInst.self)
60+
register(UnownedReleaseInst.self)
5961
register(ReleaseValueInst.self)
6062
register(DestroyValueInst.self)
6163
register(DestroyAddrInst.self)

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
3131
return !bridged.isNonTrivialOrContainsRawPointer(function.bridged.getFunction())
3232
}
3333

34+
/// True if this type is a value type (struct/enum) that requires deinitialization beyond
35+
/// destruction of its members.
36+
public var isValueTypeWithDeinit: Bool { bridged.isValueTypeWithDeinit() }
37+
3438
public func isLoadable(in function: Function) -> Bool {
3539
return bridged.isLoadable(function.bridged.getFunction())
3640
}
@@ -39,6 +43,10 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
3943
return bridged.isReferenceCounted(function.bridged.getFunction())
4044
}
4145

46+
public var isUnownedStorageType: Bool {
47+
return bridged.isUnownedStorageType()
48+
}
49+
4250
public var hasArchetype: Bool { bridged.hasArchetype() }
4351

4452
public var isNominal: Bool { bridged.getNominalOrBoundGenericNominal() != nil }

include/swift/SIL/SILBridging.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,18 @@ struct BridgedBuilder{
11051105
return {b.createStrongRelease(regularLoc(), op.getSILValue(), b.getDefaultAtomicity())};
11061106
}
11071107

1108+
SWIFT_IMPORT_UNSAFE
1109+
BridgedInstruction createUnownedRetain(BridgedValue op) const {
1110+
auto b = builder();
1111+
return {b.createUnownedRetain(regularLoc(), op.getSILValue(), b.getDefaultAtomicity())};
1112+
}
1113+
1114+
SWIFT_IMPORT_UNSAFE
1115+
BridgedInstruction createUnownedRelease(BridgedValue op) const {
1116+
auto b = builder();
1117+
return {b.createUnownedRelease(regularLoc(), op.getSILValue(), b.getDefaultAtomicity())};
1118+
}
1119+
11081120
SWIFT_IMPORT_UNSAFE
11091121
BridgedInstruction createFunctionRef(BridgedFunction function) const {
11101122
return {builder().createFunctionRef(regularLoc(), function.getFunction())};

include/swift/SIL/SILType.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ class SILType {
394394

395395
bool isReferenceCounted(SILFunction *f) const;
396396

397+
bool isUnownedStorageType() const { return is<UnownedStorageType>(); }
398+
397399
/// Returns true if the referenced type is a function type that never
398400
/// returns.
399401
bool isNoReturnFunction(SILModule &M, TypeExpansionContext context) const;
@@ -745,6 +747,10 @@ class SILType {
745747
/// for a move only wrapped type.
746748
bool isPureMoveOnly() const;
747749

750+
/// Return true if this is a value type (struct/enum) that requires
751+
/// deinitialization beyond destruction of its members.
752+
bool isValueTypeWithDeinit() const;
753+
748754
/// Returns true if and only if this type is a first class move only
749755
/// type. NOTE: Returns false if the type is a move only wrapped type.
750756
bool isMoveOnlyNominalType() const;

lib/SIL/IR/SILType.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,15 @@ bool SILType::isPureMoveOnly() const {
10481048
return false;
10491049
}
10501050

1051+
bool SILType::isValueTypeWithDeinit() const {
1052+
// Do not look inside an aggregate type that has a user-deinit, for which
1053+
// memberwise-destruction is not equivalent to aggregate destruction.
1054+
if (auto *nominal = getNominalOrBoundGenericNominal()) {
1055+
return nominal->getValueTypeDestructor() != nullptr;
1056+
}
1057+
return false;
1058+
}
1059+
10511060
SILType SILType::getInstanceTypeOfMetatype(SILFunction *function) const {
10521061
auto metaType = castTo<MetatypeType>();
10531062
CanType instanceTy = metaType.getInstanceType();

0 commit comments

Comments
 (0)