| Issue |
107819
|
| Summary |
[Clang] -O3 don't generate branch instruction
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
wooffie
|
Using -O3 optimization removes branch instruction and program gets segfault
```
clang version 18.1.8
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-pc-linux-gnu/14.2.1
Found candidate GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1
Selected GCC installation: /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/14.2.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Selected multilib: .;@m64
```
Problem occurs in ffmpeg project
Expand sections to see details about compilation
<details>
<summary>FFmpeg version/configure</summary>
git clone https://git.ffmpeg.org/ffmpeg.git --branch n4.4-dev --single-branch --depth=1
```
./ffmpeg/configure
--prefix=.
--enable-shared
--disable-static
--enable-pic
--disable-asm
--cc=clang
--cxx=clang++
--ld=clang++
--disable-stripping
--disable-inline-asm
--enable-version3
--enable-swscale
--enable-v4l2-m2m
--disable-outdev=fbdev
--disable-indev=fbdev
--disable-openssl
--disable-xmm-clobber-test
--disable-lto
--enable-debug=3
--disable-programs
--disable-postproc
--disable-doc
--disable-htmlpages
--disable-manpages
--disable-podpages
--disable-txtpages
--disable-sndio
--disable-schannel
--disable-securetransport
--disable-xlib
--disable-cuda
--disable-cuvid
--disable-nvenc
--disable-vaapi
--disable-vdpau
--disable-videotoolbox
--disable-audiotoolbox
--disable-appkit
--disable-alsa
--disable-cuda
--disable-cuvid
--disable-nvenc
--disable-vaapi
--disable-vdpau
--disable-sdl2
```
</details>
<details>
<summary>Compile options stored in ffbuild/config.mak - O3 optimization</summary>
```
CPPFLAGS= -D_ISOC99_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_POSIX_C_SOURCE=200112 -D_XOPEN_SOURCE=600 -DPIC -DZLIB_CONST
CFLAGS= -std=c11 -fomit-frame-pointer -fPIC -pthread -g3 -Wdeclaration-after-statement -Wall -Wdisabled-optimization -Wpointer-arith -Wredundant-decls -Wwrite-strings -Wtype-limits -Wundef -Wmissing-prototypes -Wno-pointer-to-int-cast -Wstrict-prototypes -Wempty-body -Wno-parentheses -Wno-switch -Wno-format-zero-length -Wno-pointer-sign -Wno-unused-const-variable -Wno-bool-operation -Wno-char-subscripts -O3 -fno-math-errno -fno-signed-zeros -mstack-alignment=16 -Qunused-arguments -Werror=implicit-function-declaration -Werror=missing-prototypes -Werror=return-type
CXXFLAGS= -D__STDC_CONSTANT_MACROS -std=c++11
```
</details>
Code fragment (ff_seek_frame_binary libavformat/utils.c:2155):
```
index = av_index_search_timestamp(st, target_ts,
flags & ~AVSEEK_FLAG_BACKWARD);
printf("index = %d\n", index);
printf("st->nb_index_entries = %d\n", st->nb_index_entries);
//expanded av_assert0(index < st->nb_index_entries);
do {
if (!(index < st->nb_index_entries)) {
av_log(NULL, AV_LOG_PANIC, "Assertion %s failed at %s:%d\n",
AV_STRINGIFY(index < st->nb_index_entries), __FILE__, __LINE__);
abort();
}
} while (0);
if (index >= 0)
printf("branch inside\n");
e = &st->index_entries[index]; // SEGFAULT
av_assert1(e->timestamp >= target_ts);
pos_max = e->pos;
ts_max = e->timestamp;
pos_limit = pos_max - e->min_distance;
av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
" dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
}
```
Index is local variable int type
Code output:
```
index = -1
st->nb_index_entries = 1
branch inside
```
Expected two branch instruction - for `index < st->nb_index_entries` and `index >= 0` but got only one:

`jge` to `ff_seek_frame_binary` is assert code. This part in disassembler:

Behavior with -O0 (manually changed in config.mak, another options same):
Code otput:
```
index = -1
st->nb_index_entries = 1
```
Asm:


Compiler with -O3 optimized out needed branch =)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs