llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.32k stars 11.69k forks source link

Under the O2 optimization level, compiler_rt has an error about instcombine optimization #63525

Open zhusihang opened 1 year ago

zhusihang commented 1 year ago

When compiling compiler-rt/lib/profile/InstrProfilingMerge.c:125:20, bad instruction folding by foldGEPICmp during instcombine.

 origin instruction:
  %cmp = icmp ult ptr %add.ptr6, %add.ptr4, !dbg !153
  new instruction after foldGEPICmp:
  <badref> = icmp slt i64 %mul, 0
 which corresponds to code or function is:
  if (SrcNameStart < SrcCountersStart)
                   ^

Unsigned comparison (aka. SrcName Start < Src Counter Start) becomes signed comparison (aka. Header->CountersSize * __llvm_profile_counter_entry_size() < 0). The original overflow judgment becomes a comparison of the second parameter of the gep instruction with 0. The result is always false, causing the corresponding overflow judgment semantics to fail.

nikic commented 1 year ago

This is a bug in compiler-rt. Overflow in pointer arithmetic is undefined behavior, so it's not surprising the overflow check gets optimized away.

zhusihang commented 1 year ago

This is a bug in compiler-rt. Overflow in pointer arithmetic is undefined behavior, so it's not surprising the overflow check gets optimized away.

So is there any recommended solution for debugging? In addition to compiler_rt, there are 50 such scenes in libcxx and libcxxabi