From patchwork Sat Mar 16 07:23:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1912782 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=QNmQPwRr; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TxXgN2hQFz1yX0 for ; Sat, 16 Mar 2024 18:24:32 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 13516385ED4B for ; Sat, 16 Mar 2024 07:24:30 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 04C45385E037 for ; Sat, 16 Mar 2024 07:24:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 04C45385E037 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 04C45385E037 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710573850; cv=none; b=dIEm/ZGKEB0KL21NaAXs2aLiIso3R0+t6EJQAbKlJpT7DlFVf5FrAWvb/zWtawDk9fw3J0Zm46uiynXMH8yJDBSERZ4Ot/vps0SDJDPGU2IyZRR/u0TAZOMvHVDxsNhwNvrZVYmZI/WnfDttOoCLeMNh4b9D6eCaB+yHYwMmyKM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710573850; c=relaxed/simple; bh=U3GAX3/NDjcgM83bn7v5wJZh2FBmQ8gavWnBOoBdAJA=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=iHO88+TMW5z49zQFAsLugvolrJxb3KvQEQGuzDYVagjxAy9Qqkqvse/CNEo4bgWDWfAYZAczMfQTBQNdgR9uuACerhZzs3Exg0jnLQ/WByV5CuwFEjTf/tSGm9CJrE6FidZ+IlGara5xqcZ5CeiLsrS+3B7Fyb8V/XVpEl64/gw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1710573846; h=from:from:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type; bh=lhCkbvtbKYYflH+EcUb3AUOgnDm3ugFuCoqaXPc9Tbk=; b=QNmQPwRrvkZ25+9LtNQEm+nbjdAMwkOqPy1vgtmCItniV/SFmnZLni3VGW6vbCeSqZ0cVZ uQA9GZu7FLeE200eRvjFZOJqPdzDWspDkSF52t+uNayxJvdoX9T0yVGM3pxNqzDgUmQREm w8GiCs1Stm03nQviHBiq/ydJhV1LgYw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-628-ArtodtDxMw6gPfohMNncfA-1; Sat, 16 Mar 2024 03:24:03 -0400 X-MC-Unique: ArtodtDxMw6gPfohMNncfA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A201129AA3BF; Sat, 16 Mar 2024 07:24:02 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.57]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 63EE4C1576F; Sat, 16 Mar 2024 07:24:02 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 42G7Nt511944255 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Sat, 16 Mar 2024 08:23:56 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 42G7NsOR1944254; Sat, 16 Mar 2024 08:23:54 +0100 Date: Sat, 16 Mar 2024 08:23:54 +0100 From: Jakub Jelinek To: "Joseph S. Myers" , Richard Biener , Uros Bizjak Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] i386: Fix setup of incoming varargs for (...) functions which return large aggregates [PR114175] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-4.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Hi! The c23-stdarg-6.c testcase I've added recently apparently works fine with -O0 but aborts with -O1 and higher on x86_64-linux. The problem is in setup of incoming varargs. Like function.cc before r14-9249 even ix86_setup_incoming_varargs assumes that TYPE_NO_NAMED_ARGS_STDARG_P don't have any named arguments and there is nothing to advance, but that is not the case for (...) functions returning by hidden reference which have one such artificial argument. If the setup_incoming_varargs hook is called from the if (TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (fndecl)) && fnargs.is_empty ()) { struct assign_parm_data_one data = {}; assign_parms_setup_varargs (&all, &data, false); } spot, i.e. where there is no hidden return argument passed, arg.type is always NULL, while when it is called in the if (cfun->stdarg && !DECL_CHAIN (parm)) assign_parms_setup_varargs (&all, &data, false); spot, even when it is TYPE_NO_NAMED_ARGS_STDARG_P arg.type will be non-NULL. The tree-stdarg.cc pass in f in c23-stdarg-6.cc at -O1 or higher determines that va_arg is used on integral types at most twice (loads 2 words), and because ix86_setup_incoming_varargs doesn't advance, the code saves just the %rdi and %rsi registers to the save area. But that isn't correct, it should save %rsi and %rdx because %rdi is the hidden return argument. With -O0 tree-stdarg.cc doesn't attempt to optimize and we save all the registers, so it works fine in that case. Fixed thusly, bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? Now, I think we'll need the same fix also on aarch64, alpha, arc, csky, ia64, loongarch, mips, mmix, nios2, riscv, visium which have pretty much the similarly looking snippet in their hooks changed by the r13-3549 commit. Then arm, epiphany, fr30, frv, ft32, m32r, mcore, nds32, rs6000, sh have different changes but most likely need something similar too. I don't have access to most of those, could test aarch64 and rs6000 I guess. 2024-03-16 Jakub Jelinek PR target/114175 * config/i386/i386.cc (ix86_setup_incoming_varargs): Only skip ix86_function_arg_advance for TYPE_NO_NAMED_ARGS_STDARG_P functions if arg.type is NULL. * gcc.dg/c23-stdarg-7.c: New test. * gcc.dg/c23-stdarg-8.c: New test. Jakub --- gcc/config/i386/i386.cc.jj 2024-03-05 10:26:19.424029862 +0100 +++ gcc/config/i386/i386.cc 2024-03-15 23:50:08.821823213 +0100 @@ -4643,7 +4643,8 @@ ix86_setup_incoming_varargs (cumulative_ /* For varargs, we do not want to skip the dummy va_dcl argument. For stdargs, we do want to skip the last named argument. */ next_cum = *cum; - if (!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl)) + if ((!TYPE_NO_NAMED_ARGS_STDARG_P (TREE_TYPE (current_function_decl)) + || arg.type != NULL_TREE) && stdarg_p (fntype)) ix86_function_arg_advance (pack_cumulative_args (&next_cum), arg); --- gcc/testsuite/gcc.dg/c23-stdarg-7.c.jj 2024-03-15 23:50:56.857185518 +0100 +++ gcc/testsuite/gcc.dg/c23-stdarg-7.c 2024-03-15 23:50:45.611334813 +0100 @@ -0,0 +1,6 @@ +/* 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 "-O2 -std=c23 -pedantic-errors" } */ + +#include "c23-stdarg-4.c" --- gcc/testsuite/gcc.dg/c23-stdarg-8.c.jj 2024-03-15 23:51:20.814867467 +0100 +++ gcc/testsuite/gcc.dg/c23-stdarg-8.c 2024-03-15 23:51:06.973051224 +0100 @@ -0,0 +1,6 @@ +/* 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 "-O2 -std=c23 -pedantic-errors" } */ + +#include "c23-stdarg-6.c"