clad icon indicating copy to clipboard operation
clad copied to clipboard

Remove Redundant Goto Statements

Open ShounakDas101 opened this issue 1 year ago • 7 comments

Solves issue https://github.com/vgvassilev/clad/issues/526. We can only remove the 'goto' label when the parent of the parent of the return statement is null.

ShounakDas101 avatar Jul 27 '23 17:07 ShounakDas101

@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.

parth-07 avatar Jul 31 '23 18:07 parth-07

@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.

ShounakDas101 avatar Aug 01 '23 04:08 ShounakDas101

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.

parth-07 avatar Aug 03 '23 20:08 parth-07

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?

ShounakDas101 avatar Aug 04 '23 04:08 ShounakDas101

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?

parth-07 avatar Aug 05 '23 20:08 parth-07

clang-tidy review says "All clean, LGTM! :+1:"

github-actions[bot] avatar Aug 10 '23 17:08 github-actions[bot]

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%) :arrow_up:
Files Changed Coverage Δ
include/clad/Differentiator/ReverseModeVisitor.h 98.70% <ø> (ø)
lib/Differentiator/ReverseModeVisitor.cpp 95.62% <100.00%> (+0.02%) :arrow_up:

codecov[bot] avatar Aug 10 '23 17:08 codecov[bot]

Fixed in #938

vgvassilev avatar Jun 15 '24 12:06 vgvassilev