c++: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295]
Message ID | Y0+rxzMBPmFcWzqe@tucnak |
---|---|
State | Unresolved |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp184944wrs; Wed, 19 Oct 2022 00:49:49 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5GvowUwvoc/OaWzws3AyJImNV8ka5075MWW668VyFVu/hi0G+K2vNtVPy+vRkclngr9loq X-Received: by 2002:a05:6402:370c:b0:453:9fab:1b53 with SMTP id ek12-20020a056402370c00b004539fab1b53mr6318904edb.28.1666165789620; Wed, 19 Oct 2022 00:49:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666165789; cv=none; d=google.com; s=arc-20160816; b=GiqkES6qCh5/AFpXD/2NZpfpmZOvaNDfecyc/V/USCYUtYzArLcbXgvduvszlY/4DR WL6KjskC++IRHS4WmPa7Ji4P3BuYWQNBU5pyFJC1a5toYpgqKb/pXp2pploVGIb4Mi0b Z+Zg/0CWtqWtBjc5gxSEJy7RhOP/DrpXwQcaZs0URx4/I3iAugyyblixpmNjvCMCL6qd RWY3yW8ag1Ce3h/3lN4y9QSP4ecr5rjAy8/xezdOofPouHgly2jk/Mt8GB+Qxo69Jy06 1bu6p6pbZiztIrITdD1+7tGjLfQNaX3RjPfc5cvrzrI2Uwi+z/OxJ8A20vPzeYQ3gx/v IiYA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=/5xd02Oot/E3gWqd6uPs7jrOKB8403tJ2uhe6UZU5vQ=; b=nS1VxZB+2Kyx75KjIQJJ6Y50W0OZjLx6UN412egL0Xust5P6oCWEhQLUf65RkckGDY abpu5wACuBx0fcabWs87tHAwsIe7wt1jIm/2Is8M2YO6Isr8u23NFB28WRLRsDe6sQy2 P9e+oqA1HGyvGGwhaqae3Q8igCna/UxtbvOE6vQs+YlHno+F9qEhddN02yItXCciEX4Z j61iQ29OVBVFfVUBJU4ZXU0nVnai/72BcT3ojYs1ikH65hQWcL0r6n+wizfmjbcGaNQs tTf5idz5BCz2HBQ0N0kulIu/n3GUVrg+UOERaM+/AcyQQICeN+3vs5Z/tlH+pR96/tEG M7jQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gIvMSOhj; 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 e8-20020a17090658c800b00781dbee4273si8901948ejs.514.2022.10.19.00.49.49 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Oct 2022 00:49:49 -0700 (PDT) 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=gIvMSOhj; 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 995C53857C77 for <ouuuleilei@gmail.com>; Wed, 19 Oct 2022 07:49:48 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 995C53857C77 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666165788; bh=/5xd02Oot/E3gWqd6uPs7jrOKB8403tJ2uhe6UZU5vQ=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=gIvMSOhjqfN/DCzXN8ZaMQKcZouKl7bidlpoIXFpqzymcYEvLdf9ftyfiRevYGRim kD7OCrb4p4+Fv6mlsnH5e+4MFlAXP0km+goc1qMDMLG3Nn9hZmOIyA+pIdl+DMevsj Ex9CH1L7i+5V6oepbwbIjjCpC7Bnvf8cgJWHN/Ns= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 947483858D39 for <gcc-patches@gcc.gnu.org>; Wed, 19 Oct 2022 07:48:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 947483858D39 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-301-qVzgz0y6PGqXSOD6Wlnozg-1; Wed, 19 Oct 2022 03:48:46 -0400 X-MC-Unique: qVzgz0y6PGqXSOD6Wlnozg-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 5A48C101E9B2 for <gcc-patches@gcc.gnu.org>; Wed, 19 Oct 2022 07:48:35 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.193.252]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 3F5A5C1912A; Wed, 19 Oct 2022 07:48:32 +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 29J7mTOl3797069 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 19 Oct 2022 09:48:29 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 29J7mSXd3797067; Wed, 19 Oct 2022 09:48:28 +0200 Date: Wed, 19 Oct 2022 09:48:23 +0200 To: Jason Merrill <jason@redhat.com> Subject: [PATCH] c++: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295] Message-ID: <Y0+rxzMBPmFcWzqe@tucnak> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-3.8 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_H2, SPF_HELO_NONE, SPF_NONE, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Jakub Jelinek <jakub@redhat.com> Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747101458746433625?= X-GMAIL-MSGID: =?utf-8?q?1747101458746433625?= |
Series |
c++: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295]
|
|
Checks
Context | Check | Description |
---|---|---|
snail/gcc-patch-check | warning | Git am fail log |
Commit Message
Jakub Jelinek
Oct. 19, 2022, 7:48 a.m. UTC
Hi! The excess precision support broke building skia (dependency of firefox) on ia32 (it has something like the a constexpr variable), but as the other cases show, it is actually a preexisting problem if one uses casts from constants with wider floating point types. The problem is that cxx_eval_constant_expression tries to short-cut processing of TREE_CONSTANT CONSTRUCTORs if they satisfy reduced_constant_expression_p - instead of calling cxx_eval_bare_aggregate on them it just verifies flags and if they are TREE_CONSTANT even after that, just fold. Now, on the testcase we have a TREE_CONSTANT CONSTRUCTOR containing TREE_CONSTANT NOP_EXPR of REAL_CST. And, fold, which isn't recursive, doesn't optimize that into VECTOR_CST, while later on we are only able to optimize VECTOR_CST arithmetics, not arithmetics with vector CONSTRUCTORs. The following patch fixes that by only returning what fold returned if for vector types it returned VECTOR_CST, otherwise let us call cxx_eval_bare_aggregate. That function will try to constant evaluate all the elements and if anything changes, return a CONSTRUCTOR, in the vector type cases with fold called on it at the end. Now, just calling cxx_eval_bare_aggregate for vector types doesn't work either (e.g. constexpr-builtin4.C breaks), because cxx_eval_bare_aggregate if nothing changes (like all elts are already REAL_CSTs or INTEGER_CSTs) will return the old CONSTRUCTOR and nothing folds it into a VECTOR_CST. Also, the reason for the short-cutting is I think trying to avoid allocating a new CONSTRUCTOR when nothing changes and we just create GC garbage by it. Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? 2022-10-19 Jakub Jelinek <jakub@redhat.com> PR c++/107295 * constexpr.cc (cxx_eval_constant_expression) <case CONSTRUCTOR>: Don't short-cut TREE_CONSTANT vector ctors if fold doesn't turn them into VECTOR_CST. * g++.dg/ext/vector42.C: New test. Jakub
Comments
On 10/19/22 03:48, Jakub Jelinek wrote: > Hi! > > The excess precision support broke building skia (dependency of firefox) > on ia32 (it has something like the a constexpr variable), but as the other > cases show, it is actually a preexisting problem if one uses casts from > constants with wider floating point types. > The problem is that cxx_eval_constant_expression tries to short-cut > processing of TREE_CONSTANT CONSTRUCTORs if they satisfy > reduced_constant_expression_p - instead of calling cxx_eval_bare_aggregate > on them it just verifies flags and if they are TREE_CONSTANT even after > that, just fold. > Now, on the testcase we have a TREE_CONSTANT CONSTRUCTOR containing > TREE_CONSTANT NOP_EXPR of REAL_CST. And, fold, which isn't recursive, > doesn't optimize that into VECTOR_CST, while later on we are only able > to optimize VECTOR_CST arithmetics, not arithmetics with vector > CONSTRUCTORs. > The following patch fixes that by only returning what fold returned > if for vector types it returned VECTOR_CST, otherwise let us > call cxx_eval_bare_aggregate. That function will try to constant > evaluate all the elements and if anything changes, return a CONSTRUCTOR, > in the vector type cases with fold called on it at the end. > Now, just calling cxx_eval_bare_aggregate for vector types doesn't work > either (e.g. constexpr-builtin4.C breaks), because cxx_eval_bare_aggregate > if nothing changes (like all elts are already REAL_CSTs or INTEGER_CSTs) > will return the old CONSTRUCTOR and nothing folds it into a VECTOR_CST. That seems like a bug; for VECTOR_TYPE we should fold even if !changed. > Also, the reason for the short-cutting is I think trying to avoid > allocating a new CONSTRUCTOR when nothing changes and we just create > GC garbage by it. We might limit the shortcut to non-vector types by hoisting the vector check in reduced_constant_expression_p out of the CONSTRUCTOR_NO_CLEARING condition: > if (CONSTRUCTOR_NO_CLEARING (t)) > { > if (TREE_CODE (TREE_TYPE (t)) == VECTOR_TYPE) > /* An initialized vector would have a VECTOR_CST. */ > return false; then we could remove the fold in the shortcut. > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > 2022-10-19 Jakub Jelinek <jakub@redhat.com> > > PR c++/107295 > * constexpr.cc (cxx_eval_constant_expression) <case CONSTRUCTOR>: > Don't short-cut TREE_CONSTANT vector ctors if fold doesn't turn them > into VECTOR_CST. > > * g++.dg/ext/vector42.C: New test. > > --- gcc/cp/constexpr.cc.jj 2022-10-17 12:29:33.518016420 +0200 > +++ gcc/cp/constexpr.cc 2022-10-19 01:29:28.761935708 +0200 > @@ -7391,7 +7391,12 @@ cxx_eval_constant_expression (const cons > VECTOR_CST if applicable. */ > verify_constructor_flags (t); > if (TREE_CONSTANT (t)) > - return fold (t); > + { > + r = fold (t); > + if (TREE_CODE (TREE_TYPE (t)) != VECTOR_TYPE > + || TREE_CODE (r) == VECTOR_CST) > + return r; > + } > } > r = cxx_eval_bare_aggregate (ctx, t, lval, > non_constant_p, overflow_p); > --- gcc/testsuite/g++.dg/ext/vector42.C.jj 2022-10-18 12:33:42.938510483 +0200 > +++ gcc/testsuite/g++.dg/ext/vector42.C 2022-10-18 12:32:27.448544476 +0200 > @@ -0,0 +1,12 @@ > +// PR c++/107295 > +// { dg-do compile { target c++11 } } > + > +template <typename T> struct A { > + typedef T __attribute__((vector_size (sizeof (int)))) V; > +}; > +template <int, typename T> using B = typename A<T>::V; > +template <typename T> using V = B<4, T>; > +using F = V<float>; > +constexpr F a = F () + 0.0f; > +constexpr F b = F () + (float) 0.0; > +constexpr F c = F () + (float) 0.0L; > > Jakub >
--- gcc/cp/constexpr.cc.jj 2022-10-17 12:29:33.518016420 +0200 +++ gcc/cp/constexpr.cc 2022-10-19 01:29:28.761935708 +0200 @@ -7391,7 +7391,12 @@ cxx_eval_constant_expression (const cons VECTOR_CST if applicable. */ verify_constructor_flags (t); if (TREE_CONSTANT (t)) - return fold (t); + { + r = fold (t); + if (TREE_CODE (TREE_TYPE (t)) != VECTOR_TYPE + || TREE_CODE (r) == VECTOR_CST) + return r; + } } r = cxx_eval_bare_aggregate (ctx, t, lval, non_constant_p, overflow_p); --- gcc/testsuite/g++.dg/ext/vector42.C.jj 2022-10-18 12:33:42.938510483 +0200 +++ gcc/testsuite/g++.dg/ext/vector42.C 2022-10-18 12:32:27.448544476 +0200 @@ -0,0 +1,12 @@ +// PR c++/107295 +// { dg-do compile { target c++11 } } + +template <typename T> struct A { + typedef T __attribute__((vector_size (sizeof (int)))) V; +}; +template <int, typename T> using B = typename A<T>::V; +template <typename T> using V = B<4, T>; +using F = V<float>; +constexpr F a = F () + 0.0f; +constexpr F b = F () + (float) 0.0; +constexpr F c = F () + (float) 0.0L;