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.133.124]) by sourceware.org (Postfix) with ESMTPS id 164A23858C29 for ; Sat, 2 Dec 2023 00:43:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 164A23858C29 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 164A23858C29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701477829; cv=none; b=klsgmIAHLlA0lU9pAv8E8wTPZNiKcLRuKzpE0+Gkjx96eSOYLaOuNFA0s6M/Z7OzJJsj+9tx5nmO9kZ8zA8ARiUxujf/xGJNJyFAhxm20gkOypeVjbFMFM+JFClLCVqeUsuvWnnEFWq4tYdTcSU3JvNrfwXjCkZ+ZPUXgguFbtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701477829; c=relaxed/simple; bh=66FD5gamtuR+B/Yn1geTpkQyQEtEtTbgQvWaFjvF4yg=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=aJO9bgsuZlepPKt4NOCbF0ADbaeUjPUh5JpbNSv1eJn/kyrU+nxd16VMane29i3NeEpFarbLV0mf3fc0FX/vZV9T8Z+mioNLZWZnOni2n5xmv3D2OB8UZOBYGkpDKobYCPyyCuKnU1LnH3keCQQpcgfsVPlUmIpq4J93W//Ij58= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701477827; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zK7gWf10uYFe8d4CByzaMAPvDu1r3ibKSs75egHx54c=; b=iQji5KZvlDF4KtRjWX4eYgVsDKD578YX+jrybqp6bMmkzvVSSGLY6S10/Zw5awzQFfP0Wn T6n6w+Xh1lNxcwt+NaXxKYI/7V/+E3a4UZdgZvLHj90hp1sWCQvQQ2p09nSxEJJa3J8jZq 2zG2+zpuaSV9FEQgiVECXiPKLWQFB5Y= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-580-KMQPCFg8OyaATRbdcvb5ag-1; Fri, 01 Dec 2023 19:43:37 -0500 X-MC-Unique: KMQPCFg8OyaATRbdcvb5ag-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-67a9e59e1b5so11836966d6.1 for ; Fri, 01 Dec 2023 16:43:37 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701477817; x=1702082617; h=content-transfer-encoding:in-reply-to:from:references:cc: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=zK7gWf10uYFe8d4CByzaMAPvDu1r3ibKSs75egHx54c=; b=C6422gYW7R7+pEfVSrEOIxFpO07KktM+2jMMyo0UykJ+T9cEJjQ/KhKaY1/oWLhROb TIqrv26urXT9blqv/rc6+fVJTxfW5dr7XOizjmUBMDwYY0j9utG87OAP6x0Q/FrCZXjZ +xT+EaieS/u4VBVV9WXb3ZNtOo0RWaTzOqJkKdGMTOwB5Aajx5nRgrfZ40lAzPUVcaVm RG6/NjQvJryME1RgA+0LVKIs0/iqeGKu6C3AzNAB80xVXVL1jBuDbh5jHn0+SSiBCacX ng/JIC6WABRRivUJXbAEMb9VuGdk4ZXFD9D3lFN0cVE+TM6n3hCcO17l8RXgBgaVtLel CW9A== X-Gm-Message-State: AOJu0YwYSDT8CPkio1oNxz8PIc6m/s/jYYaS65tk5aSYg8L13K2529XA 1YUiHxWQz1RIYP13m5axHl6xHwnajcrL1RjdWUTzenYo/HZF8LUEYMtAwqee8bQQkzZqsWJDgCr /pgdlY1mgSOE2YntVcx1kJ9itqA== X-Received: by 2002:a0c:ef45:0:b0:67a:a721:9eb5 with SMTP id t5-20020a0cef45000000b0067aa7219eb5mr352142qvs.102.1701477816665; Fri, 01 Dec 2023 16:43:36 -0800 (PST) X-Google-Smtp-Source: AGHT+IEHBPOfRt92/l+3UKRgbtuqV2bB7AbhGHE0V3W9mWlcFG/Sfzz95gmOIQEtIy/vgL4mOv5wvw== X-Received: by 2002:a0c:ef45:0:b0:67a:a721:9eb5 with SMTP id t5-20020a0cef45000000b0067aa7219eb5mr352140qvs.102.1701477816287; Fri, 01 Dec 2023 16:43:36 -0800 (PST) Received: from [192.168.1.145] (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 g11-20020ac84dcb000000b0041818df8a0dsm1966694qtw.36.2023.12.01.16.43.35 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 01 Dec 2023 16:43:35 -0800 (PST) Message-ID: Date: Fri, 1 Dec 2023 19:43:35 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v6] c++: implement P2564, consteval needs to propagate up [PR107687] To: Marek Polacek Cc: GCC Patches References: <20230823194904.1925591-1-polacek@redhat.com> <974dbde5-e1d8-90dc-a023-df214883403c@redhat.com> <462dd81b-cfb2-4066-bca4-403335147f5d@redhat.com> <6a3303b5-85b3-4d8f-a4e3-4f41455ec6d1@redhat.com> <1d9a1b66-c1ba-4aa1-80e3-09c5e1840845@redhat.com> From: Jason Merrill In-Reply-To: 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=-5.3 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_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_SORBS_WEB,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=no 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 12/1/23 18:37, Marek Polacek wrote: > On Thu, Nov 30, 2023 at 06:34:01PM -0500, Jason Merrill wrote: >> On 11/23/23 11:46, Marek Polacek wrote: >>> v5 greatly simplifies the code. >> >> Indeed, it's much cleaner now. >> >>> I still need a new ff_ flag to signal that we can return immediately >>> after seeing an i-e expr. >> >> That's still not clear to me: >> >>> + /* In turn, maybe promote the function we find ourselves in... */ >>> + if ((data->flags & ff_find_escalating_expr) >>> + && DECL_IMMEDIATE_FUNCTION_P (decl) >>> + /* ...but not if the call to DECL was constant; that is the >>> + "an immediate invocation that is not a constant expression" >>> + case. */ >>> + && (e = cxx_constant_value (stmt, tf_none), e == error_mark_node)) >>> + { >>> + /* Since we had to set DECL_ESCALATION_CHECKED_P before the walk, >>> + we call promote_function_to_consteval directly which doesn't >>> + check unchecked_immediate_escalating_function_p. */ >>> + if (current_function_decl) >>> + promote_function_to_consteval (current_function_decl); >>> + *walk_subtrees = 0; >>> + return stmt; >>> + } >> >> This is the one use of ff_find_escalating_expr, and it seems redundant with >> the code immediately below, where we use complain (derived from >> ff_mce_false) to decide whether to return immediately. Can we remove this >> hunk and the flag, and merge find_escalating_expr with cp_fold_immediate? > > Ah, that works! Hopefully done now. > >> I think you want to walk the function body for three-ish reasons: >> 1) at EOF, to check for escalation >> 2) at EOF, to check for errors >> 3) at error time, to explain escalation >> >> It's not clear to me that we need a flag to distinguish between them. When >> we encounter an immediate-escalating expression E: >> >> A) if we're in an immediate-escalating function, escalate and return E (#1, >> #3). >> B) otherwise, if we're diagnosing, error and continue (#2). >> C) otherwise, return E (individual expression mce_unknown walk from >> constexpr.cc). >> >>> @@ -1178,11 +1388,19 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_ >>> ) >>> *walk_subtrees = 0; >>> /* Don't return yet, still need the cp_fold below. */ >>> } >>> - cp_fold_immediate_r (stmt_p, walk_subtrees, data); >>> + else >>> + cp_fold_immediate_r (stmt_p, walk_subtrees, data); >>> } >>> *stmt_p = stmt = cp_fold (*stmt_p, data->flags); >>> + /* For certain trees, like +foo(), the cp_fold below will remove the +, >> >> s/below/above/? > > Fixed. > >>> +/* We've stashed immediate-escalating functions. Now see if they indeed >>> + ought to be promoted to consteval. */ >>> + >>> +void >>> +process_pending_immediate_escalating_fns () >>> +{ >>> + /* This will be null for -fno-immediate-escalation. */ >>> + if (!deferred_escalating_exprs) >>> + return; >>> + >>> + for (auto e : *deferred_escalating_exprs) >>> + if (TREE_CODE (e) == FUNCTION_DECL && !DECL_ESCALATION_CHECKED_P (e)) >>> + cp_fold_immediate (&DECL_SAVED_TREE (e), mce_false, e); >>> +} >>> + >>> +/* We've escalated every function that could have been promoted to >>> + consteval. Check that we are not taking the address of a consteval >>> + function. */ >>> + >>> +void >>> +check_immediate_escalating_refs () >>> +{ >>> + /* This will be null for -fno-immediate-escalation. */ >>> + if (!deferred_escalating_exprs) >>> + return; >>> + >>> + for (auto ref : *deferred_escalating_exprs) >>> + { >>> + if (TREE_CODE (ref) == FUNCTION_DECL) >>> + continue; >>> + tree decl = (TREE_CODE (ref) == PTRMEM_CST >>> + ? PTRMEM_CST_MEMBER (ref) >>> + : TREE_OPERAND (ref, 0)); >>> + if (DECL_IMMEDIATE_FUNCTION_P (decl)) >>> + taking_address_of_imm_fn_error (ref, decl); >>> + } >>> + >>> + deferred_escalating_exprs = nullptr; >>> } >> >> Could these be merged, so you do a single loop of cp_fold_immediate over >> function bodies or non-function expressions? I'd expect that to work. > > We seem to walk the hash table in a random order so I can't use one loop, > otherwise we could hit &f before escalating f. Is that a problem, since we recurse if we see a function that is still unchecked? > @@ -1045,90 +1191,138 @@ cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_) > /* The purpose of this is not to emit errors for mce_unknown. */ > const tsubst_flags_t complain = (data->flags & ff_mce_false > ? tf_error : tf_none); > + const tree_code code = TREE_CODE (stmt); > > /* No need to look into types or unevaluated operands. > NB: This affects cp_fold_r as well. */ > - if (TYPE_P (stmt) || unevaluated_p (TREE_CODE (stmt))) > + if (TYPE_P (stmt) || unevaluated_p (code) || cp_unevaluated_operand) Maybe check in_immediate_context here instead? > + /* [expr.const]p16 "An expression or conversion is immediate-escalating if > + it is not initially in an immediate function context and it is either > + -- an immediate invocation that is not a constant expression and is not > + a subexpression of an immediate invocation." > > + If we are in an immediate-escalating function, the immediate-escalating > + expression or conversion makes it an immediate function. So STMT does > + not need to produce a constant expression. */ > + if (DECL_IMMEDIATE_FUNCTION_P (decl)) > + { > + tree e = cxx_constant_value (stmt, tf_none); > + if (e == error_mark_node) > + { > + /* This takes care of > + template > + constexpr int f(T t) > + { > + return id(t); > + } > + where id (consteval) causes f to be promoted. */ > + if (maybe_promote_function_to_consteval (current_function_decl)) > + return NULL_TREE; Can we remove maybe_promote in favor of the promote below? > + /* If we're not complaining, we're either recursing when escalating, > + or just looking for the i-e expression. */ > + if (!(complain & tf_error)) > + { > + /* Since we had to set DECL_ESCALATION_CHECKED_P before the walk, > + we call promote_function_to_consteval directly which doesn't > + check unchecked_immediate_escalating_function_p. */ > + if (current_function_decl) > + promote_function_to_consteval (current_function_decl); > + } Shouldn't we still check immediate_escalating_function_p (not unchecked_), even if only in a gcc_checking_assert? > + else if (!in_immediate_context ()) If we check in_immediate_context at the top of the function, we don't need to check it here. Jason