[llvm-bugs] [Bug 52495] New: wrong code at -O2 and above on x86_64-linux-gnu (affecting clang-6.0 and above versions)

via llvm-bugs llvm-bugs at lists.llvm.org
Fri Nov 12 18:01:32 PST 2021


https://bugs.llvm.org/show_bug.cgi?id=52495

            Bug ID: 52495
           Summary: wrong code at -O2 and above on x86_64-linux-gnu
                    (affecting clang-6.0 and above versions)
           Product: libraries
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: Scalar Optimizations
          Assignee: unassignedbugs at nondot.org
          Reporter: haoxintu at gmail.com
                CC: llvm-bugs at lists.llvm.org

Hi all.

I guess the following program is well-defined but makes clang produce wrong
code across many versions.

$cat small.c
#include <stdio.h>
#include <stdint.h>

union a {
  int16_t b;
  uint64_t c;
} d;

uint64_t *e = &d.c;
static uint16_t f(int16_t a, int32_t b, uint64_t c);
static int64_t g(int32_t aa, uint8_t h, union a bb) {
  int16_t *i = &d.b;
  f(0, h, 0);
  *i = h;
  return 0;
}
uint16_t f(int16_t a, int32_t b, uint64_t c) {
  for (d.c = 0; 0;)
    ;
  *e = 0;
  return 0;
}

int main() {
  uint32_t j = 8;
  g(1, j, d);
  printf("%d\n", d.b);
  return 0;
}


$clang -w -O1 small.c ; ./a.out
8

$clang -w -O2 small.c ; ./a.out
0

$clang -w -O3 small.c ; ./a.out
0

$clang-5.0 -w -O small.c ; ./a.out
8

Please check more in Godbolt: https://godbolt.org/z/c3YzvYe7a

Thanks,
Haoxin

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20211113/e78b4ca8/attachment.html>


More information about the llvm-bugs mailing list