diff mbox series

[V3,3/4] OpenMP: Use enumerators for names of trait-sets and traits

Message ID 20231207155247.372718-4-sandra@codesourcery.com
State New
Headers show
Series OpenMP: Improve data abstractions for context selectors | expand

Commit Message

Sandra Loosemore Dec. 7, 2023, 3:52 p.m. UTC
This patch introduces enumerators to represent trait-set names and
trait names, which makes it easier to use tables to control other
behavior and for switch statements to dispatch on the tags.  The tags
are stored in the same place in the TREE_LIST structure (OMP_TSS_ID or
OMP_TS_ID) and are encoded there as integer constants.

gcc/ChangeLog
	* omp-selectors.h: New file.
	* omp-general.h: Include omp-selectors.h.
	(OMP_TSS_CODE, OMP_TSS_NAME): New.
	(OMP_TS_CODE, OMP_TS_NAME): New.
	(make_trait_set_selector, make_trait_selector): Adjust declarations.
	(omp_construct_traits_to_codes): Likewise.
	(omp_context_selector_set_compare): Likewise.
	(omp_get_context_selector): Likewise.
	(omp_get_context_selector_list): New.
	* omp-general.cc (omp_construct_traits_to_codes): Pass length in
	as argument instead of returning it.  Make it table-driven.
	(omp_tss_map): New.
	(kind_properties, vendor_properties, extension_properties): New.
	(atomic_default_mem_order_properties): New.
	(omp_ts_map): New.
	(omp_check_context_selector): Simplify lookup and dispatch logic.
	(omp_mark_declare_variant): Ignore variants with unknown construct
	selectors.  Adjust for new representation.
	(make_trait_set_selector, make_trait_selector): Adjust for new
	representations.
	(omp_context_selector_matches): Simplify dispatch logic.  Avoid
	fixed-sized buffers and adjust call to omp_construct_traits_to_codes.
	(omp_context_selector_props_compare): Adjust for new representations
	and simplify dispatch logic.
	(omp_context_selector_set_compare): Likewise.
	(omp_context_selector_compare): Likewise.
	(omp_get_context_selector): Adjust for new representations, and split
	out...
	(omp_get_context_selector_list): New function.
	(omp_lookup_tss_code): New.
	(omp_lookup_ts_code): New.
	(omp_context_compute_score): Adjust for new representations.  Avoid
	fixed-sized buffers and magic numbers.  Adjust call to
	omp_construct_traits_to_codes.
	* gimplify.cc (omp_construct_selector_matches): Avoid use of
	fixed-size buffer.  Adjust call to omp_construct_traits_to_codes.

gcc/c/ChangeLog
	* c-parser.cc (omp_construct_selectors): Delete.
	(omp_device_selectors): Delete.
	(omp_implementation_selectors): Delete.
	(omp_user_selectors): Delete.
	(c_parser_omp_context_selector): Adjust for new representations
	and simplify dispatch logic.  Uniformly warn instead of sometimes
	error when an unknown selector is found.
	(c_parser_omp_context_selector_specification): Likewise.
	(c_finish_omp_declare_variant): Adjust for new representations.

gcc/cp/ChangeLog
	* decl.cc (omp_declare_variant_finalize_one): Adjust for new
	representations.
	* parser.cc (omp_construct_selectors): Delete.
	(omp_device_selectors): Delete.
	(omp_implementation_selectors): Delete.
	(omp_user_selectors): Delete.
	(cp_parser_omp_context_selector): Adjust for new representations
	and simplify dispatch logic.  Uniformly warn instead of sometimes
	error when an unknown selector is found.
	(cp_parser_omp_context_selector_specification): Likewise.
	* pt.cc (tsubst_attribute): Adjust for new representations.

gcc/fortran/ChangeLog
	* gfortran.h: Include omp-selectors.h.
	(enum gfc_omp_trait_property_kind): Delete, and replace all
	references with equivalent omp_tp_type enumerators.
	(struct gfc_omp_trait_property): Update for omp_tp_type.
	(struct gfc_omp_selector): Replace string name with new enumerator.
	(struct gfc_omp_set_selector): Likewise.
	* openmp.cc (gfc_free_omp_trait_property_list): Update for
	omp_tp_type.
	(omp_construct_selectors): Delete.
	(omp_device_selectors): Delete.
	(omp_implementation_selectors): Delete.
	(omp_user_selectors): Delete.
	(gfc_ignore_trait_property_extension): New.
	(gfc_ignore_trait_property_extension_list): New.
	(gfc_match_omp_selector): Adjust for new representations and simplify
	dispatch logic.  Uniformly warn instead of sometimes error when an
	unknown selector is found.
	(gfc_match_omp_context_selector): Adjust for new representations.
	(gfc_match_omp_context_selector_specification): Likewise.
	* trans-openmp.cc (gfc_trans_omp_declare_variant): Adjust for
	new representations.

gcc/testsuite/
	* c-c++-common/gomp/declare-variant-1.c: Expect warning on
	unknown selectors.
	* c-c++-common/gomp/declare-variant-2.c: Likewise.
	* gfortran.dg/gomp/declare-variant-1.f90: Likewise.
	* gfortran.dg/gomp/declare-variant-2.f90: Likewise.
---
 gcc/c/c-parser.cc                             | 185 ++----
 gcc/cp/decl.cc                                |   8 +-
 gcc/cp/parser.cc                              | 182 ++----
 gcc/cp/pt.cc                                  |  15 +-
 gcc/fortran/gfortran.h                        |  20 +-
 gcc/fortran/openmp.cc                         | 189 +++---
 gcc/fortran/trans-openmp.cc                   |  42 +-
 gcc/gimplify.cc                               |  17 +-
 gcc/omp-general.cc                            | 561 ++++++++++++------
 gcc/omp-general.h                             |  23 +-
 gcc/omp-selectors.h                           |  93 +++
 .../c-c++-common/gomp/declare-variant-1.c     |   1 +
 .../c-c++-common/gomp/declare-variant-2.c     |  40 +-
 .../gfortran.dg/gomp/declare-variant-1.f90    |   1 +
 .../gfortran.dg/gomp/declare-variant-2.f90    |  34 +-
 15 files changed, 755 insertions(+), 656 deletions(-)
 create mode 100644 gcc/omp-selectors.h

Comments

Tobias Burnus Dec. 12, 2023, 12:05 p.m. UTC | #1
Hi Sandra,

On 07.12.23 16:52, Sandra Loosemore wrote:
> This patch introduces enumerators to represent trait-set names and
> trait names, which makes it easier to use tables to control other
> behavior and for switch statements to dispatch on the tags.  The tags
> are stored in the same place in the TREE_LIST structure (OMP_TSS_ID or
> OMP_TS_ID) and are encoded there as integer constants.

Thanks - that looks like a huge improvement.

* * *

I think it is useful to prepare for 'target_device'. However, it is currently not yet implemented
on mainline - contrary to OG13.

Can you add some kind of error diagnostic for it? On mainline, the current result is:

error: expected ‘construct’, ‘device’, ‘implementation’ or ‘user’ before ‘target_device’
    13 | #pragma omp declare variant (f05) match (target_device={kind(gpu)})
       |                                          ^~~~~~~~~~~~~

But with your patch, it is silently accepted, which is bad.

(That's a modified version of gcc/testsuite/c-c++-common/gomp/declare-variant-10.c:13)

I think you have two options:

* Either fail with the same error message as above

* Or update the error message to list 'target_device' (for C/C++/Fortran)
   and handle 'target_device' separately with a sorry.

To whatever you think makes more sense for know, knowing that we do want to add 'target_device'
in the not to far future.

(I am slightly preferring the updated-error message + sorry variant as it avoids touching
the messages later again, but either is fine.)

* * *

Otherwise, the patch LGTM.

As written before, 1/4, 2/4 and 4/4 are LGTM as posted.

Thanks,

Tobias

> gcc/ChangeLog
>       * omp-selectors.h: New file.
>       * omp-general.h: Include omp-selectors.h.
>       (OMP_TSS_CODE, OMP_TSS_NAME): New.
>       (OMP_TS_CODE, OMP_TS_NAME): New.
>       (make_trait_set_selector, make_trait_selector): Adjust declarations.
>       (omp_construct_traits_to_codes): Likewise.
>       (omp_context_selector_set_compare): Likewise.
>       (omp_get_context_selector): Likewise.
>       (omp_get_context_selector_list): New.
>       * omp-general.cc (omp_construct_traits_to_codes): Pass length in
>       as argument instead of returning it.  Make it table-driven.
>       (omp_tss_map): New.
>       (kind_properties, vendor_properties, extension_properties): New.
>       (atomic_default_mem_order_properties): New.
>       (omp_ts_map): New.
>       (omp_check_context_selector): Simplify lookup and dispatch logic.
>       (omp_mark_declare_variant): Ignore variants with unknown construct
>       selectors.  Adjust for new representation.
>       (make_trait_set_selector, make_trait_selector): Adjust for new
>       representations.
>       (omp_context_selector_matches): Simplify dispatch logic.  Avoid
>       fixed-sized buffers and adjust call to omp_construct_traits_to_codes.
>       (omp_context_selector_props_compare): Adjust for new representations
>       and simplify dispatch logic.
>       (omp_context_selector_set_compare): Likewise.
>       (omp_context_selector_compare): Likewise.
>       (omp_get_context_selector): Adjust for new representations, and split
>       out...
>       (omp_get_context_selector_list): New function.
>       (omp_lookup_tss_code): New.
>       (omp_lookup_ts_code): New.
>       (omp_context_compute_score): Adjust for new representations.  Avoid
>       fixed-sized buffers and magic numbers.  Adjust call to
>       omp_construct_traits_to_codes.
>       * gimplify.cc (omp_construct_selector_matches): Avoid use of
>       fixed-size buffer.  Adjust call to omp_construct_traits_to_codes.
>
> gcc/c/ChangeLog
>       * c-parser.cc (omp_construct_selectors): Delete.
>       (omp_device_selectors): Delete.
>       (omp_implementation_selectors): Delete.
>       (omp_user_selectors): Delete.
>       (c_parser_omp_context_selector): Adjust for new representations
>       and simplify dispatch logic.  Uniformly warn instead of sometimes
>       error when an unknown selector is found.
>       (c_parser_omp_context_selector_specification): Likewise.
>       (c_finish_omp_declare_variant): Adjust for new representations.
>
> gcc/cp/ChangeLog
>       * decl.cc (omp_declare_variant_finalize_one): Adjust for new
>       representations.
>       * parser.cc (omp_construct_selectors): Delete.
>       (omp_device_selectors): Delete.
>       (omp_implementation_selectors): Delete.
>       (omp_user_selectors): Delete.
>       (cp_parser_omp_context_selector): Adjust for new representations
>       and simplify dispatch logic.  Uniformly warn instead of sometimes
>       error when an unknown selector is found.
>       (cp_parser_omp_context_selector_specification): Likewise.
>       * pt.cc (tsubst_attribute): Adjust for new representations.
>
> gcc/fortran/ChangeLog
>       * gfortran.h: Include omp-selectors.h.
>       (enum gfc_omp_trait_property_kind): Delete, and replace all
>       references with equivalent omp_tp_type enumerators.
>       (struct gfc_omp_trait_property): Update for omp_tp_type.
>       (struct gfc_omp_selector): Replace string name with new enumerator.
>       (struct gfc_omp_set_selector): Likewise.
>       * openmp.cc (gfc_free_omp_trait_property_list): Update for
>       omp_tp_type.
>       (omp_construct_selectors): Delete.
>       (omp_device_selectors): Delete.
>       (omp_implementation_selectors): Delete.
>       (omp_user_selectors): Delete.
>       (gfc_ignore_trait_property_extension): New.
>       (gfc_ignore_trait_property_extension_list): New.
>       (gfc_match_omp_selector): Adjust for new representations and simplify
>       dispatch logic.  Uniformly warn instead of sometimes error when an
>       unknown selector is found.
>       (gfc_match_omp_context_selector): Adjust for new representations.
>       (gfc_match_omp_context_selector_specification): Likewise.
>       * trans-openmp.cc (gfc_trans_omp_declare_variant): Adjust for
>       new representations.
>
> gcc/testsuite/
>       * c-c++-common/gomp/declare-variant-1.c: Expect warning on
>       unknown selectors.
>       * c-c++-common/gomp/declare-variant-2.c: Likewise.
>       * gfortran.dg/gomp/declare-variant-1.f90: Likewise.
>       * gfortran.dg/gomp/declare-variant-2.f90: Likewise.
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955
Sandra Loosemore Dec. 17, 2023, 6:59 p.m. UTC | #2
On 12/12/23 05:05, Tobias Burnus wrote:
> Hi Sandra,
> 
> On 07.12.23 16:52, Sandra Loosemore wrote:
>> This patch introduces enumerators to represent trait-set names and
>> trait names, which makes it easier to use tables to control other
>> behavior and for switch statements to dispatch on the tags.  The tags
>> are stored in the same place in the TREE_LIST structure (OMP_TSS_ID or
>> OMP_TS_ID) and are encoded there as integer constants.
> 
> Thanks - that looks like a huge improvement.
> 
> * * *
> 
> I think it is useful to prepare for 'target_device'. However, it is currently 
> not yet implemented
> on mainline - contrary to OG13.
> 
> Can you add some kind of error diagnostic for it? On mainline, the current 
> result is:
> 
> error: expected ‘construct’, ‘device’, ‘implementation’ or ‘user’ before 
> ‘target_device’
>     13 | #pragma omp declare variant (f05) match (target_device={kind(gpu)})
>        |                                          ^~~~~~~~~~~~~
> 
> But with your patch, it is silently accepted, which is bad.
> 
> (That's a modified version of 
> gcc/testsuite/c-c++-common/gomp/declare-variant-10.c:13)
> 
> I think you have two options:
> 
> * Either fail with the same error message as above
> 
> * Or update the error message to list 'target_device' (for C/C++/Fortran)
>    and handle 'target_device' separately with a sorry.
> 
> To whatever you think makes more sense for know, knowing that we do want to add 
> 'target_device'
> in the not to far future.
> 
> (I am slightly preferring the updated-error message + sorry variant as it 
> avoids touching
> the messages later again, but either is fine.)

OK.  I had a FIXME in the code noting that listing all the valid selector-set 
keywords in the error message was prone to bit-rot anyway, so I have replaced 
it with something more generic, and added a sorry for the missing 
"target_device" support.

Also in V4 of the patch I have added a sorry for the missing "requires" 
selector support so it does that rather than ICE, as Julian discovered.

Finally, I improved the error handling for including a trait-score on selectors 
that don't permit it -- it now says explicitly that a score isn't permitted 
there, instead of a cascade of more more obscure errors.  I have a test case 
for that coming later with the metadirectives patches, which are not ready for 
GCC 14.

And....  I have a new part 5 for this series coming along too, with 
prettyprinter support for the new selector representation.  I realize we're 
long past the end of stage 1 but I think this is still reasonable to consider 
for GCC 14.  It's only for internal debugging purposes, and I think it'll be 
useful for Julian's work and implementing missing 5.1/5.2/TR11 selector 
features for declare variant as well as my current hackery on finishing 
metadirectives.  There aren't any "declare variant" test cases that examine the 
attribute on the base function in dump files, BTW, but I did inspect the output 
by hand and also do some further testing with metadirective.

> Otherwise, the patch LGTM.
> 
> As written before, 1/4, 2/4 and 4/4 are LGTM as posted.

Thanks.  I'll push parts 1-4 when part 3 is approved, and part 5 too if/when 
that's approved.

-Sandra
diff mbox series

Patch

diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 7fe449dc54a..9059a9747db 100644
--- a/gcc/c/c-parser.cc
+++ b/gcc/c/c-parser.cc
@@ -24337,16 +24337,6 @@  c_parser_omp_declare_simd (c_parser *parser, enum pragma_context context)
     }
 }
 
