<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/87836>87836</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Over-aggressively optimization on infinite loops affects RISC-V platforms as well
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          leonardopsantos
      </td>
    </tr>
</table>

<pre>
    Not exactly a duplicate of https://github.com/llvm/llvm-project/issues/60622 because that issue specifically mentions x86.

I just hit this issue in RISC-V bare-metal target with `clang 17.0.0`, so most definitively not restricted to x86. My code is fairly similar to the test case above:

```c
#define DO_WAIT 0
#define DO_ACTION 1

int g_lock = DO_WAIT;

void __attribute__((noinline)) worker() {
  for (;;) {

    volatile int i = 0;
    i++;
    g_lock = DO_WAIT;

    while (g_lock != DO_ACTION) {
    }

    i = 0xCAFECAFE;
  }
 return;
}

void dontcallme() {
    volatile int i = 0;
 i++;
}

int main() {
  worker();
  return 0;
}
```

The disassembly for that is:

```asm
worker: # @worker
# %bb.0: # %for.cond
        addi    sp, sp, -16
        sw      zero, 12(sp)
        lw      a0, 12(sp)
        addi    a0, a0, 1
        sw      a0, 12(sp)
.Lfunc_end0:
        .size   worker, .Lfunc_end0-worker
 # -- End function
        .globl  dontcallme # -- Begin function dontcallme
        .p2align        2
        .type   dontcallme,@function
dontcallme: # @dontcallme
# %bb.0: # %entry
        addi    sp, sp, -16
        sw      zero, 12(sp)
        lw      a0, 12(sp)
        addi    a0, a0, 1
        sw      a0, 12(sp)
        addi    sp, sp, 16
        ret
.Lfunc_end1:
        .size   dontcallme, .Lfunc_end1-dontcallme
                                        # -- End function
        .globl  main                            # -- Begin function main
```

There's no control instruction at the end of `worker`, so it just falls through to `dontcallme`. 

I'm baffled as to how can anyone think that's reasonable.

GCC for RISC-V _does the right thing_ and adds a `j`: https://godbolt.org/z/h55bYsdo9

I've tried the `-fsanitize=undefined` flag as suggested [here](https://github.com/llvm/llvm-project/issues/48943#issuecomment-981039980), but that doesn't work: https://godbolt.org/z/aYrqfsca7

The workaround is very counter-intuitive:

```c
    while (g_lock != DO_ACTION) {
        asm volatile("");
 }
```

Yields:

```asm
.LBB0_2: # %while.body
                                        #   Parent Loop BB0_1 Depth=1
                                        # =>  This Inner Loop Header: Depth=2
        #APP
        #NO_APP
        lw      a3, %pcrel_lo(.Lpcrel_hi0)(a0)
        bne     a3, a1, .LBB0_2
# %bb.3: # %while.end
                                        # in Loop: Header=BB0_1 Depth=1
        sw      a2, 12(sp)
        j       .LBB0_1
.Lfunc_end0:
        .size   worker, .Lfunc_end0-worker
                                        # -- End function
```

Also here: https://godbolt.org/z/f6Y6qdrMT

I also disagree that https://github.com/llvm/llvm-project/issues/60622, https://github.com/llvm/llvm-project/issues/60419, and https://github.com/llvm/llvm-project/issues/60588 should be closed. Especially because there are no proposed workarounds.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMV11v2zoS_TX0y8ACRdmy9OAHx252A_S2xW6wiz4ZlDiS2NKkL0kldX79gpQ_FDe9Tdp9uIbiwOTw6MxwOGfInZOtRlyS-Q2Zbya8952xS4VGcyvM3nHtjZtURhyWH4wH_MZrrw7AQfR7JWvuEUwDnfd7R7IVYbeE3bbSd32V1GZH2K1SD6d_0701X7D2hN1K53p0hN3mNGcMKqx57xB8xz3EOXB7rGUja67UAXaovTTawbciTwjdELoavu_gS-88dNKD76Q7rpUa_nX37_X0P1Bxi9Mdeq7Ac9uih0fpOyA5rRXXLaSLhCaU5JSwNTgDO-M8CGykll4-oDqANh4sOm9l7VGAN5ED_HGA2ggE6aDh0qoDOLmTittg4TsEj85DzR0Cr8wDhuCMeIc3xqc-_mZZfCvC5uP2v6u7e6AvTKzW93cfP0A6RpLaQ7tVpv4KJNuclpPsZmz0YKSA7ZZ7b2XVe9xuCSsIK7SRWkmNhJWElfBo7Fe0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