Skip to content

Conversation

terapines-osc-cir
Copy link
Contributor

As the scf dialect does not support early exits, it might be necessary to change the body of WhileOp to implement the semantics of ContinueOp. I choose to add a guard if (!cond) for everything following the continue.

@terapines-osc-cir
Copy link
Contributor Author

@felixdaas Thanks for your comment! There's still something I didn't understand about your infinite-loop example here.

void while_continue_2() {
  int i = 0, b;
  while (i < 10) {
    if (i == 5) {
      b = 3;
      continue;
    }
    i++;
  }
}

My understanding is that, the continue is supposed to skip everything after it, including i++, so an infinite loop is desired. You might found out the whole loop is skipped when you compile in -O2, but that's because infinite loop without side effect is UB and gets optimized away, which is not of our concern in lowering.
Could you please elaborate on why the i++ needs to be specially treated?

@felixdaas
Copy link
Contributor

@terapines-osc-cir oh yeah my bad youre right, I was somehow confusing the continue and break semantics here, so youre solution seems good

@bcardosolopes bcardosolopes merged commit 3401122 into llvm:main Jun 17, 2025
9 checks passed
terapines-osc-cir added a commit to Terapines/clangir that referenced this pull request Sep 2, 2025
As the scf dialect does not support early exits, it might be necessary
to change the body of WhileOp to implement the semantics of ContinueOp.
I choose to add a guard `if (!cond)` for everything following the
`continue`.

Co-authored-by: Yue Huang <[email protected]>
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.

4 participants