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: 1722720 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=MPlqgAQE; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Nq4Mg1mjSz23fc for ; Sun, 8 Jan 2023 02:47:11 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 305E43858D39 for ; Sat, 7 Jan 2023 15:47:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 305E43858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1673106426; bh=GxRYEtogUbXKEGdBGpTy1cR877baxicUpDmPmoPo+gA=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=MPlqgAQEQZEuHM925+QoQmF0ILshQCfqwdO1ETyFAuiY3FP+iN0Dn5zKPqFcQY1q+ 8t0Tg/ytX8BoRA9Ftm3YcGo93adGRAa/cYb3H+/ba0urfS58UsL54Ya4byHoS8FZHf /CL7YPqRktaVgWvMGnTWIUyd5+QNqR//PhPMY93c= 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" 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