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

condition jump instruction seems lost when optimization opened with -O1 or -O2 or -Oz #82744

Open
haifengdeng opened this issue Feb 23, 2024 · 1 comment

Comments

@haifengdeng
Copy link

haifengdeng commented Feb 23, 2024

when i use below command to compile FFmpeg 4.3.1 libavformat/utils.c with Clang 14 on Macmini M1 macOS 14.3.1:

/opt/homebrew/opt/llvm@14/bin/clang -I. -I./ --sysroot=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk -D_ISOC99_SOURCE -D_LARGEFILE_SOURCE -I./compat/dispatch_semaphore -DPIC -DZLIB_CONST -DHAVE_AV_CONFIG_H -DBUILDING_avformat -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS17.2.sdk -arch arm64 -mios-version-min=11.0 -no-canonical-prefixes -Wno-invalid-command-line-argument -Wno-unused-command-line-argument -funwind-tables -fstack-protector-strong -fno-addrsig -Werror=return-type -Werror=int-to-pointer-cast -Werror=implicit-function-declaration -fPIC -DTARGET_OS_IPHONE=1 -Wall -pipe -std=c99 -ffast-math -g -O0 -DDEBUG_ENABLE -I/Users/yue/gitlab/libaveditor_6/buildbin_ios/tmp/arm64/include -std=c11 -Werror=partial-availability -fomit-frame-pointer -fPIC -pthread  -DX264_API_IMPORTS -I/opt/homebrew/Cellar/x264/r3108/include -ggdb -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 -Oz -fno-math-errno -fno-signed-zeros -mstack-alignment=16 -Qunused-arguments -Werror=implicit-function-declaration -Werror=missing-prototypes -Werror=return-type  -MMD -MF libavformat/utils.d -MT libavformat/utils.o -c -o libavformat/utils.o libavformat/utils.c

then I disassembe like below:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/objdump -S libavformat/utils.o > utils14.txt

