Skip to content

[AutoDiff] Fix several issues related to captured arguments #41401

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 1 commit into from
Feb 17, 2022

Conversation

asl
Copy link
Contributor

@asl asl commented Feb 16, 2022

  • Introduce a workaround while dealing with getLoweredParameterIndices() results:
    it operates on AST and therefore does not take into account captured arguments
    which are "on side" on AST and explicit in SIL
  • Ensure captured arguments (@inout_aliased) are handled in a same way as ordinary
    @inout arguments while generating the pullback type

Resolves SR-15205

@asl
Copy link
Contributor Author

asl commented Feb 16, 2022

Tagging @dan-zheng @BradLarson

Note that the first fix is essentially a workaround mentioned in https://bugs.swift.org/browse/SR-15205. Much better fix would be teaching getLoweredParameterIndices() to take captured arguments into account and perform additional cleanups as necessary. There is already TODO in the code for this.

@compnerd
Copy link
Member

CC: @rxwei

@compnerd
Copy link
Member

@swift-ci please test

@rxwei rxwei self-requested a review February 16, 2022 16:34
Comment on lines +455 to +462

if (silParameterIndices->getCapacity() < parameterIndices->getCapacity()) {
assert(original->getCaptureInfo().hasLocalCaptures());
silParameterIndices =
silParameterIndices->extendingCapacity(original->getASTContext(),
parameterIndices->getCapacity());
}

Copy link
Contributor

Choose a reason for hiding this comment

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

How about adding a numCaptures parameter to autodiff::getLoweredParameterIndices and make sure we allocate the right capacity in the first place?

Copy link
Contributor Author

@asl asl Feb 16, 2022

Choose a reason for hiding this comment

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

There are few workarounds of similar kind sprinkled over the code, so we'd need to eliminate them as well (see https://github.com/apple/swift/blob/main/lib/SILOptimizer/Mandatory/Differentiation.cpp#L518 as an example). Just setting numCaptures seems not work in general as we need to know how the captured arguments will be lowered.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah it's not a perfect solution. Since captured arguments are always at the end of a SIL function, we could get the number of captures from CaptureInfo. Tuple captures won't be splat in SIL arguments, so we might be able to assume the number of captures in CaptureInfo is the number of captures in the SIL function, but I'm not sure entirely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Me too :) I'm planning to resolve the TODO afterwards, after checking all possibilities and fixing getLoweredParameterIndices properly. There is a tentative patch for this, but it needs more testing and checking.

Copy link
Contributor

Choose a reason for hiding this comment

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

LGTM as is since similar workarounds already exist, but it would be really nice to fix getLoweredParameterIndices. Let me know if you want to take a stab at that or rather merge this first.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok sounds good.

  - Introduce a workaround while dealing with getLoweredParameterIndices() results:
    it operates on AST and therefore does not take into account captured arguments
    which are "on side" on AST and explicit in SIL
  - Ensure captured arguments (@inout_aliased) are handled in a same way as ordinary
    @inout arguments while generating a pullback type

Fixes SR-15205
@asl asl requested a review from rxwei February 16, 2022 21:04
@rxwei
Copy link
Contributor

rxwei commented Feb 16, 2022

@swift-ci please test

@philipturner
Copy link
Contributor

Any chance this will resolve SR-15793?

@rxwei
Copy link
Contributor

rxwei commented Feb 17, 2022

No. Note this is fixing a compiler crash, not a runtime result.

@rxwei
Copy link
Contributor

rxwei commented Feb 17, 2022

Thank you @asl !

@rxwei rxwei merged commit b0a038b into swiftlang:main Feb 17, 2022
@philipturner
Copy link
Contributor

No. Note this is fixing a compiler crash, not a runtime result.

I thought these were related because my bug is caused by the parameter index being off by 1. This PR fixes a bug with parameter indices. Still, this should be a good starting point for launching an investigation into SR-15793.

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