clangir icon indicating copy to clipboard operation
clangir copied to clipboard

Loop with `break` or `continue` at top level but not last statement fails validation

Open dkolsen-pgi opened this issue 1 year ago • 1 comments

When a for, while, or do/while loop contains a break or continue statement at the top level of the loop's body, and the break or continue statement is not the last statement in the block, then ClangIR fails validation.

void test(int x) {
  for (int i = 0; i < x; ++i) {
    break;
    --x;
  }
}
loc("for-break.cpp":3:5): error: 'cir.yield' op must be the last operation in the parent block
fatal error: error in backend: CIR codegen: module verification error before running CIR passes

If the break or continue is in a nested scope within the loop (which is the normal case in real code), there is no error. If the break or continue statement is the last statement in the block then there is a different internal error which is covered by #324.

dkolsen-pgi avatar Nov 20 '23 00:11 dkolsen-pgi

Handling unreachable code is lacking some support here indeed. We should probably end the first basic block with cir.yield break, and start a new bb to stuff --x, which should end with a regular cir.yield.

bcardosolopes avatar Nov 22 '23 20:11 bcardosolopes