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 4880D3858401 for ; Wed, 3 Apr 2024 16:07:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4880D3858401 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 4880D3858401 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=1712160475; cv=none; b=G8PUitpyGPuI7YDUMb9zmWZlpioSYgQPQVX6DiY1AblCB6CGcF13NNMbtzKeIMVBHhEVpJp6dHZtcVqIv37pU0oVo8V7+JzLYw8fafBXtASutsAv0ROCpzrsXd0y1cTCTMlDXkYM7MnFlj8AX1x+oBoyzZxskE6mrKXpHE3wW6k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1712160475; c=relaxed/simple; bh=9TpQ1J4E6OfPnWGZOH42HBpsrHkt4w/piyQwiasCcfk=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=UiWmDd5r2YGFgwDyyEpF5mJvBGqo0w0TjkuvviT1tlTjUz2Ye3pESuD9ROfSoAVSNOvSvhLb5ta/u5h3+iKieZy30fH1sSvuNMlkAVAfjrmUuXk9k8VfeE20Gqweu8Xk8No2Uw4Jc0VpIBjCDNSCGQJQZOUSfKoNPxkG4X3f8Xo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1712160472; 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=Sg8UiEfQszTfVxPq/eyhH8mGkhZWm9eAYa5L0iajZnc=; b=QeNrqQ/9f7QDscuOwtHwH1DnGKhr6PzTrNMgZjsjBlCdFwphr5hY6NkZq3HmvAyxsEs9ay hIbrOYvCOxwAxig72Qt6aam19b6DcFUEhCKFFQ2CyOVoH47lcsZN588JUTM8NZ544eyEuZ 0vyyE2+sXtJyF5KFstDos2lvoS1czzk= Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-672-XKYLMAdGNnaUw3MDkyVKRw-1; Wed, 03 Apr 2024 12:07:50 -0400 X-MC-Unique: XKYLMAdGNnaUw3MDkyVKRw-1 Received: by mail-qk1-f197.google.com with SMTP id af79cd13be357-78d3352237cso194736685a.1 for ; Wed, 03 Apr 2024 09:07:50 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712160470; x=1712765270; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Sg8UiEfQszTfVxPq/eyhH8mGkhZWm9eAYa5L0iajZnc=; b=wfmDMpIwnWcPVTbVG+HAbqgONmf+kSZWG9uwJvrt6phTPVg/3U440bHoFNHqMBSuIP 6AHyFiXHxbw59/5hvSVwlAEml1A/K2h+XbSjwPoCADnPXsWNBc96tjyWfwcJtkLXQt8A ZZQe6SJvzB1XmcaTMWg4Ef5Lr5PY5FcbMFFAGNqC+b17qc0bWiTAKSqwm7M66RE8MTb/ EMMj7KVFh8JqRUkNHYoJEappdi+iP1s2/Xd4UrndOWNi1gH3UxNureP7hkQ7CcO326pI ZTF23ML2yhmrJXsDWfMUOuotutE4g9C/R7BGMepe38AWUT33PvwV5iZEeNDE4KD8727Q nCEQ== X-Gm-Message-State: AOJu0YwRexzlUcQ7ReRcahY7LmMkl3sTivowemGWI6bkTcTAPx177hN9 TM0Y71e1FtT3tYO/LJQMwzquy4JqUSUzGsj9louTui73Vqnhjyr5ddKZEd36wKpTdSsV32AsYpl uj0P+1QX6+GaTcnFkM7AFhxN1MhSDmqCoOnEktSnmgplGkQWBHqD9Efb4zdtKFL8= X-Received: by 2002:a05:620a:4007:b0:78b:bb37:6f39 with SMTP id h7-20020a05620a400700b0078bbb376f39mr185658qko.8.1712160469923; Wed, 03 Apr 2024 09:07:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHs7gq82zAkXciIyHDFuKE/gToGrHMXd3xwbn2IB0oGlk5Ij0/uZM10pCKylEnelO9X9YCqIg== X-Received: by 2002:a05:620a:4007:b0:78b:bb37:6f39 with SMTP id h7-20020a05620a400700b0078bbb376f39mr185614qko.8.1712160469493; Wed, 03 Apr 2024 09:07:49 -0700 (PDT) Received: from [192.168.1.130] (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 m17-20020ae9e011000000b0078a452ed654sm5186394qkk.68.2024.04.03.09.07.48 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 03 Apr 2024 09:07:48 -0700 (PDT) Message-ID: <8166752c-6ad7-4b56-a451-da614234e47f@redhat.com> Date: Wed, 3 Apr 2024 12:07:48 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH] c++: Implement C++26 P2809R3 - Trivial infinite loops are not Undefined Behavior To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org References: 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=-6.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_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 4/3/24 03:25, Jakub Jelinek wrote: > Hi! > > The following patch attempts to implement P2809R3, which has been voted > in as a DR. > > The middle-end has its behavior documented: > '-ffinite-loops' > Assume that a loop with an exit will eventually take the exit and > not loop indefinitely. This allows the compiler to remove loops > that otherwise have no side-effects, not considering eventual > endless looping as such. > > This option is enabled by default at '-O2' for C++ with -std=c++11 > or higher. > > So, the following patch attempts to detect trivial infinite loops and > turn their conditions into INTEGER_CSTs so that they don't really have > exits in the middle-end and so regardless of -ffinite-loops or > -fno-finite-loops they are handled as infinite loops by the middle-end. > Otherwise, if the condition would be a large expression calling various > constexpr functions, I'd be afraid we could e.g. just inline some of them > and not all of them and the middle-end could still see tests in the > condition and with -ffinite-loops optimize it by assuming that such loops > need to be finite. > > The "A trivial infinite loop is a trivially empty iteration statement for > which the converted controlling expression is a constant expression, when > interpreted as a constant-expression ([expr.const]), and evaluates to true." > wording isn't clear to me what it implies for manifest constant evaluation > of the expression, especially given the > int x = 42; > while (std::is_constant_evaluated() || --x) ; > example in the rationale. The "interpreted as a constant-expression" wording was specifically intended (per CWG email discussion) to mean manifestly constant-evaluated. I also note that the paper expects while (std::is_constant_evaluated() || --x) ; to be recognized as a trivial infinite loop, which means treating the condition as manifestly constant-evaluated. > The patch assumes that the condition expression aren't manifestly constant > evaluated. If it would be supposed to be manifestly constant evaluated, > then I think the DR would significantly change behavior of existing programs > and have really weird effects. Before the DR has been voted in, I think > void foo (int x) > { > if (x == 0) > while (std::is_constant_evaluated()) > ; > else > while (!std::is_constant_evaluated()) > ; > } > would have well defined behavior of zero loop body iterations if x == 0 and > undefined behavior otherwise. If the condition expression is manifestly > constant evaluated if it evaluates to true, and otherwise can be > non-constant or not manifestly constant evaluated otherwise, then the > behavior would be that for x == 0 it is well defined trvial infinite loop, > while for x != 0 it would keep to be undefined behavior (infinite loop, > as !std::is_constant_evaluated() is false when manifestly constant evaluated > and if we keep the condition as is, evaluates then to true. I think it > would be fairly strange if both loops are infinite even when their condition > are negated. Similar for anything that is dependent on if consteval or > std::is_constant_evaluated() inside of it. Using std::is_constant_evaluated directly in a loop condition is, as the paper says, unlikely and "horrendous code", so I'm not concerned about surprising effects, though I guess we should check for it with maybe_warn_for_constant_evaluated. > So, the patch below attempts to discover trivially empty iteration > statements at cp_fold time if it is the final mce_false folding, > attempts to maybe_constant_value with mce_false evaluate the conditions > and replaces it with the returned value if constant non-zero. Please refactor this code to share most of the implementation between the loop types. > The testcases then try to check if the FE changed the calls in the > conditions into constants. > > Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk? > > Or is it really supposed to be mce_true with the above described weird > behavior? If so, I think the standard at least should mention it in Annex C > (though, where when it is a DR?). Good question, I'll raise this with CWG. Jason