From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 ; Wed, 19 Oct 2022 07:48:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 947483858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1666165736; 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=/5xd02Oot/E3gWqd6uPs7jrOKB8403tJ2uhe6UZU5vQ=; b=GFxH3L5O5CoYA4bVjlhpJa4Pj4inUFI/wl6PcP6Ge5hZgGuMOSnaMQlOzKdkggx+rin6YS yrkkqS/TsLR0mFPkAiWB73aDZpJxOBnAU9liyADsZKm9sBCr9EfuaK6iVEJROPlXYyYol6 fBPKCypVcsTKdZl1merhyx+btYaGyOw= 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 ; 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 From: Jakub Jelinek To: Jason Merrill Cc: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: Don't shortcut TREE_CONSTANT vector type CONSTRUCTORs in cxx_eval_constant_expression [PR107295] Message-ID: Reply-To: Jakub Jelinek 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 List-Id: 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 PR c++/107295 * constexpr.cc (cxx_eval_constant_expression) : 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 struct A { + typedef T __attribute__((vector_size (sizeof (int)))) V; +}; +template using B = typename A::V; +template using V = B<4, T>; +using F = V; +constexpr F a = F () + 0.0f; +constexpr F b = F () + (float) 0.0; +constexpr F c = F () + (float) 0.0L; Jakub