[AutoDiff] Fix adjoints for loop-local active values
Fixes #78264
Tagging @asl @JaapWijnen @rxwei
@swift-ci please test
@swift-ci please test
Would be glad to see feedback from everyone interested
@swift-ci please test
Would be glad to see feedback from everyone interested
Would be glad to see feedback from everyone interested
Would be glad to see feedback from everyone interested
Would be glad to see feedback from everyone interested
@kovdan01 I cherrypicked this change onto swift-DEVELOPMENT-SNAPSHOT-2025-02-20-a and built a toolchain. I used this toolchain to build all our internal swift projects, and everything builds successfully on macOS.
The change looks ok to me. The explanation of the problem is at https://github.com/swiftlang/swift/issues/78264
Long story short: SSA values in the loop body should be considered as "new values" at each loop iteration. We used to reuse the adjoints for them before this patch (though we should create distinct adjoints on each loop iteration).
This used to work before due to a particular CFG generated for for-style loops. As a result we've been lucky and simply zeroed the adjoints in the loop header on each iteration (loop header BB was the first BB we encountered the adjoint, so it was initialized there and this zero-initialization happened on each loop iteration effectively producing new adjoint value).
However, this stopped to work for while-style loops as the CFG is different and while we produced adjoint initialization, it happened outside the loop and therefore the adjoint values got reused from the previous loop iteration.
@rxwei will you please take a look as a second pair of eyes? :)
However, this stopped to work for while-style loops
To be a bit more precise: it not stopped working, it just never worked for repeat-while loops (if not never, at least, for several latest years). For regular while loops, we had the same situation that we observed with for loops, when the behavior looked correct because of a particular CFG.
@kovdan01 I cherrypicked this change onto
swift-DEVELOPMENT-SNAPSHOT-2025-02-20-aand built a toolchain. I used this toolchain to build all our internal swift projects, and everything builds successfully on macOS.
@clackary Great, thanks for letting me know! BTW, does "everything builds successfully on macOS" imply that your internal tests are also passing?
your internal tests are also passing?
@kovdan01 No, just the compile-time checks for now. Further testing is more of a manual process at the moment, but I can get to testing a few projects this week.
@rxwei Could you please take a look at this PR? Thanks!
@kovdan01 I was able to finish testing a bunch of our internal projects. The issue we discussed earlier was caused by a flaw in my invocation, so nothing to worry about there. This looks good from my end.
@rxwei Could you please take a look at this PR? Thanks!
@swift-ci please smoke test
@swift-ci please test
@swift-ci please test
@swift-ci please smoke test