From patchwork Wed Feb 1 09:44:54 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 51250 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp179200wrn; Wed, 1 Feb 2023 01:45:57 -0800 (PST) X-Google-Smtp-Source: AK7set9VsU4eW9FW9YYJq2I9/eMeYlCtpTeEZtdC/Ek8qvC0kfD8pDlJLJVlQ+Zq/XXBIkUy3vW4 X-Received: by 2002:a17:906:12db:b0:880:ffaa:16f4 with SMTP id l27-20020a17090612db00b00880ffaa16f4mr1606449ejb.10.1675244757511; Wed, 01 Feb 2023 01:45:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675244757; cv=none; d=google.com; s=arc-20160816; b=zTm+Hrwc7aLBXyIL3qJ6KPhfHJUR3FFxDaycxY3SJk2hylyMwUPCjvpemtlmYgrON4 bYNSqFNPDj//aSUW6x4gMS7oFRxAAPpJNWaukpYbY8DW+6OLVeojUFLbxEbRP5+b6oGK o7BkteGhrelnW/UwdiSbAUvGx0QMwqKIzl60r76E1wJwV7as7uTvx/mkUc6qklZLTxEP Vj5c5rkdq5ybuel/0uTwaAgfAyFO5lwZySpvEYHyUKZiNbI+CtkYY8uUK9xhrhBcH5Qk 1gyMn7WsvtDB+hm29PBO7nyfo7q3CMQL+rUGL3PPU0Q8704H95eACaOVKoaJfK2Wo+Sp hysg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to: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=MqEBw15omnQJinA1+AiO5ekBeGP1dQd9nZuiUhl9BMc=; b=UTuHej7f1JCPUqDwjz/sIpJfZbLopTest3p6BycfEecXI23ni28+fA7uw9uFg5+ioj OAlBDf38coulOJvJNh7A0D+8PwmfTMW95cD/Cfk92ZlUMWNbZsCIMrpCWCgiUsPyht8/ kqmmW+0wskCbw1cWmA2eLgOKNj3c0CwCDGqkTvfm+f9IMJFNiztYQqJbLDNFerrkmGMZ 0nWwdTIqE9eFWHnoPBupIEOvg4O2PPiXOT9IRebF5aNNBhtxaIh5bWeQVdNKfr6foQgw IGpIF6qoh6Il/cdAogMiJWj3ewGThJSHiJOnuqS8JEbgyaMsvb1HPBB3lezPfUjJKKu3 ryDw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=m9gTs5C2; 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 (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id sh39-20020a1709076ea700b0087787ae09e7si29568366ejc.210.2023.02.01.01.45.57 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Feb 2023 01:45:57 -0800 (PST) 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=m9gTs5C2; 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 B22F03857838 for ; Wed, 1 Feb 2023 09:45:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B22F03857838 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675244752; bh=MqEBw15omnQJinA1+AiO5ekBeGP1dQd9nZuiUhl9BMc=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=m9gTs5C2r3cwyHwE7g2FxmGd70xC1C9Sl+S+HP5kfPLM7RsXmKGK8+4/jPO9kuBiW d4nOCgUFprruC55V0kmmYFndg+JZ0be1yCwtrGs1QwqMft/oLSfWRUa3d7lCjlJaCB 1eiwoht0M1hJ8BI4zf2ggnuuzICpG2wNNpyVJvuY= 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 5AF7B3858412 for ; Wed, 1 Feb 2023 09:44:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5AF7B3858412 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-615-NM4Ujm6CPf6t3gAoAYGQjw-1; Wed, 01 Feb 2023 04:44:58 -0500 X-MC-Unique: NM4Ujm6CPf6t3gAoAYGQjw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id AF27D80280C for ; Wed, 1 Feb 2023 09:44:57 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.223]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7086C2026D4B for ; Wed, 1 Feb 2023 09:44:57 +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 3119iteg3214336 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT) for ; Wed, 1 Feb 2023 10:44:55 +0100 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 3119isje3214335 for gcc-patches@gcc.gnu.org; Wed, 1 Feb 2023 10:44:54 +0100 Date: Wed, 1 Feb 2023 10:44:54 +0100 To: gcc-patches@gcc.gnu.org Subject: [committed] c++, openmp: Handle some OMP_*/OACC_* constructs during constant expression evaluation [PR108607] Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.5 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1756621446522928683?= X-GMAIL-MSGID: =?utf-8?q?1756621446522928683?= Hi! While potential_constant_expression_1 handled most of OMP_* codes (by saying that they aren't potential constant expressions), OMP_SCOPE was missing in that list. I've also added OMP_SCAN, though that is less important (similarly to OMP_SECTION it ought to appear solely inside of OMP_{FOR,SIMD} resp. OMP_SECTIONS). As the testcase shows, it isn't enough, potential_constant_expression_1 can catch only some cases, as soon as one uses switch or ifs where at least one of the possible paths could be constant expression, we can run into the same codes during cxx_eval_constant_expression, so this patch handles those there as well. Bootstrapped/regtested on x86_64-linux and i686-linux, committed to trunk so far. 2023-02-01 Jakub Jelinek PR c++/108607 * constexpr.cc (cxx_eval_constant_expression): Handle OMP_* and OACC_* constructs as non-constant. (potential_constant_expression_1): Handle OMP_SCAN and OMP_SCOPE. * g++.dg/gomp/pr108607.C: New test. Jakub --- gcc/cp/constexpr.cc.jj 2023-01-16 11:52:16.000000000 +0100 +++ gcc/cp/constexpr.cc 2023-01-31 14:44:36.686618639 +0100 @@ -8001,6 +8001,51 @@ cxx_eval_constant_expression (const cons r = cxx_eval_bit_cast (ctx, t, non_constant_p, overflow_p); break; + case OMP_PARALLEL: + case OMP_TASK: + case OMP_FOR: + case OMP_SIMD: + case OMP_DISTRIBUTE: + case OMP_TASKLOOP: + case OMP_LOOP: + case OMP_TEAMS: + case OMP_TARGET_DATA: + case OMP_TARGET: + case OMP_SECTIONS: + case OMP_ORDERED: + case OMP_CRITICAL: + case OMP_SINGLE: + case OMP_SCAN: + case OMP_SCOPE: + case OMP_SECTION: + case OMP_MASTER: + case OMP_MASKED: + case OMP_TASKGROUP: + case OMP_TARGET_UPDATE: + case OMP_TARGET_ENTER_DATA: + case OMP_TARGET_EXIT_DATA: + case OMP_ATOMIC: + case OMP_ATOMIC_READ: + case OMP_ATOMIC_CAPTURE_OLD: + case OMP_ATOMIC_CAPTURE_NEW: + case OMP_DEPOBJ: + case OACC_PARALLEL: + case OACC_KERNELS: + case OACC_SERIAL: + case OACC_DATA: + case OACC_HOST_DATA: + case OACC_LOOP: + case OACC_CACHE: + case OACC_DECLARE: + case OACC_ENTER_DATA: + case OACC_EXIT_DATA: + case OACC_UPDATE: + if (!ctx->quiet) + error_at (EXPR_LOCATION (t), + "statement is not a constant expression"); + *non_constant_p = true; + break; + default: if (STATEMENT_CODE_P (TREE_CODE (t))) { @@ -9471,6 +9516,8 @@ potential_constant_expression_1 (tree t, case OMP_ORDERED: case OMP_CRITICAL: case OMP_SINGLE: + case OMP_SCAN: + case OMP_SCOPE: case OMP_SECTION: case OMP_MASTER: case OMP_MASKED: --- gcc/testsuite/g++.dg/gomp/pr108607.C.jj 2023-01-31 14:52:11.914936116 +0100 +++ gcc/testsuite/g++.dg/gomp/pr108607.C 2023-01-31 14:52:36.501575191 +0100 @@ -0,0 +1,47 @@ +// PR c++/108607 +// { dg-do compile { target c++14 } } +// { dg-options "-fopenmp" } + +constexpr int +bar (int x) +{ + return x; +} + +constexpr int +foo (int x) // { dg-message "declared here" "" { target c++20_down } } +{ // { dg-message "is not usable as a 'constexpr' function because" "" { target c++23 } .-1 } + #pragma omp scope // { dg-warning "is not a constant expression" "" { target c++20_down } } + x = bar (x); // { dg-error "is not a constant expression" "" { target c++23 } .-1 } + return x; +} + +constexpr int +baz (int x) +{ + switch (x) + { + case 42: + return 0; + case 2: + #pragma omp scope // { dg-error "statement is not a constant expression" } + x = bar (x); + return x; + case 3: + #pragma omp parallel // { dg-error "statement is not a constant expression" } + x = bar (x); + return x; + case 4: + #pragma omp task // { dg-error "statement is not a constant expression" } + x = bar (x); + return x; + default: + return -1; + } +} + +constexpr int a = foo (1); // { dg-error "called in a constant expression" } +constexpr int b = baz (42); +constexpr int c = baz (2); +constexpr int d = baz (3); +constexpr int e = baz (4);