Skip to content

Commit dc70315

Browse files
committed
openmp: Implement discovery of implicit declare target to clauses
This attempts to implement what the OpenMP 5.0 spec in declare target section says as ammended by the 5.1 changes so far (related to device_type(host)), except that it doesn't have the device(ancestor: ...) handling yet because we do not support it yet, and I've left so far out the except lambda note, because I need that clarified. 2020-05-12 Jakub Jelinek <jakub@redhat.com> * omp-offload.h (omp_discover_implicit_declare_target): Declare. * omp-offload.c: Include context.h. (omp_declare_target_fn_p, omp_declare_target_var_p, omp_discover_declare_target_fn_r, omp_discover_declare_target_var_r, omp_discover_implicit_declare_target): New functions. * cgraphunit.c (analyze_functions): Call omp_discover_implicit_declare_target. * testsuite/libgomp.c/target-39.c: New test.
1 parent fe8c8f1 commit dc70315

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

gcc/ChangeLog

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
2020-05-12 Jakub Jelinek <jakub@redhat.com>
2+
3+
* omp-offload.h (omp_discover_implicit_declare_target): Declare.
4+
* omp-offload.c: Include context.h.
5+
(omp_declare_target_fn_p, omp_declare_target_var_p,
6+
omp_discover_declare_target_fn_r, omp_discover_declare_target_var_r,
7+
omp_discover_implicit_declare_target): New functions.
8+
* cgraphunit.c (analyze_functions): Call
9+
omp_discover_implicit_declare_target.
10+
111
2020-05-12 Richard Biener <rguenther@suse.de>
212

313
* gimple-fold.c (maybe_canonicalize_mem_ref_addr): Canonicalize

gcc/cgraphunit.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ along with GCC; see the file COPYING3. If not see
206206
#include "stringpool.h"
207207
#include "attribs.h"
208208
#include "ipa-inline.h"
209+
#include "omp-offload.h"
209210

210211
/* Queue of cgraph nodes scheduled to be added into cgraph. This is a
211212
secondary queue used during optimization to accommodate passes that
@@ -1160,6 +1161,9 @@ analyze_functions (bool first_time)
11601161
node->fixup_same_cpp_alias_visibility (node->get_alias_target ());
11611162
build_type_inheritance_graph ();
11621163

1164+
if (flag_openmp && first_time)
1165+
omp_discover_implicit_declare_target ();
1166+
11631167
/* Analysis adds static variables that in turn adds references to new functions.
11641168
So we need to iterate the process until it stabilize. */
11651169
while (changed)

gcc/omp-offload.c

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ along with GCC; see the file COPYING3. If not see
5252
#include "stringpool.h"
5353
#include "attribs.h"
5454
#include "cfgloop.h"
55+
#include "context.h"
5556

5657
/* Describe the OpenACC looping structure of a function. The entire
5758
function is held in a 'NULL' loop. */
@@ -158,6 +159,138 @@ add_decls_addresses_to_decl_constructor (vec<tree, va_gc> *v_decls,
158159
}
159160
}
160161

162+
/* Return true if DECL is a function for which its references should be
163+
analyzed. */
164+
165+
static bool
166+
omp_declare_target_fn_p (tree decl)
167+
{
168+
return (TREE_CODE (decl) == FUNCTION_DECL
169+
&& lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
170+
&& !lookup_attribute ("omp declare target host",
171+
DECL_ATTRIBUTES (decl))
172+
&& (!flag_openacc
173+
|| oacc_get_fn_attrib (decl) == NULL_TREE));
174+
}
175+
176+
/* Return true if DECL Is a variable for which its initializer references
177+
should be analyzed. */
178+
179+
static bool
180+
omp_declare_target_var_p (tree decl)
181+
{
182+
return (VAR_P (decl)
183+
&& lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
184+
&& !lookup_attribute ("omp declare target link",
185+
DECL_ATTRIBUTES (decl)));
186+
}
187+
188+
/* Helper function for omp_discover_implicit_declare_target, called through
189+
walk_tree. Mark referenced FUNCTION_DECLs implicitly as
190+
declare target to. */
191+
192+
static tree
193+
omp_discover_declare_target_fn_r (tree *tp, int *walk_subtrees, void *data)
194+
{
195+
if (TREE_CODE (*tp) == FUNCTION_DECL
196+
&& !omp_declare_target_fn_p (*tp)
197+
&& !lookup_attribute ("omp declare target host", DECL_ATTRIBUTES (*tp)))
198+
{
199+
tree id = get_identifier ("omp declare target");
200+
if (!DECL_EXTERNAL (*tp) && DECL_SAVED_TREE (*tp))
201+
((vec<tree> *) data)->safe_push (*tp);
202+
DECL_ATTRIBUTES (*tp) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (*tp));
203+
symtab_node *node = symtab_node::get (*tp);
204+
if (node != NULL)
205+
{
206+
node->offloadable = 1;
207+
if (ENABLE_OFFLOADING)
208+
g->have_offload = true;
209+
}
210+
}
211+
else if (TYPE_P (*tp))
212+
*walk_subtrees = 0;
213+
/* else if (TREE_CODE (*tp) == OMP_TARGET)
214+
{
215+
if (tree dev = omp_find_clause (OMP_TARGET_CLAUSES (*tp)))
216+
if (OMP_DEVICE_ANCESTOR (dev))
217+
*walk_subtrees = 0;
218+
} */
219+
return NULL_TREE;
220+
}
221+
222+
/* Helper function for omp_discover_implicit_declare_target, called through
223+
walk_tree. Mark referenced FUNCTION_DECLs implicitly as
224+
declare target to. */
225+
226+
static tree
227+
omp_discover_declare_target_var_r (tree *tp, int *walk_subtrees, void *data)
228+
{
229+
if (TREE_CODE (*tp) == FUNCTION_DECL)
230+
return omp_discover_declare_target_fn_r (tp, walk_subtrees, data);
231+
else if (VAR_P (*tp)
232+
&& is_global_var (*tp)
233+
&& !omp_declare_target_var_p (*tp))
234+
{
235+
tree id = get_identifier ("omp declare target");
236+
if (lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (*tp)))
237+
{
238+
error_at (DECL_SOURCE_LOCATION (*tp),
239+
"%qD specified both in declare target %<link%> and "
240+
"implicitly in %<to%> clauses", *tp);
241+
DECL_ATTRIBUTES (*tp)
242+
= remove_attribute ("omp declare target link", DECL_ATTRIBUTES (*tp));
243+
}
244+
if (TREE_STATIC (*tp) && DECL_INITIAL (*tp))
245+
((vec<tree> *) data)->safe_push (*tp);
246+
DECL_ATTRIBUTES (*tp) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (*tp));
247+
symtab_node *node = symtab_node::get (*tp);
248+
if (node != NULL && !node->offloadable)
249+
{
250+
node->offloadable = 1;
251+
if (ENABLE_OFFLOADING)
252+
{
253+
g->have_offload = true;
254+
if (is_a <varpool_node *> (node))
255+
vec_safe_push (offload_vars, node->decl);
256+
}
257+
}
258+
}
259+
else if (TYPE_P (*tp))
260+
*walk_subtrees = 0;
261+
return NULL_TREE;
262+
}
263+
264+
/* Perform the OpenMP implicit declare target to discovery. */
265+
266+
void
267+
omp_discover_implicit_declare_target (void)
268+
{
269+
cgraph_node *node;
270+
varpool_node *vnode;
271+
auto_vec<tree> worklist;
272+
273+
FOR_EACH_DEFINED_FUNCTION (node)
274+
if (omp_declare_target_fn_p (node->decl) && DECL_SAVED_TREE (node->decl))
275+
worklist.safe_push (node->decl);
276+
FOR_EACH_STATIC_INITIALIZER (vnode)
277+
if (omp_declare_target_var_p (vnode->decl))
278+
worklist.safe_push (vnode->decl);
279+
while (!worklist.is_empty ())
280+
{
281+
tree decl = worklist.pop ();
282+
if (TREE_CODE (decl) == FUNCTION_DECL)
283+
walk_tree_without_duplicates (&DECL_SAVED_TREE (decl),
284+
omp_discover_declare_target_fn_r,
285+
&worklist);
286+
else
287+
walk_tree_without_duplicates (&DECL_INITIAL (decl),
288+
omp_discover_declare_target_var_r,
289+
&worklist);
290+
}
291+
}
292+
293+
161294
/* Create new symbols containing (address, size) pairs for global variables,
162295
marked with "omp declare target" attribute, as well as addresses for the
163296
functions, which are outlined offloading regions. */

gcc/omp-offload.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,6 @@ extern GTY(()) vec<tree, va_gc> *offload_funcs;
3030
extern GTY(()) vec<tree, va_gc> *offload_vars;
3131

3232
extern void omp_finish_file (void);
33+
extern void omp_discover_implicit_declare_target (void);
3334

3435
#endif /* GCC_OMP_DEVICE_H */

libgomp/ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2020-05-12 Jakub Jelinek <jakub@redhat.com>
2+
3+
* testsuite/libgomp.c/target-39.c: New test.
4+
15
2020-04-29 Thomas Schwinge <thomas@codesourcery.com>
26

37
* config/accel/openacc.f90 (acc_device_current): Set to '-1'.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* { dg-do run } */
2+
/* { dg-options "-O0" } */
3+
4+
extern void abort (void);
5+
volatile int v;
6+
#pragma omp declare target to (v)
7+
typedef void (*fnp1) (void);
8+
typedef fnp1 (*fnp2) (void);
9+
void f1 (void) { v++; }
10+
void f2 (void) { v += 4; }
11+
void f3 (void) { v += 16; f1 (); }
12+
fnp1 f4 (void) { v += 64; return f2; }
13+
int a = 1;
14+
int *b = &a;
15+
int **c = &b;
16+
fnp2 f5 (void) { f3 (); return f4; }
17+
#pragma omp declare target to (c, f5)
18+
19+
int
20+
main ()
21+
{
22+
int err = 0;
23+
#pragma omp target map(from:err)
24+
{
25+
volatile int xa;
26+
int *volatile xb;
27+
int **volatile xc;
28+
fnp2 xd;
29+
fnp1 xe;
30+
err = 0;
31+
xa = a;
32+
err |= xa != 1;
33+
xb = b;
34+
err |= xb != &a;
35+
xc = c;
36+
err |= xc != &b;
37+
xd = f5 ();
38+
err |= v != 17;
39+
xe = xd ();
40+
err |= v != 81;
41+
xe ();
42+
err |= v != 85;
43+
}
44+
if (err)
45+
abort ();
46+
return 0;
47+
}

0 commit comments

Comments
 (0)