https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114175
--- Comment #16 from Edwin Lu <ewlu at rivosinc dot com> --- (In reply to palmer from comment #15) > It's a little easier to see from the float version of the code. > > $ cat gcc/testsuite/gcc.dg/c23-stdarg-6.c > /* Test C23 variadic functions with no named parameters, or last named > parameter with a declaration not allowed in C17. Execution tests. */ > /* { dg-do run } */ > /* { dg-options "-std=c23 -pedantic-errors" } */ > > #include <stdarg.h> > #include <stdio.h> > > extern void abort (void); > extern void exit (int); > struct s { char c[1000]; }; > > struct s > f (...) > { > va_list ap; > va_start (ap); > int r = va_arg (ap, double); > va_end (ap); > struct s ret = {}; > ret.c[0] = r; > ret.c[999] = 42; > return ret; > } > > int > main () > { > struct s x = f (1.0); > fprintf(stderr, "%d\n", x.c[0]); > if (x.c[0] != 1) > abort (); > exit (0); > } > $ riscv64-unknown-linux-gnu-gcc gcc/testsuite/gcc.dg/c23-stdarg-6.c -o test > -std=c2x -static -O3 > $ riscv64-unknown-linux-gnu-objdump -d test > ... > 0000000000010412 <main>: > ... > 1042e: 850a mv a0,sp > ... > 10438: 112000ef jal 1054a <f> > ... > 000000000001054a <f>: > 1054a: f20507d3 fmv.d.x fa5,a0 > > The psABI says > > A callee with variadic arguments is responsible for copying the contents > of registers used to pass variadic arguments to the vararg save area, > which must be contiguous with arguments passed on the stack. > > which I'm taking to mean the "1.0" is meant to be passed in a register. It > also says > > Values are returned in the same manner as a first named argument of the > same type would be passed. If such an argument would have been passed by > reference, the caller allocates memory for the return value, and passes > the address as an implicit first parameter. > The psABI also says this in the paragraph before In the base integer calling convention, variadic arguments are passed in the same manner as named arguments, with one exception. Variadic arguments with 2×XLEN-bit alignment and size at most 2×XLEN bits are passed in an aligned register pair (i.e., the first register in the pair is even-numbered), or on the stack by value if none is available. After a variadic argument has been passed on the stack, all future arguments will also be passed on the stack (i.e. the last argument register may be left unused due to the aligned register pair rule). > So I think we're screwing up both ends of this one: the caller is passing > the return struct in a0 (losing the first arg), which the callee is > obtaining the first argument from a0 (losing the return struct). > > That all very much seems like a backend bug to me. So if I understand correctly, there may also be a problem where it's trying to create that named first argument but also trying to pass it as a variadic argument.