Issue 86685
Summary [Clang-18 Miscompilation] clang miscompiles loop with break
Labels clang
Assignees
Reporter wierton
    Compiler Explorer: https://gcc.godbolt.org/z/3s1ezjzW4
I sanitized this program with ubsan,msan and asan, they reports nothing as shown in the CompilerExplorer, so I think this is likely a true miscompilation bug.

```
#include <stdio.h>
#include <stdlib.h>

void foo(int *s, float *f, long n)
{
  for (long i = 0; i < n; ++i) {
 *f = 2;
    if (i == 1) break;
    *s = 4;
  }
}

int main(void)
{
  union {int s; float f;} u = {0};
  foo(&u.s, &u.f, 2);
  if (u.f != 2) abort ();
  return 0;
}
```

In Compiler Explorer, it is observed that the program compiled with `clang -O0` outputs `2`, whereas the same program compiled with `clang -O1` outputs `0`. Given that the third argument `n` of the function `foo` is 2, the expected execution trace within `foo`'s loop would be `*f = 2`, followed by `*s = 4`, and then `*f = 2` again. This sequence should  leave `u.f` with the value `2` instead of `0`. 
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to