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

AD Cleanup: break-elimination during the CFG normalization step can cause the loop condition to be evaluated an extra time. #4188

Open
saipraveenb25 opened this issue May 17, 2024 · 0 comments
Assignees
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang kind:cleanup tech debt and rough edges

Comments

@saipraveenb25
Copy link
Collaborator

This is typically not a problem, except for when the loop condition evaluates functions with side-effects.

Example:

int x = 0;
for (int i = 0; i < f(i); i++)
{
     x = x + i;
     if (x > 5)
          break;
}

gets replaced with:

int x = 0;
bool bflag = true;
for (int i = 0; i < f(i) && bflag; i++)
{
     x = x + i;
     if (x > 5)
          bflag = false;
}

Here f(i) can be evaluated an extra time, since instead of immediately breaking out of the loop, we evaluate the condition an extra time. The evaluated result is unimportant since bflag=false, but if f(i) has side-effects, the primal part of the computation would be incorrect since it runs one extra time than the user intended.

The fix is simply to enclose the condition block in another condition, but this is tricky to do since most of the AD passes assume that the loop's condition check is in the block immediately following the loop condition.

@saipraveenb25 saipraveenb25 added kind:cleanup tech debt and rough edges goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang labels May 17, 2024
@saipraveenb25 saipraveenb25 self-assigned this May 17, 2024
@saipraveenb25 saipraveenb25 added this to the Q3 2024 (Summer) milestone May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
goal:quality & productivity Quality issues and issues that impact our productivity coding day to day inside slang kind:cleanup tech debt and rough edges
Projects
None yet
Development

No branches or pull requests

1 participant