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

Initialize adjoints of aggregate types with init lists #1163

Merged
merged 4 commits into from
Dec 10, 2024

Conversation

PetroZarytskyi
Copy link
Collaborator

With aggregate types, we don't have to rely on the default initialization or custom constructor forward passes to zero-initialize its fields:

struct aggrStruct {
  double a = 1;
  float b;
  ...
};

This option is incorrect since _d_x.a will be initialized to 1:

aggrStruct _d_x{};

This option could be correct but is just unnecessary:

... _t1 = custom_reverse_forward(...);
aggrStruct _d_x = _t1.adjoint; // unnecessary

This PR instead implements a simpler solution:

aggrStruct _d_x{0., 0.F};

Copy link

codecov bot commented Dec 5, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.56%. Comparing base (eee6faa) to head (852b4dc).
Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1163      +/-   ##
==========================================
+ Coverage   94.55%   94.56%   +0.01%     
==========================================
  Files          51       51              
  Lines        8919     8947      +28     
==========================================
+ Hits         8433     8461      +28     
  Misses        486      486              
Files with missing lines Coverage Δ
lib/Differentiator/ReverseModeVisitor.cpp 95.59% <100.00%> (+0.03%) ⬆️
lib/Differentiator/VisitorBase.cpp 97.67% <100.00%> (+0.02%) ⬆️
Files with missing lines Coverage Δ
lib/Differentiator/ReverseModeVisitor.cpp 95.59% <100.00%> (+0.03%) ⬆️
lib/Differentiator/VisitorBase.cpp 97.67% <100.00%> (+0.02%) ⬆️

@PetroZarytskyi PetroZarytskyi force-pushed the triv-constr branch 2 times, most recently from c429677 to 17786f2 Compare December 6, 2024 22:40
@@ -4286,6 +4280,10 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
if (Expr* customReverseForwFnCall = BuildCallToCustomForwPassFn(
CE->getConstructor(), primalArgs, reverseForwAdjointArgs,
/*baseExpr=*/nullptr)) {
if (RD->isAggregate())
diag(DiagnosticsEngine::Warning, CE->getBeginLoc(),
Copy link
Owner

Choose a reason for hiding this comment

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

Is here the place where we found the custom forward reverse function and we diagnose we do not need it? If not we should move the check there, and point to the declaration itself with a DiagnosticsEngine::Note(CE->getDecl()->getBeginLoc()). And of course we should add a test for it...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I kept the diagnostics in the same place but now the location comes from the declaration. Is it better now?

Copy link
Owner

Choose a reason for hiding this comment

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

We need both a warning on the call site and a note on our he definition. We also need a test.

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

@@ -418,6 +418,13 @@ namespace clad {
Expr* zero = ConstantFolder::synthesizeLiteral(T, m_Context, /*val=*/0);
return m_Sema.ActOnInitList(noLoc, {zero}, noLoc).get();
}
if (const auto* RD = T->getAsCXXRecordDecl())
if (RD->hasDefinition() && !RD->isUnion() && RD->isAggregate()) {
llvm::SmallVector<Expr*, 4> adjParams;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "llvm::SmallVector" is directly included [misc-include-cleaner]

lib/Differentiator/VisitorBase.cpp:28:

- #include <numeric>
+ #include <llvm/ADT/SmallVector.h>
+ #include <numeric>

if (const auto* RD = T->getAsCXXRecordDecl())
if (RD->hasDefinition() && !RD->isUnion() && RD->isAggregate()) {
llvm::SmallVector<Expr*, 4> adjParams;
for (const FieldDecl* FD : RD->fields())
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: no header providing "clang::FieldDecl" is directly included [misc-include-cleaner]

        for (const FieldDecl* FD : RD->fields())
                   ^

Copy link
Contributor

github-actions bot commented Dec 9, 2024

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

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 Show resolved Hide resolved
lib/Differentiator/ReverseModeVisitor.cpp Show resolved Hide resolved
}}}

double fn15(double x, double y) {
std::array<double, 2> arr; // expected-warning {{'std::array<double, 2>' is an aggregate type and its constructor does not require a user-defined forward sweep function}}
Copy link
Owner

Choose a reason for hiding this comment

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

This does not need to be a std::array because we will remove its reverse function. It can be something as simple as an empty class...

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

@@ -34,7 +34,9 @@
#include <clang/AST/OperationKinds.h>
#include <clang/Sema/Ownership.h>

#include "llvm/ADT/SmallString.h"
#include "llvm/Support/SaveAndRestore.h"
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: included header SmallString.h is not used directly [misc-include-cleaner]

Suggested change
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/SaveAndRestore.h"

@PetroZarytskyi PetroZarytskyi force-pushed the triv-constr branch 3 times, most recently from 6797990 to 6e4e5a7 Compare December 10, 2024 20:51
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 47b6504 into vgvassilev:master Dec 10, 2024
89 of 90 checks passed
@PetroZarytskyi PetroZarytskyi deleted the triv-constr branch December 13, 2024 12:11
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