Skip to content

Add forward-mode AD support for std::thread. - #1891

Open
a-elrawy wants to merge 2 commits into
vgvassilev:masterfrom
a-elrawy:gsoc/concurrency-thread-forward
Open

Add forward-mode AD support for std::thread.#1891
a-elrawy wants to merge 2 commits into
vgvassilev:masterfrom
a-elrawy:gsoc/concurrency-thread-forward

Conversation

@a-elrawy

@a-elrawy a-elrawy commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Adds forward-mode custom derivatives for std::thread (constructors, join, joinable, detach) and extends ForwardMode/Concurrency.C. The worker runs the callable’s pushforward so mutations through std::ref update derivatives (free functions and named functors; lambdas not yet supported). Also generalizes isCopyable and uses std::move for non-copyable custom constructor pushforward returns so differentiated code compiles.

@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch 2 times, most recently from 0bf129e to 9d1e866 Compare July 14, 2026 17:52

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/STLBuiltins.h
@codecov

codecov Bot commented Jul 14, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.92857% with 9 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lib/Differentiator/BaseForwardModeVisitor.cpp 82.69% 9 Missing ⚠️

📢 Thoughts on this report? Let us know!

@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch 2 times, most recently from 90a4959 to 3b26d88 Compare July 15, 2026 14:32
Add custom pushforwards in STLBuiltins.h and forward-mode tests.
@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch from 3b26d88 to 29be48d Compare July 21, 2026 23:25

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Comment thread lib/Differentiator/CladUtils.cpp
@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch from 29be48d to 2a51726 Compare July 21, 2026 23:37
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

@a-elrawy
a-elrawy marked this pull request as ready for review July 22, 2026 02:57
Copilot AI review requested due to automatic review settings July 22, 2026 02:57

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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::thread construction and basic member ops (join, joinable) in STLBuiltins.h.
  • Generalize copyability detection (isCopyable) to treat deleted/inaccessible copy ctors as non-copyable.
  • Update forward-mode constructor handling to std::move non-copyable value/pushforward members out of custom-derivative constructor returns.
  • Add/extend ForwardMode/Concurrency.C tests covering reference_wrapper and std::thread usage.

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.

Comment thread include/clad/Differentiator/STLBuiltins.h
Comment thread include/clad/Differentiator/STLBuiltins.h Outdated
Comment thread include/clad/Differentiator/STLBuiltins.h Outdated
Comment thread test/ForwardMode/Concurrency.C Outdated
@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch from 2a51726 to 1973f4e Compare July 22, 2026 13:28
@github-actions

Copy link
Copy Markdown
Contributor

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() &&

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Why do we have to special case here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch 2 times, most recently from 6207920 to 4b781ab Compare July 25, 2026 20:30

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

clang-tidy made some suggestions

Comment thread include/clad/Differentiator/STLBuiltins.h Outdated
Comment thread include/clad/Differentiator/STLBuiltins.h Outdated
Comment thread include/clad/Differentiator/STLBuiltins.h Outdated
Comment thread lib/Differentiator/BaseForwardModeVisitor.cpp
Generalize isCopyable to detect deleted copy constructors so non-copyable
return values from custom constructor pushforwards use std::move.
@a-elrawy
a-elrawy force-pushed the gsoc/concurrency-thread-forward branch from 4b781ab to 26285b3 Compare July 25, 2026 20:41
@github-actions

Copy link
Copy Markdown
Contributor

clang-tidy review says "All clean, LGTM! 👍"

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.

3 participants