Skip to content

Clang: Wrong assembly generated on Apple M1 processor for variadic ms_abi function with optimatilization -O0 #54079

Closed
@munzap

Description

@munzap

Hello,
We have issues with wrong assembly code generated. On Apple M1 processor for variadic ms_abi function with optimatilization -O0. Optimazation -O1 and above are fine.

clang version 15.0.0 (https://github.com/llvm/llvm-project.git 6cbf15e)
Target: x86_64-apple-darwin21.2.0

Example:

#include <stdio.h>
#include <stdarg.h>

void __attribute__((ms_abi)) Foo_va_list (int VaNum, const char  *Format, ...){
                __builtin_ms_va_list  Marker;
                long long    Value;

                __builtin_ms_va_start (Marker, Format);
                for (int i = 0; i < VaNum; i++ ) {
                                Value = __builtin_va_arg (Marker, int);
                                printf("Value = 0x%llx\n", Value);
                }
                __builtin_ms_va_end (Marker);
}

int main() {
  Foo_va_list (16, "0123456789abcdef= %x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x\n",0,1,2,3,4,5,6,7,8,9,0xa,0xb,0xc,0xd,0xe,0xf);
  return 0;
}

The first 6 argumets are stored to registers w2 - w7 but they are never retrieved at Foo_va_list with optimalization -O0 function as with optimalization -O1 . And output of program is wrong (start from argument value 6 instead of 0)

Output:
Value = 0x6
Value = 0x7
Value = 0x8
Value = 0x9
Value = 0xa
Value = 0xb
Value = 0xc
Value = 0xd
Value = 0xe
Value = 0xf
Value = 0x4637ea4
Value = 0x0
Value = 0x6b7cb8d0
Value = 0x49290f4
Value = 0x0
Value = 0x0

clang main.cpp -S -O0 -o -

_main:                                  ; @main
	.cfi_startproc
; %bb.0:
	sub	sp, sp, #112                    ; =112
	stp	x29, x30, [sp, #96]             ; 16-byte Folded Spill
	add	x29, sp, #96                    ; =96
	.cfi_def_cfa w29, 16
	.cfi_offset w30, -8
	.cfi_offset w29, -16
	**mov	w2, #0**
	stur	w2, [x29, #-8]                  ; 4-byte Folded Spill
	stur	wzr, [x29, #-4]
	mov	w0, #16
	adrp	x1, l_.str.1@PAGE
	add	x1, x1, l_.str.1@PAGEOFF
	**mov	w3, #1
	mov	w4, #2
	mov	w5, #3
	mov	w6, #4
	mov	w7, #5**
	mov	x9, sp

registers x2-x7 are newer retrieved.

__Z11Foo_va_listiPKcz:                  ; @_Z11Foo_va_listiPKcz
	.cfi_startproc
; %bb.0:
	sub	sp, sp, #64                     ; =64
	stp	x29, x30, [sp, #48]             ; 16-byte Folded Spill
	add	x29, sp, #48                    ; =48
	.cfi_def_cfa w29, 16
	.cfi_offset w30, -8
	.cfi_offset w29, -16
	stur	w0, [x29, #-4]
	stur	x1, [x29, #-16]
	add	x9, sp, #24                     ; =24
	add	x8, x29, #16                    ; =16
	str	x8, [x9]
	str	wzr, [sp, #12]
LBB0_1:                                 ; =>This Inner Loop Header: Depth=1
	ldr	w8, [sp, #12]
	ldur	w9, [x29, #-4]
	subs	w8, w8, w9
	b.ge	LBB0_4
; %bb.2:                                ;   in Loop: Header=BB0_1 Depth=1
	ldr	x9, [sp, #24]
	ldr	w8, [x9]
	add	x9, x9, #8                      ; =8
	str	x9, [sp, #24]
	str	w8, [sp, #8]
	ldrsw	x8, [sp, #8]
	str	x8, [sp, #16]
	ldr	x8, [sp, #16]
	adrp	x0, l_.str@PAGE
	add	x0, x0, l_.str@PAGEOFF
	mov	x9, sp
	str	x8, [x9]
	bl	_printf
; %bb.3:                                ;   in Loop: Header=BB0_1 Depth=1
	ldr	w8, [sp, #12]
	add	w8, w8, #1                      ; =1
	str	w8, [sp, #12]
	b	LBB0_1
LBB0_4:
	ldp	x29, x30, [sp, #48]             ; 16-byte Folded Reload
	add	sp, sp, #64                     ; =64
	ret
	.cfi_endproc
                 ; -- End function


main.txt
fail.O0.asm.txt
working.O1.asm.txt

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