From patchwork Sat Jan 7 15:46:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Koenig X-Patchwork-Id: 40414 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1350493wrt; Sat, 7 Jan 2023 07:47:30 -0800 (PST) X-Google-Smtp-Source: AMrXdXv9C3BBNNnO9KPl1EmTwoJvw2NUit2UckeYyI0MTWnl5Kph27H53WrNOc85AqrGU4GpAJd9 X-Received: by 2002:a17:906:22d2:b0:7fc:3787:243d with SMTP id q18-20020a17090622d200b007fc3787243dmr53113055eja.53.1673106450719; Sat, 07 Jan 2023 07:47:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1673106450; cv=none; d=google.com; s=arc-20160816; b=Rusc05FAAYLNXOieoQu9f9/fpgvFZcM8rCPEQ3+szfZImJyDi36zveE4lWDEVF7YI2 fJfJF0eHkryyHxHA2LeBuApt3Uh8Md6tEni9UTjhfq535vVr0a+3SiH9wK+aJo0Rd+Wt ysBSQ8P95ln9X30KbMRSjMZdCCmwvGByMmqDu173DR2KKpbgKU7WOjOq+7slasvRfwhI d3IFXs5A61dFCeO3MTerJ27xw0b46yfp48rP2BuDjsAsNJIhXfjW2qIFFsBNAaB9qdeF IDIlJuM7HaDYWkhO7IELIc9GcpwUooQI3SgVoeEBvvPthdH+nFGRCWn7qFfdgzxXwF++ FsXw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :content-language:user-agent:mime-version:date:message-id :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=GxRYEtogUbXKEGdBGpTy1cR877baxicUpDmPmoPo+gA=; b=hMa5C1//A99Y8dDoNivnZjc1Hm3Wx7R0TATYo4gYYQcMkrHBUgqlvXGwUWgsqUWYa8 rz2RMwq7M5XbBKfQX6erays2dhU+9X0hJNBuNXTCSwDYqAZqOhe2sYxk7HT2PrOqTDDI fU0+CSG17qeHRWTcqxAwfi2/bpZyWwAXhdhzxz3wpMBnmoYozKi6x1MiTv0HJeJrIFmv L34qdUons6jV85R/tF//eGQ7dJemIFNDC4WH/fiwXKZh2t0i8xlznmnNEtjnYFyEPWxL G1PD5LrWqTWUjYfra+a+imhDXXOauHQYYRUJyz8dZTBlPerrHfYFeLotoq8FI3FRrnaq RoDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=xwEb2m0+; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id j8-20020a50ed08000000b0046775f92f19si3800811eds.50.2023.01.07.07.47.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jan 2023 07:47:30 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=xwEb2m0+; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8542B3858D39 for ; Sat, 7 Jan 2023 15:47:29 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8542B3858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673106449; bh=GxRYEtogUbXKEGdBGpTy1cR877baxicUpDmPmoPo+gA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=xwEb2m0+qBG/O85+kEUbO70ficUrKSRbcsRu2VN5/YGke3ZPdJOcM9Ue9XRMLVQ3v MdYNcFROAQOUQ3QLqo0Yj5pKa/7GxfBCl/GyWu9R4qO6iWkraIa1Xu9lawqO87DRL5 /2Hd8hmUIiYQGri26vDh6E4INVGL8zkDcZixQLHg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from cc-smtpout2.netcologne.de (cc-smtpout2.netcologne.de [IPv6:2001:4dd0:100:1062:25:2:0:2]) by sourceware.org (Postfix) with ESMTPS id 3B522385841E; Sat, 7 Jan 2023 15:46:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3B522385841E Received: from cc-smtpin1.netcologne.de (cc-smtpin1.netcologne.de [89.1.8.201]) by cc-smtpout2.netcologne.de (Postfix) with ESMTP id A0140125B8; Sat, 7 Jan 2023 16:46:21 +0100 (CET) Received: from [IPV6:2001:4dd7:6842:0:7285:c2ff:fe6c:992d] (2001-4dd7-6842-0-7285-c2ff-fe6c-992d.ipv6dyn.netcologne.de [IPv6:2001:4dd7:6842:0:7285:c2ff:fe6c:992d]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by cc-smtpin1.netcologne.de (Postfix) with ESMTPSA id 64F6811D86; Sat, 7 Jan 2023 16:46:20 +0100 (CET) Message-ID: <7bd3545a-7b9d-a9b2-6923-0d02df809177@netcologne.de> Date: Sat, 7 Jan 2023 16:46:20 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Content-Language: en-US To: "fortran@gcc.gnu.org" , gcc-patches Subject: [patch, fortran] Fix common subexpression elimination with IEEE rounding (PR108329) X-NetCologne-Spam: L X-Rspamd-Queue-Id: 64F6811D86 X-Spamd-Bar: + X-Rspamd-Pre-Result: action=add header; module=force_actions; unknown reason X-Rspamd-Action: add header X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Thomas Koenig via Gcc-patches From: Thomas Koenig Reply-To: Thomas Koenig Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1754379269686589973?= X-GMAIL-MSGID: =?utf-8?q?1754379269686589973?= Hello world, this patch fixes Fortran's handling of common subexpression elimination across ieee_set_rouding_mode calls. It does so using a rather big hammer, by issuing a memory barrier to force reload from memory (and thus a recomputation). This is a rather big hammer, so if there are more elegant ways to fix it, I am very much open to suggestions. If PR 34678 is fixed, then this solution can also be applied here. OK for trunk? How do you feel about a backport? Best regards Thomas Add memory barrier for calls to ieee_set_rounding_mode. gcc/fortran/ChangeLog: PR fortran/108329 * trans-expr.cc (trans_memory_barrier): New functions. (gfc_conv_procedure_call): Insert memory barrier for ieee_set_rounding_mode. gcc/testsuite/ChangeLog: PR fortran/108329 * gfortran.dg/rounding_4.f90: New test. diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 4f3ae82d39c..29be7804e11 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -5981,6 +5981,20 @@ post_call: gfc_add_block_to_block (&parmse->post, &block); } +/* Helper function - generate a memory barrier. */ + +static tree +trans_memory_barrier (void) +{ + tree tmp; + + tmp = gfc_build_string_const (sizeof ("memory"), "memory"); + tmp = build5_loc (input_location, ASM_EXPR, void_type_node, + gfc_build_string_const (1, ""), NULL_TREE, NULL_TREE, + tree_cons (NULL_TREE, tmp, NULL_TREE), NULL_TREE); + ASM_VOLATILE_P (tmp) = 1; + return tmp; +} /* Generate code for a procedure call. Note can return se->post != NULL. If se->direct_byref is set then se->expr contains the return parameter. @@ -7692,6 +7706,19 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, else conv_base_obj_fcn_val (se, base_object, expr); + /* FIXME: Special handing of ieee_set_rounding_mode - we clobber + memory here to avoid common subexpression moving code past calls + to ieee_set_rounding_mode. This should only be done for + floating point, but currently gcc offers no other possibility. + See PR 108329. */ + + if (sym->from_intmod == INTMOD_IEEE_ARITHMETIC + && strcmp (sym->name, "ieee_set_rounding_mode") == 0) + { + tree tmp = trans_memory_barrier (); + gfc_add_expr_to_block (&post, tmp); + } + /* If there are alternate return labels, function type should be integer. Can't modify the type in place though, since it can be shared with other functions. For dummy arguments, the typing is done to diff --git a/gcc/testsuite/gfortran.dg/rounding_4.f90 b/gcc/testsuite/gfortran.dg/rounding_4.f90 new file mode 100644 index 00000000000..e8799da67dc --- /dev/null +++ b/gcc/testsuite/gfortran.dg/rounding_4.f90 @@ -0,0 +1,31 @@ +! { dg-do run } +module y + implicit none + integer, parameter :: wp = selected_real_kind(15) +contains + subroutine foo(a,b,c) + use ieee_arithmetic + real(kind=wp), dimension(4), intent(out) :: a + real(kind=wp), intent(in) :: b, c + type (ieee_round_type), dimension(4), parameter :: mode = & + [ieee_nearest, ieee_to_zero, ieee_up, ieee_down] + call ieee_set_rounding_mode (mode(1)) + a(1) = b + c + call ieee_set_rounding_mode (mode(2)) + a(2) = b + c + call ieee_set_rounding_mode (mode(3)) + a(3) = b + c + call ieee_set_rounding_mode (mode(4)) + a(4) = b + c + end subroutine foo +end module y + +program main + use y + real(kind=wp), dimension(4) :: a + call foo(a,0.1_wp,0.2_wp) + if (a(1) <= a(2)) stop 1 + if (a(3) <= a(4)) stop 2 + if (a(1) /= a(3)) stop 3 + if (a(2) /= a(4)) stop 4 +end program main