Add forward-mode AD support for std::thread. - #1891
Conversation
0bf129e to
9d1e866
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
90a4959 to
3b26d88
Compare
Add custom pushforwards in STLBuiltins.h and forward-mode tests.
3b26d88 to
29be48d
Compare
29be48d to
2a51726
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
There was a problem hiding this comment.
Pull request overview
Adds forward-mode (pushforward) custom-derivative coverage needed for std::thread-based code paths to compile under Clad, and introduces/extends forward-mode tests around concurrency patterns using std::reference_wrapper and std::thread. Also tightens copyability detection and updates forward-mode constructor handling to move out non-copyable ValueAndPushforward results.
Changes:
- Add forward-mode custom derivatives for
std::threadconstruction and basic member ops (join,joinable) inSTLBuiltins.h. - Generalize copyability detection (
isCopyable) to treat deleted/inaccessible copy ctors as non-copyable. - Update forward-mode constructor handling to
std::movenon-copyablevalue/pushforwardmembers out of custom-derivative constructor returns. - Add/extend
ForwardMode/Concurrency.Ctests coveringreference_wrapperandstd::threadusage.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| test/ForwardMode/Concurrency.C | Adds forward-mode tests exercising reference_wrapper and std::thread constructs. |
| lib/Differentiator/CladUtils.cpp | Expands isCopyable to detect inaccessible/deleted copy constructors. |
| lib/Differentiator/BaseForwardModeVisitor.cpp | Moves non-copyable constructor pushforward results to avoid copy attempts. |
| include/clad/Differentiator/STLBuiltins.h | Introduces std::thread pushforwards and additional reference_wrapper pushforwards. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
2a51726 to
1973f4e
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
| if (!clonedArgs.empty() && !derivedArgs.empty()) { | ||
| QualType recTy = CE->getType().getNonReferenceType().getCanonicalType(); | ||
| if (const auto* RD = recTy->getAsCXXRecordDecl()) { | ||
| if (RD->getName() == "thread" && RD->isInStdNamespace() && |
There was a problem hiding this comment.
Why do we have to special case here?
There was a problem hiding this comment.
Constructing std::thread doesn’t call the callable, so VisitCallExpr never nest-diffs it. The callable’s tangent is just {}/nullptr, not F_pushforward, which the custom derivative needs to run AD through the worker. We can generalize this later if you prefer.
6207920 to
4b781ab
Compare
Generalize isCopyable to detect deleted copy constructors so non-copyable return values from custom constructor pushforwards use std::move.
4b781ab to
26285b3
Compare
|
clang-tidy review says "All clean, LGTM! 👍" |
Adds forward-mode custom derivatives for
std::thread(constructors, join, joinable, detach) and extendsForwardMode/Concurrency.C. The worker runs the callable’s pushforward so mutations throughstd::refupdate derivatives (free functions and named functors; lambdas not yet supported). Also generalizesisCopyableand usesstd::movefor non-copyable custom constructor pushforward returns so differentiated code compiles.