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 23D3D3858C2D for ; Tue, 24 Oct 2023 21:11:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 23D3D3858C2D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 23D3D3858C2D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698181894; cv=none; b=Cj4/OLcDVcgSfDAFViUzU1OBLEeWf61nNoIylau7v9pUaEjORkUCZfg7pM9tgNuFjYSy8UiOImswBGztjnO/KyXVEkE3/UKAaXD4JeiYGQ6lWaG6YkLkUyiMDWRDXFJkxGplGf/qYHWI/WPoO8qdS3KunhxQRqFVA5EpSNH/A4E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698181894; c=relaxed/simple; bh=GaWczpdTOTwrjygkudVwqitEHE4C2MaRjVnoY5YzVhs=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=AEpp5MYHc8nEoGiauTWVBGgnGjBvJzJNF0AQIX+zjPRLdqwAKBOqkDXBw+ENbHvZCcOV4R77uIHdzmeeVrGj5/qoB+54j+KKMyPk2dHRJqw4DTk2j+Cfy0YYIxwSJRmK9eHpiRV9K+ow2PN2YaFjZKPgsl6CE1JM0tVOZvsfKM0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698181892; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uexsONvsXlw1BJLNReKLz+I4k/0y1QJz8y8dC6IQc2M=; b=aNEb038HbdzW2QHt12YMBITMYqeTYxbMrm0ButwVQ/Ybbw2AsB/AiKBT6KKfN3E78ZW4sC Ya9XQmaEPzt8jyhPuk3QM92Qt3x7ocqiyl4wWUdCklmyIsL6mKEgo9KHLno+e9b4lTp65F CfzvwC/muHYcUc5BfPwzrcu/Av2BeqU= Received: from mail-oa1-f72.google.com (mail-oa1-f72.google.com [209.85.160.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-mFwfelJwMD2t4z_nI4ROrw-1; Tue, 24 Oct 2023 17:11:31 -0400 X-MC-Unique: mFwfelJwMD2t4z_nI4ROrw-1 Received: by mail-oa1-f72.google.com with SMTP id 586e51a60fabf-1ea1bc5d50dso6180964fac.0 for ; Tue, 24 Oct 2023 14:11:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698181891; x=1698786691; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=uexsONvsXlw1BJLNReKLz+I4k/0y1QJz8y8dC6IQc2M=; b=fyHtht47ULZbbMdS9mL7cfU+IkElo4rxgblCTXjQCoX58zf3KCXil6x6pQPd/eyuYT /MyxvBBRehKKY6DPl8uLI9yWpOAuxWKryti7llA0ubO0xqDDN1h7ELVB9c1KE95aIm7i lCXhk6ZpQ8cLTleCTR8DjosAB7Hkld58Atmgd0aA+7ReeUGiOoOKcw0SqYULX2t2qPjn 2rAUyy2fE/qeY5TbcSds6nXYU6osPeSYJ1ya6uGUrVu34IS7p+TEv8nqh4fQifNlYwgz 1hGHS4cKHk9AoNq5S1vaOZrrYqLZGvk8r6ESqNs1JEzdkJXTjpv1mUYa7r+pJiihO5TX 0gTA== X-Gm-Message-State: AOJu0YzaQrgdWEIKjrFX4awJmM+JsREW4wO/uEG+EkYnkYyJFZwMF40H Mk1FxFI2woHGdA+y1SrEBCWHtBtcZlmfjHGT9LutNR+vZGppKHAlfd1otci/HkQaeq+3U4kUTDs 2iBgTuUjnanr6AeC0Eg== X-Received: by 2002:a05:6870:15cf:b0:1ea:8645:6352 with SMTP id k15-20020a05687015cf00b001ea86456352mr11446406oad.11.1698181890678; Tue, 24 Oct 2023 14:11:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGFl/XTJXMvQRKfbp0jr4fN2PoeWNm0od+oJqeSPo/qxS92wahAbRZUrDlv5xqc9LsSrD78lg== X-Received: by 2002:a05:6870:15cf:b0:1ea:8645:6352 with SMTP id k15-20020a05687015cf00b001ea86456352mr11446391oad.11.1698181890296; Tue, 24 Oct 2023 14:11:30 -0700 (PDT) Received: from [192.168.1.108] (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id b25-20020ac85419000000b00418be3e365fsm3782116qtq.1.2023.10.24.14.11.29 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 24 Oct 2023 14:11:29 -0700 (PDT) Message-ID: Date: Tue, 24 Oct 2023 17:11:26 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: build_new_1 and non-dep array size [PR111929] To: Patrick Palka , gcc-patches@gcc.gnu.org References: <20231024170316.3919946-1-ppalka@redhat.com> From: Jason Merrill In-Reply-To: <20231024170316.3919946-1-ppalka@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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: On 10/24/23 13:03, Patrick Palka wrote: > Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look > like the right approach? > > -- >8 -- > > This PR is another instance of NON_DEPENDENT_EXPR having acted as an > "analysis barrier" for middle-end routines, and now that it's gone we > may end up passing weird templated trees (that have a generic tree code) > to the middle-end which leads to an ICE. In the testcase below the > non-dependent array size 'var + 42' is expressed as an ordinary > PLUS_EXPR, but whose operand types have different precisions -- long and > int respectively -- naturally because templated trees encode only the > syntactic form of an expression devoid of e.g. implicit conversions > (typically). This type incoherency triggers a wide_int assert during > the call to size_binop in build_new_1 which requires the operand types > have the same precision. > > This patch fixes this by replacing our incremental folding of 'size' > within build_new_1 with a single call to cp_fully_fold (which is a no-op > in template context) once 'size' is fully built. This is OK, but we could probably also entirely skip a lot of the calculation in a template, since we don't care about any values. Can we skip the entire if (array_p) block? > PR c++/111929 > > gcc/cp/ChangeLog: > > * init.cc (build_new_1): Use convert, build2, build3 instead of > fold_convert, size_binop and fold_build3 when building 'size'. > > gcc/testsuite/ChangeLog: > > * g++.dg/template/non-dependent28.C: New test. > --- > gcc/cp/init.cc | 9 +++++---- > gcc/testsuite/g++.dg/template/non-dependent28.C | 6 ++++++ > 2 files changed, 11 insertions(+), 4 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/template/non-dependent28.C > > diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc > index d48bb16c7c5..56c1b5e9f5e 100644 > --- a/gcc/cp/init.cc > +++ b/gcc/cp/init.cc > @@ -3261,7 +3261,7 @@ build_new_1 (vec **placement, tree type, tree nelts, > max_outer_nelts = wi::udiv_trunc (max_size, inner_size); > max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts); > > - size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts)); > + size = build2 (MULT_EXPR, sizetype, size, convert (sizetype, nelts)); > > if (TREE_CODE (cst_outer_nelts) == INTEGER_CST) > { > @@ -3344,7 +3344,7 @@ build_new_1 (vec **placement, tree type, tree nelts, > /* Use a class-specific operator new. */ > /* If a cookie is required, add some extra space. */ > if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) > - size = size_binop (PLUS_EXPR, size, cookie_size); > + size = build2 (PLUS_EXPR, sizetype, size, cookie_size); > else > { > cookie_size = NULL_TREE; > @@ -3358,8 +3358,8 @@ build_new_1 (vec **placement, tree type, tree nelts, > if (cxx_dialect >= cxx11 && flag_exceptions) > errval = throw_bad_array_new_length (); > if (outer_nelts_check != NULL_TREE) > - size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check, > - size, errval); > + size = build3 (COND_EXPR, sizetype, outer_nelts_check, size, errval); > + size = cp_fully_fold (size); > /* Create the argument list. */ > vec_safe_insert (*placement, 0, size); > /* Do name-lookup to find the appropriate operator. */ > @@ -3418,6 +3418,7 @@ build_new_1 (vec **placement, tree type, tree nelts, > /* If size is zero e.g. due to type having zero size, try to > preserve outer_nelts for constant expression evaluation > purposes. */ > + size = cp_fully_fold (size); > if (integer_zerop (size) && outer_nelts) > size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts); > > diff --git a/gcc/testsuite/g++.dg/template/non-dependent28.C b/gcc/testsuite/g++.dg/template/non-dependent28.C > new file mode 100644 > index 00000000000..3e45154f61d > --- /dev/null > +++ b/gcc/testsuite/g++.dg/template/non-dependent28.C > @@ -0,0 +1,6 @@ > +// PR c++/111929 > + > +template > +void f(long var) { > + new int[var + 42]; > +}