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

Remove Redundant Goto Statements #608

Closed
wants to merge 5 commits into from

Conversation

ShounakDas101
Copy link
Contributor

Solves issue #526. We can only remove the 'goto' label when the parent of the parent of the return statement is null.

@vgvassilev vgvassilev requested a review from parth-07 July 27, 2023 18:14
@parth-07
Copy link
Collaborator

@ShounakDas101 I suggest we wait on this pull request until we all agree that it's useful.

I am not convinced that this pull request is useful. It's solving a very specific case. I believe it will only
be beneficial if we can remove gotos in other general cases as well. @ShounakDas101 Please let me know your
thoughts on this.

@ShounakDas101
Copy link
Contributor Author

@parth-07 In this pull request (PR), I have decided to remove 'goto statements' only if the 'return statement' is the direct child node of the 'Compound statement' that contains the entire function body(This is the only case where we should remove 'goto statement'). The other 'return statements' will be within some control flow block. I believe that 'goto statements' for such return statements are essential and should not be removed.

Could you please suggest a test case where this approach fails? This PR has removed all redundant goto statements in the test cases available in CLAD.

@parth-07
Copy link
Collaborator

parth-07 commented Aug 3, 2023

Hi @ShounakDas101

Could you please suggest a test case where this approach fails?

I did not say that your approach fails in any case. I just do not see any advantage of adding extra complexity to
remove goto's for a special case of the return statement. For this to be beneficial, we also need to remove other
goto's.

@ShounakDas101
Copy link
Contributor Author

ShounakDas101 commented Aug 4, 2023

Hi @parth-07 I have tried to solve issue #526. I believe the issue specifically only wants to remove redundant goto statements like this
...
goto _label0;
_label0:
...

For this to be beneficial, we also need to remove other goto's.

By other goto's do you mean all goto's should be removed?

@parth-07
Copy link
Collaborator

parth-07 commented Aug 5, 2023

I believe the issue specifically only wants to remove redundant goto statements like this

Yes, the issue specifically wants to remove gotos from the special case only. But I am still not sure if we
benefit from this functionality. The compiler will optimize the redundant goto statements anyways.

The main concern is that goto statement prohibits further differentiation. Solving a special case wouldn't lift this restriction.

@vgvassilev What do you think about removing the redundant goto statements?

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.

I am still concerned by the amount of test failures we have. I think if we manage to solve that case with sound implementation would be acceptable to me.

}else{
OnlyReturn=true;
}
}
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 be able to detect in a smarter way if we need to emit a goto-statement given a forward sweep and then conditionally emit 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

@@ -744,6 +744,20 @@ Expr* getArraySizeExpr(const ArrayType* AT, ASTContext& context,
beginBlock(direction::forward);
beginBlock(direction::reverse);
for (Stmt* S : CS->body()) {
std::string cppString="ReturnStmt";
const char* cString = S->getStmtClassName();
if(cppString.compare(cString) == 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: do not use 'compare' to test equality of strings; use the string equality operator instead [readability-string-compare]

Suggested change
if(cppString.compare(cString) == 0 ){
if(cppString == cString ){

if (!parents.empty()){
const Stmt* parentStmt = parents[0].get<Stmt>();
if(parentStmt==nullptr)
OnlyReturn=true;
Copy link
Contributor

Choose a reason for hiding this comment

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

warning: redundant boolean literal in conditional assignment [readability-simplify-boolean-expr]

lib/Differentiator/ReverseModeVisitor.cpp:752:

-           if(parentStmt==nullptr)
-             OnlyReturn=true;
-           else
-             OnlyReturn=false;
+           OnlyReturn = parentStmt==nullptr;

@github-actions
Copy link
Contributor

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

@codecov
Copy link

codecov bot commented Aug 10, 2023

Codecov Report

Merging #608 (6cf27f3) into master (790d978) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #608      +/-   ##
==========================================
+ Coverage   93.83%   93.84%   +0.01%     
==========================================
  Files          41       41              
  Lines        6032     6042      +10     
==========================================
+ Hits         5660     5670      +10     
  Misses        372      372              
Files Changed Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.70% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 95.62% <100.00%> (+0.02%) ⬆️
Files Changed Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.70% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 95.62% <100.00%> (+0.02%) ⬆️

@vgvassilev
Copy link
Owner

Fixed in #938

@vgvassilev vgvassilev closed this Jun 15, 2024
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