From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id A39733858C74; Thu, 29 Feb 2024 23:33:38 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A39733858C74 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1709249618; bh=YyDT/Z8QMUdl5KMvSks/XIwhOzji2Cjzk4m76cjUuZA=; h=From:To:Subject:Date:In-Reply-To:References:From; b=RRrXMnfW1uky8hisXUDAxdywpS44lUZEJuJ6/yEg1ZSf228jPMIBoEhAZmq76I75e JUV5pTIRiPPC1kqLjKUoJwiDD3bwijBXMCMvS3G7SHN2OKbR0toJM8kD+eQNA26F80 EIu6PhBz/uNMMeUr/VDlT2wPKgORGwXddXDxMJvY= From: "palmer at gcc dot gnu.org" To: gcc-bugs@gcc.gnu.org Subject: [Bug target/114175] [13/14] RISC-V: Execution test failures on gcc.dg/c23-stdarg-6.c Date: Thu, 29 Feb 2024 23:33:37 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: changed X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: target X-Bugzilla-Version: 14.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: palmer at gcc dot gnu.org X-Bugzilla-Status: NEW X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: Message-ID: In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D114175 --- Comment #17 from palmer at gcc dot gnu.org --- (In reply to Edwin Lu from comment #16) > (In reply to palmer from comment #15) > > It's a little easier to see from the float version of the code. > >=20 > > $ cat gcc/testsuite/gcc.dg/c23-stdarg-6.c=20 > > /* 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=3Dc23 -pedantic-errors" } */ > >=20 > > #include > > #include > >=20 > > extern void abort (void); > > extern void exit (int); > > struct s { char c[1000]; }; > >=20 > > struct s > > f (...) > > { > > va_list ap; > > va_start (ap); > > int r =3D va_arg (ap, double); > > va_end (ap); > > struct s ret =3D {}; > > ret.c[0] =3D r; > > ret.c[999] =3D 42; > > return ret; > > } > >=20 > > int > > main () > > { > > struct s x =3D f (1.0); > > fprintf(stderr, "%d\n", x.c[0]); > > if (x.c[0] !=3D 1) > > abort (); > > exit (0); > > } > > $ riscv64-unknown-linux-gnu-gcc gcc/testsuite/gcc.dg/c23-stdarg-6.c -o = test > > -std=3Dc2x -static -O3 > > $ riscv64-unknown-linux-gnu-objdump -d test > > ... > > 0000000000010412
: > > ... > > 1042e: 850a mv a0,sp > > ... > > 10438: 112000ef jal 1054a > > ... > > 000000000001054a : > > 1054a: f20507d3 fmv.d.x fa5,a0 > >=20 > > The psABI says > >=20 > > A callee with variadic arguments is responsible for copying the con= tents > > of registers used to pass variadic arguments to the vararg save are= a, > > which must be contiguous with arguments passed on the stack. > >=20 > > which I'm taking to mean the "1.0" is meant to be passed in a register.= It > > also says > >=20 > > 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 pass= ed by > > reference, the caller allocates memory for the return value, and pa= sses > > the address as an implicit first parameter. > >=20 >=20 > The psABI also says this in the paragraph before >=20 > In the base integer calling convention, variadic arguments are pass= ed=20 > in the same manner as named arguments, with one exception. Variadic= =20 > arguments with 2=C3=97XLEN-bit alignment and size at most 2=C3=97XL= EN bits are > passed in an aligned register pair (i.e., the first register in the > pair=20 > 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=20 > unused due to the aligned register pair rule). Edwin and I were talking in the office a bit before he posted this. My interpretation (and IIUC he agrees) is that this clause doesn't apply here:= the psABI says the return value is passed as if it was a named argument, so even though it's passed on the stack we should continue to pass small variadic arguments in registers. We should check with LLVM, though, just to make sure everyone is interpreti= ng things the same way. GCC is inconsistent between the caller and callee her= e, so we might as well match what LLVM is doing. > > So I think we're screwing up both ends of this one: the caller is passi= ng > > the return struct in a0 (losing the first arg), which the callee is > > obtaining the first argument from a0 (losing the return struct). > >=20 > > That all very much seems like a backend bug to me. >=20 > So if I understand correctly, there may also be a problem where it's tryi= ng > to create that named first argument but also trying to pass it as a varia= dic > argument. Ya, sounds like that could very likely be the source of the bug.=