Fix for redundant break statements in nested scopes.#4082
Draft
copybara-service[bot] wants to merge 1 commit intomainfrom
Draft
Fix for redundant break statements in nested scopes.#4082copybara-service[bot] wants to merge 1 commit intomainfrom
copybara-service[bot] wants to merge 1 commit intomainfrom
Conversation
50c0d06 to
508326e
Compare
Generated by Gemini. Its explanation:
✦ The PipelinedLoopOuterExit test case fails when the break on line 10929 of the test file is present because of a bug in xls/contrib/xlscc/translator.cc where
relative_break_condition was being overwritten by subsequent break statements instead of being accumulated.
Root Cause Analysis
In translator.cc, within the GenerateIR_Stmt function, the handling for clang::BreakStmt and clang::ContinueStmt used simple assignment (=) for relative_break_condition and
relative_continue_condition.
1 // Original buggy code in translator.cc
2 context().relative_break_condition = context().relative_condition_bval(loc);
When a loop body contains multiple break statements, such as:
1 if (x > 10) {
2 if (x == 55) {
3 exit_function = true;
4 break; // Break 1
5 }
6 break; // Break 2
7 }
The translator processes both break statements. When x == 55, Break 1 correctly sets the relative_break_condition. However, the translator continues to process Break 2. Although Break 2 is
logically unreachable in C++ when Break 1 is taken, the translator still evaluates it with its current relative_condition. Because Break 1 calls and_condition(0) to make the rest of the
block a no-op, the relative_condition for Break 2 becomes 0. The assignment relative_break_condition = 0 then overwrites the valid break condition from Break 1, causing the generated IR to
lose the signal to exit the loop.
Fix
The fix involves using or_condition_util to OR the new break/continue condition with any existing ones, ensuring that all possible exit paths are preserved.
1 // Fixed code in translator.cc
2 context().or_condition_util(context().relative_condition_bval(loc),
3 context().relative_break_condition, loc);
This ensures that if any break in the current context is triggered, the relative_break_condition remains true. I have applied this fix to both BreakStmt (for both for and switch bodies)
and ContinueStmt in translator.cc.
PiperOrigin-RevId: 896698882
508326e to
7a8e8fa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix for redundant break statements in nested scopes.
Generated by Gemini. Its explanation:
✦ The PipelinedLoopOuterExit test case fails when the break on line 10929 of the test file is present because of a bug in xls/contrib/xlscc/translator.cc where
relative_break_condition was being overwritten by subsequent break statements instead of being accumulated.
Root Cause Analysis
In translator.cc, within the GenerateIR_Stmt function, the handling for clang::BreakStmt and clang::ContinueStmt used simple assignment (=) for relative_break_condition and
relative_continue_condition.
1 // Original buggy code in translator.cc
2 context().relative_break_condition = context().relative_condition_bval(loc);
When a loop body contains multiple break statements, such as:
1 if (x > 10) {
2 if (x == 55) {
3 exit_function = true;
4 break; // Break 1
5 }
6 break; // Break 2
7 }
The translator processes both break statements. When x == 55, Break 1 correctly sets the relative_break_condition. However, the translator continues to process Break 2. Although Break 2 is
logically unreachable in C++ when Break 1 is taken, the translator still evaluates it with its current relative_condition. Because Break 1 calls and_condition(0) to make the rest of the
block a no-op, the relative_condition for Break 2 becomes 0. The assignment relative_break_condition = 0 then overwrites the valid break condition from Break 1, causing the generated IR to
lose the signal to exit the loop.
Fix
The fix involves using or_condition_util to OR the new break/continue condition with any existing ones, ensuring that all possible exit paths are preserved.
1 // Fixed code in translator.cc
2 context().or_condition_util(context().relative_condition_bval(loc),
3 context().relative_break_condition, loc);
This ensures that if any break in the current context is triggered, the relative_break_condition remains true. I have applied this fix to both BreakStmt (for both for and switch bodies)
and ContinueStmt in translator.cc.