From patchwork Tue Nov 15 20:45:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 20553 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2933141wru; Tue, 15 Nov 2022 12:45:53 -0800 (PST) X-Google-Smtp-Source: AA0mqf6ZSv/QKlwlvpIsrGUE8WD3l6/Nf7mxIC6FiskoJAZatsIZLZME436H76FV2us8zZ5djKbN X-Received: by 2002:a17:906:eb5b:b0:78d:47c8:e80f with SMTP id mc27-20020a170906eb5b00b0078d47c8e80fmr15172750ejb.700.1668545153617; Tue, 15 Nov 2022 12:45:53 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668545153; cv=none; d=google.com; s=arc-20160816; b=OlVNOVuuz9kdqjyh6j/1Pn9VkYEEp1UCksNY8FEj0cV1NJV9BnGjbEiibrPABV7NF2 EO2kiKVpadugDg/APH/bH5awYWrrN9GVArXQmW/R7ZwkFTNE9sKHZdQGG5Rg0A9VB+V4 uH7LxXEl0XKH9Yahldy1oZMc5oPUzzYxAZd+YfV0tGU2mozEJsYUAEgrv5Wd6HuNSj9Z O2ktf3HzSCeniM70QRPI0K/30OIDU7ZJCdzeWY6BQR1YPiD1EY4CL3MvfQhg3cmKYXq1 Gizzt4kVn9GTYEZoOYhrsLEgrCjrDt+iSAvQyyTm9oDQ50k6y9P0SNfci/qIhkRaycD5 DraQ== 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:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=XRHDESSiV3/mfsGSQ1Jy2M07L18irJO+IzVTieJ6Ek0=; b=uF4bGbSwEHq1pqRXKrCo0srYDvkzhwOeq+D/6E9e+XX6eFBeiqjKW98hC8gidlhFQD ZK0aRtofVa61hl7rSSxqhJtTrJw566nuEY+W2byg+v0wqDVhBZYAncz9XVjlhoQ/xU81 4vFIJJLyHkNKcxLP1tQ3/eIDci8aQqHRRzrBamNF9Fk5MGy63VYhljPEmH1t/AqlRSaA AmMzqn2D+m7U9dXjGkNQak4kTqHNYHd742fuYQlUsOKh5dZz9bpro56uWiA6623zY4OM YIVakbiR9WNCN3ZGELlc3/PZub6v2qN80+I2g8zNJg8cJiLhC/1Gt95jDMg87DMJE9FW gouQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=uHrQ7qpx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id ga5-20020a1709070c0500b0079d5da30399si13428672ejc.427.2022.11.15.12.45.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 15 Nov 2022 12:45:53 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=uHrQ7qpx; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 7A46F3939C30 for ; Tue, 15 Nov 2022 20:45:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7A46F3939C30 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1668545152; bh=XRHDESSiV3/mfsGSQ1Jy2M07L18irJO+IzVTieJ6Ek0=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=uHrQ7qpxru4SCnsGbz6Mfe3fzbJj/FMbBVqeP6zBJ0sQ2H2idy8z/l80KYcZPuAMz 0lCfN7yCTv0GsRstu+hrCQw12ovqGcoZFg+plUu4KVBZqais/UqNAPpKB3hvIJGLgp h4kjmspsl5zMVnuxNuqFtCA9gEZEZ0gCTncFv7qA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by sourceware.org (Postfix) with ESMTPS id 9C3F3392AC30; Tue, 15 Nov 2022 20:45:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9C3F3392AC30 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [93.207.83.65] ([93.207.83.65]) by web-mail.gmx.net (3c-app-gmx-bs61.server.lan [172.19.170.145]) (via HTTP); Tue, 15 Nov 2022 21:45:04 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: ICE in simplification of array expression involving power [PR107680] Date: Tue, 15 Nov 2022 21:45:04 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:b2hAkHp5IDhCtb6m2R+WtJgclUmSWDEFtvpA81YDOWK6cAXv/CwemKxvhnHykmJIeOaTa KT3uuHQFXzoUk/Hv8aMHBCEhf5ge3E6UwFyELBDLSpuwI3XJCKbiE2KfGl84n1bOhSVHsNIt0jxg 0ixNntbQpYOnR/1+TQEx2OUHmrJI+RhhLmXGhZv2etvfslFgmCU1YeiRn31GjdFyzjCaokSYpSE7 aNB3qyDH1nz0HxnIds7Sjhwxtpqud3iEaO0xKupdgxP86MEj+O3w1PciWQU+lS2d9BtRkPxNoh6P IA= UI-OutboundReport: notjunk:1;M01:P0:BJJhoepAvt4=;GL5RZpCNaGsHzEVoIajTEdVOLmO 3CT9AgtbOxzi4HTrA5wI1r/HkVOKBw38SAFGd82rAVALSBQiRWvfAneiBE5BgLbfJSwFsBqn/ Jjt9d78MowFxnt4a5UmXxSWTbp2bazJvBfTPVe68ctyL3eEfbc+ZD1rD6KcTE9XpU9ldUaRV4 hRHDSIyx0GePUDguo00R+9Hk9uyH3kAxGJtrQvgc6xrGUUJUD/UWcgJn80fFIrD9HFVB7Pitn WNbkJP1UGcAp6n7q8LC4U2jor8HTAKc443n1Z1ss+i2McA4i0iH87G5tMKlCMtl26xIqhX/6z EtTsIjQ/aEDrO0Oh5Wgf7lV9g2WMgnTzgUCYFYoHmMUPPPXFz/Mk8onWerrDE0F71myAa5cqh WwPsKJ+GkjdFCDyg1sa9yFvYtpXU77ZxEl4lSLJp4DmbeOmWF5i/zq3/nKgrm+yX/NRmK2JgB ALURCN4uHd9YCjcGGOLc6DsGYvzmEYbee53/UHsHgXHgUQG8oereP/CwI8fVXnZXZU0OmQHaq IagUhulldTw5TF9VfdKJ39zMqTaUvpbHJucC03W/bA6VU+VugfJx+WA4PqGMo6y9t86kcj9hD DXxMDb+JEA3iv9VCP8+7D89uyGWhc5HPv9/CcxPbqyRdZCzdimvmrdkujaJa/QrkZbjBxwKNO /d2jSvl8xdzej3XmubmPYYr+sbxm0RDGLBo+V2pBGXQ3KhRV3jwX+8WNmniugixFu+uC3dqsN bLHqYT+1CnZMgwvwJyqf/tFEwbhhueA5XYjvUmShG5FRqEi55mgBr7NBfjqHg7bWVkpHgaPUv IoFzMi7/x4iAICth0eSshUOQ== X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf 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?1749596403343664558?= X-GMAIL-MSGID: =?utf-8?q?1749596403343664558?= Dear all, when constant expressions involve parentheses, array constructors, typespecs, and the power operator (**), we could fail with an ICE during simplification in arith_power. Debugging of the testcase showed we call the proper type conversions needed for the arithmetic operation, but under certain circumstances we seem to lose the typespec on the way to the invocation of the simplification. We then run into unhandled combinations of operand types. The attached patch is likely a sort of a band-aid to the problem: we check the operand types in arith_power, and if we see that a conversion is (still) needed, we punt and defer the simplification. AFAICT this is safe. It does not address a possibly deeply covered issue in gfortran, which was suspected when analyzing pr107000. But as this is elusive, that may be hard to locate and fix. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From efe9dafabc2e2cc2dab079dfa3be3d09b3471c0f Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Tue, 15 Nov 2022 21:20:20 +0100 Subject: [PATCH] Fortran: ICE in simplification of array expression involving power [PR107680] gcc/fortran/ChangeLog: PR fortran/107680 * arith.cc (arith_power): Check that operands are properly converted before attempting to simplify. gcc/testsuite/ChangeLog: PR fortran/107680 * gfortran.dg/pr107680.f90: New test. --- gcc/fortran/arith.cc | 7 ++++++ gcc/testsuite/gfortran.dg/pr107680.f90 | 34 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/pr107680.f90 diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc index fc9224ebc5c..c4ab75b401c 100644 --- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -845,6 +845,13 @@ arith_power (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) if (!gfc_numeric_ts (&op1->ts) || !gfc_numeric_ts (&op2->ts)) return ARITH_INVALID_TYPE; + /* The result type is derived from op1 and must be compatible with the + result of the simplification. Otherwise postpone simplification until + after operand conversions usually done by gfc_type_convert_binary. */ + if ((op1->ts.type == BT_INTEGER && op2->ts.type != BT_INTEGER) + || (op1->ts.type == BT_REAL && op2->ts.type == BT_COMPLEX)) + return ARITH_NOT_REDUCED; + rc = ARITH_OK; result = gfc_get_constant_expr (op1->ts.type, op1->ts.kind, &op1->where); diff --git a/gcc/testsuite/gfortran.dg/pr107680.f90 b/gcc/testsuite/gfortran.dg/pr107680.f90 new file mode 100644 index 00000000000..4ed431eb06f --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr107680.f90 @@ -0,0 +1,34 @@ +! { dg-do compile } +! { dg-options "-fdump-tree-original" } +! PR fortran/107680 - ICE in arith_power +! Contributed by G.Steinmetz + +program p + real, parameter :: x(*) = [real :: ([1])] ** 2.0 + complex, parameter :: y(*) = [real :: ([1])] ** (2.0,1.0) + complex, parameter :: z(*) = [complex :: ([1])] ** (2.0,1.0) + complex, parameter :: u(*) = [complex :: ([1.0])] ** (2.0,1.0) + complex, parameter :: v(*) = [real :: ([(1.0,2.0)])] ** (3.0,1.0) + complex, parameter :: w(*) = [integer :: ([(1.0,2.0)])] ** (3.0,1.0) + print *, [real :: ([3])] ** 2 + print *, [real :: ([3])] ** 2.0 + print *, [real :: ([1])] ** (1.0,2.0) + print *, [real :: ([1.0])] ** (1.0,2.0) + print *, [complex :: ([3])] ** 2 + print *, [complex :: ([3])] ** 2.0 + print *, [complex :: ([1])] ** (1.0,2.0) + print *, [complex :: ([1.0])] ** (1.0,2.0) + print *, [integer :: ([3.0])] ** 2 + print *, [integer :: ([3.0])] ** 2.0 + print *, [integer :: ([1.0])] ** (1.0,2.0) + print *, [integer :: ([(1.0,2.0)])] ** (3.0,1.0) + print *, v(1) + if (u(1) /= 1) stop 1 + if (v(1) /= 1) stop 2 + if (w(1) /= 1) stop 3 + if (x(1) /= 1) stop 4 + if (y(1) /= 1) stop 5 + if (z(1) /= 1) stop 6 +end + +! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "original" } } -- 2.35.3