Skip to content

[AutoDiff] fix ownership instructions (SR-13973) #35196

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

Closed
wants to merge 3 commits into from

Conversation

marcrasi
Copy link

I don't really understand what I am doing, but my reasoning is: #34693 made getOwnershipKind() start returning None when the function doesn't have ownership, and this caused Differentiation to insert fewer copy value operations, which caused https://bugs.swift.org/browse/SR-13973. So modify the condition to insert copy value operations when the function doesn't have ownership.

Only the changes on L503 and L1217 are covered by the new test, but I changed the other similar-looking conditions too, because it seems like they probably need the same change.

@marcrasi marcrasi requested a review from dan-zheng December 22, 2020 20:53
@marcrasi
Copy link
Author

@swift-ci please test

@marcrasi marcrasi requested a review from gottesmm December 22, 2020 22:11

struct SR13973 {
let x: Float = 0

Copy link
Contributor

Choose a reason for hiding this comment

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

Can I have a SIL test?

Copy link
Contributor

Choose a reason for hiding this comment

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

For changes like this, both are important!

Copy link
Author

Choose a reason for hiding this comment

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

I added a FileCheck to test/AutoDiff/SILOptimizer/differentiation_function_canonicalization.sil. It's pretty much the same thing that I do in this file, except in SIL.

@dan-zheng dan-zheng requested a review from rxwei December 22, 2020 22:56
Copy link
Contributor

@dan-zheng dan-zheng left a comment

Choose a reason for hiding this comment

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

If this change fixes the crash, it nominally LGTM!

I'd like to understand more deeply #34693 and the underlying reasons for the crash and why the runtime ASAN error wasn't caught by SIL verification.

@marcrasi marcrasi force-pushed the marcrasi-fix-sr13973 branch from 1640d0a to 38b5112 Compare December 22, 2020 23:23
// CHECK: [[CURRIED_JVP:%.*]] = partial_apply [callee_guaranteed] [[METHOD_JVP]]([[SELF]]) : $@convention(thin) (Float, S) -> (Float, @owned @callee_guaranteed (Float) -> Float)
// CHECK: [[METHOD_VJP:%.*]] = differentiability_witness_function [vjp] [parameters 0] [results 0] @method_thin : $@convention(thin) (Float, S) -> Float
// CHECK: [[CURRIED_VJP:%.*]] = partial_apply [callee_guaranteed] [[METHOD_VJP]]([[SELF]]) : $@convention(thin) (Float, S) -> (Float, @owned @callee_guaranteed (Float) -> Float)
// CHECK: strong_retain [[CURRIED]] : $@callee_guaranteed (Float) -> Float
Copy link
Author

Choose a reason for hiding this comment

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

This strong_retain gets added by this PR.


struct SR13973 {
let x: Float = 0

Copy link
Author

Choose a reason for hiding this comment

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

I added a FileCheck to test/AutoDiff/SILOptimizer/differentiation_function_canonicalization.sil. It's pretty much the same thing that I do in this file, except in SIL.

Copy link
Contributor

@rxwei rxwei left a comment

Choose a reason for hiding this comment

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

Would a more principled fix be enabling OSSA for AD curry and index subset thunks?

return %5 : $()
}

// CHECK-LABEL: sil {{.*}} @AD__method_curried__differentiable_curry_thunk_src_0_wrt_0
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a ':' at the end of this to prevent prefix double matches?

Can you also add an } // end sil function '$NAME' to the end.

This is just basic SIL test hygiene.

Copy link
Author

Choose a reason for hiding this comment

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

Done


// CHECK-LABEL: sil {{.*}} @AD__method_curried__differentiable_curry_thunk_src_0_wrt_0
// CHECK: bb0([[SELF:%.*]] : $S):
// CHECK: [[METHOD:%.*]] = function_ref @method_thin : $@convention(thin) (Float, S) -> Float
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add an ossa test? Doesn't this transform only run on OSSA? I guess not?

Copy link
Author

Choose a reason for hiding this comment

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

Does that mean add a test where the sil functions say [ossa]? Done.

I guess this transform doesn't only run on OSSA...

@marcrasi marcrasi closed this Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants