Skip to content

Commit acfaa91

Browse files
authored
Merge pull request swiftlang#27903 from gottesmm/pr-db9fd274761ac09be56a104f6eec8792448e7544
[ownership] Update the ownership utils infrastructure to treat mark_dependence as forwarding.
2 parents 0161f43 + 179cddc commit acfaa91

File tree

5 files changed

+38
-11
lines changed

5 files changed

+38
-11
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -842,11 +842,11 @@ inline SingleValueInstruction *SILNode::castToSingleValueInstruction() {
842842
inst->getKind() <= SILInstructionKind::Last_##ID; \
843843
}
844844

845-
/// A single value inst that also forwards either owned or guaranteed ownership.
845+
/// A single value inst that forwards a static ownership from one (or all) of
846+
/// its operands.
846847
///
847-
/// The specific forwarded ownership is static since it is set upon
848-
/// construction. After that point the instruction can not have a different form
849-
/// of ownership.
848+
/// The ownership kind is set on construction and afterwards must be changed
849+
/// explicitly using setOwnershipKind().
850850
class OwnershipForwardingSingleValueInst : public SingleValueInstruction {
851851
ValueOwnershipKind ownershipKind;
852852

@@ -5457,8 +5457,7 @@ class SelectEnumInstBase
54575457
NullablePtr<EnumElementDecl> getSingleTrueElement() const;
54585458
};
54595459

5460-
/// A select enum inst that produces a static OwnershipKind set upon the
5461-
/// instruction's construction.
5460+
/// A select enum inst that produces a static OwnershipKind.
54625461
class OwnershipForwardingSelectEnumInstBase : public SelectEnumInstBase {
54635462
ValueOwnershipKind ownershipKind;
54645463

@@ -6409,16 +6408,34 @@ class UncheckedOwnershipConversionInst
64096408
/// the base must not be moved before any instructions which depend on the
64106409
/// result of this instruction, exactly as if the address had been obviously
64116410
/// derived from that operand (e.g. using ``ref_element_addr``). The result is
6412-
/// always equal to the first operand.
6411+
/// always equal to the first operand and thus forwards ownership through the
6412+
/// first operand. This is a "regular" use of the second operand (i.e. the
6413+
/// second operand must be live at the use point).
6414+
///
6415+
/// Example:
6416+
///
6417+
/// %base = ...
6418+
/// %value = ... @trivial value ...
6419+
/// %value_dependent_on_base = mark_dependence %value on %base
6420+
/// ...
6421+
/// use(%value_dependent_on_base) (1)
6422+
/// ...
6423+
/// destroy_value %base (2)
6424+
///
6425+
/// (2) can never move before (1). In English this is a way for the compiler
6426+
/// writer to say to the optimizer: 'This subset of uses of "value" (the uses of
6427+
/// result) have a dependence on "base" being alive. Do not allow for things
6428+
/// that /may/ destroy base to be moved earlier than any of these uses of
6429+
/// "value"'.
64136430
class MarkDependenceInst
64146431
: public InstructionBase<SILInstructionKind::MarkDependenceInst,
6415-
SingleValueInstruction> {
6432+
OwnershipForwardingSingleValueInst> {
64166433
friend SILBuilder;
64176434

64186435
FixedOperandList<2> Operands;
64196436

64206437
MarkDependenceInst(SILDebugLocation DebugLoc, SILValue value, SILValue base)
6421-
: InstructionBase(DebugLoc, value->getType()),
6438+
: InstructionBase(DebugLoc, value->getType(), value.getOwnershipKind()),
64226439
Operands{this, value, base} {}
64236440

64246441
public:

lib/SIL/OperandOwnership.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ OperandOwnershipKindMap OperandOwnershipKindClassifier::visitMarkDependenceInst(
854854
MarkDependenceInst *mdi) {
855855
// If we are analyzing "the value", we forward ownership.
856856
if (getValue() == mdi->getValue()) {
857-
auto kind = getValue().getOwnershipKind();
857+
auto kind = mdi->getOwnershipKind();
858858
if (kind == ValueOwnershipKind::None)
859859
return Map::allLive();
860860
auto lifetimeConstraint = kind.getForwardingLifetimeConstraint();

lib/SIL/OwnershipUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ bool swift::isOwnershipForwardingValueKind(SILNodeKind kind) {
4444
case SILNodeKind::CondBranchInst:
4545
case SILNodeKind::DestructureStructInst:
4646
case SILNodeKind::DestructureTupleInst:
47+
case SILNodeKind::MarkDependenceInst:
4748
return true;
4849
default:
4950
return false;

lib/SIL/ValueOwnership.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ ValueOwnershipKind ValueOwnershipKindClassifier::visitSILFunctionArgument(
293293
// This is a forwarding instruction through only one of its arguments.
294294
ValueOwnershipKind
295295
ValueOwnershipKindClassifier::visitMarkDependenceInst(MarkDependenceInst *MDI) {
296-
return MDI->getValue().getOwnershipKind();
296+
return MDI->getOwnershipKind();
297297
}
298298

299299
ValueOwnershipKind ValueOwnershipKindClassifier::visitApplyInst(ApplyInst *ai) {

test/SIL/ownership-verifier/use_verifier.sil

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,3 +1187,12 @@ bb0(%0 : $@sil_unmanaged Builtin.NativeObject):
11871187
%1 = strong_copy_unmanaged_value %0 : $@sil_unmanaged Builtin.NativeObject
11881188
return %1 : $Builtin.NativeObject
11891189
}
1190+
1191+
sil [ossa] @mark_dependence_test_guaranteed : $@convention(thin) (@guaranteed Builtin.NativeObject, @owned Builtin.NativeObject) -> () {
1192+
bb0(%0 : @guaranteed $Builtin.NativeObject, %1 : @owned $Builtin.NativeObject):
1193+
%2 = mark_dependence %0 : $Builtin.NativeObject on %1 : $Builtin.NativeObject
1194+
destroy_value %1 : $Builtin.NativeObject
1195+
%9999 = tuple()
1196+
return %9999 : $()
1197+
}
1198+

0 commit comments

Comments
 (0)