← Back to Pull requests

Generate adjoints for pointer-typed conditional operators #2210

Open
Vedant2005goyal opened this pull request with 88 lines diff across 3 files
View on GitHub
Vedant2005goyal Vedant2005goyal commented on Sep 27, 2026 13:10 UTC
Author

Currently, Clad only generated adjoints for conditional operators that were modifiable lvalues. For pointer-typed conditionals, the adjoint pointer was left uninitialized.

Example:

 double* p = cond ? new double[n] : nullptr;  // _d_p was left null.

This led to a crash in the reverse sweep. This patch generates adjoints for pointer-typed conditionals.

Diff: 88 lines (+79 / -9) across 3 files Reviewer: vgvassilev Review duration: 13682m
All Vouch review confidence checks passed
Residual risk 0.04 = Change Risk (0.38) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.38 · 88 lines diff across 3 files
0.38
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 5 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
89%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.269
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
7 items
github-actions[bot] github-actions[bot] commented on Sep 27, 2026 13:13 UTC
Bot

clang-tidy review says "All clean, LGTM! :+1:"

github-actions[bot] github-actions[bot] commented on Sep 27, 2026 19:07 UTC
Bot

clang-tidy review says "All clean, LGTM! :+1:"

codecov[bot] codecov[bot] commented on Sep 27, 2026 19:20 UTC
Bot

Codecov Report


:white_check_mark: All modified and coverable lines are covered by tests.

:loudspeaker: Thoughts on this report? Let us know!

vgvassilev vgvassilev commented on code on Sep 28, 2026 17:19 UTC
Code Review
lib/Differentiator/ReverseModeVisitor.cpp:1
@@ -1077,15 +1077,16 @@ Expr* ReverseModeVisitor::getStdInitListSizeExpr(const Expr* E) {
             .get();
     // If result is a glvalue, we should keep it as it can potentially be
     // assigned as in (c ? a : b) = x;
+    bool isPtr = CO->getType()->isPointerType();
     Expr* ResultRef = nullptr;
-    if ((CO->isModifiableLvalue(m_Context) == Expr::MLV_Valid) &&
+    if ((isPtr || CO->isModifiableLvalue(m_Context) == Expr::MLV_Valid) &&

This lifts a refusal that was deliberate, and nothing shows the lifted case now gives the right answer.

The case VariedAdjointInitializer.C used to pin -- double* r = c ? &p : &q -- has pointer type, so it takes this branch now and is no longer refused. I checked on master that it is refused today, and that the lambda you replaced it with still produces both the warning and the error. But the comment in that test says refusing was chosen over answering with a wrong gradient, so moving the test to a different construct leaves the conditional case untested in either direction.

That function is easy to assert on: f(x) = xx through **r, so the gradient at 3 is 6. If it comes out right, it belongs in Pointers.C next to condNewPointer; if it does not, the refusal should stay for pointer-to-pointer.

Relatedly, dropping the modifiable-lvalue re-check for pointers keeps a ResultRef that is a prvalue. That is fine where the adjoint is dereferenced, as in your test, but the comment just above says this exists for (c ? a : b) = x, and a prvalue cannot be assigned to.

vgvassilev vgvassilev commented on Sep 28, 2026 17:21 UTC
Reviewer

One remark inline.

This review came from ai-vgvassilev.

github-actions[bot] github-actions[bot] commented on Sep 30, 2026 11:31 UTC
Bot

clang-tidy review says "All clean, LGTM! :+1:"

Reviewers
Assignees
Vedant2005goyal Vedant2005goyal
Labels
risk: low confidence: 89.8% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 37.8% (MEDIUM) · Higher = larger blast radius
37.8%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 89.8% · Lower = rushed or shallow review
89.8%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 3.8% · Safe to merge
3.8%