Skip to content

Commit 6ddc858

Browse files
committed
[ownership] Treat mark_dependence as forwarding in its first parameter.
Previously, we only did this for the first parameter if it was converting from escape to no-escape. We want to /always/ do this.
1 parent b9eb17c commit 6ddc858

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6192,8 +6192,12 @@ class UncheckedOwnershipConversionInst
61926192
}
61936193
};
61946194

6195-
/// MarkDependenceInst - Marks that one value depends on another for
6196-
/// validity in a non-obvious way.
6195+
/// Indicates that the validity of the first operand ("the value") depends on
6196+
/// the value of the second operand ("the base"). Operations that would destroy
6197+
/// the base must not be moved before any instructions which depend on the
6198+
/// result of this instruction, exactly as if the address had been obviously
6199+
/// derived from that operand (e.g. using ``ref_element_addr``). The result is
6200+
/// always equal to the first operand.
61976201
class MarkDependenceInst
61986202
: public InstructionBase<SILInstructionKind::MarkDependenceInst,
61996203
SingleValueInstruction> {
@@ -6209,6 +6213,7 @@ class MarkDependenceInst
62096213
enum { Value, Base };
62106214

62116215
SILValue getValue() const { return Operands[Value].get(); }
6216+
62126217
SILValue getBase() const { return Operands[Base].get(); }
62136218

62146219
void setValue(SILValue newVal) {

lib/SIL/OperandOwnership.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -859,19 +859,19 @@ OperandOwnershipKindClassifier::visitCopyBlockWithoutEscapingInst(
859859

860860
OperandOwnershipKindMap OperandOwnershipKindClassifier::visitMarkDependenceInst(
861861
MarkDependenceInst *mdi) {
862+
// If we are analyzing "the value", we forward ownership.
863+
if (getValue() == mdi->getValue()) {
864+
auto kind = getValue().getOwnershipKind();
865+
if (kind == ValueOwnershipKind::Any)
866+
return Map::allLive();
867+
auto lifetimeConstraint = kind.getForwardingLifetimeConstraint();
868+
return Map::compatibilityMap(kind, lifetimeConstraint);
869+
}
862870

863-
// Forward ownership if the mark_dependence instruction marks a dependence
864-
// on a @noescape function type for an escaping function type.
865-
if (getValue() == mdi->getValue())
866-
if (auto resFnTy = mdi->getType().getAs<SILFunctionType>())
867-
if (auto baseFnTy = mdi->getBase()->getType().getAs<SILFunctionType>())
868-
if (!resFnTy->isNoEscape() && baseFnTy->isNoEscape())
869-
return Map::compatibilityMap(
870-
ValueOwnershipKind::Owned,
871-
UseLifetimeConstraint::MustBeInvalidated);
872-
873-
// We always treat mark dependence as a use that keeps a value alive. We will
874-
// be introducing a begin_dependence/end_dependence version of this later.
871+
// If we are not the "value" of the mark_dependence, then we must be the
872+
// "base". This means that any use that would destroy "value" can not be moved
873+
// before any uses of "base". We treat this as non-consuming and rely on the
874+
// rest of the optimizer to respect the movement restrictions.
875875
return Map::allLive();
876876
}
877877

test/SIL/ownership-verifier/use_verifier.sil

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,11 @@ bb0:
11271127
return %9999 : $()
11281128
}
11291129

1130+
sil [ossa] @mark_dependence_test_2 : $@convention(thin) (@owned Builtin.NativeObject, @owned Builtin.NativeObject) -> () {
1131+
bb0(%0 : @owned $Builtin.NativeObject, %1 : @owned $Builtin.NativeObject):
1132+
%2 = mark_dependence %0 : $Builtin.NativeObject on %1 : $Builtin.NativeObject
1133+
destroy_value %1 : $Builtin.NativeObject
1134+
destroy_value %2 : $Builtin.NativeObject
1135+
%9999 = tuple()
1136+
return %9999 : $()
1137+
}

0 commit comments

Comments
 (0)