Skip to content
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

Store/restore reference args only if they are lvalue and non-const #1117

Merged
merged 1 commit into from
Oct 15, 2024

Conversation

PetroZarytskyi
Copy link
Collaborator

Currently, in the reverse mode, the argument is stored when the function parameter is of a reference type and the argument is not a temporary expression (e.g. 0 when bound to a const int& parameter). A better approach to this is checking if the parameter is an lvalue non-const reference. This lets us avoid unnecessary stores and errors:

double g(const double& t) {...}

double f (...) {
  double x = ...;
  const double y = ...;
  g(x);   // No need to store x since the function isn't able to modify it
  g(y);   // An attempt to restore y will lead to an error because it's a const
}

This PR fixes the issue in the ReverseModeVisitor and TBRAnalyzer.

…-const.

Currently, in the reverse mode, the argument is stored when the function parameter is of a reference type and the argument is not a temporary expression (e.g. ``0`` when bound to a ``const int&`` parameter). A better approach to this is checking if the parameter is an lvalue non-const reference. This lets us avoid unnecessary stores and errors:
```
double g(const double& t) {...}

double f (...) {
  double x = ...;
  const double y = ...;
  g(x);   // No need to store x since the function isn't able to modify it
  g(y);   // An attempt to restore y will lead to an error because it's a const
}
```
This commit fixes the issue in the ReverseModeVisitor and TBRAnalyzer.
Copy link
Contributor

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

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.24%. Comparing base (f86eede) to head (3cf9f46).
Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1117   +/-   ##
=======================================
  Coverage   94.24%   94.24%           
=======================================
  Files          48       48           
  Lines        8164     8169    +5     
=======================================
+ Hits         7694     7699    +5     
  Misses        470      470           
Files with missing lines Coverage Δ
lib/Differentiator/ReverseModeVisitor.cpp 95.47% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/TBRAnalyzer.cpp 96.16% <100.00%> (+0.03%) ⬆️
Files with missing lines Coverage Δ
lib/Differentiator/ReverseModeVisitor.cpp 95.47% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/TBRAnalyzer.cpp 96.16% <100.00%> (+0.03%) ⬆️

Copy link
Owner

@vgvassilev vgvassilev left a comment

Choose a reason for hiding this comment

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

LGTM!

@vgvassilev vgvassilev merged commit 332358e into vgvassilev:master Oct 15, 2024
90 checks passed
@PetroZarytskyi PetroZarytskyi deleted the ray-tracer branch October 15, 2024 17:49
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.

2 participants