Skip to content

Commit eb597cb

Browse files
committed
Swift SIL: add a few ownership APIs to ForwardingInstruction
* `canForwardGuaranteedValues` * `canForwardOwnedValues` * `setForwardingOwnership` * `Operand.canAccept(ownership:)`
1 parent 44e1e54 commit eb597cb

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/Context.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,13 @@ extension TermInst {
526526
}
527527
}
528528

529+
extension ForwardingInstruction {
530+
func setForwardingOwnership(to ownership: Ownership, _ context: some MutatingContext) {
531+
context.notifyInstructionsChanged()
532+
bridged.ForwardingInst_setForwardingOwnership(ownership._bridged)
533+
}
534+
}
535+
529536
extension Function {
530537
func set(needStackProtection: Bool, _ context: FunctionPassContext) {
531538
context.notifyEffectsChanged()

SwiftCompilerSources/Sources/SIL/ForwardingInstruction.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public protocol ForwardingInstruction : Instruction {
1818

1919
/// Return true if the forwarded value has the same representation. If true, then the result can be mapped to the same storage without a move or copy.
2020
var preservesRepresentation: Bool { get }
21+
22+
var canForwardGuaranteedValues: Bool { get }
23+
24+
var canForwardOwnedValues: Bool { get }
2125
}
2226

2327
extension ForwardingInstruction {
@@ -47,6 +51,14 @@ extension ForwardingInstruction {
4751
public var preservesReferenceCounts: Bool {
4852
bridged.ForwardingInst_preservesOwnership()
4953
}
54+
55+
// Most forwarding instructions can forward owned and guaranteed values.
56+
// Therefore define a default implementation of `true`.
57+
public var canForwardGuaranteedValues: Bool { true }
58+
59+
// Most forwarding instructions can forward owned and guaranteed values.
60+
// Therefore define a default implementation of `true`.
61+
public var canForwardOwnedValues: Bool { true }
5062
}
5163

5264
// An instruction that forwards a single value to a single result.
@@ -242,12 +254,15 @@ extension MarkUninitializedInst {
242254

243255
extension StructExtractInst {
244256
public var preservesRepresentation: Bool { true }
257+
public var canForwardOwnedValues: Bool { false }
245258
}
246259

247260
extension TupleExtractInst {
248261
public var preservesRepresentation: Bool { true }
262+
public var canForwardOwnedValues: Bool { false }
249263
}
250264

251265
extension TuplePackExtractInst {
252266
public var preservesRepresentation: Bool { true }
267+
public var canForwardOwnedValues: Bool { false }
253268
}

SwiftCompilerSources/Sources/SIL/Operand.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public struct Operand : CustomStringConvertible, NoReflectionChildren {
4242
public var isTypeDependent: Bool { bridged.isTypeDependent() }
4343

4444
public var endsLifetime: Bool { bridged.isLifetimeEnding() }
45-
45+
46+
public func canAccept(ownership: Ownership) -> Bool { bridged.canAcceptOwnership(ownership._bridged) }
47+
4648
public var description: String { "operand #\(index) of \(instruction)" }
4749
}
4850

include/swift/SIL/SILBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ struct BridgedOperand {
215215

216216
BRIDGED_INLINE bool isTypeDependent() const;
217217
BRIDGED_INLINE bool isLifetimeEnding() const;
218+
BRIDGED_INLINE bool canAcceptOwnership(BridgedValue::Ownership ownership) const;
218219
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedOperand getNextUse() const;
219220
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedValue getValue() const;
220221
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedInstruction getUser() const;
@@ -537,6 +538,7 @@ struct BridgedInstruction {
537538
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedOperand ForwardingInst_singleForwardedOperand() const;
538539
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedOperandArray ForwardingInst_forwardedOperands() const;
539540
BRIDGED_INLINE BridgedValue::Ownership ForwardingInst_forwardingOwnership() const;
541+
BRIDGED_INLINE void ForwardingInst_setForwardingOwnership(BridgedValue::Ownership ownership) const;
540542
BRIDGED_INLINE bool ForwardingInst_preservesOwnership() const;
541543

542544
// =========================================================================//

include/swift/SIL/SILBridgingImpl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ bool BridgedOperand::isTypeDependent() const { return op->isTypeDependent(); }
269269

270270
bool BridgedOperand::isLifetimeEnding() const { return op->isLifetimeEnding(); }
271271

272+
bool BridgedOperand::canAcceptOwnership(BridgedValue::Ownership ownership) const {
273+
return op->canAcceptKind(BridgedValue::castToOwnership(ownership));
274+
}
275+
272276
OptionalBridgedOperand BridgedOperand::getNextUse() const {
273277
return {op->getNextUse()};
274278
}
@@ -683,6 +687,11 @@ BridgedValue::Ownership BridgedInstruction::ForwardingInst_forwardingOwnership()
683687
return castOwnership(forwardingInst->getForwardingOwnershipKind());
684688
}
685689

690+
void BridgedInstruction::ForwardingInst_setForwardingOwnership(BridgedValue::Ownership ownership) const {
691+
auto *forwardingInst = swift::ForwardingInstruction::get(unbridged());
692+
return forwardingInst->setForwardingOwnershipKind(BridgedValue::castToOwnership(ownership));
693+
}
694+
686695
bool BridgedInstruction::ForwardingInst_preservesOwnership() const {
687696
return swift::ForwardingInstruction::get(unbridged())->preservesOwnership();
688697
}

0 commit comments

Comments
 (0)