public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Tobias Burnus <burnus@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/omp/gcc-12] c++, openmp: Handle some OMP_*/OACC_* constructs during constant expression evaluation [PR108607]
Date: Wed,  1 Feb 2023 10:25:17 +0000 (GMT)	[thread overview]
Message-ID: <20230201102517.5BDA33858D37@sourceware.org> (raw)

https://gcc.gnu.org/g:6cb86a3da0cc16581706776ace04a6f17d349da1

commit 6cb86a3da0cc16581706776ace04a6f17d349da1
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Wed Feb 1 11:24:11 2023 +0100

    c++, openmp: Handle some OMP_*/OACC_* constructs during constant expression evaluation [PR108607]
    
    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.
    
    2023-02-01  Jakub Jelinek  <jakub@redhat.com>
    
            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.
    
    (cherry picked from commit bfc070595bfb00abef88a002eee5d9117f5b86a7)

Diff:
---
 gcc/cp/ChangeLog.omp                 | 10 ++++++++
 gcc/cp/constexpr.cc                  | 47 ++++++++++++++++++++++++++++++++++++
 gcc/testsuite/ChangeLog.omp          |  8 ++++++
 gcc/testsuite/g++.dg/gomp/pr108607.C | 47 ++++++++++++++++++++++++++++++++++++
 4 files changed, 112 insertions(+)

diff --git a/gcc/cp/ChangeLog.omp b/gcc/cp/ChangeLog.omp
index f386cdea157..1153c67e839 100644
--- a/gcc/cp/ChangeLog.omp
+++ b/gcc/cp/ChangeLog.omp
@@ -1,3 +1,13 @@
+2023-02-01  Tobias Burnus  <tobias@codesourcery.com>
+
+	Backport from mainline:
+	2023-02-01  Jakub Jelinek  <jakub@redhat.com>
+
+	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.
+
 2023-01-09  Tobias Burnus  <tobias@codesourcery.com>
 
 	Backport from mainline:
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 3566f6ef724..6ac1b33d41d 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7598,6 +7598,51 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, tree t,
       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)))
 	{
@@ -9041,6 +9086,8 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
     case OMP_ORDERED:
     case OMP_CRITICAL:
     case OMP_SINGLE:
+    case OMP_SCAN:
+    case OMP_SCOPE:
     case OMP_SECTION:
     case OMP_MASTER:
     case OMP_MASKED:
diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp
index f35568d83a9..9279ac7c23a 100644
--- a/gcc/testsuite/ChangeLog.omp
+++ b/gcc/testsuite/ChangeLog.omp
@@ -1,3 +1,11 @@
+2023-02-01  Tobias Burnus  <tobias@codesourcery.com>
+
+	Backport from mainline:
+	2023-02-01  Jakub Jelinek  <jakub@redhat.com>
+
+	PR c++/108607
+	* g++.dg/gomp/pr108607.C: New test.
+
 2023-01-20  Thomas Schwinge  <thomas@codesourcery.com>
 
 	* gcc.dg/no_profile_instrument_function-attr-1.c: GCC/nvptx is
diff --git a/gcc/testsuite/g++.dg/gomp/pr108607.C b/gcc/testsuite/g++.dg/gomp/pr108607.C
new file mode 100644
index 00000000000..9e5137b63de
--- /dev/null
+++ b/gcc/testsuite/g++.dg/gomp/pr108607.C
@@ -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);

                 reply	other threads:[~2023-02-01 10:25 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230201102517.5BDA33858D37@sourceware.org \
    --to=burnus@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).