Skip to content

[ownership] Try harder to make sure we do not propagate ownership info when ownership is disabled. #34693

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/SIL/IR/OperandOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,19 @@ OwnershipConstraintClassifier::visitBuiltinInst(BuiltinInst *bi) {
Optional<OwnershipConstraint> Operand::getOwnershipConstraint() const {
if (isTypeDependent())
return None;

// If we do not have ownership enabled, just return any. This ensures that we
// do not have any consuming uses and everything from an ownership perspective
// is just a liveness use short-circuiting many of the optimizations.
//
// We do not ever call this function when an instruction isn't in a block.
assert(getUser()->getParent() &&
"Can not lookup ownership constraint unless inserted into block");
if (auto *block = getUser()->getParent())
if (auto *func = block->getParent())
if (!func->hasOwnership())
return {{OwnershipKind::Any, UseLifetimeConstraint::NonLifetimeEnding}};

OwnershipConstraintClassifier classifier(getUser()->getModule(), *this);
return classifier.visit(const_cast<SILInstruction *>(getUser()));
}
2 changes: 2 additions & 0 deletions lib/SIL/IR/SILUndef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using namespace swift;

static ValueOwnershipKind getOwnershipKindForUndef(SILType type, const SILFunction &f) {
if (!f.hasOwnership())
return OwnershipKind::None;
if (type.isAddress() || type.isTrivial(f))
return OwnershipKind::None;
return OwnershipKind::Owned;
Expand Down
13 changes: 13 additions & 0 deletions lib/SIL/IR/ValueOwnership.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,19 @@ ValueOwnershipKindClassifier::visitBuiltinInst(BuiltinInst *BI) {
//===----------------------------------------------------------------------===//

ValueOwnershipKind SILValue::getOwnershipKind() const {
// If we do not have an undef, we should always be able to get to our function
// here. If we do not have ownership enabled, just return none for everything
// to short circuit ownership optimizations. If we have an undef we may still
// get some results that are slightly wonky but hopefully when we lower
// ownership we remove that.
//
// We assume that any time we are in SILBuilder and call this without having a
// value in a block yet, ossa is enabled.
if (auto *block = Value->getParentBlock())
if (auto *f = block->getParent())
if (!f->hasOwnership())
return OwnershipKind::None;

ValueOwnershipKindClassifier Classifier;
auto result = Classifier.visit(const_cast<ValueBase *>(Value));
assert(result && "Returned ownership kind invalid on values");
Expand Down
29 changes: 27 additions & 2 deletions lib/SIL/Verifier/SILVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,8 +1022,11 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
assert(F && "Expected value base with parent function");
// If we do not have qualified ownership, then do not verify value base
// ownership.
if (!F->hasOwnership())
if (!F->hasOwnership()) {
require(SILValue(V).getOwnershipKind() == OwnershipKind::None,
"Once ownership is gone, all values should have none ownership");
return;
}
SILValue(V).verifyOwnership(&DEBlocks);
}

Expand Down Expand Up @@ -1100,14 +1103,36 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
"instruction's operand's owner isn't the instruction");
require(isInValueUses(&operand), "operand value isn't used by operand");

if (I->isTypeDependentOperand(operand)) {
if (operand.isTypeDependent()) {
require(isa<SILInstruction>(I),
"opened archetype operand should refer to a SILInstruction");
}

// Make sure that if operand is generic that its primary archetypes match
// the function context.
checkLegalType(I->getFunction(), operand.get(), I);

// If we are not in OSSA, our operand constraint should be invalid for a
// type dependent operand (that is Optional::None) and if we have a non
// type dependent operand then we should have a constraint of
// OwnershipKind::Any, UseLifetimeConstraint::NonLifetimeEnding.
if (!I->getFunction()->hasOwnership()) {
if (operand.isTypeDependent()) {
require(
!operand.getOwnershipConstraint(),
"Non Optional::None constraint for a type dependent operand?!");
} else {
auto constraint = operand.getOwnershipConstraint();
require(constraint.hasValue(),
"All non-type dependent operands must have a "
"non-Optional::None constraint?!");
require(constraint->getPreferredKind() == OwnershipKind::Any &&
constraint->getLifetimeConstraint() ==
UseLifetimeConstraint::NonLifetimeEnding,
"In non-ossa all non-type dependent operands must have a "
"constraint of Any, NonLifetimeEnding");
}
}
}

// TODO: There should be a use of an opened archetype inside the instruction for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ bb0(%0 : $@differentiable @callee_guaranteed (Float) -> Float):
// CHECK: bb0([[ARG:%.*]] : $@differentiable @callee_guaranteed (Float) -> Float):
// CHECK: [[ORIG_FN:%.*]] = differentiable_function_extract [original] [[ARG]]
// CHECK: [[JVP_FN:%.*]] = differentiable_function_extract [jvp] [[ARG]]
// CHECK: strong_retain [[JVP_FN]]
// CHECK: [[VJP_FN:%.*]] = differentiable_function_extract [vjp] [[ARG]]
// CHECK: strong_retain [[VJP_FN]]
Comment on lines -48 to -50

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these retains are actually necessary, because I'm seeing ASAN failures happening starting at this commit (https://bugs.swift.org/browse/SR-13973).

This commit stops emitting these changes because it makes the .getOwnershipKind() check at https://github.com/apple/swift/blob/d3d1f8d22d07233ae5714d810c26aff57066b453/lib/SILOptimizer/Mandatory/Differentiation.cpp#L503 & https://github.com/apple/swift/blob/d3d1f8d22d07233ae5714d810c26aff57066b453/lib/SILOptimizer/Mandatory/Differentiation.cpp#L1216 return None.

I don't really understand any of this, so I don't know what would be a good way to fix it. Is there some other check that we can do in Differentiation.cpp that does the right thing?

I did try completely deleting the check (so that Differentiation.cpp always emits the copy value operation), but that caused a lot of "Error! Found a leaked owned value that was never consumed." So I guess there are some cases where we do need a copy value and some cases where we don't need it, and I don't know the right way to distinguish between them :)

(There are also some other calls to getOwnershipKind() in Differentiation.cpp that might need to be updated, though I don't yet have any concrete examples of things that they break.)

// CHECK: differentiable_function [parameters 0] [results 0] [[ORIG_FN]] : {{.*}} with_derivative {[[JVP_FN]] : {{.*}}, [[VJP_FN]] : {{.*}}}
// CHECK: }

Expand Down Expand Up @@ -134,8 +132,6 @@ bb0(%0 : $Class):
// CHECK-LABEL: sil @test_class_method_partial_apply
// CHECK: bb0([[ARG:%.*]] : $Class):
// CHECK: [[ORIG_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method
// CHECK: strong_retain [[ARG]]
// CHECK: strong_retain [[ARG]]
// CHECK: [[ORIG_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[ORIG_FN]]([[ARG]])
// CHECK: [[JVP_FN:%.*]] = class_method [[ARG]] : $Class, #Class.method!jvp.SU
// CHECK: [[JVP_FN_PARTIALLY_APPLIED:%.*]] = partial_apply [callee_guaranteed] [[JVP_FN]]([[ARG]])
Expand Down
7 changes: 4 additions & 3 deletions test/SIL/Parser/basic.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1591,11 +1591,12 @@ bb0(%0 : $Builtin.NativeObject):
return undef : $()
}

// CHECK-LABEL: sil @test_unchecked_ownership_conversion : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
// CHECK-LABEL: sil [ossa] @test_unchecked_ownership_conversion : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
// CHECK: unchecked_ownership_conversion {{%.*}} : $Builtin.NativeObject, @guaranteed to @owned
sil @test_unchecked_ownership_conversion : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : $Builtin.NativeObject):
sil [ossa] @test_unchecked_ownership_conversion : $@convention(thin) (@guaranteed Builtin.NativeObject) -> () {
bb0(%0 : @guaranteed $Builtin.NativeObject):
%1 = unchecked_ownership_conversion %0 : $Builtin.NativeObject, @guaranteed to @owned
end_lifetime %1 : $Builtin.NativeObject
return undef : $()
}

Expand Down