I found code if (st->index_entries) { in function ff_seek_frame_binary compiled like below, when I run to here, it always crashes:

;     av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
    2b00: 4f00e400     	movi.16b	v0, #0
    2b04: ad0403e0     	stp	q0, q0, [sp, #128]
    2b08: 910203f4     	add	x20, sp, #128
    2b0c: 910203e0     	add	x0, sp, #128
    2b10: aa0203e1     	mov	x1, x2
    2b14: 94000000     	bl	0x2b14 <_ff_seek_frame_binary+0x5c>
    2b18: a90053f7     	stp	x23, x20, [sp]
    2b1c: 90000002     	adrp	x2, 0x2000 <_ff_seek_frame_binary+0x64>
    2b20: 91000042     	add	x2, x2, #0
    2b24: 94000000     	bl	0x2b24 <_ff_seek_frame_binary+0x6c>
;     st = s->streams[stream_index];
    2b28: f9401a68     	ldr	x8, [x19, #48]
    2b2c: f8775914     	ldr	x20, [x8, w23, uxtw  #3]
**;     if (st->index_entries) {
    2b30: f940e69a     	ldr	x26, [x20, #456]**
;                                           flags | AVSEEK_FLAG_BACKWARD);
    2b34: 320002c2     	orr	w2, w22, #0x1
    2b38: 94000000     	bl	0x2b38 <_ff_seek_frame_binary+0x80>
;         index = FFMAX(index, 0);
    2b3c: 0aa07c08     	bic	w8, w0, w0, asr #31
;         if (e->timestamp <= target_ts || e->pos == e->min_distance) {
    2b40: 52800309     	mov	w9, #24
    2b44: 9ba96909     	umaddl	x9, w8, w9, x26
    2b48: a9406139     	ldp	x25, x24, [x9]
    2b4c: eb15031f     	cmp	x24, x21
    2b50: 540000cd     	b.le	0x2b68 <_ff_seek_frame_binary+0xb0>
    2b54: 52800309     	mov	w9, #24
;         if (e->timestamp <= target_ts || e->pos == e->min_distance) {

but when I use Clang 13 with same the compile command, it generates the code below:

;     av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
    2b14: 4f00e400     	movi.16b	v0, #0
    2b18: ad0383e0     	stp	q0, q0, [sp, #112]
    2b1c: 9101c3f4     	add	x20, sp, #112
    2b20: 9101c3e0     	add	x0, sp, #112
    2b24: aa0203e1     	mov	x1, x2
    2b28: 94000000     	bl	0x2b28 <_ff_seek_frame_binary+0x58>
    2b2c: a90053f7     	stp	x23, x20, [sp]
    2b30: 90000002     	adrp	x2, 0x2000 <_ff_seek_frame_binary+0x60>
    2b34: 91000042     	add	x2, x2, #0
    2b38: 94000000     	bl	0x2b38 <_ff_seek_frame_binary+0x68>
;     st = s->streams[stream_index];
    2b3c: f9401a68     	ldr	x8, [x19, #48]
    2b40: f8775914     	ldr	x20, [x8, w23, uxtw  #3]
**;     if (st->index_entries) {
    2b44: f940e69a     	ldr	x26, [x20, #456]
    2b48: b400033a     	cbz	x26, 0x2bac <_ff_seek_frame_binary+0xdc>**
;                                           flags | AVSEEK_FLAG_BACKWARD);
    2b4c: 320002c2     	orr	w2, w22, #0x1
    2b50: 94000000     	bl	0x2b50 <_ff_seek_frame_binary+0x80>
;         index = FFMAX(index, 0);
    2b54: 0aa07c08     	bic	w8, w0, w0, asr #31
;         if (e->timestamp <= target_ts || e->pos == e->min_distance) {

someone can help me find if my compile command has an error somewhere?

utils.zip

@dmitrys99
Copy link

I found similar issue https://gitlab.com/embeddable-common-lisp/ecl/-/issues/737 and can confirm problem exists.

cl_object si_make_seq_iterator_va(cl_narg narg, ...)
{
 cl_object x[2];
 va_list args; va_start(args,narg);
 x[0] = va_arg(args,cl_object);
 for (int i = 1; i < narg; i++){
  x[i] = va_arg(args,cl_object);
 }
 va_end(args);
 return si_make_seq_iterator(narg, x[0], x[1]);
}

When -O2 or -O3 flag given, loop condition silently skipped and loop body is executed yet it shouldn't.

Without optimization things work correctly.

Broken function

si_make_seq_iterator_va:
	sub    sp, sp, #0x40            
	stp    x29, x30, [sp, #0x30]    
	add    x29, sp, #0x30           
	adrp   x8, 642                  
	ldr    x8, [x8, #0x58]          
	ldr    x8, [x8]                 
	stur   x8, [x29, #-0x8]         
	add    x8, x29, #0x10           
	orr    x8, x8, #0x8             
	str    x8, [sp, #0x10]          
	ldr    x1, [x29, #0x10]         
	mov    w8, #0x1                 
	add    x9, sp, #0x18            
	ldr    x10, [sp, #0x10]         
	add    x11, x10, #0x8           
	str    x11, [sp, #0x10]         
	ldr    x10, [x10]               
	str    x10, [x9, x8, lsl #3]    
	add    x8, x8, #0x1             
	cmp    x0, x8                   
	b.ne   0x1ef24                    ; <+52> at seq.c:480:10
	ldr    x8, [sp, #0x20]          
	str    x8, [sp]                 
	bl     0x1ed80                    ; si_make_seq_iterator at seq.c:404
	ldur   x8, [x29, #-0x8]         
	adrp   x9, 642                  
	ldr    x9, [x9, #0x58]          
	ldr    x9, [x9]                 
	cmp    x9, x8                   
	b.ne   0x1ef74                    ; <+132> at seq.c
	ldp    x29, x30, [sp, #0x30]    
	add    sp, sp, #0x40            
	ret                             
	bl     0x1947e4                   ; symbol stub for: __stack_chk_fail

Correct function

si_make_seq_iterator_va:
	sub    sp, sp, #0x60            
	stp    x29, x30, [sp, #0x50]    
	add    x29, sp, #0x50           
	adrp   x8, 985                  
	ldr    x8, [x8, #0x60]          
	ldr    x8, [x8]                 
	stur   x8, [x29, #-0x8]         
	stur   x0, [x29, #-0x20]        
	add    x9, sp, #0x28            
	add    x8, x29, #0x10           
	str    x8, [x9]                 
	ldr    x9, [sp, #0x28]          
	ldr    x8, [x9]                 
	add    x9, x9, #0x8             
	str    x9, [sp, #0x28]          
	str    x8, [sp, #0x20]          
	ldr    x8, [sp, #0x20]          
	stur   x8, [x29, #-0x18]        
	mov    w8, #0x1                 
	str    w8, [sp, #0x1c]          
	b      0x3f61c                    ; <+84> at seq.c:479:18
	ldrsw  x8, [sp, #0x1c]          
	ldur   x9, [x29, #-0x20]        
	subs   x8, x8, x9               
	cset   w8, ge                   
	tbnz   w8, #0x0, 0x3f66c          ; <+164> at seq.c:483:30
	b      0x3f634                    ; <+108> at seq.c:480:10
	ldr    x9, [sp, #0x28]          
	ldr    x8, [x9]                 
	add    x9, x9, #0x8             
	str    x9, [sp, #0x28]          
	str    x8, [sp, #0x10]          
	ldr    x8, [sp, #0x10]          
	ldrsw  x10, [sp, #0x1c]         
	sub    x9, x29, #0x18           
	str    x8, [x9, x10, lsl #3]    
	b      0x3f65c                    ; <+148> at seq.c:479:29
	ldr    w8, [sp, #0x1c]          
	add    w8, w8, #0x1             
	str    w8, [sp, #0x1c]          
	b      0x3f61c                    ; <+84> at seq.c:479:18
	ldur   x0, [x29, #-0x20]        
	ldur   x1, [x29, #-0x18]        
	ldur   x8, [x29, #-0x10]        
	mov    x9, sp                   
	str    x8, [x9]                 
	bl     0x3f228                    ; si_make_seq_iterator at seq.c:404
	str    x0, [sp, #0x8]           
	ldur   x9, [x29, #-0x8]         
	adrp   x8, 985                  
	ldr    x8, [x8, #0x60]          
	ldr    x8, [x8]                 
	subs   x8, x8, x9               
	cset   w8, eq                   
	tbnz   w8, #0x0, 0x3f6ac          ; <+228> at seq.c
	b      0x3f6a8                    ; <+224> at seq.c:483:2
	bl     0x30e424                   ; symbol stub for: __stack_chk_fail
	ldr    x0, [sp, #0x8]           
	ldp    x29, x30, [sp, #0x50]    
	add    sp, sp, #0x60            
	ret       

Checked with latest git version of LLVM/Clang.

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

No branches or pull requests

3 participants