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

Try to improve literal types #998

Merged
merged 27 commits into from
Sep 3, 2024
Merged

Conversation

vaithak
Copy link
Collaborator

@vaithak vaithak commented Jul 22, 2024

This is related to the review comments of PR #975.

Copy link
Contributor

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

@@ -1008,8 +1008,11 @@ StmtDiff BaseForwardModeVisitor::VisitDeclRefExpr(const DeclRefExpr* DRE) {
// If DRE is of type pointer, then the derivative is a null pointer.
if (clonedDRE->getType()->isPointerType())
return StmtDiff(clonedDRE, nullptr);
QualType literalTy = utils::GetValueType(clonedDRE->getType());
Copy link
Owner

Choose a reason for hiding this comment

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

We should extend visitorbase::getZeroInit as discussed here #989 (comment)

cc: @gojakuch

@vgvassilev
Copy link
Owner

This pull request, if the review comment is addressed, should fix #1001.

@kchristin22
Copy link
Collaborator

The cuda bug was a result of not handling the enum types correctly. clad::utils::GetValueType() doesn't return the underlying type of the enum but the enum itself (as a type). When checking isRealType() for that node, the cudaError_t enum passes the test so it's type is not set as an Int. This lead to a compilation error down the line.

I fixed that part in clad::utils::GetValueType() in a branch based on Vaibhav's cuda-bug branch. Should I open a new PR and reference this one and continue addressing the comments of this and its previous PR there?

@vgvassilev
Copy link
Owner

The cuda bug was a result of not handling the enum types correctly. clad::utils::GetValueType() doesn't return the underlying type of the enum but the enum itself (as a type). When checking isRealType() for that node, the cudaError_t enum passes the test so it's type is not set as an Int. This lead to a compilation error down the line.

I fixed that part in clad::utils::GetValueType() in a branch based on Vaibhav's cuda-bug branch. Should I open a new PR and reference this one and continue addressing the comments of this and its previous PR there?

Can you push to this PR?

Copy link
Contributor

github-actions bot commented Aug 2, 2024

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

@kchristin22
Copy link
Collaborator

The cuda bug was a result of not handling the enum types correctly. clad::utils::GetValueType() doesn't return the underlying type of the enum but the enum itself (as a type). When checking isRealType() for that node, the cudaError_t enum passes the test so it's type is not set as an Int. This lead to a compilation error down the line.
I fixed that part in clad::utils::GetValueType() in a branch based on Vaibhav's cuda-bug branch. Should I open a new PR and reference this one and continue addressing the comments of this and its previous PR there?

Can you push to this PR?

Sure, but I have no permissions in Vaibhav's repo, @vaithak could you change it temporarily(till this PR is merged)?

@vgvassilev
Copy link
Owner

If you accept my invite you should be able to push to this PR.

Copy link
Contributor

github-actions bot commented Aug 3, 2024

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

Copy link

codecov bot commented Aug 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.40%. Comparing base (8f7d247) to head (0413ab7).
Report is 2 commits behind head on master.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #998      +/-   ##
==========================================
- Coverage   94.41%   94.40%   -0.02%     
==========================================
  Files          55       55              
  Lines        8410     8430      +20     
==========================================
+ Hits         7940     7958      +18     
- Misses        470      472       +2     
Files with missing lines Coverage Δ
lib/Differentiator/BaseForwardModeVisitor.cpp 98.53% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/CladUtils.cpp 94.17% <100.00%> (-0.20%) ⬇️
lib/Differentiator/ConstantFolder.cpp 38.94% <100.00%> (+13.30%) ⬆️
lib/Differentiator/VisitorBase.cpp 96.90% <100.00%> (-0.01%) ⬇️

... and 1 file with indirect coverage changes

Files with missing lines Coverage Δ
lib/Differentiator/BaseForwardModeVisitor.cpp 98.53% <100.00%> (+<0.01%) ⬆️
lib/Differentiator/CladUtils.cpp 94.17% <100.00%> (-0.20%) ⬇️
lib/Differentiator/ConstantFolder.cpp 38.94% <100.00%> (+13.30%) ⬆️
lib/Differentiator/VisitorBase.cpp 96.90% <100.00%> (-0.01%) ⬇️

... and 1 file with indirect coverage changes

Copy link
Contributor

github-actions bot commented Aug 7, 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

ExprResult Zero =
ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0);
else if (T->isScalarType() && !T->isPointerType()) {
ExprResult Zero = ConstantFolder::synthesizeLiteral(T, m_Context, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]

      ExprResult Zero = ConstantFolder::synthesizeLiteral(T, m_Context, 0);
                                                                        ^

this fix will not be applied because it overlaps with another fix

lib/Differentiator/VisitorBase.cpp 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

ExprResult Zero =
ConstantFolder::synthesizeLiteral(m_Context.IntTy, m_Context, 0);
if (T->isScalarType() && !T->isPointerType()) {
ExprResult Zero = ConstantFolder::synthesizeLiteral(T, m_Context, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]

Suggested change
ExprResult Zero = ConstantFolder::synthesizeLiteral(T, m_Context, 0);
ExprResult Zero = ConstantFolder::synthesizeLiteral(T, m_Context, /*val=*/0);

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

@@ -128,14 +134,18 @@ namespace clad {
uint64_t val) {
//SourceLocation noLoc;
Expr* Result = 0;
if (QT->isIntegralType(C)) {
if (QT->isBooleanType()) {
printf("synthesizing boolean literal\n");
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 call c-style vararg functions [cppcoreguidelines-pro-type-vararg]

      printf("synthesizing boolean literal\n");
      ^

llvm::APFloat APVal(C.getFloatTypeSemantics(QT), val);
Result = clad::synthesizeLiteral(QT, C, APVal);
} else {
Result = ConstantFolder::synthesizeLiteral(C.IntTy, C, 0);
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: argument comment missing for literal argument 'val' [bugprone-argument-comment]

Suggested change
Result = ConstantFolder::synthesizeLiteral(C.IntTy, C, 0);
Result = ConstantFolder::synthesizeLiteral(C.IntTy, C, /*val=*/0);

Copy link
Contributor

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

@kchristin22 kchristin22 marked this pull request as ready for review August 26, 2024 15:30
Copy link
Contributor

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

@@ -1070,8 +1070,11 @@ StmtDiff BaseForwardModeVisitor::VisitDeclRefExpr(const DeclRefExpr* DRE) {
// If DRE is of type pointer, then the derivative is a null pointer.
if (clonedDRE->getType()->isPointerType())
return StmtDiff(clonedDRE, nullptr);
QualType literalTy = utils::GetValueType(clonedDRE->getType());
if (!literalTy->isRealType())
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 need this special check here?

Comment on lines 1382 to 1383
if (!literalTy->isRealType())
literalTy = m_Context.IntTy;
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should not need this. This gets handled in ConstantFolder::synthesizeLiteral anyways.

Copy link
Collaborator

Choose a reason for hiding this comment

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

True, it was a leftover from Vaibhav's PR. I will remove these lines.

@@ -379,11 +379,10 @@ namespace clad {
// FIXME: Consolidate other uses of synthesizeLiteral for creation 0 or 1.
if (T->isVoidType())
return nullptr;
if (T->isScalarType()) {
if (T->isScalarType() && !T->isPointerType()) {
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 need this here? Shouldn't ConstantFolder::synthesizeLiteral check if T is a pointer type and synthesize nullptr?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Right now, the pointers are initialized as an empty list, which is equivalent to nullptr, the same way clad tapes are initialized. I can alter them to be equal to nullptr if that's safer.

Copy link
Owner

Choose a reason for hiding this comment

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

Yes, that'd be more readable, too.

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

github-actions bot commented Sep 2, 2024

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

Copy link
Contributor

github-actions bot commented Sep 2, 2024

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.

We are almost there. See this one comment.

CastKind CK = m_Sema.PrepareScalarCast(Zero, T);
return m_Sema.ImpCastExprToType(Zero.get(), T, CK).get();
ConstantFolder::synthesizeLiteral(T, m_Context, /*val=*/0);
return Zero.get();
}
return m_Sema.ActOnInitList(noLoc, {}, noLoc).get();
Copy link
Owner

Choose a reason for hiding this comment

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

We should probably sink ActOnInitList into synthesizeLiteral but let's do it outside of this pr.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it makes sense to be in getZeroInit as we initialize with an empty list, while synthesizeLiteral is used in other places as well

// CHECK-NEXT: double _r0 = 0;
// CHECK-NEXT: char _r1 = 0;
// CHECK-NEXT: double _r0 = 0.;
// CHECK-NEXT: char _r1 = 0i8;
Copy link
Owner

Choose a reason for hiding this comment

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

That will fail to compile I think.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yes indeed, fixed

// CHECK-NEXT: {{.*}}class_functions::operator_subscript_pullback(&_t26, 0, _r_d6, &_d_vec, &_r10);
// CHECK-NEXT: {{.*}} _r11 = 0;
// CHECK-NEXT: size_type _r11 = {{0U|0UL}};
Copy link
Owner

@vgvassilev vgvassilev Sep 3, 2024

Choose a reason for hiding this comment

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

That will not compile. We probably mean std::vector<...>::size_type. I do not think that's a problem for this PR.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'll open an issue

Copy link
Contributor

github-actions bot commented Sep 3, 2024

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 4810b6d into vgvassilev:master Sep 3, 2024
89 checks passed
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.

3 participants