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

Add support for std::initializer_list in the reverse mode #1018

Merged
merged 6 commits into from
Aug 21, 2024

Conversation

PetroZarytskyi
Copy link
Collaborator

@PetroZarytskyi PetroZarytskyi commented Aug 1, 2024

This PR adds support for std::initializer_list in the reverse mode.
Also, this PR adds support for non-array type ranges and removes redundant assignments by simplifying the logic for range-based for loops. Apart from that, it adds support for std::begin/std::end, and introduces begin/end functions to clad::array.
Fixes #830.

@PetroZarytskyi PetroZarytskyi force-pushed the issue-830 branch 2 times, most recently from 075d682 to 31bd298 Compare August 1, 2024 15:21
Copy link
Contributor

@github-actions github-actions bot left a comment

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

lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
@PetroZarytskyi PetroZarytskyi force-pushed the issue-830 branch 2 times, most recently from 46fcab0 to f76201c Compare August 1, 2024 16:11
Copy link
Contributor

github-actions bot commented Aug 1, 2024

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

Copy link

codecov bot commented Aug 1, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.28%. Comparing base (b7f8e5d) to head (d93e52c).
Report is 6 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1018      +/-   ##
==========================================
+ Coverage   94.12%   94.28%   +0.16%     
==========================================
  Files          55       55              
  Lines        8266     8293      +27     
==========================================
+ Hits         7780     7819      +39     
+ Misses        486      474      -12     
Files Coverage Δ
include/clad/Differentiator/EstimationModel.h 100.00% <ø> (ø)
include/clad/Differentiator/ReverseModeVisitor.h 97.97% <ø> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/EstimationModel.cpp 100.00% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 97.80% <100.00%> (+0.02%) ⬆️
lib/Differentiator/VisitorBase.cpp 96.86% <100.00%> (+0.13%) ⬆️

... and 2 files with indirect coverage changes

Files Coverage Δ
include/clad/Differentiator/EstimationModel.h 100.00% <ø> (ø)
include/clad/Differentiator/ReverseModeVisitor.h 97.97% <ø> (ø)
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/EstimationModel.cpp 100.00% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 97.80% <100.00%> (+0.02%) ⬆️
lib/Differentiator/VisitorBase.cpp 96.86% <100.00%> (+0.13%) ⬆️

... and 2 files with indirect coverage changes

Copy link
Contributor

github-actions bot commented Aug 5, 2024

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

@PetroZarytskyi PetroZarytskyi force-pushed the issue-830 branch 2 times, most recently from b0ea09e to e9e6ed5 Compare August 7, 2024 23:24
Copy link
Contributor

@github-actions github-actions bot left a comment

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

lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

github-actions bot commented Aug 8, 2024

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

2 similar comments
Copy link
Contributor

github-actions bot commented Aug 9, 2024

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

Copy link
Contributor

github-actions bot commented Aug 9, 2024

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

Copy link
Contributor

github-actions bot commented Aug 9, 2024

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

@PetroZarytskyi PetroZarytskyi force-pushed the issue-830 branch 3 times, most recently from 1952e6b to 09402df Compare August 13, 2024 11:27
Copy link
Contributor

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

Copy link
Contributor

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

Copy link
Contributor

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

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.

Looks like we are heading in a good direction. See some comments inline.

lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Show resolved Hide resolved
lib/Differentiator/TBRAnalyzer.cpp Outdated Show resolved Hide resolved
Copy link
Contributor

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

lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
unsigned numInits = ILE->getNumInits();
VDDerivedInit = ConstantFolder::synthesizeLiteral(
m_Context.getSizeType(), m_Context, numInits);
VDCloneType = VDDerivedType;
Copy link
Owner

Choose a reason for hiding this comment

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

Why not assigning directly to VDCloneType rather to a temporary VDDerivedType.

Copy link
Collaborator Author

@PetroZarytskyi PetroZarytskyi Aug 20, 2024

Choose a reason for hiding this comment

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

VDDerivedType is not temporary, it represents the type of the adjoint variable, whereas VDCloneType represents the type of the cloned variable. The same type is used for both the clone and the adjoint, hence the assignment later.

Copy link
Owner

Choose a reason for hiding this comment

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

Ok, I did not realize it was used below. This routine has become quite complicated...

lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
Comment on lines +2753 to +2769
if (promoteToFnScope) {
VDDerivedInit = BuildOp(UO_AddrOf, initDiff.getExpr_dx());
VDDerivedType = VDDerivedInit->getType();
} else {
VDDerivedInit = initDiff.getExpr_dx();
VDDerivedType =
m_Context.getLValueReferenceType(VDDerivedInit->getType());
}
VDCloneType = VDDerivedType;
Copy link
Owner

Choose a reason for hiding this comment

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

Suggested change
if (promoteToFnScope) {
VDDerivedInit = BuildOp(UO_AddrOf, initDiff.getExpr_dx());
VDDerivedType = VDDerivedInit->getType();
} else {
VDDerivedInit = initDiff.getExpr_dx();
VDDerivedType =
m_Context.getLValueReferenceType(VDDerivedInit->getType());
}
VDCloneType = VDDerivedType;
if (promoteToFnScope) {
VDDerivedInit = BuildOp(UO_AddrOf, initDiff.getExpr_dx());
VDCloneType = VDDerivedInit->getType();
} else {
VDDerivedInit = initDiff.getExpr_dx();
VDCloneType =
m_Context.getLValueReferenceType(VDDerivedInit->getType());
}

Copy link
Collaborator Author

@PetroZarytskyi PetroZarytskyi Aug 20, 2024

Choose a reason for hiding this comment

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

This way, VDDerivedType is not modified and the adjoints remain of type std::initializer_list. This breaks the tests for multiple reasons.

VDCloneType = VDDerivedType;
}
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

I believe this block could be further simplified.

Copy link
Contributor

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

unsigned numInits = ILE->getNumInits();
VDDerivedInit = ConstantFolder::synthesizeLiteral(
m_Context.getSizeType(), m_Context, numInits);
VDCloneType = VDDerivedType;
Copy link
Owner

Choose a reason for hiding this comment

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

Ok, I did not realize it was used below. This routine has become quite complicated...

lib/Differentiator/ReverseModeVisitor.cpp Show resolved Hide resolved
Copy link
Contributor

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

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 66b6af4 into vgvassilev:master Aug 21, 2024
89 checks passed
@PetroZarytskyi PetroZarytskyi deleted the issue-830 branch August 21, 2024 15:47
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.

Support for std::initializer_list
2 participants