Skip to content

RISCV64 miscompile at -O2/-O1 #80052

Closed
@patrick-rivos

Description

@patrick-rivos

Testcase:

int printf(const char *, ...);
int a[10000];
int b, c, d, e, f;
char g, h;
int j[100][100];
int m(long n) {
  b = ((b >> 4) ^ (b ^ n));
  b = ((b >> 4)) ^ a[(b ^ 0x0f) & 0x0F];
  b = ((b >> 4)) ^ a[(b ^ 0x0f) & 0x0F];
  b = ((b >> 4)) ^ a[(b ^ 0x0f) & 0x0F];
  printf("%s %lX\n", &g, b ^ 0xFFFFFFFFUL);
  return 0;
}
int main() {
  int i = 0;
  for (; c < 256; c++)
    a[c] = c;
  volatile int k = 3;
  h = 0;
  for (; (h <= 4); h += 1) {
    int *l = &d;
    *l = k;
    k = 0;
  }
  for (; i < 6; i++)
    for (; e < 1; e++) {
      m(j[i][e]);
      if (f)
        printf("%d%d\n", i, e);
    }
  return m(d);
}

Commands:

> /scratch/tc-testing/llvm-jan-24/build/bin/clang -O2 red.c -o user-config-O2.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 user-config-O2.out
 FFFFFFF0
 FFFFFFFC
> /scratch/tc-testing/llvm-jan-24/build/bin/clang -O0 red.c -o user-config-O0.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 user-config-O0.out
 FFFFFFF0
 FFFFFFFF

This raw testcase differs at -O2, but after feeding it through bugpoint, I got a .bc file that differs at -01.

> /scratch/tc-testing/llvm-jan-24/build/bin/clang red.c -O2 -o user-config.bc -emit-llvm -c
> /scratch/tc-testing/llvm-jan-24/build/build-llvm-linux/bin/bugpoint user-config.bc --run-llc
> /scratch/tc-testing/llvm-jan-24/build/build-llvm-linux/bin/clang bugpoint-reduced-named-md.bc -O0 -o bugpoint-O0.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 bugpoint-O0.out
 FFFFFFF0
 FFFFFFFF
> /scratch/tc-testing/llvm-jan-24/build/build-llvm-linux/bin/clang bugpoint-reduced-named-md.bc -O1 -o bugpoint-O1.out
> QEMU_CPU=rv64 /scratch/tc-testing/llvm-jan-24/build/bin/qemu-riscv64 bugpoint-O1.out
 FFFFFFF0
 FFFFFFFC

bugpoint.zip

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions