diff options
author | Jakub Jelinek <jakub@redhat.com> | 2022-10-04 10:37:14 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2022-10-04 10:37:14 +0200 |
commit | b6d5d72bd0b71ac96a8b2ee537367c46107dcb73 (patch) | |
tree | c65b16d4db14beffbbcd2161d871d096bcd60c2a /gcc/c | |
parent | 7df3693f745eb909aacd710613811e5951e8af3b (diff) | |
download | gcc-b6d5d72bd0b71ac96a8b2ee537367c46107dcb73.zip gcc-b6d5d72bd0b71ac96a8b2ee537367c46107dcb73.tar.gz gcc-b6d5d72bd0b71ac96a8b2ee537367c46107dcb73.tar.bz2 |
openmp: Add begin declare target support
The following patch adds support for the begin declare target construct,
which is another spelling for declare target construct without clauses
(where it needs paired end declare target), but unlike that one accepts
clauses.
This is an OpenMP 5.1 feature, implemented with 5.2 clarification because
in 5.1 we had a restriction in the declare target chapter shared by
declare target and begin declare target that if there are any clauses
specified at least one of them needs to be to or link. But that
was of course meant just for declare target and not begin declare target,
because begin declare target doesn't even allow to/link/enter clauses.
In addition to that, the patch also makes device_type clause duplication
an error (as stated in 5.1) and similarly makes declare target with
just device_type clause an error rather than warning.
What this patch doesn't do is:
1) OpenMP 5.1 also added an indirect clause, we don't support that
neither on declare target nor begin declare target
and I couldn't find it in our features pages (neither libgomp.texi
nor web)
2) I think device_type(nohost)/device_type(host) support can't work for
variables (in 5.0 it only talked about procedures so this could be
also thought as 5.1 feature that we should just add to the list
and implement)
3) I don't see any use of the "omp declare target nohost" attribute, so
I'm not sure if device_type(nohost) works at all
2022-10-04 Jakub Jelinek <jakub@redhat.com>
gcc/c-family/
* c-omp.cc (c_omp_directives): Uncomment begin declare target
entry.
gcc/c/
* c-lang.h (struct c_omp_declare_target_attr): New type.
(current_omp_declare_target_attribute): Change type from
int to vec<c_omp_declare_target_attr, va_gc> *.
* c-parser.cc (c_parser_translation_unit): Adjust for that change.
If last pushed directive was begin declare target, use different
wording and simplify format strings for easier translations.
(c_parser_omp_clause_device_type): Uncomment
check_no_duplicate_clause call.
(c_parser_omp_declare_target): Adjust for the
current_omp_declare_target_attribute type change, push { -1 }.
Use error_at rather than warning_at for declare target with
only device_type clauses.
(OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK): Define.
(c_parser_omp_begin): Add begin declare target support.
(c_parser_omp_end): Adjust for the
current_omp_declare_target_attribute type change, adjust
diagnostics wording and simplify format strings for easier
translations.
* c-decl.cc (current_omp_declare_target_attribute): Change type from
int to vec<c_omp_declare_target_attr, va_gc> *.
(c_decl_attributes): Adjust for the
current_omp_declare_target_attribute type change. If device_type
was present on begin declare target, add "omp declare target host"
and/or "omp declare target nohost" attributes.
gcc/cp/
* cp-tree.h (struct omp_declare_target_attr): Rename to ...
(cp_omp_declare_target_attr): ... this. Add device_type member.
(omp_begin_assumes_data): Rename to ...
(cp_omp_begin_assumes_data): ... this.
(struct saved_scope): Change types of omp_declare_target_attribute
and omp_begin_assumes.
* parser.cc (cp_parser_omp_clause_device_type): Uncomment
check_no_duplicate_clause call.
(cp_parser_omp_all_clauses): Fix up pasto, c_name for OMP_CLAUSE_LINK
should be "link" rather than "to".
(cp_parser_omp_declare_target): Adjust for omp_declare_target_attr
to cp_omp_declare_target_attr changes, push -1 as device_type. Use
error_at rather than warning_at for declare target with only
device_type clauses.
(OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK): Define.
(cp_parser_omp_begin): Add begin declare target support. Adjust
for omp_begin_assumes_data to cp_omp_begin_assumes_data change.
(cp_parser_omp_end): Adjust for the
omp_declare_target_attr to cp_omp_declare_target_attr and
omp_begin_assumes_data to cp_omp_begin_assumes_data type changes,
adjust diagnostics wording and simplify format strings for easier
translations.
* semantics.cc (finish_translation_unit): Likewise.
* decl2.cc (cplus_decl_attributes): If device_type was present on
begin declare target, add "omp declare target host" and/or
"omp declare target nohost" attributes.
gcc/testsuite/
* c-c++-common/gomp/declare-target-4.c: Move tests that are now
rejected into declare-target-7.c.
* c-c++-common/gomp/declare-target-6.c: Adjust expected diagnostics.
* c-c++-common/gomp/declare-target-7.c: New test.
* c-c++-common/gomp/begin-declare-target-1.c: New test.
* c-c++-common/gomp/begin-declare-target-2.c: New test.
* c-c++-common/gomp/begin-declare-target-3.c: New test.
* c-c++-common/gomp/begin-declare-target-4.c: New test.
* g++.dg/gomp/attrs-9.C: Add begin declare target tests.
* g++.dg/gomp/attrs-18.C: New test.
libgomp/
* libgomp.texi (Support begin/end declare target syntax in C/C++):
Mark as implemented.
Diffstat (limited to 'gcc/c')
-rw-r--r-- | gcc/c/c-decl.cc | 24 | ||||
-rw-r--r-- | gcc/c/c-lang.h | 7 | ||||
-rw-r--r-- | gcc/c/c-parser.cc | 75 |
3 files changed, 82 insertions, 24 deletions
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc index bac8e6c..ffa63dc 100644 --- a/gcc/c/c-decl.cc +++ b/gcc/c/c-decl.cc @@ -153,9 +153,9 @@ static int warn_about_return_type; static bool undef_nested_function; -/* If non-zero, implicit "omp declare target" attribute is added into the - attribute lists. */ -int current_omp_declare_target_attribute; +/* Vector of implicit "omp declare target" attributes to be added into + the attribute lists. */ +vec<c_omp_declare_target_attr, va_gc> *current_omp_declare_target_attribute; /* If non-zero, we are inside of #pragma omp begin assumes ... #pragma omp end assumes region. */ @@ -5105,7 +5105,7 @@ static tree c_decl_attributes (tree *node, tree attributes, int flags) { /* Add implicit "omp declare target" attribute if requested. */ - if (current_omp_declare_target_attribute + if (vec_safe_length (current_omp_declare_target_attribute) && ((VAR_P (*node) && is_global_var (*node)) || TREE_CODE (*node) == FUNCTION_DECL)) { @@ -5119,6 +5119,22 @@ c_decl_attributes (tree *node, tree attributes, int flags) attributes = tree_cons (get_identifier ("omp declare target block"), NULL_TREE, attributes); } + if (TREE_CODE (*node) == FUNCTION_DECL) + { + int device_type + = current_omp_declare_target_attribute->last ().device_type; + device_type = MAX (device_type, 0); + if ((device_type & OMP_CLAUSE_DEVICE_TYPE_HOST) != 0 + && !lookup_attribute ("omp declare target host", attributes)) + attributes + = tree_cons (get_identifier ("omp declare target host"), + NULL_TREE, attributes); + if ((device_type & OMP_CLAUSE_DEVICE_TYPE_NOHOST) != 0 + && !lookup_attribute ("omp declare target nohost", attributes)) + attributes + = tree_cons (get_identifier ("omp declare target nohost"), + NULL_TREE, attributes); + } } /* Look up the current declaration with all the attributes merged diff --git a/gcc/c/c-lang.h b/gcc/c/c-lang.h index 861abe8..49a5ec3 100644 --- a/gcc/c/c-lang.h +++ b/gcc/c/c-lang.h @@ -60,9 +60,14 @@ struct GTY(()) language_function { int warn_about_return_type; }; +struct GTY(()) c_omp_declare_target_attr { + int device_type; +}; + /* If non-zero, implicit "omp declare target" attribute is added into the attribute lists. */ -extern GTY(()) int current_omp_declare_target_attribute; +extern GTY(()) vec<c_omp_declare_target_attr, va_gc> + *current_omp_declare_target_attribute; /* Similarly whether we are in between #pragma omp begin assumes and #pragma omp end assumes (and how many times when nested). */ extern GTY(()) int current_omp_begin_assumes; diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc index f2498dc..f6a94ba 100644 --- a/gcc/c/c-parser.cc +++ b/gcc/c/c-parser.cc @@ -1675,18 +1675,22 @@ c_parser_translation_unit (c_parser *parser) if (DECL_SIZE (decl) == NULL_TREE && TREE_TYPE (decl) != error_mark_node) error ("storage size of %q+D isn%'t known", decl); - if (current_omp_declare_target_attribute) + if (vec_safe_length (current_omp_declare_target_attribute)) { + c_omp_declare_target_attr + a = current_omp_declare_target_attribute->pop (); if (!errorcount) - error ("%<#pragma omp declare target%> without corresponding " - "%<#pragma omp end declare target%>"); - current_omp_declare_target_attribute = 0; + error ("%qs without corresponding %qs", + a.device_type >= 0 ? "#pragma omp begin declare target" + : "#pragma omp declare target", + "#pragma omp end declare target"); + vec_safe_truncate (current_omp_declare_target_attribute, 0); } if (current_omp_begin_assumes) { if (!errorcount) - error ("%<#pragma omp begin assumes%> without corresponding " - "%<#pragma omp end assumes%>"); + error ("%qs without corresponding %qs", + "#pragma omp begin assumes", "#pragma omp end assumes"); current_omp_begin_assumes = 0; } } @@ -16818,8 +16822,8 @@ c_parser_omp_clause_device_type (c_parser *parser, tree list) else goto invalid_kind; - /* check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, - "device_type"); */ + check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE_TYPE, + "device_type"); c_parser_consume_token (parser); parens.skip_until_found_close (parser); c = build_omp_clause (clause_loc, OMP_CLAUSE_DEVICE_TYPE); @@ -22351,7 +22355,8 @@ c_parser_omp_declare_target (c_parser *parser) else { c_parser_skip_to_pragma_eol (parser); - current_omp_declare_target_attribute++; + c_omp_declare_target_attr attr = { -1 }; + vec_safe_push (current_omp_declare_target_attribute, attr); return; } for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c)) @@ -22429,12 +22434,17 @@ c_parser_omp_declare_target (c_parser *parser) } } if (device_type && only_device_type) - warning_at (OMP_CLAUSE_LOCATION (clauses), 0, - "directive with only %<device_type%> clauses ignored"); + error_at (OMP_CLAUSE_LOCATION (clauses), + "directive with only %<device_type%> clause"); } /* OpenMP 5.1 - #pragma omp begin assumes clauses[optseq] new-line */ + #pragma omp begin assumes clauses[optseq] new-line + + #pragma omp begin declare target clauses[optseq] new-line */ + +#define OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK \ + (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE_TYPE) static void c_parser_omp_begin (c_parser *parser) @@ -22443,7 +22453,33 @@ c_parser_omp_begin (c_parser *parser) c_parser_consume_pragma (parser); if (c_parser_next_token_is (parser, CPP_NAME)) p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); - if (strcmp (p, "assumes") == 0) + if (strcmp (p, "declare") == 0) + { + c_parser_consume_token (parser); + p = ""; + if (c_parser_next_token_is (parser, CPP_NAME)) + p = IDENTIFIER_POINTER (c_parser_peek_token (parser)->value); + if (strcmp (p, "target") == 0) + { + c_parser_consume_token (parser); + tree clauses + = c_parser_omp_all_clauses (parser, + OMP_BEGIN_DECLARE_TARGET_CLAUSE_MASK, + "#pragma omp begin declare target"); + int device_type = 0; + for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c)) + if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_DEVICE_TYPE) + device_type |= OMP_CLAUSE_DEVICE_TYPE_KIND (c); + c_omp_declare_target_attr attr = { device_type }; + vec_safe_push (current_omp_declare_target_attribute, attr); + } + else + { + c_parser_error (parser, "expected %<target%>"); + c_parser_skip_to_pragma_eol (parser); + } + } + else if (strcmp (p, "assumes") == 0) { c_parser_consume_token (parser); c_parser_omp_assumption_clauses (parser, false); @@ -22451,7 +22487,7 @@ c_parser_omp_begin (c_parser *parser) } else { - c_parser_error (parser, "expected %<assumes%>"); + c_parser_error (parser, "expected %<declare target%> or %<assumes%>"); c_parser_skip_to_pragma_eol (parser); } } @@ -22484,19 +22520,20 @@ c_parser_omp_end (c_parser *parser) return; } c_parser_skip_to_pragma_eol (parser); - if (!current_omp_declare_target_attribute) + if (!vec_safe_length (current_omp_declare_target_attribute)) error_at (loc, "%<#pragma omp end declare target%> without " - "corresponding %<#pragma omp declare target%>"); + "corresponding %<#pragma omp declare target%> or " + "%<#pragma omp begin declare target%>"); else - current_omp_declare_target_attribute--; + current_omp_declare_target_attribute->pop (); } else if (strcmp (p, "assumes") == 0) { c_parser_consume_token (parser); c_parser_skip_to_pragma_eol (parser); if (!current_omp_begin_assumes) - error_at (loc, "%<#pragma omp end assumes%> without " - "corresponding %<#pragma omp begin assumes%>"); + error_at (loc, "%qs without corresponding %qs", + "#pragma omp end assumes", "#pragma omp begin assumes"); else current_omp_begin_assumes--; } |