-
Notifications
You must be signed in to change notification settings - Fork 126
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 declarations for variables reused in the reverse pass #688
Conversation
994fdd3
to
b9c1ace
Compare
There was a problem hiding this 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
There was a problem hiding this 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
@PetroZarytskyi Can you please describe the solution in the commit message / pull request description? |
b54ec5f
to
dea82f4
Compare
There was a problem hiding this 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
There was a problem hiding this 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
c2aa94c
to
17d22e6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here is some partial review. Could you go over your FIXME notes and try to address them as it seems most of them need to be addressed within this PR.
cond = Visit(FS->getCond()); | ||
const auto* IDRE = dyn_cast<DeclRefExpr>(FS->getInc()); | ||
bool IDRE = isa<DeclRefExpr>(FS->getInc()); | ||
/// FIXME: Why do we need to differentiate DeclRefExpr twice? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which DeclRefExpr are you referring here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From what I get, inc
is supposed to be the original increment (it's always differentiated later). Why is it differentiated specifically when it is a DeclRefExpr?
StmtDiff IdxDiff = Visit(Indices[i]); | ||
clonedIndices[i] = Clone(IdxDiff.getExpr()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this change necessary for this pull request? If not, then this should be fixed in a separate pull-request.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, it's necessary to replace getExpr
with getRevSweepAsExpr
for reverseIndices
. I could move Clone
to where they were previously if that matters.
test/Gradient/Loops.C
Outdated
//CHECK-NEXT: clad::push(_t1, t); | ||
//CHECK-NEXT: t *= x; | ||
//CHECK-NEXT: { | ||
//CHECK-NEXT: int i; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it necessary to declare the loop init statement outside the loop?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue here is that we want to store the last value of the loop init. So we need to access i
once right after the last condition. But we can't do that outside the loop since the variable is not declared there. My solution doesn't too look nice though so if you have a better idea feel free to share it.
9d152f6
to
c6961bf
Compare
Codecov ReportAttention:
Additional details and impacted files@@ Coverage Diff @@
## master #688 +/- ##
==========================================
+ Coverage 94.54% 94.64% +0.09%
==========================================
Files 49 49
Lines 7189 7281 +92
==========================================
+ Hits 6797 6891 +94
+ Misses 392 390 -2
... and 1 file with indirect coverage changes
|
bcf4dfc
to
44c754b
Compare
44c754b
to
0b6373f
Compare
// If the declaration is not located in the function global scope, | ||
// we have to redeclare it in the reverse sweep since it won't be | ||
// visible otherwise. | ||
// FIXME: Is there a better way to determine non-function-global scope |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, there must be a better way for this. Have you tried VD->getDeclContext() != m_Function
?
Ideally, function global scope variables should have the primal FunctionDecl
as their DeclContext
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how DeclContext
works exactly but getDeclContext
seems to return m_Function
for variables declared inside loops etc.
3ecbcf5
to
b388bec
Compare
The produced gradient of the below code has compile-time error in the local variable redeclaration part. double fn(double u, double v) {
double r;
{
double res;
double v_copy = v;
double &v_ref = v_copy;
res = v_ref;
r = res;
}
return r;
} The local variable redeclaration in the reverse sweep is as follows: double &v_ref = v_copy;
double v_copy = _t3;
double res = _t2;
|
m_Blocks.pop_back(); | ||
else | ||
EmitReverseSweepDeclarations(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add a test case covering this branch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, EndBlockWithoutCreatingCS
is only used once in the reverse direction. There is no way to call it in the forward direction just with tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that dead code then?
a94a447
to
fa168e2
Compare
fa168e2
to
1661a22
Compare
@PetroZarytskyi, is that PR superseded by #738? Can we close it? |
I think the solution implemented in #738 is better. We should probably close this PR. |
The problem this PR attempts to fix is described in #659 (which in turn caused #681). Also, this PR introduces placeholders to
DelayedGlobalStoreAndRef
to rewrite expressions after they are added to the current block if there is no point in storing them. This is done to avoid cloning while handling multiplication differentiation.Fixes #659. Fixes #681.