-static const char *const omp_construct_selectors[] = {
-  "simd", "target", "teams", "parallel", "for", NULL };
-static const char *const omp_device_selectors[] = {
-  "kind", "isa", "arch", NULL };
-static const char *const omp_implementation_selectors[] = {
-  "vendor", "extension", "atomic_default_mem_order", "unified_address",
-  "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
-static const char *const omp_user_selectors[] = {
-  "condition", NULL };
-
 /* OpenMP 5.0:
 
    trait-selector:
@@ -24359,7 +24349,8 @@  static const char *const omp_user_selectors[] = {
    trait-selector-set SET.  */
 
 static tree
-c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
+c_parser_omp_context_selector (c_parser *parser, enum omp_tss_code set,
+			       tree parms)
 {
   tree ret = NULL_TREE;
   do
@@ -24373,80 +24364,41 @@  c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
 	  c_parser_error (parser, "expected trait selector name");
 	  return error_mark_node;
 	}
+      enum omp_ts_code sel
+	= omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
 
-      tree properties = NULL_TREE;
-      tree scoreval = NULL_TREE;
-      const char *const *selectors = NULL;
-      bool allow_score = true;
-      bool allow_user = false;
-      int property_limit = 0;
-      enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
-	     CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
-	     CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
-      switch (IDENTIFIER_POINTER (set)[0])
-	{
-	case 'c': /* construct */
-	  selectors = omp_construct_selectors;
-	  allow_score = false;
-	  property_limit = 1;
-	  property_kind = CTX_PROPERTY_SIMD;
-	  break;
-	case 'd': /* device */
-	  selectors = omp_device_selectors;
-	  allow_score = false;
-	  allow_user = true;
-	  property_limit = 3;
-	  property_kind = CTX_PROPERTY_NAME_LIST;
-	  break;
-	case 'i': /* implementation */
-	  selectors = omp_implementation_selectors;
-	  allow_user = true;
-	  property_limit = 3;
-	  property_kind = CTX_PROPERTY_NAME_LIST;
-	  break;
-	case 'u': /* user */
-	  selectors = omp_user_selectors;
-	  property_limit = 1;
-	  property_kind = CTX_PROPERTY_EXPR;
-	  break;
-	default:
-	  gcc_unreachable ();
-	}
-      for (int i = 0; ; i++)
+      if (sel == OMP_TRAIT_INVALID)
 	{
-	  if (selectors[i] == NULL)
+	  /* Per the spec, "Implementations can ignore specified selectors
+	     that are not those described in this section"; however, we
+	     must record such selectors because they cause match failures.  */
+	  warning_at (c_parser_peek_token (parser)->location, OPT_Wopenmp,
+		      "unknown selector %qs for context selector set %qs",
+		      IDENTIFIER_POINTER (selector),  omp_tss_map[set]);
+	  c_parser_consume_token (parser);
+	  ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
+	  if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
+	    c_parser_balanced_token_sequence (parser);
+	  if (c_parser_next_token_is (parser, CPP_COMMA))
 	    {
-	      if (allow_user)
-		{
-		  property_kind = CTX_PROPERTY_USER;
-		  break;
-		}
-	      else
-		{
-		  error_at (c_parser_peek_token (parser)->location,
-			    "selector %qs not allowed for context selector "
-			    "set %qs", IDENTIFIER_POINTER (selector),
-			    IDENTIFIER_POINTER (set));
-		  c_parser_consume_token (parser);
-		  return error_mark_node;
-		}
+	      c_parser_consume_token (parser);
+	      continue;
 	    }
-	  if (i == property_limit)
-	    property_kind = CTX_PROPERTY_NONE;
-	  if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
+	  else
 	    break;
 	}
-      if (property_kind == CTX_PROPERTY_NAME_LIST
-	  && IDENTIFIER_POINTER (set)[0] == 'i'
-	  && strcmp (IDENTIFIER_POINTER (selector),
-		     "atomic_default_mem_order") == 0)
-	property_kind = CTX_PROPERTY_ID;
 
       c_parser_consume_token (parser);
 
+      tree properties = NULL_TREE;
+      tree scoreval = NULL_TREE;
+      enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
+      bool allow_score = omp_ts_map[sel].allow_score;
+      tree t;
+
       if (c_parser_next_token_is (parser, CPP_OPEN_PAREN))
 	{
-	  if (property_kind == CTX_PROPERTY_NONE)
+	  if (property_kind == OMP_TRAIT_PROPERTY_NONE)
 	    {
 	      error_at (c_parser_peek_token (parser)->location,
 			"selector %qs does not accept any properties",
@@ -24489,38 +24441,7 @@  c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
 
 	  switch (property_kind)
 	    {
-	      tree t;
-	    case CTX_PROPERTY_USER:
-	      do
-		{
-		  t = c_parser_expr_no_commas (parser, NULL).value;
-		  if (TREE_CODE (t) == STRING_CST)
-		    properties = make_trait_property (NULL_TREE, t,
-						      properties);
-		  else if (t != error_mark_node)
-		    {
-		      mark_exp_read (t);
-		      t = c_fully_fold (t, false, NULL);
-		      if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
-			  || !tree_fits_shwi_p (t))
-			error_at (token->location, "property must be "
-				  "constant integer expression or string "
-				  "literal");
-		      else
-			properties = make_trait_property (NULL_TREE, t,
-							  properties);
-		    }
-		  else
-		    return error_mark_node;
-
-		  if (c_parser_next_token_is (parser, CPP_COMMA))
-		    c_parser_consume_token (parser);
-		  else
-		    break;
-		}
-	      while (1);
-	      break;
-	    case CTX_PROPERTY_ID:
+	    case OMP_TRAIT_PROPERTY_ID:
 	      if (c_parser_next_token_is (parser, CPP_KEYWORD)
 		  || c_parser_next_token_is (parser, CPP_NAME))
 		{
@@ -24535,7 +24456,7 @@  c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
 		  return error_mark_node;
 		}
 	      break;
-	    case CTX_PROPERTY_NAME_LIST:
+	    case OMP_TRAIT_PROPERTY_NAME_LIST:
 	      do
 		{
 		  tree prop = OMP_TP_NAMELIST_NODE;
@@ -24565,12 +24486,14 @@  c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
 		}
 	      while (1);
 	      break;
-	    case CTX_PROPERTY_EXPR:
+	    case OMP_TRAIT_PROPERTY_EXPR:
 	      t = c_parser_expr_no_commas (parser, NULL).value;
 	      if (t != error_mark_node)
 		{
 		  mark_exp_read (t);
 		  t = c_fully_fold (t, false, NULL);
+		  /* FIXME: this is bogus, both device_num and
+		     condition selectors allow arbitrary expressions.  */
 		  if (!INTEGRAL_TYPE_P (TREE_TYPE (t))
 		      || !tree_fits_shwi_p (t))
 		    error_at (token->location, "property must be "
@@ -24582,7 +24505,9 @@  c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
 	      else
 		return error_mark_node;
 	      break;
-	    case CTX_PROPERTY_SIMD:
+	    case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
+	      gcc_assert (sel == OMP_TRAIT_CONSTRUCT_SIMD);
+
 	      if (parms == NULL_TREE)
 		{
 		  error_at (token->location, "properties for %<simd%> "
@@ -24607,15 +24532,15 @@  c_parser_omp_context_selector (c_parser *parser, tree set, tree parms)
 	  parens.skip_until_found_close (parser);
 	  properties = nreverse (properties);
 	}
-      else if (property_kind == CTX_PROPERTY_NAME_LIST
-	       || property_kind == CTX_PROPERTY_ID
-	       || property_kind == CTX_PROPERTY_EXPR)
+      else if (property_kind != OMP_TRAIT_PROPERTY_NONE
+	       && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
+	       && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
 	{
 	  c_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>");
 	  return error_mark_node;
 	}
 
-      ret = make_trait_selector (selector, scoreval, properties, ret);
+      ret = make_trait_selector (sel, scoreval, properties, ret);
 
       if (c_parser_next_token_is (parser, CPP_COMMA))
 	c_parser_consume_token (parser);
@@ -24649,35 +24574,17 @@  c_parser_omp_context_selector_specification (c_parser *parser, tree parms)
       const char *setp = "";
       if (c_parser_next_token_is (parser, CPP_NAME))
 	setp = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value);
-      switch (setp[0])
-	{
-	case 'c':
-	  if (strcmp (setp, "construct") == 0)
-	    setp = NULL;
-	  break;
-	case 'd':
-	  if (strcmp (setp, "device") == 0)
-	    setp = NULL;
-	  break;
-	case 'i':
-	  if (strcmp (setp, "implementation") == 0)
-	    setp = NULL;
-	  break;
-	case 'u':
-	  if (strcmp (setp, "user") == 0)
-	    setp = NULL;
-	  break;
-	default:
-	  break;
-	}
-      if (setp)
+      enum omp_tss_code set = omp_lookup_tss_code (setp);
+
+      if (set == OMP_TRAIT_SET_INVALID)
 	{
+	  /* FIXME: hardwired list of names here is incomplete and
+	     liable to bit-rot.  */
 	  c_parser_error (parser, "expected %<construct%>, %<device%>, "
 				  "%<implementation%> or %<user%>");
 	  return error_mark_node;
 	}
 
-      tree set = c_parser_peek_token (parser)->value;
       c_parser_consume_token (parser);
 
       if (!c_parser_require (parser, CPP_EQ, "expected %<=%>"))
@@ -24774,7 +24681,8 @@  c_finish_omp_declare_variant (c_parser *parser, tree fndecl, tree parms)
 	  error_at (token->location, "variant %qD is not a function", variant);
 	  variant = error_mark_node;
 	}
-      else if (omp_get_context_selector (ctx, "construct", "simd") == NULL_TREE
+      else if (!omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
+					  OMP_TRAIT_CONSTRUCT_SIMD)
 	       && !comptypes (TREE_TYPE (fndecl), TREE_TYPE (variant)))
 	{
 	  error_at (token->location, "variant %qD and base %qD have "
@@ -24795,7 +24703,8 @@  c_finish_omp_declare_variant (c_parser *parser, tree fndecl, tree parms)
       if (variant != error_mark_node)
 	{
 	  C_DECL_USED (variant) = 1;
-	  tree construct = omp_get_context_selector (ctx, "construct", NULL);
+	  tree construct
+	    = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
 	  omp_mark_declare_variant (match_loc, variant, construct);
 	  if (omp_context_selector_matches (ctx))
 	    {
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 4b685270097..1844c923b7f 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -8097,12 +8097,13 @@  omp_declare_variant_finalize_one (tree decl, tree attr)
     }
 
   tree ctx = TREE_VALUE (TREE_VALUE (attr));
-  tree simd = omp_get_context_selector (ctx, "construct", "simd");
+  tree simd = omp_get_context_selector (ctx, OMP_TRAIT_SET_CONSTRUCT,
+					OMP_TRAIT_CONSTRUCT_SIMD);
   if (simd)
     {
       TREE_VALUE (simd)
 	= c_omp_declare_simd_clauses_to_numbers (DECL_ARGUMENTS (decl),
-						 TREE_VALUE (simd));
+						 OMP_TS_PROPERTIES (simd));
       /* FIXME, adjusting simd args unimplemented.  */
       return true;
     }
@@ -8195,7 +8196,8 @@  omp_declare_variant_finalize_one (tree decl, tree attr)
 	}
       else
 	{
-	  tree construct = omp_get_context_selector (ctx, "construct", NULL);
+	  tree construct
+	    = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
 	  omp_mark_declare_variant (match_loc, variant, construct);
 	  if (!omp_context_selector_matches (ctx))
 	    return true;
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index eb1b0f26003..6cde6159cf1 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -47325,16 +47325,6 @@  cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
     }
 }
 
-static const char *const omp_construct_selectors[] = {
-  "simd", "target", "teams", "parallel", "for", NULL };
-static const char *const omp_device_selectors[] = {
-  "kind", "isa", "arch", NULL };
-static const char *const omp_implementation_selectors[] = {
-  "vendor", "extension", "atomic_default_mem_order", "unified_address",
-  "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
-static const char *const omp_user_selectors[] = {
-  "condition", NULL };
-
 /* OpenMP 5.0:
 
    trait-selector:
@@ -47347,7 +47337,8 @@  static const char *const omp_user_selectors[] = {
    trait-selector-set SET.  */
 
 static tree
-cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
+cp_parser_omp_context_selector (cp_parser *parser, enum omp_tss_code set,
+				bool has_parms_p)
 {
   tree ret = NULL_TREE;
   do
@@ -47362,78 +47353,44 @@  cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
 	  return error_mark_node;
 	}
 
-      tree properties = NULL_TREE;
-      tree scoreval = NULL_TREE;
-      const char *const *selectors = NULL;
-      bool allow_score = true;
-      bool allow_user = false;
-      int property_limit = 0;
-      enum { CTX_PROPERTY_NONE, CTX_PROPERTY_USER, CTX_PROPERTY_NAME_LIST,
-	     CTX_PROPERTY_ID, CTX_PROPERTY_EXPR,
-	     CTX_PROPERTY_SIMD } property_kind = CTX_PROPERTY_NONE;
-      switch (IDENTIFIER_POINTER (set)[0])
-	{
-	case 'c': /* construct */
-	  selectors = omp_construct_selectors;
-	  allow_score = false;
-	  property_limit = 1;
-	  property_kind = CTX_PROPERTY_SIMD;
-	  break;
-	case 'd': /* device */
-	  selectors = omp_device_selectors;
-	  allow_score = false;
-	  allow_user = true;
-	  property_limit = 3;
-	  property_kind = CTX_PROPERTY_NAME_LIST;
-	  break;
-	case 'i': /* implementation */
-	  selectors = omp_implementation_selectors;
-	  allow_user = true;
-	  property_limit = 3;
-	  property_kind = CTX_PROPERTY_NAME_LIST;
-	  break;
-	case 'u': /* user */
-	  selectors = omp_user_selectors;
-	  property_limit = 1;
-	  property_kind = CTX_PROPERTY_EXPR;
-	  break;
-	default:
-	  gcc_unreachable ();
-	}
-      for (int i = 0; ; i++)
+      enum omp_ts_code sel
+	= omp_lookup_ts_code (set, IDENTIFIER_POINTER (selector));
+
+      if (sel == OMP_TRAIT_INVALID)
 	{
-	  if (selectors[i] == NULL)
+	  /* Per the spec, "Implementations can ignore specified selectors
+	     that are not those described in this section"; however, we
+	     must record such selectors because they cause match failures.  */
+	  warning_at (cp_lexer_peek_token (parser->lexer)->location,
+		      OPT_Wopenmp,
+		      "unknown selector %qs for context selector set %qs",
+		      IDENTIFIER_POINTER (selector),  omp_tss_map[set]);
+	  cp_lexer_consume_token (parser->lexer);
+	  ret = make_trait_selector (sel, NULL_TREE, NULL_TREE, ret);
+	  if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
+	    for (size_t n = cp_parser_skip_balanced_tokens (parser, 1) - 1;
+		 n; --n)
+	      cp_lexer_consume_token (parser->lexer);
+	  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
 	    {
-	      if (allow_user)
-		{
-		  property_kind = CTX_PROPERTY_USER;
-		  break;
-		}
-	      else
-		{
-		  error ("selector %qs not allowed for context selector "
-			 "set %qs", IDENTIFIER_POINTER (selector),
-			 IDENTIFIER_POINTER (set));
-		  cp_lexer_consume_token (parser->lexer);
-		  return error_mark_node;
-		}
+	      cp_lexer_consume_token (parser->lexer);
+	      continue;
 	    }
-	  if (i == property_limit)
-	    property_kind = CTX_PROPERTY_NONE;
-	  if (strcmp (selectors[i], IDENTIFIER_POINTER (selector)) == 0)
+	  else
 	    break;
 	}
-      if (property_kind == CTX_PROPERTY_NAME_LIST
-	  && IDENTIFIER_POINTER (set)[0] == 'i'
-	  && strcmp (IDENTIFIER_POINTER (selector),
-		     "atomic_default_mem_order") == 0)
-	property_kind = CTX_PROPERTY_ID;
 
       cp_lexer_consume_token (parser->lexer);
 
+      tree properties = NULL_TREE;
+      tree scoreval = NULL_TREE;
+      enum omp_tp_type property_kind = omp_ts_map[sel].tp_type;
+      bool allow_score = omp_ts_map[sel].allow_score;
+      tree t;
+
       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
 	{
-	  if (property_kind == CTX_PROPERTY_NONE)
+	  if (property_kind == OMP_TRAIT_PROPERTY_NONE)
 	    {
 	      error ("selector %qs does not accept any properties",
 		     IDENTIFIER_POINTER (selector));
@@ -47490,38 +47447,7 @@  cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
 
 	  switch (property_kind)
 	    {
-	      tree t;
-	    case CTX_PROPERTY_USER:
-	      do
-		{
-		  t = cp_parser_constant_expression (parser);
-		  if (t != error_mark_node)
-		    {
-		      t = fold_non_dependent_expr (t);
-		      if (TREE_CODE (t) == STRING_CST)
-			properties = make_trait_property (NULL_TREE, t,
-							  properties);
-		      else if (!value_dependent_expression_p (t)
-			       && (!INTEGRAL_TYPE_P (TREE_TYPE (t))
-				   || !tree_fits_shwi_p (t)))
-			error_at (token->location, "property must be "
-				  "constant integer expression or string "
-				  "literal");
-		      else
-			properties = make_trait_property (NULL_TREE, t,
-							  properties);
-		    }
-		  else
-		    return error_mark_node;
-
-		  if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
-		    cp_lexer_consume_token (parser->lexer);
-		  else
-		    break;
-		}
-	      while (1);
-	      break;
-	    case CTX_PROPERTY_ID:
+	    case OMP_TRAIT_PROPERTY_ID:
 	      if (cp_lexer_next_token_is (parser->lexer, CPP_KEYWORD)
 		  || cp_lexer_next_token_is (parser->lexer, CPP_NAME))
 		{
@@ -47536,7 +47462,7 @@  cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
 		  return error_mark_node;
 		}
 	      break;
-	    case CTX_PROPERTY_NAME_LIST:
+	    case OMP_TRAIT_PROPERTY_NAME_LIST:
 	      do
 		{
 		  tree prop = OMP_TP_NAMELIST_NODE;
@@ -47567,7 +47493,9 @@  cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
 		}
 	      while (1);
 	      break;
-	    case CTX_PROPERTY_EXPR:
+	    case OMP_TRAIT_PROPERTY_EXPR:
+	      /* FIXME: this is bogus, the expression need
+		 not be constant.  */
 	      t = cp_parser_constant_expression (parser);
 	      if (t != error_mark_node)
 		{
@@ -47584,7 +47512,9 @@  cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
 	      else
 		return error_mark_node;
 	      break;
-	    case CTX_PROPERTY_SIMD:
+	    case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
+	      gcc_assert (sel == OMP_TRAIT_CONSTRUCT_SIMD);
+
 	      if (!has_parms_p)
 		{
 		  error_at (token->location, "properties for %<simd%> "
@@ -47606,15 +47536,15 @@  cp_parser_omp_context_selector (cp_parser *parser, tree set, bool has_parms_p)
 
 	  properties = nreverse (properties);
 	}
-      else if (property_kind == CTX_PROPERTY_NAME_LIST
-	       || property_kind == CTX_PROPERTY_ID
-	       || property_kind == CTX_PROPERTY_EXPR)
+      else if (property_kind != OMP_TRAIT_PROPERTY_NONE
+	       && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
+	       && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
 	{
 	  cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
 	  return error_mark_node;
 	}
 
-      ret = make_trait_selector (selector, scoreval, properties, ret);
+      ret = make_trait_selector (sel, scoreval, properties, ret);
 
       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
 	cp_lexer_consume_token (parser->lexer);
@@ -47650,35 +47580,17 @@  cp_parser_omp_context_selector_specification (cp_parser *parser,
       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
 	setp
 	  = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
-      switch (setp[0])
-	{
-	case 'c':
-	  if (strcmp (setp, "construct") == 0)
-	    setp = NULL;
-	  break;
-	case 'd':
-	  if (strcmp (setp, "device") == 0)
-	    setp = NULL;
-	  break;
-	case 'i':
-	  if (strcmp (setp, "implementation") == 0)
-	    setp = NULL;
-	  break;
-	case 'u':
-	  if (strcmp (setp, "user") == 0)
-	    setp = NULL;
-	  break;
-	default:
-	  break;
-	}
-      if (setp)
+      enum omp_tss_code set = omp_lookup_tss_code (setp);
+
+      if (set == OMP_TRAIT_SET_INVALID)
 	{
+	  /* FIXME: hardwired list of names here is incomplete and
+	     liable to bit-rot.  */
 	  cp_parser_error (parser, "expected %<construct%>, %<device%>, "
 				   "%<implementation%> or %<user%>");
 	  return error_mark_node;
 	}
 
-      tree set = cp_lexer_peek_token (parser->lexer)->u.value;
       cp_lexer_consume_token (parser->lexer);
 
       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index ca4975dcd6f..42d020b105d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -11818,18 +11818,20 @@  tsubst_attribute (tree t, tree *decl_p, tree args,
       tree chain = TREE_CHAIN (val);
       location_t match_loc = cp_expr_loc_or_input_loc (TREE_PURPOSE (chain));
       tree ctx = copy_list (TREE_VALUE (val));
-      tree simd = get_identifier ("simd");
-      tree condition = get_identifier ("condition");
       for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
 	{
-	  const char *set = IDENTIFIER_POINTER (OMP_TSS_ID (tss));
+	  enum omp_tss_code set = OMP_TSS_CODE (tss);
 	  tree selectors = NULL_TREE;
 	  for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
 	       ts = TREE_CHAIN (ts))
 	    {
 	      tree properties = NULL_TREE;
 	      tree scoreval = NULL_TREE;
-	      if (OMP_TS_ID (ts) == simd && set[0] == 'c')
+	      /* FIXME: The body of this loop should really be dispatching
+		 according to omp_ts_map[OMP_TS_CODE (TS)].tp_type instead
+		 of having hard-wired knowledge of specific selectors.  */
+	      if (OMP_TS_CODE (ts) == OMP_TRAIT_CONSTRUCT_SIMD
+		  && set == OMP_TRAIT_SET_CONSTRUCT)
 		{
 		  tree clauses = OMP_TS_PROPERTIES (ts);
 		  clauses = tsubst_omp_clauses (clauses,
@@ -11874,7 +11876,8 @@  tsubst_attribute (tree t, tree *decl_p, tree args,
 		    else if (OMP_TP_VALUE (p))
 		      {
 			bool allow_string
-			  = (OMP_TS_ID (ts) != condition || set[0] != 'u');
+			  = (OMP_TS_CODE (ts) != OMP_TRAIT_USER_CONDITION
+			     || set != OMP_TRAIT_SET_USER);
 			tree v = OMP_TP_VALUE (p);
 			if (TREE_CODE (v) == STRING_CST && allow_string)
 			  continue;
@@ -11898,7 +11901,7 @@  tsubst_attribute (tree t, tree *decl_p, tree args,
 			OMP_TP_VALUE (p) = v;
 		      }
 		}
-	      selectors = make_trait_selector (OMP_TS_ID (ts), scoreval,
+	      selectors = make_trait_selector (OMP_TS_CODE (ts), scoreval,
 					       properties, selectors);
 	    }
 	  OMP_TSS_TRAIT_SELECTORS (tss) = nreverse (selectors);
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index a77441f38e7..df423f3b4ba 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -1641,21 +1641,13 @@  typedef struct gfc_omp_declare_simd
 gfc_omp_declare_simd;
 #define gfc_get_omp_declare_simd() XCNEW (gfc_omp_declare_simd)
 
-
-enum gfc_omp_trait_property_kind
-{
-  CTX_PROPERTY_NONE,
-  CTX_PROPERTY_USER,
-  CTX_PROPERTY_NAME_LIST,
-  CTX_PROPERTY_ID,
-  CTX_PROPERTY_EXPR,
-  CTX_PROPERTY_SIMD
-};
+/* For OpenMP trait selector enum types and tables.  */
+#include "omp-selectors.h"
 
 typedef struct gfc_omp_trait_property
 {
   struct gfc_omp_trait_property *next;
-  enum gfc_omp_trait_property_kind property_kind;
+  enum omp_tp_type property_kind;
   bool is_name : 1;
 
   union
@@ -1671,8 +1663,7 @@  typedef struct gfc_omp_trait_property
 typedef struct gfc_omp_selector
 {
   struct gfc_omp_selector *next;
-
-  char *trait_selector_name;
+  enum omp_ts_code code;
   gfc_expr *score;
   struct gfc_omp_trait_property *properties;
 } gfc_omp_selector;
@@ -1681,8 +1672,7 @@  typedef struct gfc_omp_selector
 typedef struct gfc_omp_set_selector
 {
   struct gfc_omp_set_selector *next;
-
-  const char *trait_set_selector_name;
+  enum omp_tss_code code;
   struct gfc_omp_selector *trait_selectors;
 } gfc_omp_set_selector;
 #define gfc_get_omp_set_selector() XCNEW (gfc_omp_set_selector)
diff --git a/gcc/fortran/openmp.cc b/gcc/fortran/openmp.cc
index 794df19a4d1..1cf04722d00 100644
--- a/gcc/fortran/openmp.cc
+++ b/gcc/fortran/openmp.cc
@@ -269,14 +269,14 @@  gfc_free_omp_trait_property_list (gfc_omp_trait_property *list)
       list = list->next;
       switch (current->property_kind)
 	{
-	case CTX_PROPERTY_ID:
+	case OMP_TRAIT_PROPERTY_ID:
 	  free (current->name);
 	  break;
-	case CTX_PROPERTY_NAME_LIST:
+	case OMP_TRAIT_PROPERTY_NAME_LIST:
 	  if (current->is_name)
 	    free (current->name);
 	  break;
-	case CTX_PROPERTY_SIMD:
+	case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
 	  gfc_free_omp_clauses (current->clauses);
 	  break;
 	default:
@@ -5584,17 +5584,55 @@  cleanup:
   return MATCH_ERROR;
 }
 
+/* Skip over and ignore trait-property-extensions.
 
-static const char *const omp_construct_selectors[] = {
-  "simd", "target", "teams", "parallel", "do", NULL };
-static const char *const omp_device_selectors[] = {
-  "kind", "isa", "arch", NULL };
-static const char *const omp_implementation_selectors[] = {
-  "vendor", "extension", "atomic_default_mem_order", "unified_address",
-  "unified_shared_memory", "dynamic_allocators", "reverse_offload", NULL };
-static const char *const omp_user_selectors[] = {
-  "condition", NULL };
+   trait-property-extension :
+     trait-property-name
+     identifier (trait-property-extension[, trait-property-extension[, ...]])
+     constant integer expression
+ */
 
+static match gfc_ignore_trait_property_extension_list (void);
+
+static match
+gfc_ignore_trait_property_extension (void)
+{
+  char buf[GFC_MAX_SYMBOL_LEN + 1];
+  gfc_expr *expr;
+
+  /* Identifier form of trait-property name, possibly followed by
+     a list of (recursive) trait-property-extensions.  */
+  if (gfc_match_name (buf) == MATCH_YES)
+    {
+      if (gfc_match (" (") == MATCH_YES)
+	return gfc_ignore_trait_property_extension_list ();
+      return MATCH_YES;
+    }
+
+  /* Literal constant.  */
+  if (gfc_match_literal_constant (&expr, 0) == MATCH_YES)
+    return MATCH_YES;
+
+  /* FIXME: constant integer expressions.  */
+  gfc_error ("Expected trait-property-extension at %C");
+  return MATCH_ERROR;
+}
+
+static match
+gfc_ignore_trait_property_extension_list (void)
+{
+  while (1)
+    {
+      if (gfc_ignore_trait_property_extension () != MATCH_YES)
+	return MATCH_ERROR;
+      if (gfc_match (" ,") == MATCH_YES)
+	continue;
+      if (gfc_match (" )") == MATCH_YES)
+	return MATCH_YES;
+      gfc_error ("expected %<)%> at %C");
+      return MATCH_ERROR;
+    }
+}
 
 /* OpenMP 5.0:
 
@@ -5618,75 +5656,37 @@  gfc_match_omp_context_selector (gfc_omp_set_selector *oss)
 	}
 
       gfc_omp_selector *os = gfc_get_omp_selector ();
-      os->trait_selector_name = XNEWVEC (char, strlen (selector) + 1);
-      strcpy (os->trait_selector_name, selector);
+      if (oss->code == OMP_TRAIT_SET_CONSTRUCT
+	  && !strcmp (selector, "do"))
+	os->code = OMP_TRAIT_CONSTRUCT_FOR;
+      else if (oss->code == OMP_TRAIT_SET_CONSTRUCT
+	       && !strcmp (selector, "for"))
+	os->code = OMP_TRAIT_INVALID;
+      else
+	os->code = omp_lookup_ts_code (oss->code, selector);
       os->next = oss->trait_selectors;
       oss->trait_selectors = os;
 
-      const char *const *selectors = NULL;
-      bool allow_score = true;
-      bool allow_user = false;
-      int property_limit = 0;
-      enum gfc_omp_trait_property_kind property_kind = CTX_PROPERTY_NONE;
-      switch (oss->trait_set_selector_name[0])
-	{
-	case 'c': /* construct */
-	  selectors = omp_construct_selectors;
-	  allow_score = false;
-	  property_limit = 1;
-	  property_kind = CTX_PROPERTY_SIMD;
-	  break;
-	case 'd': /* device */
-	  selectors = omp_device_selectors;
-	  allow_score = false;
-	  allow_user = true;
-	  property_limit = 3;
-	  property_kind = CTX_PROPERTY_NAME_LIST;
-	  break;
-	case 'i': /* implementation */
-	  selectors = omp_implementation_selectors;
-	  allow_user = true;
-	  property_limit = 3;
-	  property_kind = CTX_PROPERTY_NAME_LIST;
-	  break;
-	case 'u': /* user */
-	  selectors = omp_user_selectors;
-	  property_limit = 1;
-	  property_kind = CTX_PROPERTY_EXPR;
-	  break;
-	default:
-	  gcc_unreachable ();
-	}
-      for (int i = 0; ; i++)
+      if (os->code == OMP_TRAIT_INVALID)
 	{
-	  if (selectors[i] == NULL)
-	    {
-	      if (allow_user)
-		{
-		  property_kind = CTX_PROPERTY_USER;
-		  break;
-		}
-	      else
-		{
-		  gfc_error ("selector %qs not allowed for context selector "
-			     "set %qs at %C",
-			     selector, oss->trait_set_selector_name);
-		  return MATCH_ERROR;
-		}
-	    }
-	  if (i == property_limit)
-	    property_kind = CTX_PROPERTY_NONE;
-	  if (strcmp (selectors[i], selector) == 0)
-	    break;
+	  gfc_warning (OPT_Wopenmp,
+		       "unknown selector %qs for context selector set %qs "
+		       "at %C",
+		       selector, omp_tss_map[oss->code]);
+	  if (gfc_match (" (") == MATCH_YES
+	      && gfc_ignore_trait_property_extension_list () != MATCH_YES)
+	    return MATCH_ERROR;
+	  if (gfc_match (" ,") == MATCH_YES)
+	    continue;
+	  break;
 	}
-      if (property_kind == CTX_PROPERTY_NAME_LIST
-	  && oss->trait_set_selector_name[0] == 'i'
-	  && strcmp (selector, "atomic_default_mem_order") == 0)
-	property_kind = CTX_PROPERTY_ID;
+
+      enum omp_tp_type property_kind = omp_ts_map[os->code].tp_type;
+      bool allow_score = omp_ts_map[os->code].allow_score;
 
       if (gfc_match (" (") == MATCH_YES)
 	{
-	  if (property_kind == CTX_PROPERTY_NONE)
+	  if (property_kind == OMP_TRAIT_PROPERTY_NONE)
 	    {
 	      gfc_error ("selector %qs does not accept any properties at %C",
 			 selector);
@@ -5737,22 +5737,7 @@  gfc_match_omp_context_selector (gfc_omp_set_selector *oss)
 
 	  switch (property_kind)
 	    {
-	    case CTX_PROPERTY_USER:
-	      do
-		{
-		  if (gfc_match_expr (&otp->expr) != MATCH_YES)
-		    {
-		      gfc_error ("property must be constant integer "
-				 "expression or string literal at %C");
-		      return MATCH_ERROR;
-		    }
-
-		  if (gfc_match (" ,") != MATCH_YES)
-		    break;
-		}
-	      while (1);
-	      break;
-	    case CTX_PROPERTY_ID:
+	    case OMP_TRAIT_PROPERTY_ID:
 	      {
 		char buf[GFC_MAX_SYMBOL_LEN + 1];
 		if (gfc_match_name (buf) == MATCH_YES)
@@ -5767,7 +5752,7 @@  gfc_match_omp_context_selector (gfc_omp_set_selector *oss)
 		  }
 	      }
 	      break;
-	    case CTX_PROPERTY_NAME_LIST:
+	    case OMP_TRAIT_PROPERTY_NAME_LIST:
 	      do
 		{
 		  char buf[GFC_MAX_SYMBOL_LEN + 1];
@@ -5798,7 +5783,7 @@  gfc_match_omp_context_selector (gfc_omp_set_selector *oss)
 		}
 	      while (1);
 	      break;
-	    case CTX_PROPERTY_EXPR:
+	    case OMP_TRAIT_PROPERTY_EXPR:
 	      if (gfc_match_expr (&otp->expr) != MATCH_YES)
 		{
 		  gfc_error ("expected expression at %C");
@@ -5814,7 +5799,7 @@  gfc_match_omp_context_selector (gfc_omp_set_selector *oss)
 		  return MATCH_ERROR;
 		}
 	      break;
-	    case CTX_PROPERTY_SIMD:
+	    case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
 	      {
 		if (gfc_match_omp_clauses (&otp->clauses,
 					   OMP_DECLARE_SIMD_CLAUSES,
@@ -5836,9 +5821,9 @@  gfc_match_omp_context_selector (gfc_omp_set_selector *oss)
 	      return MATCH_ERROR;
 	    }
 	}
-      else if (property_kind == CTX_PROPERTY_NAME_LIST
-	       || property_kind == CTX_PROPERTY_ID
-	       || property_kind == CTX_PROPERTY_EXPR)
+      else if (property_kind != OMP_TRAIT_PROPERTY_NONE
+	       && property_kind != OMP_TRAIT_PROPERTY_CLAUSE_LIST
+	       && property_kind != OMP_TRAIT_PROPERTY_EXTENSION)
 	{
 	  if (gfc_match (" (") != MATCH_YES)
 	    {
@@ -5874,20 +5859,16 @@  gfc_match_omp_context_selector_specification (gfc_omp_declare_variant *odv)
   do
     {
       match m;
-      const char *selector_sets[] = { "construct", "device",
-				      "implementation", "user" };
-      const int selector_set_count = ARRAY_SIZE (selector_sets);
-      int i;
       char buf[GFC_MAX_SYMBOL_LEN + 1];
+      enum omp_tss_code set = OMP_TRAIT_SET_INVALID;
 
       m = gfc_match_name (buf);
       if (m == MATCH_YES)
-	for (i = 0; i < selector_set_count; i++)
-	  if (strcmp (buf, selector_sets[i]) == 0)
-	    break;
+	set = omp_lookup_tss_code (buf);
 
-      if (m != MATCH_YES || i == selector_set_count)
+      if (set == OMP_TRAIT_SET_INVALID)
 	{
+	  /* FIXME: hard-coded list of set names is prone to bit-rot.  */
 	  gfc_error ("expected %<construct%>, %<device%>, %<implementation%> "
 		     "or %<user%> at %C");
 	  return MATCH_ERROR;
@@ -5909,7 +5890,7 @@  gfc_match_omp_context_selector_specification (gfc_omp_declare_variant *odv)
 
       gfc_omp_set_selector *oss = gfc_get_omp_set_selector ();
       oss->next = odv->set_selectors;
-      oss->trait_set_selector_name = selector_sets[i];
+      oss->code = set;
       odv->set_selectors = oss;
 
       if (gfc_match_omp_context_selector (oss) != MATCH_YES)
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 60154ff3751..ea7ae0fa0c0 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -8208,18 +8208,32 @@  gfc_trans_omp_declare_variant (gfc_namespace *ns)
 	{
 	  tree selectors = NULL_TREE;
 	  gfc_omp_selector *os;
+	  enum omp_tss_code set = oss->code;
+	  gcc_assert (set != OMP_TRAIT_SET_INVALID);
+
 	  for (os = oss->trait_selectors; os; os = os->next)
 	    {
 	      tree scoreval = NULL_TREE;
 	      tree properties = NULL_TREE;
 	      gfc_omp_trait_property *otp;
+	      enum omp_ts_code sel = os->code;
+
+	      /* Per the spec, "Implementations can ignore specified
+		 selectors that are not those described in this section";
+		 however, we  must record such selectors because they
+		 cause match failures.  */
+	      if (sel == OMP_TRAIT_INVALID)
+		{
+		  selectors = make_trait_selector (sel, NULL_TREE, NULL_TREE,
+						   selectors);
+		  continue;
+		}
 
 	      for (otp = os->properties; otp; otp = otp->next)
 		{
 		  switch (otp->property_kind)
 		    {
-		    case CTX_PROPERTY_USER:
-		    case CTX_PROPERTY_EXPR:
+		    case OMP_TRAIT_PROPERTY_EXPR:
 		      {
 			gfc_se se;
 			gfc_init_se (&se, NULL);
@@ -8228,12 +8242,12 @@  gfc_trans_omp_declare_variant (gfc_namespace *ns)
 							  properties);
 		      }
 		      break;
-		    case CTX_PROPERTY_ID:
+		    case OMP_TRAIT_PROPERTY_ID:
 		      properties
 			= make_trait_property (get_identifier (otp->name),
 					       NULL_TREE, properties);
 		      break;
-		    case CTX_PROPERTY_NAME_LIST:
+		    case OMP_TRAIT_PROPERTY_NAME_LIST:
 		      {
 			tree prop = OMP_TP_NAMELIST_NODE;
 			tree value = NULL_TREE;
@@ -8246,7 +8260,7 @@  gfc_trans_omp_declare_variant (gfc_namespace *ns)
 							  properties);
 		      }
 		      break;
-		    case CTX_PROPERTY_SIMD:
+		    case OMP_TRAIT_PROPERTY_CLAUSE_LIST:
 		      properties = gfc_trans_omp_clauses (NULL, otp->clauses,
 							  odv->where, true);
 		      break;
@@ -8263,13 +8277,10 @@  gfc_trans_omp_declare_variant (gfc_namespace *ns)
 		  scoreval = se.expr;
 		}
 
-	      tree ts_name = get_identifier (os->trait_selector_name);
-	      selectors	= make_trait_selector (ts_name, scoreval,
+	      selectors	= make_trait_selector (sel, scoreval,
 					       properties, selectors);
 	    }
-
-	  tree tss_name = get_identifier (oss->trait_set_selector_name);
-	  set_selectors = make_trait_set_selector (tss_name, selectors,
+	  set_selectors = make_trait_set_selector (set, selectors,
 						   set_selectors);
 	}
 
@@ -8298,8 +8309,10 @@  gfc_trans_omp_declare_variant (gfc_namespace *ns)
 			 variant_proc_name, &odv->where);
 	      variant_proc_sym = NULL;
 	    }
-	  else if (omp_get_context_selector (set_selectors, "construct",
-					     "simd") == NULL_TREE)
+	  else if (omp_get_context_selector (set_selectors,
+					     OMP_TRAIT_SET_CONSTRUCT,
+					     OMP_TRAIT_CONSTRUCT_SIMD)
+		   == NULL_TREE)
 	    {
 	      char err[256];
 	      if (!gfc_compare_interfaces (ns->proc_name, variant_proc_sym,
@@ -8316,8 +8329,9 @@  gfc_trans_omp_declare_variant (gfc_namespace *ns)
 	  if (variant_proc_sym != NULL)
 	    {
 	      gfc_set_sym_referenced (variant_proc_sym);
-	      tree construct = omp_get_context_selector (set_selectors,
-							 "construct", NULL);
+	      tree construct
+		= omp_get_context_selector_list (set_selectors,
+						 OMP_TRAIT_SET_CONSTRUCT);
 	      omp_mark_declare_variant (gfc_get_location (&odv->where),
 					gfc_get_symbol_decl (variant_proc_sym),
 					construct);
diff --git a/gcc/gimplify.cc b/gcc/gimplify.cc
index a71ef23c37b..ee4b0f70b86 100644
--- a/gcc/gimplify.cc
+++ b/gcc/gimplify.cc
@@ -13560,12 +13560,17 @@  omp_construct_selector_matches (enum tree_code *constructs, int nconstructs,
   if (tree attr = lookup_attribute ("omp declare variant variant",
 				    DECL_ATTRIBUTES (current_function_decl)))
     {
-      enum tree_code variant_constructs[5];
-      int variant_nconstructs = 0;
-      if (!target_seen)
-	variant_nconstructs
-	  = omp_construct_traits_to_codes (TREE_VALUE (attr),
-					   variant_constructs);
+      tree selectors = TREE_VALUE (attr);
+      int variant_nconstructs = list_length (selectors);
+      enum tree_code *variant_constructs = NULL;
+      if (!target_seen && variant_nconstructs)
+	{
+	  variant_constructs
+	    = (enum tree_code *) alloca (variant_nconstructs
+					 * sizeof (enum tree_code));
+	  omp_construct_traits_to_codes (selectors, variant_nconstructs,
+					 variant_constructs);
+	}
       for (int i = 0; i < variant_nconstructs; i++)
 	{
 	  ++cnt;
diff --git a/gcc/omp-general.cc b/gcc/omp-general.cc
index 156508eabb5..5f0cb041ffa 100644
--- a/gcc/omp-general.cc
+++ b/gcc/omp-general.cc
@@ -1013,32 +1013,29 @@  omp_max_simt_vf (void)
   return 0;
 }
 
-/* Store the construct selectors as tree codes from last to first,
-   return their number.  */
+/* Store the construct selectors as tree codes from last to first.
+   CTX is a list of trait selectors, nconstructs must be equal to its
+   length, and the array CONSTRUCTS holds the output.  */
 
-int
-omp_construct_traits_to_codes (tree ctx, enum tree_code *constructs)
+void
+omp_construct_traits_to_codes (tree ctx, int nconstructs,
+			       enum tree_code *constructs)
 {
-  int nconstructs = list_length (ctx);
   int i = nconstructs - 1;
+
+  /* Order must match the OMP_TRAIT_CONSTRUCT_* enumerators in
+     enum omp_ts_code.  */
+  static enum tree_code code_map[]
+    = { OMP_TARGET, OMP_TEAMS, OMP_PARALLEL, OMP_FOR, OMP_SIMD };
+
   for (tree ts = ctx; ts; ts = TREE_CHAIN (ts), i--)
     {
-      const char *sel = IDENTIFIER_POINTER (OMP_TS_ID (ts));
-      if (!strcmp (sel, "target"))
-	constructs[i] = OMP_TARGET;
-      else if (!strcmp (sel, "teams"))
-	constructs[i] = OMP_TEAMS;
-      else if (!strcmp (sel, "parallel"))
-	constructs[i] = OMP_PARALLEL;
-      else if (!strcmp (sel, "for") || !strcmp (sel, "do"))
-	constructs[i] = OMP_FOR;
-      else if (!strcmp (sel, "simd"))
-	constructs[i] = OMP_SIMD;
-      else
-	gcc_unreachable ();
+      enum omp_ts_code sel = OMP_TS_CODE (ts);
+      int j = (int)sel - (int)OMP_TRAIT_CONSTRUCT_TARGET;
+      gcc_assert (j >= 0 && (unsigned int) j < ARRAY_SIZE (code_map));
+      constructs[i] = code_map[j];
     }
   gcc_assert (i == -1);
-  return nconstructs;
 }
 
 /* Return true if PROP is possibly present in one of the offloading target's
@@ -1114,6 +1111,124 @@  omp_maybe_offloaded (void)
   return false;
 }
 
+/* Lookup tables for context selectors.  */
+const char *omp_tss_map[] =
+  {
+   "construct",
+   "device",
+   "target_device",
+   "implementation",
+   "user",
+   NULL
+};
+
+/* Arrays of property candidates must be null-terminated.  */
+static const char *const kind_properties[] =
+  { "host", "nohost", "cpu", "gpu", "fpga", "any", NULL };
+static const char *const vendor_properties[] =
+  { "amd", "arm", "bsc", "cray", "fujitsu", "gnu", "ibm", "intel",
+    "llvm", "nvidia", "pgi", "ti", "unknown", NULL };
+static const char *const extension_properties[] =
+  { NULL };
+static const char *const atomic_default_mem_order_properties[] =
+  { "seq_cst", "relaxed", "acq_rel", NULL };
+
+struct omp_ts_info omp_ts_map[] =
+  {
+   { "kind",
+     (1 << OMP_TRAIT_SET_DEVICE) | (1 << OMP_TRAIT_SET_TARGET_DEVICE),
+     OMP_TRAIT_PROPERTY_NAME_LIST, false,
+     kind_properties
+   },
+   { "isa",
+     (1 << OMP_TRAIT_SET_DEVICE) | (1 << OMP_TRAIT_SET_TARGET_DEVICE),
+     OMP_TRAIT_PROPERTY_NAME_LIST, false,
+     NULL
+   },
+   { "arch",
+     (1 << OMP_TRAIT_SET_DEVICE) | (1 << OMP_TRAIT_SET_TARGET_DEVICE),
+     OMP_TRAIT_PROPERTY_NAME_LIST, false,
+     NULL
+   },
+   { "device_num",
+     (1 << OMP_TRAIT_SET_TARGET_DEVICE),
+     OMP_TRAIT_PROPERTY_EXPR, false,
+     NULL
+   },
+   { "vendor",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_NAME_LIST, true,
+     vendor_properties,
+   },
+   { "extension",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_NAME_LIST, true,
+     extension_properties,
+   },
+   { "atomic_default_mem_order",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_ID, true,
+     atomic_default_mem_order_properties,
+   },
+   { "requires",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_CLAUSE_LIST, true,
+     NULL
+   },
+   { "unified_address",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_NONE, true,
+     NULL
+   },
+   { "unified_shared_memory",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_NONE, true,
+     NULL
+   },
+   { "dynamic_allocators",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_NONE, true,
+     NULL
+   },
+   { "reverse_offload",
+     (1 << OMP_TRAIT_SET_IMPLEMENTATION),
+     OMP_TRAIT_PROPERTY_NONE, true,
+     NULL
+   },
+   { "condition",
+     (1 << OMP_TRAIT_SET_USER),
+     OMP_TRAIT_PROPERTY_EXPR, true,
+     NULL
+   },
+   { "target",
+     (1 << OMP_TRAIT_SET_CONSTRUCT),
+     OMP_TRAIT_PROPERTY_NONE, false,
+     NULL
+   },
+   { "teams",
+     (1 << OMP_TRAIT_SET_CONSTRUCT),
+     OMP_TRAIT_PROPERTY_NONE, false,
+     NULL
+   },
+   { "parallel",
+     (1 << OMP_TRAIT_SET_CONSTRUCT),
+     OMP_TRAIT_PROPERTY_NONE, false,
+     NULL
+   },
+   { "for",
+     (1 << OMP_TRAIT_SET_CONSTRUCT),
+     OMP_TRAIT_PROPERTY_NONE, false,
+     NULL
+   },
+   { "simd",
+     (1 << OMP_TRAIT_SET_CONSTRUCT),
+     OMP_TRAIT_PROPERTY_CLAUSE_LIST,  false,
+     NULL
+   },
+   { NULL, 0, OMP_TRAIT_PROPERTY_NONE, false, NULL }  /* OMP_TRAIT_LAST */
+  };
+
+
 /* Return a name from PROP, a property in selectors accepting
    name lists.  */
 
@@ -1145,124 +1260,115 @@  omp_context_name_list_prop (tree prop)
 tree
 omp_check_context_selector (location_t loc, tree ctx)
 {
-  /* Each trait-set-selector-name can only be specified once.
-     There are just 4 set names.  */
-  for (tree t1 = ctx; t1; t1 = TREE_CHAIN (t1))
-    for (tree t2 = TREE_CHAIN (t1); t2; t2 = TREE_CHAIN (t2))
-      if (OMP_TSS_ID (t1) == OMP_TSS_ID (t2))
-	{
-	  error_at (loc, "selector set %qs specified more than once",
-		    IDENTIFIER_POINTER (OMP_TSS_ID (t1)));
-	  return error_mark_node;
-	}
+  bool tss_seen[OMP_TRAIT_SET_LAST], ts_seen[OMP_TRAIT_LAST];
+
+  memset (tss_seen, 0, sizeof (tss_seen));
   for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
     {
-      /* Each trait-selector-name can only be specified once.  */
-      if (list_length (OMP_TSS_TRAIT_SELECTORS (tss)) < 5)
+      enum omp_tss_code tss_code = OMP_TSS_CODE (tss);
+
+      /* Each trait-set-selector-name can only be specified once.  */
+      if (tss_seen[tss_code])
 	{
-	  for (tree ts1 = OMP_TSS_TRAIT_SELECTORS (tss); ts1;
-	       ts1 = TREE_CHAIN (ts1))
-	    for (tree ts2 = TREE_CHAIN (ts1); ts2; ts2 = TREE_CHAIN (ts2))
-	      if (OMP_TS_ID (ts1) == OMP_TS_ID (ts2))
-		{
-		  error_at (loc,
-			    "selector %qs specified more than once in set %qs",
-			    IDENTIFIER_POINTER (OMP_TS_ID (ts1)),
-			    IDENTIFIER_POINTER (OMP_TSS_ID (tss)));
-		  return error_mark_node;
-		}
+	  error_at (loc, "selector set %qs specified more than once",
+		    OMP_TSS_NAME (tss));
+	  return error_mark_node;
 	}
       else
+	tss_seen[tss_code] = true;
+
+      memset (ts_seen, 0, sizeof (ts_seen));
+      for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
 	{
-	  hash_set<tree> pset;
-	  for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts;
-	       ts = TREE_CHAIN (ts))
-	    if (pset.add (OMP_TS_ID (ts)))
+	  enum omp_ts_code ts_code = OMP_TS_CODE (ts);
+
+	  /* Ignore unknown traits.  */
+	  if (ts_code == OMP_TRAIT_INVALID)
+	    continue;
+
+	  /* Each trait-selector-name can only be specified once.  */
+	  if (ts_seen[ts_code])
+	    {
+	      error_at (loc,
+			"selector %qs specified more than once in set %qs",
+			OMP_TS_NAME (ts),
+			OMP_TSS_NAME (tss));
+	      return error_mark_node;
+	    }
+	  else
+	    ts_seen[ts_code] = true;
+
+	  if (omp_ts_map[ts_code].valid_properties == NULL)
+	    continue;
+
+	  for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
+	    for (unsigned j = 0; ; j++)
 	      {
-		error_at (loc,
-			  "selector %qs specified more than once in set %qs",
-			  IDENTIFIER_POINTER (OMP_TS_ID (ts)),
-			  IDENTIFIER_POINTER (OMP_TSS_ID (tss)));
-		return error_mark_node;
+		const char *candidate
+		  = omp_ts_map[ts_code].valid_properties[j];
+		if (candidate == NULL)
+		  {
+		    /* We've reached the end of the candidate array.  */
+		    if (ts_code == OMP_TRAIT_IMPLEMENTATION_ADMO)
+		      /* FIXME: not sure why this is an error vs warnings
+			 for the others, + incorrect/unknown wording?  */
+		      {
+			error_at (loc,
+				  "incorrect property %qs of %qs selector",
+				  IDENTIFIER_POINTER (OMP_TP_NAME (p)),
+				  "atomic_default_mem_order");
+			return error_mark_node;
+		      }
+		    if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE
+			&& (TREE_CODE (OMP_TP_VALUE (p)) == STRING_CST))
+		      warning_at (loc, OPT_Wopenmp,
+				  "unknown property %qE of %qs selector",
+				  OMP_TP_VALUE (p),
+				  OMP_TS_NAME (ts));
+		    else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
+		      warning_at (loc, OPT_Wopenmp,
+				  "unknown property %qs of %qs selector",
+				  omp_context_name_list_prop (p),
+				  OMP_TS_NAME (ts));
+		    else if (OMP_TP_NAME (p))
+		      warning_at (loc, OPT_Wopenmp,
+				  "unknown property %qs of %qs selector",
+				  IDENTIFIER_POINTER (OMP_TP_NAME (p)),
+				  OMP_TS_NAME (ts));
+		    break;
+		  }
+		else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
+		  /* Property-list traits.  */
+		  {
+		    const char *str = omp_context_name_list_prop (p);
+		    if (str && !strcmp (str, candidate))
+		      break;
+		  }
+		else if (!strcmp (IDENTIFIER_POINTER (OMP_TP_NAME (p)),
+				  candidate))
+		  /* Identifier traits.  */
+		  break;
 	      }
 	}
-
-      static const char *const kind[] = {
-	"host", "nohost", "cpu", "gpu", "fpga", "any", NULL };
-      static const char *const vendor[] = {
-	"amd", "arm", "bsc", "cray", "fujitsu", "gnu", "ibm", "intel",
-	"llvm", "nvidia", "pgi", "ti", "unknown", NULL };
-      static const char *const extension[] = { NULL };
-      static const char *const atomic_default_mem_order[] = {
-	"seq_cst", "relaxed", "acq_rel", NULL };
-      struct known_properties { const char *set; const char *selector;
-				const char *const *props; };
-      known_properties props[] = {
-	{ "device", "kind", kind },
-	{ "implementation", "vendor", vendor },
-	{ "implementation", "extension", extension },
-	{ "implementation", "atomic_default_mem_order",
-	  atomic_default_mem_order } };
-      for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
-	for (unsigned i = 0; i < ARRAY_SIZE (props); i++)
-	  if (!strcmp (IDENTIFIER_POINTER (OMP_TS_ID (ts)),
-					   props[i].selector)
-	      && !strcmp (IDENTIFIER_POINTER (OMP_TSS_ID (tss)),
-					      props[i].set))
-	    for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
-	      for (unsigned j = 0; ; j++)
-		{
-		  if (props[i].props[j] == NULL)
-		    {
-		      if (props[i].props == atomic_default_mem_order)
-			{
-			  error_at (loc,
-				    "incorrect property %qs of %qs selector",
-				    IDENTIFIER_POINTER (TREE_PURPOSE (p)),
-				    "atomic_default_mem_order");
-			  return error_mark_node;
-			}
-		      else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE
-			       && (TREE_CODE (OMP_TP_VALUE (p)) == STRING_CST))
-			warning_at (loc, 0,
-				    "unknown property %qE of %qs selector",
-				    OMP_TP_VALUE (p),
-				    props[i].selector);
-		      else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
-			warning_at (loc, 0,
-				    "unknown property %qs of %qs selector",
-				    omp_context_name_list_prop (p),
-				    props[i].selector);
-		      else if (OMP_TP_NAME (p))
-			warning_at (loc, OPT_Wopenmp,
-				    "unknown property %qs of %qs selector",
-				    IDENTIFIER_POINTER (OMP_TP_NAME (p)),
-				    props[i].selector);
-		      break;
-		    }
-		  else if (OMP_TP_NAME (p) == OMP_TP_NAMELIST_NODE)
-		    /* Property-list traits.  */
-		    {
-		      const char *str = omp_context_name_list_prop (p);
-		      if (str && !strcmp (str, props[i].props[j]))
-			break;
-		    }
-		  else if (!strcmp (IDENTIFIER_POINTER (OMP_TP_NAME (p)),
-				    props[i].props[j]))
-		    break;
-		}
     }
   return ctx;
 }
 
 
 /* Register VARIANT as variant of some base function marked with
-   #pragma omp declare variant.  CONSTRUCT is corresponding construct
-   selector set.  */
-
+   #pragma omp declare variant.  CONSTRUCT is corresponding list of
+   trait-selectors for the construct selector set.  This is stashed as the
+   value of the "omp declare variant variant" attribute on VARIANT.  */
 void
 omp_mark_declare_variant (location_t loc, tree variant, tree construct)
 {
+  /* Ignore this variant if it contains unknown construct selectors.
+     It will never match, and the front ends have already issued a warning
+     about it.  */
+  for (tree c = construct; c; c = TREE_CHAIN (c))
+    if (OMP_TS_CODE (c) == OMP_TRAIT_INVALID)
+      return;
+
   tree attr = lookup_attribute ("omp declare variant variant",
 				DECL_ATTRIBUTES (variant));
   if (attr == NULL_TREE)
@@ -1275,7 +1381,8 @@  omp_mark_declare_variant (location_t loc, tree variant, tree construct)
     }
   if ((TREE_VALUE (attr) != NULL_TREE) != (construct != NULL_TREE)
       || (construct != NULL_TREE
-	  && omp_context_selector_set_compare ("construct", TREE_VALUE (attr),
+	  && omp_context_selector_set_compare (OMP_TRAIT_SET_CONSTRUCT,
+					       TREE_VALUE (attr),
 					       construct)))
     error_at (loc, "%qD used as a variant with incompatible %<construct%> "
 		   "selector sets", variant);
@@ -1285,18 +1392,21 @@  omp_mark_declare_variant (location_t loc, tree variant, tree construct)
 /* Constructors for context selectors.  */
 
 tree
-make_trait_set_selector (tree name, tree selectors, tree chain)
+make_trait_set_selector (enum omp_tss_code code, tree selectors, tree chain)
 {
-  return tree_cons (name, selectors, chain);
+  return tree_cons (build_int_cst (integer_type_node, code),
+		    selectors, chain);
 }
 
 tree
-make_trait_selector (tree name, tree score, tree properties, tree chain)
+make_trait_selector (enum omp_ts_code code, tree score, tree properties,
+		     tree chain)
 {
   if (score == NULL_TREE)
-    return tree_cons (name, properties, chain);
+    return tree_cons (build_int_cst (integer_type_node, code),
+		      properties, chain);
   else
-    return tree_cons (name,
+    return tree_cons (build_int_cst (integer_type_node, code),
 		      tree_cons (OMP_TS_SCORE_NODE, score, properties),
 		      chain);
 }
@@ -1319,8 +1429,16 @@  omp_context_selector_matches (tree ctx)
   int ret = 1;
   for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
     {
-      char set = IDENTIFIER_POINTER (OMP_TSS_ID (tss))[0];
-      if (set == 'c')
+      enum omp_tss_code set = OMP_TSS_CODE (tss);
+      tree selectors = OMP_TSS_TRAIT_SELECTORS (tss);
+
+      /* Immediately reject the match if there are any ignored
+	 selectors present.  */
+      for (tree ts = selectors; ts; ts = TREE_CHAIN (ts))
+	if (OMP_TS_CODE (ts) == OMP_TRAIT_INVALID)
+	  return 0;
+
+      if (set == OMP_TRAIT_SET_CONSTRUCT)
 	{
 	  /* For now, ignore the construct set.  While something can be
 	     determined already during parsing, we don't know until end of TU
@@ -1335,10 +1453,20 @@  omp_context_selector_matches (tree ctx)
 	      continue;
 	    }
 
-	  enum tree_code constructs[5];
-	  int nconstructs
-	    = omp_construct_traits_to_codes (OMP_TSS_TRAIT_SELECTORS (tss),
+	  int nconstructs = list_length (selectors);
+	  enum tree_code *constructs = NULL;
+	  if (nconstructs)
+	    {
+	      /* Even though this alloca appears in a loop over selector
+		 sets, it does not repeatedly grow the stack, because
+		 there can be only one construct selector set specified.
+		 This is enforced by omp_check_context_selector.  */
+	      constructs
+		= (enum tree_code *) alloca (nconstructs
+					     * sizeof (enum tree_code));
+	      omp_construct_traits_to_codes (selectors, nconstructs,
 					     constructs);
+	    }
 
 	  if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
 	    {
@@ -1369,13 +1497,13 @@  omp_context_selector_matches (tree ctx)
 	    ret = -1;
 	  continue;
 	}
-      for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
+      for (tree ts = selectors; ts; ts = TREE_CHAIN (ts))
 	{
-	  const char *sel = IDENTIFIER_POINTER (OMP_TS_ID (ts));
-	  switch (*sel)
+	  enum omp_ts_code sel = OMP_TS_CODE (ts);
+	  switch (sel)
 	    {
-	    case 'v':
-	      if (set == 'i' && !strcmp (sel, "vendor"))
+	    case OMP_TRAIT_IMPLEMENTATION_VENDOR:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
 		  {
 		    const char *prop = omp_context_name_list_prop (p);
@@ -1386,13 +1514,13 @@  omp_context_selector_matches (tree ctx)
 		    return 0;
 		  }
 	      break;
-	    case 'e':
-	      if (set == 'i' && !strcmp (sel, "extension"))
+	    case OMP_TRAIT_IMPLEMENTATION_EXTENSION:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		/* We don't support any extensions right now.  */
 		return 0;
 	      break;
-	    case 'a':
-	      if (set == 'i' && !strcmp (sel, "atomic_default_mem_order"))
+	    case OMP_TRAIT_IMPLEMENTATION_ADMO:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		{
 		  if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
 		    break;
@@ -1424,7 +1552,9 @@  omp_context_selector_matches (tree ctx)
 			   && omo != OMP_MEMORY_ORDER_ACQ_REL)
 		    return 0;
 		}
-	      if (set == 'd' && !strcmp (sel, "arch"))
+	      break;
+	    case OMP_TRAIT_DEVICE_ARCH:
+	      if (set == OMP_TRAIT_SET_DEVICE)
 		for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
 		  {
 		    const char *arch = omp_context_name_list_prop (p);
@@ -1461,8 +1591,8 @@  omp_context_selector_matches (tree ctx)
 		      ret = -1;
 		  }
 	      break;
-	    case 'u':
-	      if (set == 'i' && !strcmp (sel, "unified_address"))
+	    case OMP_TRAIT_IMPLEMENTATION_UNIFIED_ADDRESS:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		{
 		  if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
 		    break;
@@ -1474,9 +1604,10 @@  omp_context_selector_matches (tree ctx)
 		      else
 			return 0;
 		    }
-		  break;
 		}
-	      if (set == 'i' && !strcmp (sel, "unified_shared_memory"))
+	      break;
+	    case OMP_TRAIT_IMPLEMENTATION_UNIFIED_SHARED_MEMORY:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		{
 		  if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
 		    break;
@@ -1489,11 +1620,10 @@  omp_context_selector_matches (tree ctx)
 		      else
 			return 0;
 		    }
-		  break;
 		}
 	      break;
-	    case 'd':
-	      if (set == 'i' && !strcmp (sel, "dynamic_allocators"))
+	    case OMP_TRAIT_IMPLEMENTATION_DYNAMIC_ALLOCATORS:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		{
 		  if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
 		    break;
@@ -1506,11 +1636,10 @@  omp_context_selector_matches (tree ctx)
 		      else
 			return 0;
 		    }
-		  break;
 		}
 	      break;
-	    case 'r':
-	      if (set == 'i' && !strcmp (sel, "reverse_offload"))
+	    case OMP_TRAIT_IMPLEMENTATION_REVERSE_OFFLOAD:
+	      if (set == OMP_TRAIT_SET_IMPLEMENTATION)
 		{
 		  if (cfun && (cfun->curr_properties & PROP_gimple_any) != 0)
 		    break;
@@ -1522,11 +1651,10 @@  omp_context_selector_matches (tree ctx)
 		      else
 			return 0;
 		    }
-		  break;
 		}
 	      break;
-	    case 'k':
-	      if (set == 'd' && !strcmp (sel, "kind"))
+	    case OMP_TRAIT_DEVICE_KIND:
+	      if (set == OMP_TRAIT_SET_DEVICE)
 		for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
 		  {
 		    const char *prop = omp_context_name_list_prop (p);
@@ -1586,8 +1714,8 @@  omp_context_selector_matches (tree ctx)
 		      ret = -1;
 		  }
 	      break;
-	    case 'i':
-	      if (set == 'd' && !strcmp (sel, "isa"))
+	    case OMP_TRAIT_DEVICE_ISA:
+	      if (set == OMP_TRAIT_SET_DEVICE)
 		for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
 		  {
 		    const char *isa = omp_context_name_list_prop (p);
@@ -1639,8 +1767,8 @@  omp_context_selector_matches (tree ctx)
 		      ret = -1;
 		  }
 	      break;
-	    case 'c':
-	      if (set == 'u' && !strcmp (sel, "condition"))
+	    case OMP_TRAIT_USER_CONDITION:
+	      if (set == OMP_TRAIT_SET_USER)
 		for (tree p = OMP_TS_PROPERTIES (ts); p; p = TREE_CHAIN (p))
 		  if (OMP_TP_NAME (p) == NULL_TREE)
 		    {
@@ -1782,7 +1910,8 @@  omp_construct_simd_compare (tree clauses1, tree clauses2)
    Unlike set names or selector names, properties can have duplicates.  */
 
 static int
-omp_context_selector_props_compare (const char *set, const char *sel,
+omp_context_selector_props_compare (enum omp_tss_code set,
+				    enum omp_ts_code sel,
 				    tree ctx1, tree ctx2)
 {
   int ret = 0;
@@ -1795,7 +1924,8 @@  omp_context_selector_props_compare (const char *set, const char *sel,
 	    {
 	      if (OMP_TP_NAME (p1) == NULL_TREE)
 		{
-		  if (set[0] == 'u' && strcmp (sel, "condition") == 0)
+		  if (set == OMP_TRAIT_SET_USER
+		      && sel == OMP_TRAIT_USER_CONDITION)
 		    {
 		      if (integer_zerop (OMP_TP_VALUE (p1))
 			  != integer_zerop (OMP_TP_VALUE (p2)))
@@ -1842,8 +1972,18 @@  omp_context_selector_props_compare (const char *set, const char *sel,
    2 if neither context is a subset of another one.  */
 
 int
-omp_context_selector_set_compare (const char *set, tree ctx1, tree ctx2)
+omp_context_selector_set_compare (enum omp_tss_code set, tree ctx1, tree ctx2)
 {
+
+  /* If either list includes an ignored selector trait, neither can
+     be a subset of the other.  */
+  for (tree ts = ctx1; ts; ts = TREE_CHAIN (ts))
+    if (OMP_TS_CODE (ts) == OMP_TRAIT_INVALID)
+      return 2;
+  for (tree ts = ctx2; ts; ts = TREE_CHAIN (ts))
+    if (OMP_TS_CODE (ts) == OMP_TRAIT_INVALID)
+      return 2;
+
   bool swapped = false;
   int ret = 0;
   int len1 = list_length (ctx1);
@@ -1855,18 +1995,18 @@  omp_context_selector_set_compare (const char *set, tree ctx1, tree ctx2)
       std::swap (ctx1, ctx2);
       std::swap (len1, len2);
     }
-  if (set[0] == 'c')
+
+  if (set == OMP_TRAIT_SET_CONSTRUCT)
     {
       tree ts1;
       tree ts2 = ctx2;
-      tree simd = get_identifier ("simd");
       /* Handle construct set specially.  In this case the order
 	 of the selector matters too.  */
       for (ts1 = ctx1; ts1; ts1 = TREE_CHAIN (ts1))
-	if (OMP_TS_ID (ts1) == OMP_TS_ID (ts2))
+	if (OMP_TS_CODE (ts1) == OMP_TS_CODE (ts2))
 	  {
 	    int r = 0;
-	    if (OMP_TS_ID (ts1) == simd)
+	    if (OMP_TS_CODE (ts1) == OMP_TRAIT_CONSTRUCT_SIMD)
 	      r = omp_construct_simd_compare (OMP_TS_PROPERTIES (ts1),
 					      OMP_TS_PROPERTIES (ts2));
 	    if (r == 2 || (ret && r && (ret < 0) != (r < 0)))
@@ -1898,17 +2038,17 @@  omp_context_selector_set_compare (const char *set, tree ctx1, tree ctx2)
     }
   for (tree ts1 = ctx1; ts1; ts1 = TREE_CHAIN (ts1))
     {
+      enum omp_ts_code sel = OMP_TS_CODE (ts1);
       tree ts2;
       for (ts2 = ctx2; ts2; ts2 = TREE_CHAIN (ts2))
-	if (OMP_TS_ID (ts1) == OMP_TS_ID (ts2))
+	if (sel == OMP_TS_CODE (ts2))
 	  {
 	    tree score1 = OMP_TS_SCORE (ts1);
 	    tree score2 = OMP_TS_SCORE (ts2);
 	    if (score1 && score2 && !simple_cst_equal (score1, score2))
 	      return 2;
 
-	    const char *sel = IDENTIFIER_POINTER (OMP_TS_ID (ts1));
-	    int r = omp_context_selector_props_compare (set, sel,
+	    int r = omp_context_selector_props_compare (set, OMP_TS_CODE (ts1),
 							OMP_TS_PROPERTIES (ts1),
 							OMP_TS_PROPERTIES (ts2));
 	    if (r == 2 || (ret && r && (ret < 0) != (r < 0)))
@@ -1954,11 +2094,11 @@  omp_context_selector_compare (tree ctx1, tree ctx2)
     }
   for (tree tss1 = ctx1; tss1; tss1 = TREE_CHAIN (tss1))
     {
+      enum omp_tss_code set = OMP_TSS_CODE (tss1);
       tree tss2;
       for (tss2 = ctx2; tss2; tss2 = TREE_CHAIN (tss2))
-	if (OMP_TSS_ID (tss1) == OMP_TSS_ID (tss2))
+	if (set == OMP_TSS_CODE (tss2))
 	  {
-	    const char *set = IDENTIFIER_POINTER (OMP_TSS_ID (tss1));
 	    int r
 	      = omp_context_selector_set_compare
 		  (set, OMP_TSS_TRAIT_SELECTORS (tss1),
@@ -1985,26 +2125,51 @@  omp_context_selector_compare (tree ctx1, tree ctx2)
 }
 
 /* From context selector CTX, return trait-selector with name SEL in
-   trait-selector-set with name SET if any, or NULL_TREE if not found.
-   If SEL is NULL, return the list of trait-selectors in SET.  */
+   trait-selector-set with name SET if any, or NULL_TREE if not found.  */
+tree
+omp_get_context_selector (tree ctx, enum omp_tss_code set,
+			  enum omp_ts_code sel)
+{
+  for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
+    if (OMP_TSS_CODE (tss) == set)
+      for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
+	if (OMP_TS_CODE (ts) == sel)
+	  return ts;
+  return NULL_TREE;
+}
 
+/* Similar, but returns the whole trait-selector list for SET in CTX.  */
 tree
-omp_get_context_selector (tree ctx, const char *set, const char *sel)
+omp_get_context_selector_list (tree ctx, enum omp_tss_code set)
 {
-  tree setid = get_identifier (set);
-  tree selid = sel ? get_identifier (sel) : NULL_TREE;
   for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
-    if (OMP_TSS_ID (tss) == setid)
-      {
-	if (sel == NULL)
-	  return OMP_TSS_TRAIT_SELECTORS (tss);
-	for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
-	  if (OMP_TS_ID (ts) == selid)
-	    return ts;
-      }
+    if (OMP_TSS_CODE (tss) == set)
+      return OMP_TSS_TRAIT_SELECTORS (tss);
   return NULL_TREE;
 }
 
+/* Map string S onto a trait selector set code.  */
+enum omp_tss_code
+omp_lookup_tss_code (const char * s)
+{
+  for (int i = 0; i < OMP_TRAIT_SET_LAST; i++)
+    if (strcmp (s, omp_tss_map[i]) == 0)
+      return (enum omp_tss_code) i;
+  return OMP_TRAIT_SET_INVALID;
+}
+
+/* Map string S onto a trait selector code for set SET.  */
+enum omp_ts_code
+omp_lookup_ts_code (enum omp_tss_code set, const char *s)
+{
+  unsigned int mask = 1 << set;
+  for (int i = 0; i < OMP_TRAIT_LAST; i++)
+    if ((mask & omp_ts_map[i].tss_mask) != 0
+	&& strcmp (s, omp_ts_map[i].name) == 0)
+      return (enum omp_ts_code) i;
+  return OMP_TRAIT_INVALID;
+}
+
 /* Needs to be a GC-friendly widest_int variant, but precision is
    desirable to be the same on all targets.  */
 typedef generic_wide_int <fixed_wide_int_storage <1024> > score_wide_int;
@@ -2017,14 +2182,18 @@  typedef generic_wide_int <fixed_wide_int_storage <1024> > score_wide_int;
 static bool
 omp_context_compute_score (tree ctx, score_wide_int *score, bool declare_simd)
 {
-  tree construct = omp_get_context_selector (ctx, "construct", NULL);
-  bool has_kind = omp_get_context_selector (ctx, "device", "kind");
-  bool has_arch = omp_get_context_selector (ctx, "device", "arch");
-  bool has_isa = omp_get_context_selector (ctx, "device", "isa");
+  tree selectors
+    = omp_get_context_selector_list (ctx, OMP_TRAIT_SET_CONSTRUCT);
+  bool has_kind = omp_get_context_selector (ctx, OMP_TRAIT_SET_DEVICE,
+					    OMP_TRAIT_DEVICE_KIND);
+  bool has_arch = omp_get_context_selector (ctx, OMP_TRAIT_SET_DEVICE,
+					    OMP_TRAIT_DEVICE_ARCH);
+  bool has_isa = omp_get_context_selector (ctx, OMP_TRAIT_SET_DEVICE,
+					   OMP_TRAIT_DEVICE_ISA);
   bool ret = false;
   *score = 1;
   for (tree tss = ctx; tss; tss = TREE_CHAIN (tss))
-    if (OMP_TSS_TRAIT_SELECTORS (tss) != construct)
+    if (OMP_TSS_TRAIT_SELECTORS (tss) != selectors)
       for (tree ts = OMP_TSS_TRAIT_SELECTORS (tss); ts; ts = TREE_CHAIN (ts))
 	{
 	  tree s = OMP_TS_SCORE (ts);
@@ -2033,13 +2202,19 @@  omp_context_compute_score (tree ctx, score_wide_int *score, bool declare_simd)
 					    TYPE_SIGN (TREE_TYPE (s)));
 	}
 
-  if (construct || has_kind || has_arch || has_isa)
+  if (selectors || has_kind || has_arch || has_isa)
     {
-      int scores[12];
-      enum tree_code constructs[5];
-      int nconstructs = 0;
-      if (construct)
-	nconstructs = omp_construct_traits_to_codes (construct, constructs);
+      int nconstructs = list_length (selectors);
+      enum tree_code *constructs = NULL;
+      if (nconstructs)
+	{
+	  constructs
+	    = (enum tree_code *) alloca (nconstructs
+					 * sizeof (enum tree_code));
+	  omp_construct_traits_to_codes (selectors, nconstructs, constructs);
+	}
+      int *scores
+	= (int *) alloca ((2 * nconstructs + 2) * sizeof (int));
       if (omp_construct_selector_matches (constructs, nconstructs, scores)
 	  == 2)
 	ret = true;
diff --git a/gcc/omp-general.h b/gcc/omp-general.h
index dc5e5f6ba1f..8a0338b9942 100644
--- a/gcc/omp-general.h
+++ b/gcc/omp-general.h
@@ -24,6 +24,7 @@  along with GCC; see the file COPYING3.  If not see
 
 #include "gomp-constants.h"
 #include "omp-api.h"
+#include "omp-selectors.h"
 
 /*  Flags for an OpenACC loop.  */
 
@@ -135,8 +136,18 @@  struct omp_for_data
 #define OMP_TP_VALUE(NODE) \
   TREE_VALUE (NODE)
 
-extern tree make_trait_set_selector (tree, tree, tree);
-extern tree make_trait_selector (tree, tree, tree, tree);
+#define OMP_TSS_CODE(t)						\
+  ((enum omp_tss_code) TREE_INT_CST_LOW (OMP_TSS_ID (t)))
+#define OMP_TSS_NAME(t)			\
+  (omp_tss_map[OMP_TSS_CODE (t)])
+
+#define OMP_TS_CODE(t)						\
+  ((enum omp_ts_code) TREE_INT_CST_LOW (OMP_TS_ID (t)))
+#define OMP_TS_NAME(t)			\
+  (omp_ts_map[OMP_TS_CODE (t)].name)
+
+extern tree make_trait_set_selector (enum omp_tss_code, tree, tree);
+extern tree make_trait_selector (enum omp_ts_code, tree, tree, tree);
 extern tree make_trait_property (tree, tree, tree);
 
 extern tree omp_find_clause (tree clauses, enum omp_clause_code kind);
@@ -153,13 +164,15 @@  extern gimple *omp_build_barrier (tree lhs);
 extern tree find_combined_omp_for (tree *, int *, void *);
 extern poly_uint64 omp_max_vf (void);
 extern int omp_max_simt_vf (void);
-extern int omp_construct_traits_to_codes (tree, enum tree_code *);
+extern void omp_construct_traits_to_codes (tree, int, enum tree_code *);
 extern tree omp_check_context_selector (location_t loc, tree ctx);
 extern void omp_mark_declare_variant (location_t loc, tree variant,
 				      tree construct);
 extern int omp_context_selector_matches (tree);
-extern int omp_context_selector_set_compare (const char *, tree, tree);
-extern tree omp_get_context_selector (tree, const char *, const char *);
+extern int omp_context_selector_set_compare (enum omp_tss_code, tree, tree);
+extern tree omp_get_context_selector (tree, enum omp_tss_code,
+				      enum omp_ts_code);
+extern tree omp_get_context_selector_list (tree, enum omp_tss_code);
 extern tree omp_resolve_declare_variant (tree);
 extern tree oacc_launch_pack (unsigned code, tree device, unsigned op);
 extern tree oacc_replace_fn_attrib_attr (tree attribs, tree dims);
diff --git a/gcc/omp-selectors.h b/gcc/omp-selectors.h
new file mode 100644
index 00000000000..825a082c939
--- /dev/null
+++ b/gcc/omp-selectors.h
@@ -0,0 +1,93 @@ 
+/* Data structures for OpenMP context selectors.  This is in a separate file
+   from omp-general.h so that it may also be used in the Fortran parser
+   without reference to tree data structures.
+
+   Copyright (C) 2023 Free Software Foundation, Inc.
+
+This file is part of GCC.
+
+GCC is free software; you can redistribute it and/or modify it under
+the terms of the GNU General Public License as published by the Free
+Software Foundation; either version 3, or (at your option) any later
+version.
+
+GCC is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+for more details.
+
+You should have received a copy of the GNU General Public License
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
+
+
+#ifndef GCC_OMP_SELECTORS_H
+#define GCC_OMP_SELECTORS_H
+
+/* Trait set selector keywords.  */
+enum omp_tss_code {
+  OMP_TRAIT_SET_CONSTRUCT,
+  OMP_TRAIT_SET_DEVICE,
+  OMP_TRAIT_SET_TARGET_DEVICE,
+  OMP_TRAIT_SET_IMPLEMENTATION,
+  OMP_TRAIT_SET_USER,
+  OMP_TRAIT_SET_LAST,
+  OMP_TRAIT_SET_INVALID = -1
+};
+
+/* Trait selector keywords.  */
+enum omp_ts_code {
+  OMP_TRAIT_DEVICE_KIND,
+  OMP_TRAIT_DEVICE_ISA,
+  OMP_TRAIT_DEVICE_ARCH,
+  OMP_TRAIT_DEVICE_NUM,
+  OMP_TRAIT_IMPLEMENTATION_VENDOR,
+  OMP_TRAIT_IMPLEMENTATION_EXTENSION,
+  OMP_TRAIT_IMPLEMENTATION_ADMO,
+  OMP_TRAIT_IMPLEMENTATION_REQUIRES,
+  OMP_TRAIT_IMPLEMENTATION_UNIFIED_ADDRESS,
+  OMP_TRAIT_IMPLEMENTATION_UNIFIED_SHARED_MEMORY,
+  OMP_TRAIT_IMPLEMENTATION_DYNAMIC_ALLOCATORS,
+  OMP_TRAIT_IMPLEMENTATION_REVERSE_OFFLOAD,
+  OMP_TRAIT_USER_CONDITION,
+  OMP_TRAIT_CONSTRUCT_TARGET,
+  OMP_TRAIT_CONSTRUCT_TEAMS,
+  OMP_TRAIT_CONSTRUCT_PARALLEL,
+  OMP_TRAIT_CONSTRUCT_FOR,
+  OMP_TRAIT_CONSTRUCT_SIMD,
+  OMP_TRAIT_LAST,
+  OMP_TRAIT_INVALID = -1
+};
+
+/* All trait property forms.  */
+enum omp_tp_type {
+  OMP_TRAIT_PROPERTY_NONE,
+  OMP_TRAIT_PROPERTY_ID,
+  OMP_TRAIT_PROPERTY_NAME_LIST,
+  OMP_TRAIT_PROPERTY_EXPR,
+  OMP_TRAIT_PROPERTY_CLAUSE_LIST,
+  OMP_TRAIT_PROPERTY_EXTENSION
+};
+
+/* Map trait set selector name keywords onto strings.  */
+extern const char *omp_tss_map [];
+
+/* Map trait selector keywords onto strings, allowed contexts, and
+   allowed property names for OMP_TRAIT_PROPERTY_NAME_LIST and
+   OMP_TRAIT_PROPERTY_ID properties.  If valid_properties is null,
+   it means that any required checking has to be done explicitly
+   somewhere instead of being driven by the table.  Otherwise it's a
+   null-terminated array of strings.  */
+struct omp_ts_info {
+  const char *name;
+  unsigned int tss_mask;
+  enum omp_tp_type tp_type;
+  bool allow_score;
+  const char * const *valid_properties;
+};
+extern struct omp_ts_info omp_ts_map[];
+
+extern enum omp_tss_code omp_lookup_tss_code (const char *);
+extern enum omp_ts_code omp_lookup_ts_code (enum omp_tss_code, const char *);
+
+#endif /* GCC_OMP_SELECTORS_H */
diff --git a/gcc/testsuite/c-c++-common/gomp/declare-variant-1.c b/gcc/testsuite/c-c++-common/gomp/declare-variant-1.c
index 8b3cd7fed9f..75fcb7bf0e7 100644
--- a/gcc/testsuite/c-c++-common/gomp/declare-variant-1.c
+++ b/gcc/testsuite/c-c++-common/gomp/declare-variant-1.c
@@ -9,6 +9,7 @@  int bar (int, int, int *);
   match (device={arch(x86_64,powerpc64),isa(avx512f,popcntb)}, \
 	 implementation={atomic_default_mem_order(seq_cst),made_up_selector("foo", 13, "bar")}, \
 	 user={condition(3-3)})
+/* { dg-warning "unknown selector 'made_up_selector'" "" { target *-*-* } .-2 } */
 int baz (int, int, int *);
 
 int
diff --git a/gcc/testsuite/c-c++-common/gomp/declare-variant-2.c b/gcc/testsuite/c-c++-common/gomp/declare-variant-2.c
index 3c2c12c9e03..97285fa3b74 100644
--- a/gcc/testsuite/c-c++-common/gomp/declare-variant-2.c
+++ b/gcc/testsuite/c-c++-common/gomp/declare-variant-2.c
@@ -42,10 +42,10 @@  void f20 (void);
 void f21 (void);						/* { dg-error "cannot appear in a constant-expression" "" { target c++98_only } .-1 } */
 #pragma omp declare variant (f1) match(user={condition(1, 2, 3)})	/* { dg-error "expected '\\)' before ',' token" } */
 void f22 (void);
-#pragma omp declare variant (f1) match(construct={master})	/* { dg-error "selector 'master' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={master})	/* { dg-warning "unknown selector 'master' for context selector set 'construct'" } */
 void f23 (void);
-#pragma omp declare variant (f1) match(construct={teams,parallel,master,for})	/* { dg-error "selector 'master' not allowed for context selector set 'construct'" } */
-void f24 (void);						/* { dg-error "expected '\\\}' before ',' token" "" { target c } .-1 } */
+#pragma omp declare variant (f1) match(construct={teams,parallel,master,for})	/* { dg-warning "unknown selector 'master' for context selector set 'construct'" } */
+void f24 (void);
 #pragma omp declare variant (f1) match(construct={parallel(1	/* { dg-error "selector 'parallel' does not accept any properties" } */
 void f25 (void);						/* { dg-error "expected '\\\}' before end of line" "" { target c++ } .-1 } */
 								/* { dg-error "expected '\\\}' before '\\(' token" "" { target c } .-2 } */
@@ -79,11 +79,11 @@  void f38 (void);							/* { dg-warning "unknown property 'foobar' of 'kind' sele
 void f39 (void);
 #pragma omp declare variant (f1) match(device={arch(17)})	/* { dg-error "expected identifier or string literal before numeric constant" } */
 void f40 (void);
-#pragma omp declare variant (f1) match(device={foobar(3)})
+#pragma omp declare variant (f1) match(device={foobar(3)})	/* { dg-warning "unknown selector 'foobar' for context selector set 'device'" } */
 void f41 (void);
 #pragma omp declare variant (f1) match(device={arch(x86_64)},device={isa(avx512vl)})	/* { dg-error "selector set 'device' specified more than once" } */
 void f42 (void);
-#pragma omp declare variant (f1) match(implementation={foobar(3)})
+#pragma omp declare variant (f1) match(implementation={foobar(3)})	/* { dg-warning "unknown selector 'foobar' for context selector set 'implementation'" } */
 void f43 (void);
 #pragma omp declare variant (f1) match(implementation={vendor})	/* { dg-error "expected '\\(' before '\\\}' token" } */
 void f44 (void);
@@ -115,10 +115,10 @@  void f55 (void);
 void f56 (void);
 #pragma omp declare variant (f1) match(implementation={atomic_default_mem_order(relaxed)},implementation={atomic_default_mem_order(relaxed)})	/* { dg-error "selector set 'implementation' specified more than once" } */
 void f57 (void);
-#pragma omp declare variant (f1) match(user={foobar(3)})	/* { dg-error "selector 'foobar' not allowed for context selector set 'user'" } */
-void f58 (void);						/* { dg-error "expected '\\\}' before '\\(' token" "" { target c } .-1 } */
-#pragma omp declare variant (f1) match(construct={foobar(3)})	/* { dg-error "selector 'foobar' not allowed for context selector set 'construct'" } */
-void f59 (void);						/* { dg-error "expected '\\\}' before '\\(' token" "" { target c } .-1 } */
+#pragma omp declare variant (f1) match(user={foobar(3)})	/* { dg-warning "unknown selector 'foobar' for context selector set 'user'" } */
+void f58 (void);
+#pragma omp declare variant (f1) match(construct={foobar(3)})	/* { dg-warning "unknown selector 'foobar' for context selector set 'construct'" } */
+void f59 (void);
 #pragma omp declare variant (f1) match(construct={parallel},foobar={bar})	/* { dg-error "expected 'construct', 'device', 'implementation' or 'user' before 'foobar'" } */
 void f60 (void);
 #pragma omp declare variant (f1) match(construct={parallel,parallel})	/* { dg-error "selector 'parallel' specified more than once in set 'construct'" } */
@@ -127,27 +127,27 @@  void f61 (void);
 void f62 (void);
 #pragma omp declare variant (f1) match(construct={target,teams,teams})	/* { dg-error "selector 'teams' specified more than once in set 'construct'" } */
 void f63 (void);
-#pragma omp declare variant (f1) match(construct={single})	/* { dg-error "selector 'single' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={single})	/* { dg-warning "unknown selector 'single' for context selector set 'construct'" } */
 void f64 (void);
-#pragma omp declare variant (f1) match(construct={taskgroup})	/* { dg-error "selector 'taskgroup' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={taskgroup})	/* { dg-warning "unknown selector 'taskgroup' for context selector set 'construct'" } */
 void f65 (void);
-#pragma omp declare variant (f1) match(construct={do})	/* { dg-error "selector 'do' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={do})	/* { dg-warning "unknown selector 'do' for context selector set 'construct'" } */
 void f66 (void);
-#pragma omp declare variant (f1) match(construct={threadprivate})	/* { dg-error "selector 'threadprivate' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={threadprivate})	/* { dg-warning "unknown selector 'threadprivate' for context selector set 'construct'" } */
 void f67 (void);
-#pragma omp declare variant (f1) match(construct={critical})	/* { dg-error "selector 'critical' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={critical})	/* { dg-warning "unknown selector 'critical' for context selector set 'construct'" } */
 void f68 (void);
-#pragma omp declare variant (f1) match(construct={task})	/* { dg-error "selector 'task' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={task})	/* { dg-warning "unknown selector 'task' for context selector set 'construct'" } */
 void f69 (void);
-#pragma omp declare variant (f1) match(construct={taskloop})	/* { dg-error "selector 'taskloop' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={taskloop})	/* { dg-warning "unknown selector 'taskloop' for context selector set 'construct'" } */
 void f70 (void);
-#pragma omp declare variant (f1) match(construct={sections})	/* { dg-error "selector 'sections' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={sections})	/* { dg-warning "unknown selector 'sections' for context selector set 'construct'" } */
 void f71 (void);
-#pragma omp declare variant (f1) match(construct={section})	/* { dg-error "selector 'section' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={section})	/* { dg-warning "unknown selector 'section' for context selector set 'construct'" } */
 void f72 (void);
-#pragma omp declare variant (f1) match(construct={workshare})	/* { dg-error "selector 'workshare' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={workshare})	/* { dg-warning "unknown selector 'workshare' for context selector set 'construct'" } */
 void f73 (void);
-#pragma omp declare variant (f1) match(construct={requires})	/* { dg-error "selector 'requires' not allowed for context selector set 'construct'" } */
+#pragma omp declare variant (f1) match(construct={requires})	/* { dg-warning "unknown selector 'requires' for context selector set 'construct'" } */
 void f74 (void);
 void f75a (void);
 #pragma omp declare variant (f75a),match(construct={parallel})
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-variant-1.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-variant-1.f90
index de09dbfe806..50d7e41613f 100644
--- a/gcc/testsuite/gfortran.dg/gomp/declare-variant-1.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/declare-variant-1.f90
@@ -25,6 +25,7 @@  module main
       !$omp & match (device={arch(x86_64,powerpc64),isa(avx512f,popcntb)}, &
       !$omp & implementation={atomic_default_mem_order(seq_cst),made_up_selector("foo", 13, "bar")}, &
       !$omp & user={condition(3-3)})
+! { dg-warning "unknown selector 'made_up_selector'" "" { target *-*-* } .-2 }
     end function
 
     subroutine quux
diff --git a/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90 b/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90
index 63d77780196..0aae0fe956d 100644
--- a/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/declare-variant-2.f90
@@ -69,10 +69,10 @@  contains
     !$omp declare variant (f1) match(user={condition(1, 2, 3)})	! { dg-error "expected '\\)' at .1." }
   end subroutine
   subroutine f23 ()
-    !$omp declare variant (f1) match(construct={master})	! { dg-error "selector 'master' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={master})	! { dg-warning "unknown selector 'master' for context selector set 'construct'" }
   end subroutine
   subroutine f24 ()
-    !$omp declare variant (f1) match(construct={teams,parallel,master,do})	! { dg-error "selector 'master' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={teams,parallel,master,do})	! { dg-warning "unknown selector 'master' for context selector set 'construct'" }
   end subroutine
   subroutine f25 ()
     !$omp declare variant (f1) match(construct={parallel(1	! { dg-error "selector 'parallel' does not accept any properties at .1." }
@@ -105,10 +105,10 @@  contains
     !$omp declare variant (f1) match(device={arch(17)})	! { dg-error "expected identifier or string literal at .1." }
   end subroutine
   subroutine f41 ()
-    !$omp declare variant (f1) match(device={foobar(3)})
+    !$omp declare variant (f1) match(device={foobar(3)}) ! { dg-warning "unknown selector 'foobar' for context selector set 'device' at .1." }
   end subroutine
   subroutine f43 ()
-    !$omp declare variant (f1) match(implementation={foobar(3)})
+    !$omp declare variant (f1) match(implementation={foobar(3)}) ! { dg-warning "unknown selector 'foobar' for context selector set 'implementation' at .1." }
   end subroutine
   subroutine f44 ()
     !$omp declare variant (f1) match(implementation={vendor})	! { dg-error "expected '\\(' at .1." }
@@ -141,46 +141,46 @@  contains
     !$omp declare variant (f1) match(implementation={atomic_default_mem_order(relaxed,seq_cst)})	! { dg-error "expected '\\)' at .1." }
   end subroutine
   subroutine f58 ()
-    !$omp declare variant (f1) match(user={foobar(3)})	! { dg-error "selector 'foobar' not allowed for context selector set 'user' at .1." }
+    !$omp declare variant (f1) match(user={foobar(3)})	! { dg-warning "unknown selector 'foobar' for context selector set 'user' at .1." }
   end subroutine
   subroutine f59 ()
-    !$omp declare variant (f1) match(construct={foobar(3)})	! { dg-error "selector 'foobar' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={foobar(3)})	! { dg-warning "unknown selector 'foobar' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f60 ()
     !$omp declare variant (f1) match(construct={parallel},foobar={bar})	! { dg-error "expected 'construct', 'device', 'implementation' or 'user' at .1." }
   end subroutine
   subroutine f64 ()
-    !$omp declare variant (f1) match(construct={single})	! { dg-error "selector 'single' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={single})	! { dg-warning "unknown selector 'single' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f65 ()
-    !$omp declare variant (f1) match(construct={taskgroup})	! { dg-error "selector 'taskgroup' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={taskgroup})	! { dg-warning "unknown selector 'taskgroup' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f66 ()
-    !$omp declare variant (f1) match(construct={for})	! { dg-error "selector 'for' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={for})	! { dg-warning "unknown selector 'for' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f67 ()
-    !$omp declare variant (f1) match(construct={threadprivate})	! { dg-error "selector 'threadprivate' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={threadprivate})	! { dg-warning "unknown selector 'threadprivate' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f68 ()
-    !$omp declare variant (f1) match(construct={critical})	! { dg-error "selector 'critical' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={critical})	! { dg-warning "unknown selector 'critical' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f69 ()
-    !$omp declare variant (f1) match(construct={task})	! { dg-error "selector 'task' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={task})	! { dg-warning "unknown selector 'task' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f70 ()
-    !$omp declare variant (f1) match(construct={taskloop})	! { dg-error "selector 'taskloop' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={taskloop})	! { dg-warning "unknown selector 'taskloop' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f71 ()
-    !$omp declare variant (f1) match(construct={sections})	! { dg-error "selector 'sections' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={sections})	! { dg-warning "unknown selector 'sections' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f72 ()
-    !$omp declare variant (f1) match(construct={section})	! { dg-error "selector 'section' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={section})	! { dg-warning "unknown selector 'section' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f73 ()
-    !$omp declare variant (f1) match(construct={workshare})	! { dg-error "selector 'workshare' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={workshare})	! { dg-warning "unknown selector 'workshare' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f74 ()
-    !$omp declare variant (f1) match(construct={requires})	! { dg-error "selector 'requires' not allowed for context selector set 'construct' at .1." }
+    !$omp declare variant (f1) match(construct={requires})	! { dg-warning "unknown selector 'requires' for context selector set 'construct' at .1." }
   end subroutine
   subroutine f75 ()
     !$omp declare variant (f1),match(construct={parallel})	! { dg-error "expected 'match' at .1." }