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

Fix the generation of invalid code in some common cases #1088

Merged
merged 1 commit into from
Oct 8, 2024

Conversation

gojakuch
Copy link
Collaborator

@gojakuch gojakuch commented Sep 10, 2024

This commit fixes the way Clad generates code. Specifically, it addresses the way operators appear in the generated code in the reverse mode and the way nested name qualifiers are built in both modes. This includes a partial fix to #1050.

Fixes: #1087

@gojakuch gojakuch marked this pull request as draft September 10, 2024 19:14
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/VisitorBase.cpp Show resolved Hide resolved
CSS.Extend(m_Context, NS, noLoc, noLoc);
} else if (NNS->getKind() == NestedNameSpecifier::TypeSpec) {
const Type* T = NNS->getAsType();
if (auto* RT = const_cast<RecordType*>(T->getAs<RecordType>())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast]

        if (auto* RT = const_cast<RecordType*>(T->getAs<RecordType>())) {
                       ^

Copy link

codecov bot commented Sep 11, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.23%. Comparing base (8ed2707) to head (8243b3c).
Report is 1 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1088      +/-   ##
==========================================
+ Coverage   94.21%   94.23%   +0.01%     
==========================================
  Files          48       48              
  Lines        8131     8158      +27     
==========================================
+ Hits         7661     7688      +27     
  Misses        470      470              
Files with missing lines Coverage Δ
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.73% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/ReverseModeVisitor.cpp 95.47% <100.00%> (+0.02%) ⬆️
lib/Differentiator/VisitorBase.cpp 97.14% <100.00%> (+0.07%) ⬆️
Files with missing lines Coverage Δ
include/clad/Differentiator/VisitorBase.h 100.00% <ø> (ø)
lib/Differentiator/BaseForwardModeVisitor.cpp 98.73% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/ReverseModeVisitor.cpp 95.47% <100.00%> (+0.02%) ⬆️
lib/Differentiator/VisitorBase.cpp 97.14% <100.00%> (+0.07%) ⬆️

@gojakuch gojakuch marked this pull request as ready for review September 11, 2024 16:35

std::reverse(NNChain.begin(), NNChain.end());

for (auto& n : NNChain) {
Copy link
Owner

@vgvassilev vgvassilev Sep 11, 2024

Choose a reason for hiding this comment

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

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 saw it but I didn't dive deep into reading it, since I'd thought it's unrelated, cos it's about type names only.

Copy link
Owner

Choose a reason for hiding this comment

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

The routine is practically the same and more complete to what we are trying to do here. It’s not possible to call it though. Maybe we should at least add a fixme and refer to it as a potential place to steal code from. It has a lot of tests and presumably should work more reliably.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

alright, a fixme sounds fair. I'll also try to maybe incorporate some more things from it

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
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
lib/Differentiator/VisitorBase.cpp Outdated Show resolved Hide resolved
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
lib/Differentiator/ReverseModeVisitor.cpp Outdated Show resolved Hide resolved
@gojakuch
Copy link
Collaborator Author

gojakuch commented Oct 6, 2024

@vgvassilev thanks for the rebase! I'm still working on fixing the last check, seems that it hasn't been affected that much

@gojakuch
Copy link
Collaborator Author

gojakuch commented Oct 6, 2024

@vgvassilev it finally passed all the checks, I'm just going to squash everything now

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.

Can you address the clang-tidy comments?

@@ -0,0 +1,80 @@
// XFAIL: asserts
Copy link
Owner

Choose a reason for hiding this comment

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

Why do we fail in debug builds here?

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 think I just forgot to remove this line. thanks for pointing it out. it should be fine, but let's wait for the checks to pass

Copy link
Collaborator Author

@gojakuch gojakuch Oct 7, 2024

Choose a reason for hiding this comment

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

oh, seems like it was actually doing something, now I remember. unfortunately, the debug build is very picky. I have a couple of FIXME messages in that place and once they are addressed the XFAIL line can be removed.

the problem is that inside Clang they're using locations to build these nested names, while I'm using other things here, since we don't really have valid locations to provide to those Clang methods (providing valid locations from the original source code doesn't seem to work here). my approach works for nested names with namespace and basic class names, but the debug build runs into some asserts if you try to extend a CXXScopeSpec with a class name this way.

I can remove the XFAIL and update the tests so that they don't contain a class name in the nested name specifier chain and then open another separate issue for this case. does that sound good? or we can just not flag the issue #1050 as solved in this PR and update it to say that it's only partially solved.

Copy link
Owner

Choose a reason for hiding this comment

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

Maybe update the tests and remove the xfail flag. Then we can adjust them back when fixing the issue #1050.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@vgvassilev all done. the checks have passed

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

@@ -2167,6 +2170,28 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
utils::BuildMemberExpr(m_Sema, getCurrentScope(), callRes, "adjoint");
return StmtDiff(resValue, resAdjoint, resAdjoint);
} // Recreate the original call expression.

if (const auto* OCE = dyn_cast<CXXOperatorCallExpr>(CE)) {
auto* FD = const_cast<CXXMethodDecl*>(
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: do not use const_cast [cppcoreguidelines-pro-type-const-cast]

      auto* FD = const_cast<CXXMethodDecl*>(
                 ^

@gojakuch gojakuch force-pushed the issue-1050 branch 2 times, most recently from d47e2b7 to 82a6527 Compare October 8, 2024 08:07
This commit fixes the way Clad generates code. Specifically,
it addresses the way operators appear in the generated code
in the reverse mode and the way nested name qualifiers are
built in both modes. This partially f ixes vgvassilev#1050.

Fixes: vgvassilev#1087
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 4f2a378 into vgvassilev:master Oct 8, 2024
90 checks passed
@gojakuch gojakuch deleted the issue-1050 branch December 7, 2024 16:44
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.

Invalid code generated in the gradient when using operators
2 participants