Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RISCV] Miscompile with -O3 #87042

Closed
dtcxzyw opened this issue Mar 29, 2024 · 3 comments · Fixed by #87075
Closed

[RISCV] Miscompile with -O3 #87042

dtcxzyw opened this issue Mar 29, 2024 · 3 comments · Fixed by #87075

Comments

@dtcxzyw
Copy link
Member

dtcxzyw commented Mar 29, 2024

Reduced test case:

#include <stdio.h>
#include <stdint.h>
int16_t g;
int32_t f;
uint32_t h;
uint64_t j = 6;
static uint64_t o(int16_t x, uint32_t *a, int16_t ac) {
  if (!x) j |= f;
  f = ac ? 7 % ac : 7;
  return j;
}
int main() {
  uint64_t v1 =  o(g, &h, 8);
  uint64_t v2 = o(v1, &h, 0);
  o(v2 >> 8, &h, 0);
  printf("%lu\n", j);
  return 0;
}
$ gcc -O0 -fsanitize=address,undefined test.c && ./a.out
7
$ bin/clang -O3 --target=riscv64-linux-gnu test.c
$ qemu-riscv64 -L /usr/riscv64-linux-gnu ./a.out
13

llvm version: ba6b2d2
qemu version: v8.2.0

@llvmbot
Copy link
Member

llvmbot commented Mar 29, 2024

@llvm/issue-subscribers-backend-risc-v

Author: Yingwei Zheng (dtcxzyw)

Reduced test case: ``` #include <stdio.h> #include <stdint.h> int16_t g; int32_t f; uint32_t h; uint64_t j = 6; static uint64_t o(int16_t x, uint32_t *a, int16_t ac) { if (!x) j |= f; f = ac ? 7 % ac : 7; return j; } int main() { uint64_t v1 = o(g, &h, 8); uint64_t v2 = o(v1, &h, 0); o(v2 >> 8, &h, 0); printf("%lu\n", j); return 0; } ```
$ gcc -O0 -fsanitize=address,undefined test.c &amp;&amp; ./a.out
7
$ bin/clang -O3 --target=riscv64-linux-gnu test.c
$ qemu-riscv64 -L /usr/riscv64-linux-gnu ./a.out
13

llvm version: ba6b2d2
qemu version: v8.2.0

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Mar 29, 2024

It should be a middle-end bug since the disjoint flag is incorrectly inferred.

@dtcxzyw
Copy link
Member Author

dtcxzyw commented Mar 29, 2024

Reduced LLVM IR: https://alive2.llvm.org/ce/z/d5utst

define i64 @src(i64 %x) {
entry:
  %and1 = and i64 %x, 65535
  %cmp1 = icmp eq i64 %and1, 0
  %or1 = or disjoint i64 %x, 7
  %sel1 = select i1 %cmp1, i64 %or1, i64 %x
  %and2 = and i64 %sel1, 16776960
  %cmp2 = icmp eq i64 %and2, 0
  %or2 = or i64 %sel1, 7
  %sel2 = select i1 %cmp2, i64 %or2, i64 %sel1
  ret i64 %sel2
}

define i64 @tgt(i64 %x) {
  %and1 = and i64 %x, 65535
  %cmp1 = icmp eq i64 %and1, 0
  %or1 = or disjoint i64 %x, 7
  %sel1 = select i1 %cmp1, i64 %or1, i64 %x
  %and2 = and i64 %sel1, 16776960
  %cmp2 = icmp eq i64 %and2, 0
  %sel2 = select i1 %cmp2, i64 %or1, i64 %sel1
  ret i64 %sel2
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment