llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

wrong code at -O1 on x86_64-linux-gnu

Open zhendongsu opened this issue 3 months ago • 11 comments

Compiler Explorer: https://godbolt.org/z/9PY637h41

Note:

  • fails: 16.0.0. to trunk
  • works: 15.0.0 and earlier
[621] % clangtk -v
clang version 24.0.0git (https://github.com/llvm/llvm-project.git f3bc5801e20ce00feb55313735d7ee6b4d62bb23)
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /local/home/suz/suz-local/software/local/clang-trunk/bin
Build config: +assertions
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/10
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/13
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/14
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/9
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/14
Candidate multilib: .;@m64
Selected multilib: .;@m64
[622] % 
[622] % clangtk -O0 small.c; ./a.out
[623] % 
[623] % clangtk -O1 small.c
[624] % ./a.out
Segmentation fault
struct a {
  int b;
  short c;
};
int *c, e, *f, g, k, m, b, d;
char h = -40, i, j, a;
short l = 3;
void q(char r) {
  c = &b;
  if (r >= 0) {
    c = 0;
    a = e;
  }
  d = *c;
}
int main() {
  f = &e;
  struct a n[2] = {{1, 2}, {9, -4209}};
  short *o;
  int *p;
al:
  if (l) {
    p = &m;
    l = 0;
    o = &n[1].c;
    if (!*p)
      goto al;
    p = &n[0].b + 3;
    *p = 0;
  }
  *o = 0;
  if (!~k)
    goto al;
  e = n[1].c;
  g = *f;
  i = h ^ g;
  j = -i;
  q(-j);
  return 0;
}

zhendongsu avatar Aug 18 '26 15:08 zhendongsu

id like to work on this

im-lunex avatar Aug 18 '26 16:08 im-lunex

this issue seems a bit confusing. so i've traced this quite a bit but here is what im confused about ->

  • first of all this felt like it was something wrong about LICM but actually the issue seems to happen because Alias analysis misses the shared memory.

  • while looking into it, i also noticed the original testcase appears to contain UB [ &n[0].b + 3 and then accessing a short object through an int* ]

now that, Im not sure whether this should be viewed as consequence of UB or something with current optimization pipeline or something related to SROA/TBAA/LICM...

im-lunex avatar Aug 19 '26 15:08 im-lunex

&n[0].b + 3 and then accessing a short object through an int* ]

I don't think this code is reachable. We always execute the goto. "undefined behavior" is only meaningful for code that actually executes at runtime.

Maybe some optimization is speculatively executing an undefined operation.

efriedma-quic avatar Aug 20 '26 16:08 efriedma-quic

@efriedma-quic soo here i gone deeper and the code is unreachable thats true.. so miscompile doesnt seem to come from executing that UB directly... what i got is that first thing that went bad is in LICM so lemme break it down,

before LICM, SROA narrows a store from i32 to i16 while preserving its original TBAA tag... that leaves two stores in the loop which can refer to the same memory, but carry different TBAA tags (int vs short). LICM then relies on the resulting alias analysis result and promotes the live store if i remove the dead path fully or remove the TBAA from the dead-path store, LICM no longer performs the promotion and the miscompile is gone..

now in this point im not sure about the intended behavior: when SROA narrows a store, is preserving the original scalar TBAA expected, or should the metadata be adjusted to avoid this situation?

im-lunex avatar Aug 20 '26 18:08 im-lunex

@dtcxzyw at this point i need a guide to move forward with a decision soo it would be great if you give this a check.. THANKS

and im new to this repo and can make dumb mistakes hope so you dont mind.. and apology if i have overlooked something obvious...

im-lunex avatar Aug 20 '26 18:08 im-lunex

The reproducer reported in https://github.com/dtcxzyw/llvm-autoreduce/issues/320 is incorrect. The out-of-tree llubi treats storing poison to memory as an immediate UB. You can try to use the in-tree llubi to get the right reproducer first.

dtcxzyw avatar Aug 20 '26 18:08 dtcxzyw

The reproducer reported in dtcxzyw/llvm-autoreduce#320 is incorrect. The out-of-tree llubi treats storing poison to memory as an immediate UB. You can try to use the in-tree llubi to get the right reproducer first.

okay hope it will be helpful thanks..

im-lunex avatar Aug 20 '26 18:08 im-lunex

when SROA narrows a store, is preserving the original scalar TBAA expected

This is fine.

There is no such thing as correct TBAA for memory operations in dead code. Dead code never executes, so any TBAA marking on a dead load or store is vacuously correct.

efriedma-quic avatar Aug 20 '26 19:08 efriedma-quic

@efriedma-quic okay.. now the issue is there are 3 suspects TBAA, The IR/metadata, LICM. so evrytime im facing the issue like in those what exactly which should be fixed, to make a decision i need to know that weather, should TBAA's NoAlias result be considered valid or not? - this would help me a lot to narrow down the investigation part...

im-lunex avatar Aug 21 '26 05:08 im-lunex

This looks familiar. I think the root cause here is https://github.com/llvm/llvm-project/issues/60860#issuecomment-1474006326.

We can't blindly use AA metadata in LICM scalar promotion, because the metadata is only meaningful if actually executed. We end up concluding that accesses don't alias based on metadata that is never executed.

nikic avatar Aug 21 '26 10:08 nikic

@nikic yeah this issue seems deeper than expected someone more experienced than me.. like you should handle this...

im-lunex avatar Aug 21 '26 13:08 im-lunex