1 /* Bits of OpenMP and OpenACC handling that is specific to device offloading
2 and a lowering pass for OpenACC device directives.
4 Copyright (C) 2005-2017 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "tree-pass.h"
32 #include "pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "internal-fn.h"
36 #include "langhooks.h"
38 #include "gimple-iterator.h"
39 #include "gimplify-me.h"
40 #include "gimple-walk.h"
42 #include "tree-into-ssa.h"
43 #include "tree-nested.h"
44 #include "stor-layout.h"
45 #include "common/common-target.h"
46 #include "omp-general.h"
47 #include "omp-offload.h"
48 #include "lto-section-names.h"
49 #include "gomp-constants.h"
50 #include "gimple-pretty-print.h"
53 /* Describe the OpenACC looping structure of a function. The entire
54 function is held in a 'NULL' loop. */
58 oacc_loop
*parent
; /* Containing loop. */
60 oacc_loop
*child
; /* First inner loop. */
62 oacc_loop
*sibling
; /* Next loop within same parent. */
64 location_t loc
; /* Location of the loop start. */
66 gcall
*marker
; /* Initial head marker. */
68 gcall
*heads
[GOMP_DIM_MAX
]; /* Head marker functions. */
69 gcall
*tails
[GOMP_DIM_MAX
]; /* Tail marker functions. */
71 tree routine
; /* Pseudo-loop enclosing a routine. */
73 unsigned mask
; /* Partitioning mask. */
74 unsigned e_mask
; /* Partitioning of element loops (when tiling). */
75 unsigned inner
; /* Partitioning of inner loops. */
76 unsigned flags
; /* Partitioning flags. */
77 vec
<gcall
*> ifns
; /* Contained loop abstraction functions. */
78 tree chunk_size
; /* Chunk size. */
79 gcall
*head_end
; /* Final marker of head sequence. */
82 /* Holds offload tables with decls. */
83 vec
<tree
, va_gc
> *offload_funcs
, *offload_vars
;
85 /* Return level at which oacc routine may spawn a partitioned loop, or
86 -1 if it is not a routine (i.e. is an offload fn). */
89 oacc_fn_attrib_level (tree attr
)
91 tree pos
= TREE_VALUE (attr
);
93 if (!TREE_PURPOSE (pos
))
97 for (ix
= 0; ix
!= GOMP_DIM_MAX
;
98 ix
++, pos
= TREE_CHAIN (pos
))
99 if (!integer_zerop (TREE_PURPOSE (pos
)))
105 /* Helper function for omp_finish_file routine. Takes decls from V_DECLS and
106 adds their addresses and sizes to constructor-vector V_CTOR. */
109 add_decls_addresses_to_decl_constructor (vec
<tree
, va_gc
> *v_decls
,
110 vec
<constructor_elt
, va_gc
> *v_ctor
)
112 unsigned len
= vec_safe_length (v_decls
);
113 for (unsigned i
= 0; i
< len
; i
++)
115 tree it
= (*v_decls
)[i
];
116 bool is_var
= VAR_P (it
);
119 #ifdef ACCEL_COMPILER
120 && DECL_HAS_VALUE_EXPR_P (it
)
122 && lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (it
));
124 tree size
= NULL_TREE
;
126 size
= fold_convert (const_ptr_type_node
, DECL_SIZE_UNIT (it
));
130 addr
= build_fold_addr_expr (it
);
133 #ifdef ACCEL_COMPILER
134 /* For "omp declare target link" vars add address of the pointer to
135 the target table, instead of address of the var. */
136 tree value_expr
= DECL_VALUE_EXPR (it
);
137 tree link_ptr_decl
= TREE_OPERAND (value_expr
, 0);
138 varpool_node::finalize_decl (link_ptr_decl
);
139 addr
= build_fold_addr_expr (link_ptr_decl
);
141 addr
= build_fold_addr_expr (it
);
144 /* Most significant bit of the size marks "omp declare target link"
145 vars in host and target tables. */
146 unsigned HOST_WIDE_INT isize
= tree_to_uhwi (size
);
147 isize
|= 1ULL << (int_size_in_bytes (const_ptr_type_node
)
148 * BITS_PER_UNIT
- 1);
149 size
= wide_int_to_tree (const_ptr_type_node
, isize
);
152 CONSTRUCTOR_APPEND_ELT (v_ctor
, NULL_TREE
, addr
);
154 CONSTRUCTOR_APPEND_ELT (v_ctor
, NULL_TREE
, size
);
158 /* Create new symbols containing (address, size) pairs for global variables,
159 marked with "omp declare target" attribute, as well as addresses for the
160 functions, which are outlined offloading regions. */
162 omp_finish_file (void)
164 unsigned num_funcs
= vec_safe_length (offload_funcs
);
165 unsigned num_vars
= vec_safe_length (offload_vars
);
167 if (num_funcs
== 0 && num_vars
== 0)
170 if (targetm_common
.have_named_sections
)
172 vec
<constructor_elt
, va_gc
> *v_f
, *v_v
;
173 vec_alloc (v_f
, num_funcs
);
174 vec_alloc (v_v
, num_vars
* 2);
176 add_decls_addresses_to_decl_constructor (offload_funcs
, v_f
);
177 add_decls_addresses_to_decl_constructor (offload_vars
, v_v
);
179 tree vars_decl_type
= build_array_type_nelts (pointer_sized_int_node
,
181 tree funcs_decl_type
= build_array_type_nelts (pointer_sized_int_node
,
183 SET_TYPE_ALIGN (vars_decl_type
, TYPE_ALIGN (pointer_sized_int_node
));
184 SET_TYPE_ALIGN (funcs_decl_type
, TYPE_ALIGN (pointer_sized_int_node
));
185 tree ctor_v
= build_constructor (vars_decl_type
, v_v
);
186 tree ctor_f
= build_constructor (funcs_decl_type
, v_f
);
187 TREE_CONSTANT (ctor_v
) = TREE_CONSTANT (ctor_f
) = 1;
188 TREE_STATIC (ctor_v
) = TREE_STATIC (ctor_f
) = 1;
189 tree funcs_decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
190 get_identifier (".offload_func_table"),
192 tree vars_decl
= build_decl (UNKNOWN_LOCATION
, VAR_DECL
,
193 get_identifier (".offload_var_table"),
195 TREE_STATIC (funcs_decl
) = TREE_STATIC (vars_decl
) = 1;
196 /* Do not align tables more than TYPE_ALIGN (pointer_sized_int_node),
197 otherwise a joint table in a binary will contain padding between
198 tables from multiple object files. */
199 DECL_USER_ALIGN (funcs_decl
) = DECL_USER_ALIGN (vars_decl
) = 1;
200 SET_DECL_ALIGN (funcs_decl
, TYPE_ALIGN (funcs_decl_type
));
201 SET_DECL_ALIGN (vars_decl
, TYPE_ALIGN (vars_decl_type
));
202 DECL_INITIAL (funcs_decl
) = ctor_f
;
203 DECL_INITIAL (vars_decl
) = ctor_v
;
204 set_decl_section_name (funcs_decl
, OFFLOAD_FUNC_TABLE_SECTION_NAME
);
205 set_decl_section_name (vars_decl
, OFFLOAD_VAR_TABLE_SECTION_NAME
);
207 varpool_node::finalize_decl (vars_decl
);
208 varpool_node::finalize_decl (funcs_decl
);
212 for (unsigned i
= 0; i
< num_funcs
; i
++)
214 tree it
= (*offload_funcs
)[i
];
215 targetm
.record_offload_symbol (it
);
217 for (unsigned i
= 0; i
< num_vars
; i
++)
219 tree it
= (*offload_vars
)[i
];
220 targetm
.record_offload_symbol (it
);
225 /* Call dim_pos (POS == true) or dim_size (POS == false) builtins for
226 axis DIM. Return a tmp var holding the result. */
229 oacc_dim_call (bool pos
, int dim
, gimple_seq
*seq
)
231 tree arg
= build_int_cst (unsigned_type_node
, dim
);
232 tree size
= create_tmp_var (integer_type_node
);
233 enum internal_fn fn
= pos
? IFN_GOACC_DIM_POS
: IFN_GOACC_DIM_SIZE
;
234 gimple
*call
= gimple_build_call_internal (fn
, 1, arg
);
236 gimple_call_set_lhs (call
, size
);
237 gimple_seq_add_stmt (seq
, call
);
242 /* Find the number of threads (POS = false), or thread number (POS =
243 true) for an OpenACC region partitioned as MASK. Setup code
244 required for the calculation is added to SEQ. */
247 oacc_thread_numbers (bool pos
, int mask
, gimple_seq
*seq
)
249 tree res
= pos
? NULL_TREE
: build_int_cst (unsigned_type_node
, 1);
252 /* Start at gang level, and examine relevant dimension indices. */
253 for (ix
= GOMP_DIM_GANG
; ix
!= GOMP_DIM_MAX
; ix
++)
254 if (GOMP_DIM_MASK (ix
) & mask
)
258 /* We had an outer index, so scale that by the size of
260 tree n
= oacc_dim_call (false, ix
, seq
);
261 res
= fold_build2 (MULT_EXPR
, integer_type_node
, res
, n
);
265 /* Determine index in this dimension. */
266 tree id
= oacc_dim_call (true, ix
, seq
);
268 res
= fold_build2 (PLUS_EXPR
, integer_type_node
, res
, id
);
274 if (res
== NULL_TREE
)
275 res
= integer_zero_node
;
280 /* Transform IFN_GOACC_LOOP calls to actual code. See
281 expand_oacc_for for where these are generated. At the vector
282 level, we stride loops, such that each member of a warp will
283 operate on adjacent iterations. At the worker and gang level,
284 each gang/warp executes a set of contiguous iterations. Chunking
285 can override this such that each iteration engine executes a
286 contiguous chunk, and then moves on to stride to the next chunk. */
289 oacc_xform_loop (gcall
*call
)
291 gimple_stmt_iterator gsi
= gsi_for_stmt (call
);
292 enum ifn_goacc_loop_kind code
293 = (enum ifn_goacc_loop_kind
) TREE_INT_CST_LOW (gimple_call_arg (call
, 0));
294 tree dir
= gimple_call_arg (call
, 1);
295 tree range
= gimple_call_arg (call
, 2);
296 tree step
= gimple_call_arg (call
, 3);
297 tree chunk_size
= NULL_TREE
;
298 unsigned mask
= (unsigned) TREE_INT_CST_LOW (gimple_call_arg (call
, 5));
299 tree lhs
= gimple_call_lhs (call
);
300 tree type
= TREE_TYPE (lhs
);
301 tree diff_type
= TREE_TYPE (range
);
303 gimple_seq seq
= NULL
;
304 bool chunking
= false, striding
= true;
305 unsigned outer_mask
= mask
& (~mask
+ 1); // Outermost partitioning
306 unsigned inner_mask
= mask
& ~outer_mask
; // Inner partitioning (if any)
308 #ifdef ACCEL_COMPILER
309 chunk_size
= gimple_call_arg (call
, 4);
310 if (integer_minus_onep (chunk_size
) /* Force static allocation. */
311 || integer_zerop (chunk_size
)) /* Default (also static). */
313 /* If we're at the gang level, we want each to execute a
314 contiguous run of iterations. Otherwise we want each element
316 striding
= !(outer_mask
& GOMP_DIM_MASK (GOMP_DIM_GANG
));
321 /* Chunk of size 1 is striding. */
322 striding
= integer_onep (chunk_size
);
323 chunking
= !striding
;
327 /* striding=true, chunking=true
329 striding=true, chunking=false
331 striding=false,chunking=true
332 -> chunks=ceil (range/(chunksize*threads*step))
333 striding=false,chunking=false
334 -> chunk_size=ceil(range/(threads*step)),chunks=1 */
335 push_gimplify_context (true);
339 default: gcc_unreachable ();
341 case IFN_GOACC_LOOP_CHUNKS
:
343 r
= build_int_cst (type
, 1);
347 = (range - dir) / (chunks * step * num_threads) + dir */
348 tree per
= oacc_thread_numbers (false, mask
, &seq
);
349 per
= fold_convert (type
, per
);
350 chunk_size
= fold_convert (type
, chunk_size
);
351 per
= fold_build2 (MULT_EXPR
, type
, per
, chunk_size
);
352 per
= fold_build2 (MULT_EXPR
, type
, per
, step
);
353 r
= build2 (MINUS_EXPR
, type
, range
, dir
);
354 r
= build2 (PLUS_EXPR
, type
, r
, per
);
355 r
= build2 (TRUNC_DIV_EXPR
, type
, r
, per
);
359 case IFN_GOACC_LOOP_STEP
:
361 /* If striding, step by the entire compute volume, otherwise
362 step by the inner volume. */
363 unsigned volume
= striding
? mask
: inner_mask
;
365 r
= oacc_thread_numbers (false, volume
, &seq
);
366 r
= build2 (MULT_EXPR
, type
, fold_convert (type
, r
), step
);
370 case IFN_GOACC_LOOP_OFFSET
:
373 r
= oacc_thread_numbers (true, mask
, &seq
);
374 r
= fold_convert (diff_type
, r
);
378 tree inner_size
= oacc_thread_numbers (false, inner_mask
, &seq
);
379 tree outer_size
= oacc_thread_numbers (false, outer_mask
, &seq
);
380 tree volume
= fold_build2 (MULT_EXPR
, TREE_TYPE (inner_size
),
381 inner_size
, outer_size
);
383 volume
= fold_convert (diff_type
, volume
);
385 chunk_size
= fold_convert (diff_type
, chunk_size
);
388 tree per
= fold_build2 (MULT_EXPR
, diff_type
, volume
, step
);
390 chunk_size
= build2 (MINUS_EXPR
, diff_type
, range
, dir
);
391 chunk_size
= build2 (PLUS_EXPR
, diff_type
, chunk_size
, per
);
392 chunk_size
= build2 (TRUNC_DIV_EXPR
, diff_type
, chunk_size
, per
);
395 tree span
= build2 (MULT_EXPR
, diff_type
, chunk_size
,
396 fold_convert (diff_type
, inner_size
));
397 r
= oacc_thread_numbers (true, outer_mask
, &seq
);
398 r
= fold_convert (diff_type
, r
);
399 r
= build2 (MULT_EXPR
, diff_type
, r
, span
);
401 tree inner
= oacc_thread_numbers (true, inner_mask
, &seq
);
402 inner
= fold_convert (diff_type
, inner
);
403 r
= fold_build2 (PLUS_EXPR
, diff_type
, r
, inner
);
407 tree chunk
= fold_convert (diff_type
, gimple_call_arg (call
, 6));
409 = fold_build2 (MULT_EXPR
, diff_type
, volume
, chunk_size
);
410 per
= build2 (MULT_EXPR
, diff_type
, per
, chunk
);
412 r
= build2 (PLUS_EXPR
, diff_type
, r
, per
);
415 r
= fold_build2 (MULT_EXPR
, diff_type
, r
, step
);
416 if (type
!= diff_type
)
417 r
= fold_convert (type
, r
);
420 case IFN_GOACC_LOOP_BOUND
:
425 tree inner_size
= oacc_thread_numbers (false, inner_mask
, &seq
);
426 tree outer_size
= oacc_thread_numbers (false, outer_mask
, &seq
);
427 tree volume
= fold_build2 (MULT_EXPR
, TREE_TYPE (inner_size
),
428 inner_size
, outer_size
);
430 volume
= fold_convert (diff_type
, volume
);
432 chunk_size
= fold_convert (diff_type
, chunk_size
);
435 tree per
= fold_build2 (MULT_EXPR
, diff_type
, volume
, step
);
437 chunk_size
= build2 (MINUS_EXPR
, diff_type
, range
, dir
);
438 chunk_size
= build2 (PLUS_EXPR
, diff_type
, chunk_size
, per
);
439 chunk_size
= build2 (TRUNC_DIV_EXPR
, diff_type
, chunk_size
, per
);
442 tree span
= build2 (MULT_EXPR
, diff_type
, chunk_size
,
443 fold_convert (diff_type
, inner_size
));
445 r
= fold_build2 (MULT_EXPR
, diff_type
, span
, step
);
447 tree offset
= gimple_call_arg (call
, 6);
448 r
= build2 (PLUS_EXPR
, diff_type
, r
,
449 fold_convert (diff_type
, offset
));
450 r
= build2 (integer_onep (dir
) ? MIN_EXPR
: MAX_EXPR
,
451 diff_type
, r
, range
);
453 if (diff_type
!= type
)
454 r
= fold_convert (type
, r
);
458 gimplify_assign (lhs
, r
, &seq
);
460 pop_gimplify_context (NULL
);
462 gsi_replace_with_seq (&gsi
, seq
, true);
465 /* Transform a GOACC_TILE call. Determines the element loop span for
466 the specified loop of the nest. This is 1 if we're not tiling.
468 GOACC_TILE (collapse_count, loop_no, tile_arg, gwv_tile, gwv_element); */
471 oacc_xform_tile (gcall
*call
)
473 gimple_stmt_iterator gsi
= gsi_for_stmt (call
);
474 unsigned collapse
= tree_to_uhwi (gimple_call_arg (call
, 0));
475 /* Inner loops have higher loop_nos. */
476 unsigned loop_no
= tree_to_uhwi (gimple_call_arg (call
, 1));
477 tree tile_size
= gimple_call_arg (call
, 2);
478 unsigned e_mask
= tree_to_uhwi (gimple_call_arg (call
, 4));
479 tree lhs
= gimple_call_lhs (call
);
480 tree type
= TREE_TYPE (lhs
);
481 gimple_seq seq
= NULL
;
482 tree span
= build_int_cst (type
, 1);
485 & ~(GOMP_DIM_MASK (GOMP_DIM_VECTOR
)
486 | GOMP_DIM_MASK (GOMP_DIM_WORKER
))));
487 push_gimplify_context (!seen_error ());
489 #ifndef ACCEL_COMPILER
490 /* Partitioning disabled on host compilers. */
494 /* Not paritioning. */
495 span
= integer_one_node
;
496 else if (!integer_zerop (tile_size
))
497 /* User explicitly specified size. */
501 /* Pick a size based on the paritioning of the element loop and
502 the number of loop nests. */
503 tree first_size
= NULL_TREE
;
504 tree second_size
= NULL_TREE
;
506 if (e_mask
& GOMP_DIM_MASK (GOMP_DIM_VECTOR
))
507 first_size
= oacc_dim_call (false, GOMP_DIM_VECTOR
, &seq
);
508 if (e_mask
& GOMP_DIM_MASK (GOMP_DIM_WORKER
))
509 second_size
= oacc_dim_call (false, GOMP_DIM_WORKER
, &seq
);
513 first_size
= second_size
;
514 second_size
= NULL_TREE
;
517 if (loop_no
+ 1 == collapse
)
520 if (!loop_no
&& second_size
)
521 span
= fold_build2 (MULT_EXPR
, TREE_TYPE (span
),
524 else if (loop_no
+ 2 == collapse
)
530 /* There's no obvious element size for this loop. Options
531 are 1, first_size or some non-unity constant (32 is my
532 favourite). We should gather some statistics. */
536 span
= fold_convert (type
, span
);
537 gimplify_assign (lhs
, span
, &seq
);
539 pop_gimplify_context (NULL
);
541 gsi_replace_with_seq (&gsi
, seq
, true);
544 /* Default partitioned and minimum partitioned dimensions. */
546 static int oacc_default_dims
[GOMP_DIM_MAX
];
547 static int oacc_min_dims
[GOMP_DIM_MAX
];
549 /* Parse the default dimension parameter. This is a set of
550 :-separated optional compute dimensions. Each specified dimension
551 is a positive integer. When device type support is added, it is
552 planned to be a comma separated list of such compute dimensions,
553 with all but the first prefixed by the colon-terminated device
557 oacc_parse_default_dims (const char *dims
)
561 for (ix
= GOMP_DIM_MAX
; ix
--;)
563 oacc_default_dims
[ix
] = -1;
564 oacc_min_dims
[ix
] = 1;
567 #ifndef ACCEL_COMPILER
568 /* Cannot be overridden on the host. */
573 const char *pos
= dims
;
575 for (ix
= 0; *pos
&& ix
!= GOMP_DIM_MAX
; ix
++)
590 val
= strtol (pos
, CONST_CAST (char **, &eptr
), 10);
591 if (errno
|| val
<= 0 || (int) val
!= val
)
594 oacc_default_dims
[ix
] = (int) val
;
600 error_at (UNKNOWN_LOCATION
,
601 "-fopenacc-dim operand is malformed at '%s'", pos
);
605 /* Allow the backend to validate the dimensions. */
606 targetm
.goacc
.validate_dims (NULL_TREE
, oacc_default_dims
, -1);
607 targetm
.goacc
.validate_dims (NULL_TREE
, oacc_min_dims
, -2);
610 /* Validate and update the dimensions for offloaded FN. ATTRS is the
611 raw attribute. DIMS is an array of dimensions, which is filled in.
612 LEVEL is the partitioning level of a routine, or -1 for an offload
613 region itself. USED is the mask of partitioned execution in the
617 oacc_validate_dims (tree fn
, tree attrs
, int *dims
, int level
, unsigned used
)
619 tree purpose
[GOMP_DIM_MAX
];
621 tree pos
= TREE_VALUE (attrs
);
623 /* Make sure the attribute creator attached the dimension
627 for (ix
= 0; ix
!= GOMP_DIM_MAX
; ix
++)
629 purpose
[ix
] = TREE_PURPOSE (pos
);
630 tree val
= TREE_VALUE (pos
);
631 dims
[ix
] = val
? TREE_INT_CST_LOW (val
) : -1;
632 pos
= TREE_CHAIN (pos
);
635 bool changed
= targetm
.goacc
.validate_dims (fn
, dims
, level
);
637 /* Default anything left to 1 or a partitioned default. */
638 for (ix
= 0; ix
!= GOMP_DIM_MAX
; ix
++)
641 /* The OpenACC spec says 'If the [num_gangs] clause is not
642 specified, an implementation-defined default will be used;
643 the default may depend on the code within the construct.'
644 (2.5.6). Thus an implementation is free to choose
645 non-unity default for a parallel region that doesn't have
646 any gang-partitioned loops. However, it appears that there
647 is a sufficient body of user code that expects non-gang
648 partitioned regions to not execute in gang-redundant mode.
649 So we (a) don't warn about the non-portability and (b) pick
650 the minimum permissible dimension size when there is no
651 partitioned execution. Otherwise we pick the global
652 default for the dimension, which the user can control. The
653 same wording and logic applies to num_workers and
654 vector_length, however the worker- or vector- single
655 execution doesn't have the same impact as gang-redundant
656 execution. (If the minimum gang-level partioning is not 1,
657 the target is probably too confusing.) */
658 dims
[ix
] = (used
& GOMP_DIM_MASK (ix
)
659 ? oacc_default_dims
[ix
] : oacc_min_dims
[ix
]);
665 /* Replace the attribute with new values. */
667 for (ix
= GOMP_DIM_MAX
; ix
--;)
668 pos
= tree_cons (purpose
[ix
],
669 build_int_cst (integer_type_node
, dims
[ix
]), pos
);
670 oacc_replace_fn_attrib (fn
, pos
);
674 /* Create an empty OpenACC loop structure at LOC. */
677 new_oacc_loop_raw (oacc_loop
*parent
, location_t loc
)
679 oacc_loop
*loop
= XCNEW (oacc_loop
);
681 loop
->parent
= parent
;
685 loop
->sibling
= parent
->child
;
686 parent
->child
= loop
;
693 /* Create an outermost, dummy OpenACC loop for offloaded function
697 new_oacc_loop_outer (tree decl
)
699 return new_oacc_loop_raw (NULL
, DECL_SOURCE_LOCATION (decl
));
702 /* Start a new OpenACC loop structure beginning at head marker HEAD.
703 Link into PARENT loop. Return the new loop. */
706 new_oacc_loop (oacc_loop
*parent
, gcall
*marker
)
708 oacc_loop
*loop
= new_oacc_loop_raw (parent
, gimple_location (marker
));
710 loop
->marker
= marker
;
712 /* TODO: This is where device_type flattening would occur for the loop
715 loop
->flags
= TREE_INT_CST_LOW (gimple_call_arg (marker
, 3));
717 tree chunk_size
= integer_zero_node
;
718 if (loop
->flags
& OLF_GANG_STATIC
)
719 chunk_size
= gimple_call_arg (marker
, 4);
720 loop
->chunk_size
= chunk_size
;
725 /* Create a dummy loop encompassing a call to a openACC routine.
726 Extract the routine's partitioning requirements. */
729 new_oacc_loop_routine (oacc_loop
*parent
, gcall
*call
, tree decl
, tree attrs
)
731 oacc_loop
*loop
= new_oacc_loop_raw (parent
, gimple_location (call
));
732 int level
= oacc_fn_attrib_level (attrs
);
734 gcc_assert (level
>= 0);
737 loop
->routine
= decl
;
738 loop
->mask
= ((GOMP_DIM_MASK (GOMP_DIM_MAX
) - 1)
739 ^ (GOMP_DIM_MASK (level
) - 1));
742 /* Finish off the current OpenACC loop ending at tail marker TAIL.
743 Return the parent loop. */
746 finish_oacc_loop (oacc_loop
*loop
)
748 /* If the loop has been collapsed, don't partition it. */
749 if (loop
->ifns
.is_empty ())
750 loop
->mask
= loop
->flags
= 0;
754 /* Free all OpenACC loop structures within LOOP (inclusive). */
757 free_oacc_loop (oacc_loop
*loop
)
760 free_oacc_loop (loop
->sibling
);
762 free_oacc_loop (loop
->child
);
764 loop
->ifns
.release ();
768 /* Dump out the OpenACC loop head or tail beginning at FROM. */
771 dump_oacc_loop_part (FILE *file
, gcall
*from
, int depth
,
772 const char *title
, int level
)
774 enum ifn_unique_kind kind
775 = (enum ifn_unique_kind
) TREE_INT_CST_LOW (gimple_call_arg (from
, 0));
777 fprintf (file
, "%*s%s-%d:\n", depth
* 2, "", title
, level
);
778 for (gimple_stmt_iterator gsi
= gsi_for_stmt (from
);;)
780 gimple
*stmt
= gsi_stmt (gsi
);
782 if (gimple_call_internal_p (stmt
, IFN_UNIQUE
))
784 enum ifn_unique_kind k
785 = ((enum ifn_unique_kind
) TREE_INT_CST_LOW
786 (gimple_call_arg (stmt
, 0)));
788 if (k
== kind
&& stmt
!= from
)
791 print_gimple_stmt (file
, stmt
, depth
* 2 + 2);
794 while (gsi_end_p (gsi
))
795 gsi
= gsi_start_bb (single_succ (gsi_bb (gsi
)));
799 /* Dump OpenACC loops LOOP, its siblings and its children. */
802 dump_oacc_loop (FILE *file
, oacc_loop
*loop
, int depth
)
806 fprintf (file
, "%*sLoop %x(%x) %s:%u\n", depth
* 2, "",
807 loop
->flags
, loop
->mask
,
808 LOCATION_FILE (loop
->loc
), LOCATION_LINE (loop
->loc
));
811 print_gimple_stmt (file
, loop
->marker
, depth
* 2);
814 fprintf (file
, "%*sRoutine %s:%u:%s\n",
815 depth
* 2, "", DECL_SOURCE_FILE (loop
->routine
),
816 DECL_SOURCE_LINE (loop
->routine
),
817 IDENTIFIER_POINTER (DECL_NAME (loop
->routine
)));
819 for (ix
= GOMP_DIM_GANG
; ix
!= GOMP_DIM_MAX
; ix
++)
821 dump_oacc_loop_part (file
, loop
->heads
[ix
], depth
, "Head", ix
);
822 for (ix
= GOMP_DIM_MAX
; ix
--;)
824 dump_oacc_loop_part (file
, loop
->tails
[ix
], depth
, "Tail", ix
);
827 dump_oacc_loop (file
, loop
->child
, depth
+ 1);
829 dump_oacc_loop (file
, loop
->sibling
, depth
);
832 void debug_oacc_loop (oacc_loop
*);
834 /* Dump loops to stderr. */
837 debug_oacc_loop (oacc_loop
*loop
)
839 dump_oacc_loop (stderr
, loop
, 0);
842 /* DFS walk of basic blocks BB onwards, creating OpenACC loop
843 structures as we go. By construction these loops are properly
847 oacc_loop_discover_walk (oacc_loop
*loop
, basic_block bb
)
852 if (bb
->flags
& BB_VISITED
)
856 bb
->flags
|= BB_VISITED
;
858 /* Scan for loop markers. */
859 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
862 gimple
*stmt
= gsi_stmt (gsi
);
864 if (!is_gimple_call (stmt
))
867 gcall
*call
= as_a
<gcall
*> (stmt
);
869 /* If this is a routine, make a dummy loop for it. */
870 if (tree decl
= gimple_call_fndecl (call
))
871 if (tree attrs
= oacc_get_fn_attrib (decl
))
873 gcc_assert (!marker
);
874 new_oacc_loop_routine (loop
, call
, decl
, attrs
);
877 if (!gimple_call_internal_p (call
))
880 switch (gimple_call_internal_fn (call
))
887 /* Record the abstraction function, so we can manipulate it
889 loop
->ifns
.safe_push (call
);
893 enum ifn_unique_kind kind
894 = (enum ifn_unique_kind
) (TREE_INT_CST_LOW
895 (gimple_call_arg (call
, 0)));
896 if (kind
== IFN_UNIQUE_OACC_HEAD_MARK
897 || kind
== IFN_UNIQUE_OACC_TAIL_MARK
)
899 if (gimple_call_num_args (call
) == 2)
901 gcc_assert (marker
&& !remaining
);
903 if (kind
== IFN_UNIQUE_OACC_TAIL_MARK
)
904 loop
= finish_oacc_loop (loop
);
906 loop
->head_end
= call
;
910 int count
= TREE_INT_CST_LOW (gimple_call_arg (call
, 2));
914 if (kind
== IFN_UNIQUE_OACC_HEAD_MARK
)
915 loop
= new_oacc_loop (loop
, call
);
918 gcc_assert (count
== remaining
);
922 if (kind
== IFN_UNIQUE_OACC_HEAD_MARK
)
923 loop
->heads
[marker
] = call
;
925 loop
->tails
[remaining
] = call
;
932 if (remaining
|| marker
)
934 bb
= single_succ (bb
);
935 gcc_assert (single_pred_p (bb
) && !(bb
->flags
& BB_VISITED
));
939 /* Walk successor blocks. */
943 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
944 oacc_loop_discover_walk (loop
, e
->dest
);
947 /* LOOP is the first sibling. Reverse the order in place and return
948 the new first sibling. Recurse to child loops. */
951 oacc_loop_sibling_nreverse (oacc_loop
*loop
)
953 oacc_loop
*last
= NULL
;
957 loop
->child
= oacc_loop_sibling_nreverse (loop
->child
);
959 oacc_loop
*next
= loop
->sibling
;
960 loop
->sibling
= last
;
969 /* Discover the OpenACC loops marked up by HEAD and TAIL markers for
970 the current function. */
973 oacc_loop_discovery ()
975 /* Clear basic block flags, in particular BB_VISITED which we're going to use
979 oacc_loop
*top
= new_oacc_loop_outer (current_function_decl
);
980 oacc_loop_discover_walk (top
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
982 /* The siblings were constructed in reverse order, reverse them so
983 that diagnostics come out in an unsurprising order. */
984 top
= oacc_loop_sibling_nreverse (top
);
989 /* Transform the abstract internal function markers starting at FROM
990 to be for partitioning level LEVEL. Stop when we meet another HEAD
994 oacc_loop_xform_head_tail (gcall
*from
, int level
)
996 enum ifn_unique_kind kind
997 = (enum ifn_unique_kind
) TREE_INT_CST_LOW (gimple_call_arg (from
, 0));
998 tree replacement
= build_int_cst (unsigned_type_node
, level
);
1000 for (gimple_stmt_iterator gsi
= gsi_for_stmt (from
);;)
1002 gimple
*stmt
= gsi_stmt (gsi
);
1004 if (gimple_call_internal_p (stmt
, IFN_UNIQUE
))
1006 enum ifn_unique_kind k
1007 = ((enum ifn_unique_kind
)
1008 TREE_INT_CST_LOW (gimple_call_arg (stmt
, 0)));
1010 if (k
== IFN_UNIQUE_OACC_FORK
|| k
== IFN_UNIQUE_OACC_JOIN
)
1011 *gimple_call_arg_ptr (stmt
, 2) = replacement
;
1012 else if (k
== kind
&& stmt
!= from
)
1015 else if (gimple_call_internal_p (stmt
, IFN_GOACC_REDUCTION
))
1016 *gimple_call_arg_ptr (stmt
, 3) = replacement
;
1019 while (gsi_end_p (gsi
))
1020 gsi
= gsi_start_bb (single_succ (gsi_bb (gsi
)));
1024 /* Process the discovered OpenACC loops, setting the correct
1025 partitioning level etc. */
1028 oacc_loop_process (oacc_loop
*loop
)
1031 oacc_loop_process (loop
->child
);
1033 if (loop
->mask
&& !loop
->routine
)
1036 tree mask_arg
= build_int_cst (unsigned_type_node
, loop
->mask
);
1037 tree e_mask_arg
= build_int_cst (unsigned_type_node
, loop
->e_mask
);
1038 tree chunk_arg
= loop
->chunk_size
;
1041 for (ix
= 0; loop
->ifns
.iterate (ix
, &call
); ix
++)
1042 switch (gimple_call_internal_fn (call
))
1044 case IFN_GOACC_LOOP
:
1046 bool is_e
= gimple_call_arg (call
, 5) == integer_minus_one_node
;
1047 gimple_call_set_arg (call
, 5, is_e
? e_mask_arg
: mask_arg
);
1049 gimple_call_set_arg (call
, 4, chunk_arg
);
1053 case IFN_GOACC_TILE
:
1054 gimple_call_set_arg (call
, 3, mask_arg
);
1055 gimple_call_set_arg (call
, 4, e_mask_arg
);
1062 unsigned dim
= GOMP_DIM_GANG
;
1063 unsigned mask
= loop
->mask
| loop
->e_mask
;
1064 for (ix
= 0; ix
!= GOMP_DIM_MAX
&& mask
; ix
++)
1066 while (!(GOMP_DIM_MASK (dim
) & mask
))
1069 oacc_loop_xform_head_tail (loop
->heads
[ix
], dim
);
1070 oacc_loop_xform_head_tail (loop
->tails
[ix
], dim
);
1072 mask
^= GOMP_DIM_MASK (dim
);
1077 oacc_loop_process (loop
->sibling
);
1080 /* Walk the OpenACC loop heirarchy checking and assigning the
1081 programmer-specified partitionings. OUTER_MASK is the partitioning
1082 this loop is contained within. Return mask of partitioning
1083 encountered. If any auto loops are discovered, set GOMP_DIM_MAX
1087 oacc_loop_fixed_partitions (oacc_loop
*loop
, unsigned outer_mask
)
1089 unsigned this_mask
= loop
->mask
;
1090 unsigned mask_all
= 0;
1093 #ifdef ACCEL_COMPILER
1094 /* When device_type is supported, we want the device compiler to be
1095 noisy, if the loop parameters are device_type-specific. */
1101 bool auto_par
= (loop
->flags
& OLF_AUTO
) != 0;
1102 bool seq_par
= (loop
->flags
& OLF_SEQ
) != 0;
1103 bool tiling
= (loop
->flags
& OLF_TILE
) != 0;
1105 this_mask
= ((loop
->flags
>> OLF_DIM_BASE
)
1106 & (GOMP_DIM_MASK (GOMP_DIM_MAX
) - 1));
1108 /* Apply auto partitioning if this is a non-partitioned regular
1109 loop, or (no more than) single axis tiled loop. */
1111 = !seq_par
&& this_mask
== (tiling
? this_mask
& -this_mask
: 0);
1113 if ((this_mask
!= 0) + auto_par
+ seq_par
> 1)
1116 error_at (loop
->loc
,
1118 ? G_("%<seq%> overrides other OpenACC loop specifiers")
1119 : G_("%<auto%> conflicts with other OpenACC loop "
1122 loop
->flags
&= ~OLF_AUTO
;
1126 &= ~((GOMP_DIM_MASK (GOMP_DIM_MAX
) - 1) << OLF_DIM_BASE
);
1131 if (maybe_auto
&& (loop
->flags
& OLF_INDEPENDENT
))
1133 loop
->flags
|= OLF_AUTO
;
1134 mask_all
|= GOMP_DIM_MASK (GOMP_DIM_MAX
);
1138 if (this_mask
& outer_mask
)
1140 const oacc_loop
*outer
;
1141 for (outer
= loop
->parent
; outer
; outer
= outer
->parent
)
1142 if ((outer
->mask
| outer
->e_mask
) & this_mask
)
1149 error_at (loop
->loc
,
1151 ? G_("routine call uses same OpenACC parallelism"
1152 " as containing loop")
1153 : G_("inner loop uses same OpenACC parallelism"
1154 " as containing loop"));
1155 inform (outer
->loc
, "containing loop here");
1158 error_at (loop
->loc
,
1160 ? G_("routine call uses OpenACC parallelism disallowed"
1161 " by containing routine")
1162 : G_("loop uses OpenACC parallelism disallowed"
1163 " by containing routine"));
1166 inform (DECL_SOURCE_LOCATION (loop
->routine
),
1167 "routine %qD declared here", loop
->routine
);
1169 this_mask
&= ~outer_mask
;
1173 unsigned outermost
= least_bit_hwi (this_mask
);
1175 if (outermost
&& outermost
<= outer_mask
)
1179 error_at (loop
->loc
,
1180 "incorrectly nested OpenACC loop parallelism");
1182 const oacc_loop
*outer
;
1183 for (outer
= loop
->parent
;
1184 outer
->flags
&& outer
->flags
< outermost
;
1185 outer
= outer
->parent
)
1187 inform (outer
->loc
, "containing loop here");
1190 this_mask
&= ~outermost
;
1194 mask_all
|= this_mask
;
1196 if (loop
->flags
& OLF_TILE
)
1198 /* When tiling, vector goes to the element loop, and failing
1199 that we put worker there. The std doesn't contemplate
1200 specifying all three. We choose to put worker and vector on
1201 the element loops in that case. */
1202 unsigned this_e_mask
= this_mask
& GOMP_DIM_MASK (GOMP_DIM_VECTOR
);
1203 if (!this_e_mask
|| this_mask
& GOMP_DIM_MASK (GOMP_DIM_GANG
))
1204 this_e_mask
|= this_mask
& GOMP_DIM_MASK (GOMP_DIM_WORKER
);
1206 loop
->e_mask
= this_e_mask
;
1207 this_mask
^= this_e_mask
;
1210 loop
->mask
= this_mask
;
1213 fprintf (dump_file
, "Loop %s:%d user specified %d & %d\n",
1214 LOCATION_FILE (loop
->loc
), LOCATION_LINE (loop
->loc
),
1215 loop
->mask
, loop
->e_mask
);
1219 unsigned tmp_mask
= outer_mask
| this_mask
| loop
->e_mask
;
1220 loop
->inner
= oacc_loop_fixed_partitions (loop
->child
, tmp_mask
);
1221 mask_all
|= loop
->inner
;
1225 mask_all
|= oacc_loop_fixed_partitions (loop
->sibling
, outer_mask
);
1230 /* Walk the OpenACC loop heirarchy to assign auto-partitioned loops.
1231 OUTER_MASK is the partitioning this loop is contained within.
1232 OUTER_ASSIGN is true if an outer loop is being auto-partitioned.
1233 Return the cumulative partitioning used by this loop, siblings and
1237 oacc_loop_auto_partitions (oacc_loop
*loop
, unsigned outer_mask
,
1240 bool assign
= (loop
->flags
& OLF_AUTO
) && (loop
->flags
& OLF_INDEPENDENT
);
1242 bool tiling
= loop
->flags
& OLF_TILE
;
1244 #ifdef ACCEL_COMPILER
1245 /* When device_type is supported, we want the device compiler to be
1246 noisy, if the loop parameters are device_type-specific. */
1250 if (assign
&& (!outer_assign
|| loop
->inner
))
1252 /* Allocate outermost and non-innermost loops at the outermost
1253 non-innermost available level. */
1254 unsigned this_mask
= GOMP_DIM_MASK (GOMP_DIM_GANG
);
1256 /* Find the first outermost available partition. */
1257 while (this_mask
<= outer_mask
)
1260 /* Grab two axes if tiling, and we've not assigned anything */
1261 if (tiling
&& !(loop
->mask
| loop
->e_mask
))
1262 this_mask
|= this_mask
<< 1;
1264 /* Prohibit the innermost partitioning at the moment. */
1265 this_mask
&= GOMP_DIM_MASK (GOMP_DIM_MAX
- 1) - 1;
1267 /* Don't use any dimension explicitly claimed by an inner loop. */
1268 this_mask
&= ~loop
->inner
;
1270 if (tiling
&& !loop
->e_mask
)
1272 /* If we got two axes, allocate the inner one to the element
1274 loop
->e_mask
= this_mask
& (this_mask
<< 1);
1275 this_mask
^= loop
->e_mask
;
1278 loop
->mask
|= this_mask
;
1283 unsigned tmp_mask
= outer_mask
| loop
->mask
| loop
->e_mask
;
1284 loop
->inner
= oacc_loop_auto_partitions (loop
->child
, tmp_mask
,
1285 outer_assign
| assign
);
1288 if (assign
&& (!loop
->mask
|| (tiling
&& !loop
->e_mask
) || !outer_assign
))
1290 /* Allocate the loop at the innermost available level. Note
1291 that we do this even if we already assigned this loop the
1292 outermost available level above. That way we'll partition
1293 this along 2 axes, if they are available. */
1294 unsigned this_mask
= 0;
1296 /* Determine the outermost partitioning used within this loop. */
1297 this_mask
= loop
->inner
| GOMP_DIM_MASK (GOMP_DIM_MAX
);
1298 this_mask
= least_bit_hwi (this_mask
);
1300 /* Pick the partitioning just inside that one. */
1303 /* And avoid picking one use by an outer loop. */
1304 this_mask
&= ~outer_mask
;
1306 /* If tiling and we failed completely above, grab the next one
1307 too. Making sure it doesn't hit an outer loop. */
1310 this_mask
&= ~(loop
->e_mask
| loop
->mask
);
1311 unsigned tile_mask
= ((this_mask
>> 1)
1312 & ~(outer_mask
| loop
->e_mask
| loop
->mask
));
1314 if (tile_mask
|| loop
->mask
)
1316 loop
->e_mask
|= this_mask
;
1317 this_mask
= tile_mask
;
1319 if (!loop
->e_mask
&& noisy
)
1320 warning_at (loop
->loc
, 0,
1321 "insufficient partitioning available"
1322 " to parallelize element loop");
1325 loop
->mask
|= this_mask
;
1326 if (!loop
->mask
&& noisy
)
1327 warning_at (loop
->loc
, 0,
1329 ? G_("insufficient partitioning available"
1330 " to parallelize tile loop")
1331 : G_("insufficient partitioning available"
1332 " to parallelize loop"));
1335 if (assign
&& dump_file
)
1336 fprintf (dump_file
, "Auto loop %s:%d assigned %d & %d\n",
1337 LOCATION_FILE (loop
->loc
), LOCATION_LINE (loop
->loc
),
1338 loop
->mask
, loop
->e_mask
);
1340 unsigned inner_mask
= 0;
1343 inner_mask
|= oacc_loop_auto_partitions (loop
->sibling
,
1344 outer_mask
, outer_assign
);
1346 inner_mask
|= loop
->inner
| loop
->mask
| loop
->e_mask
;
1351 /* Walk the OpenACC loop heirarchy to check and assign partitioning
1352 axes. Return mask of partitioning. */
1355 oacc_loop_partition (oacc_loop
*loop
, unsigned outer_mask
)
1357 unsigned mask_all
= oacc_loop_fixed_partitions (loop
, outer_mask
);
1359 if (mask_all
& GOMP_DIM_MASK (GOMP_DIM_MAX
))
1361 mask_all
^= GOMP_DIM_MASK (GOMP_DIM_MAX
);
1362 mask_all
|= oacc_loop_auto_partitions (loop
, outer_mask
, false);
1367 /* Default fork/join early expander. Delete the function calls if
1368 there is no RTL expander. */
1371 default_goacc_fork_join (gcall
*ARG_UNUSED (call
),
1372 const int *ARG_UNUSED (dims
), bool is_fork
)
1375 return targetm
.have_oacc_fork ();
1377 return targetm
.have_oacc_join ();
1380 /* Default goacc.reduction early expander.
1382 LHS-opt = IFN_REDUCTION (KIND, RES_PTR, VAR, LEVEL, OP, OFFSET)
1383 If RES_PTR is not integer-zerop:
1384 SETUP - emit 'LHS = *RES_PTR', LHS = NULL
1385 TEARDOWN - emit '*RES_PTR = VAR'
1390 default_goacc_reduction (gcall
*call
)
1392 unsigned code
= (unsigned)TREE_INT_CST_LOW (gimple_call_arg (call
, 0));
1393 gimple_stmt_iterator gsi
= gsi_for_stmt (call
);
1394 tree lhs
= gimple_call_lhs (call
);
1395 tree var
= gimple_call_arg (call
, 2);
1396 gimple_seq seq
= NULL
;
1398 if (code
== IFN_GOACC_REDUCTION_SETUP
1399 || code
== IFN_GOACC_REDUCTION_TEARDOWN
)
1401 /* Setup and Teardown need to copy from/to the receiver object,
1403 tree ref_to_res
= gimple_call_arg (call
, 1);
1405 if (!integer_zerop (ref_to_res
))
1407 tree dst
= build_simple_mem_ref (ref_to_res
);
1410 if (code
== IFN_GOACC_REDUCTION_SETUP
)
1416 gimple_seq_add_stmt (&seq
, gimple_build_assign (dst
, src
));
1420 /* Copy VAR to LHS, if there is an LHS. */
1422 gimple_seq_add_stmt (&seq
, gimple_build_assign (lhs
, var
));
1424 gsi_replace_with_seq (&gsi
, seq
, true);
1427 /* Main entry point for oacc transformations which run on the device
1428 compiler after LTO, so we know what the target device is at this
1429 point (including the host fallback). */
1432 execute_oacc_device_lower ()
1434 tree attrs
= oacc_get_fn_attrib (current_function_decl
);
1437 /* Not an offloaded function. */
1440 /* Parse the default dim argument exactly once. */
1441 if ((const void *)flag_openacc_dims
!= &flag_openacc_dims
)
1443 oacc_parse_default_dims (flag_openacc_dims
);
1444 flag_openacc_dims
= (char *)&flag_openacc_dims
;
1447 bool is_oacc_kernels
1448 = (lookup_attribute ("oacc kernels",
1449 DECL_ATTRIBUTES (current_function_decl
)) != NULL
);
1450 bool is_oacc_kernels_parallelized
1451 = (lookup_attribute ("oacc kernels parallelized",
1452 DECL_ATTRIBUTES (current_function_decl
)) != NULL
);
1454 /* Discover, partition and process the loops. */
1455 oacc_loop
*loops
= oacc_loop_discovery ();
1456 int fn_level
= oacc_fn_attrib_level (attrs
);
1461 fprintf (dump_file
, "Function is OpenACC routine level %d\n",
1463 else if (is_oacc_kernels
)
1464 fprintf (dump_file
, "Function is %s OpenACC kernels offload\n",
1465 (is_oacc_kernels_parallelized
1466 ? "parallelized" : "unparallelized"));
1468 fprintf (dump_file
, "Function is OpenACC parallel offload\n");
1471 unsigned outer_mask
= fn_level
>= 0 ? GOMP_DIM_MASK (fn_level
) - 1 : 0;
1472 unsigned used_mask
= oacc_loop_partition (loops
, outer_mask
);
1473 /* OpenACC kernels constructs are special: they currently don't use the
1474 generic oacc_loop infrastructure and attribute/dimension processing. */
1475 if (is_oacc_kernels
&& is_oacc_kernels_parallelized
)
1477 /* Parallelized OpenACC kernels constructs use gang parallelism. See
1478 also tree-parloops.c:create_parallel_loop. */
1479 used_mask
|= GOMP_DIM_MASK (GOMP_DIM_GANG
);
1482 int dims
[GOMP_DIM_MAX
];
1483 oacc_validate_dims (current_function_decl
, attrs
, dims
, fn_level
, used_mask
);
1487 const char *comma
= "Compute dimensions [";
1488 for (int ix
= 0; ix
!= GOMP_DIM_MAX
; ix
++, comma
= ", ")
1489 fprintf (dump_file
, "%s%d", comma
, dims
[ix
]);
1490 fprintf (dump_file
, "]\n");
1493 oacc_loop_process (loops
);
1496 fprintf (dump_file
, "OpenACC loops\n");
1497 dump_oacc_loop (dump_file
, loops
, 0);
1498 fprintf (dump_file
, "\n");
1501 /* Offloaded targets may introduce new basic blocks, which require
1502 dominance information to update SSA. */
1503 calculate_dominance_info (CDI_DOMINATORS
);
1505 /* Now lower internal loop functions to target-specific code
1508 FOR_ALL_BB_FN (bb
, cfun
)
1509 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
1511 gimple
*stmt
= gsi_stmt (gsi
);
1512 if (!is_gimple_call (stmt
))
1518 gcall
*call
= as_a
<gcall
*> (stmt
);
1519 if (!gimple_call_internal_p (call
))
1525 /* Rewind to allow rescan. */
1527 bool rescan
= false, remove
= false;
1528 enum internal_fn ifn_code
= gimple_call_internal_fn (call
);
1534 case IFN_GOACC_TILE
:
1535 oacc_xform_tile (call
);
1539 case IFN_GOACC_LOOP
:
1540 oacc_xform_loop (call
);
1544 case IFN_GOACC_REDUCTION
:
1545 /* Mark the function for SSA renaming. */
1546 mark_virtual_operands_for_renaming (cfun
);
1548 /* If the level is -1, this ended up being an unused
1549 axis. Handle as a default. */
1550 if (integer_minus_onep (gimple_call_arg (call
, 3)))
1551 default_goacc_reduction (call
);
1553 targetm
.goacc
.reduction (call
);
1559 enum ifn_unique_kind kind
1560 = ((enum ifn_unique_kind
)
1561 TREE_INT_CST_LOW (gimple_call_arg (call
, 0)));
1568 case IFN_UNIQUE_OACC_FORK
:
1569 case IFN_UNIQUE_OACC_JOIN
:
1570 if (integer_minus_onep (gimple_call_arg (call
, 2)))
1572 else if (!targetm
.goacc
.fork_join
1573 (call
, dims
, kind
== IFN_UNIQUE_OACC_FORK
))
1577 case IFN_UNIQUE_OACC_HEAD_MARK
:
1578 case IFN_UNIQUE_OACC_TAIL_MARK
:
1586 if (gsi_end_p (gsi
))
1587 /* We rewound past the beginning of the BB. */
1588 gsi
= gsi_start_bb (bb
);
1590 /* Undo the rewind. */
1595 if (gimple_vdef (call
))
1596 replace_uses_by (gimple_vdef (call
), gimple_vuse (call
));
1597 if (gimple_call_lhs (call
))
1599 /* Propagate the data dependency var. */
1600 gimple
*ass
= gimple_build_assign (gimple_call_lhs (call
),
1601 gimple_call_arg (call
, 1));
1602 gsi_replace (&gsi
, ass
, false);
1605 gsi_remove (&gsi
, true);
1608 /* If not rescanning, advance over the call. */
1612 free_oacc_loop (loops
);
1617 /* Default launch dimension validator. Force everything to 1. A
1618 backend that wants to provide larger dimensions must override this
1622 default_goacc_validate_dims (tree
ARG_UNUSED (decl
), int *dims
,
1623 int ARG_UNUSED (fn_level
))
1625 bool changed
= false;
1627 for (unsigned ix
= 0; ix
!= GOMP_DIM_MAX
; ix
++)
1639 /* Default dimension bound is unknown on accelerator and 1 on host. */
1642 default_goacc_dim_limit (int ARG_UNUSED (axis
))
1644 #ifdef ACCEL_COMPILER
1653 const pass_data pass_data_oacc_device_lower
=
1655 GIMPLE_PASS
, /* type */
1656 "oaccdevlow", /* name */
1657 OPTGROUP_OMP
, /* optinfo_flags */
1658 TV_NONE
, /* tv_id */
1659 PROP_cfg
, /* properties_required */
1660 0 /* Possibly PROP_gimple_eomp. */, /* properties_provided */
1661 0, /* properties_destroyed */
1662 0, /* todo_flags_start */
1663 TODO_update_ssa
| TODO_cleanup_cfg
, /* todo_flags_finish */
1666 class pass_oacc_device_lower
: public gimple_opt_pass
1669 pass_oacc_device_lower (gcc::context
*ctxt
)
1670 : gimple_opt_pass (pass_data_oacc_device_lower
, ctxt
)
1673 /* opt_pass methods: */
1674 virtual bool gate (function
*) { return flag_openacc
; };
1676 virtual unsigned int execute (function
*)
1678 return execute_oacc_device_lower ();
1681 }; // class pass_oacc_device_lower
1686 make_pass_oacc_device_lower (gcc::context
*ctxt
)
1688 return new pass_oacc_device_lower (ctxt
);
1692 /* Rewrite GOMP_SIMT_ENTER_ALLOC call given by GSI and remove the preceding
1693 GOMP_SIMT_ENTER call identifying the privatized variables, which are
1694 turned to structure fields and receive a DECL_VALUE_EXPR accordingly.
1695 Set *REGIMPLIFY to true, except if no privatized variables were seen. */
1698 ompdevlow_adjust_simt_enter (gimple_stmt_iterator
*gsi
, bool *regimplify
)
1700 gimple
*alloc_stmt
= gsi_stmt (*gsi
);
1701 tree simtrec
= gimple_call_lhs (alloc_stmt
);
1702 tree simduid
= gimple_call_arg (alloc_stmt
, 0);
1703 gimple
*enter_stmt
= SSA_NAME_DEF_STMT (simduid
);
1704 gcc_assert (gimple_call_internal_p (enter_stmt
, IFN_GOMP_SIMT_ENTER
));
1705 tree rectype
= lang_hooks
.types
.make_type (RECORD_TYPE
);
1706 TYPE_ARTIFICIAL (rectype
) = TYPE_NAMELESS (rectype
) = 1;
1707 TREE_ADDRESSABLE (rectype
) = 1;
1708 TREE_TYPE (simtrec
) = build_pointer_type (rectype
);
1709 for (unsigned i
= 1; i
< gimple_call_num_args (enter_stmt
); i
++)
1711 tree
*argp
= gimple_call_arg_ptr (enter_stmt
, i
);
1712 if (*argp
== null_pointer_node
)
1714 gcc_assert (TREE_CODE (*argp
) == ADDR_EXPR
1715 && VAR_P (TREE_OPERAND (*argp
, 0)));
1716 tree var
= TREE_OPERAND (*argp
, 0);
1718 tree field
= build_decl (DECL_SOURCE_LOCATION (var
), FIELD_DECL
,
1719 DECL_NAME (var
), TREE_TYPE (var
));
1720 SET_DECL_ALIGN (field
, DECL_ALIGN (var
));
1721 DECL_USER_ALIGN (field
) = DECL_USER_ALIGN (var
);
1722 TREE_THIS_VOLATILE (field
) = TREE_THIS_VOLATILE (var
);
1724 insert_field_into_struct (rectype
, field
);
1726 tree t
= build_simple_mem_ref (simtrec
);
1727 t
= build3 (COMPONENT_REF
, TREE_TYPE (var
), t
, field
, NULL
);
1728 TREE_THIS_VOLATILE (t
) = TREE_THIS_VOLATILE (var
);
1729 SET_DECL_VALUE_EXPR (var
, t
);
1730 DECL_HAS_VALUE_EXPR_P (var
) = 1;
1733 layout_type (rectype
);
1734 tree size
= TYPE_SIZE_UNIT (rectype
);
1735 tree align
= build_int_cst (TREE_TYPE (size
), TYPE_ALIGN_UNIT (rectype
));
1738 = gimple_build_call_internal (IFN_GOMP_SIMT_ENTER_ALLOC
, 2, size
, align
);
1739 gimple_call_set_lhs (alloc_stmt
, simtrec
);
1740 gsi_replace (gsi
, alloc_stmt
, false);
1741 gimple_stmt_iterator enter_gsi
= gsi_for_stmt (enter_stmt
);
1742 enter_stmt
= gimple_build_assign (simduid
, gimple_call_arg (enter_stmt
, 0));
1743 gsi_replace (&enter_gsi
, enter_stmt
, false);
1747 if (single_imm_use (simtrec
, &use
, &exit_stmt
))
1749 gcc_assert (gimple_call_internal_p (exit_stmt
, IFN_GOMP_SIMT_EXIT
));
1750 gimple_stmt_iterator exit_gsi
= gsi_for_stmt (exit_stmt
);
1751 tree clobber
= build_constructor (rectype
, NULL
);
1752 TREE_THIS_VOLATILE (clobber
) = 1;
1753 exit_stmt
= gimple_build_assign (build_simple_mem_ref (simtrec
), clobber
);
1754 gsi_insert_before (&exit_gsi
, exit_stmt
, GSI_SAME_STMT
);
1757 gcc_checking_assert (has_zero_uses (simtrec
));
1760 /* Callback for walk_gimple_stmt used to scan for SIMT-privatized variables. */
1763 find_simtpriv_var_op (tree
*tp
, int *walk_subtrees
, void *)
1768 && DECL_HAS_VALUE_EXPR_P (t
)
1769 && lookup_attribute ("omp simt private", DECL_ATTRIBUTES (t
)))
1777 /* Cleanup uses of SIMT placeholder internal functions: on non-SIMT targets,
1778 VF is 1 and LANE is 0; on SIMT targets, VF is folded to a constant, and
1779 LANE is kept to be expanded to RTL later on. Also cleanup all other SIMT
1780 internal functions on non-SIMT targets, and likewise some SIMD internal
1781 functions on SIMT targets. */
1784 execute_omp_device_lower ()
1786 int vf
= targetm
.simt
.vf
? targetm
.simt
.vf () : 1;
1787 bool regimplify
= false;
1789 gimple_stmt_iterator gsi
;
1790 FOR_EACH_BB_FN (bb
, cfun
)
1791 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1793 gimple
*stmt
= gsi_stmt (gsi
);
1794 if (!is_gimple_call (stmt
) || !gimple_call_internal_p (stmt
))
1796 tree lhs
= gimple_call_lhs (stmt
), rhs
= NULL_TREE
;
1797 tree type
= lhs
? TREE_TYPE (lhs
) : integer_type_node
;
1798 switch (gimple_call_internal_fn (stmt
))
1800 case IFN_GOMP_USE_SIMT
:
1801 rhs
= vf
== 1 ? integer_zero_node
: integer_one_node
;
1803 case IFN_GOMP_SIMT_ENTER
:
1804 rhs
= vf
== 1 ? gimple_call_arg (stmt
, 0) : NULL_TREE
;
1805 goto simtreg_enter_exit
;
1806 case IFN_GOMP_SIMT_ENTER_ALLOC
:
1808 ompdevlow_adjust_simt_enter (&gsi
, ®implify
);
1809 rhs
= vf
== 1 ? null_pointer_node
: NULL_TREE
;
1810 goto simtreg_enter_exit
;
1811 case IFN_GOMP_SIMT_EXIT
:
1815 unlink_stmt_vdef (stmt
);
1817 case IFN_GOMP_SIMT_LANE
:
1818 case IFN_GOMP_SIMT_LAST_LANE
:
1819 rhs
= vf
== 1 ? build_zero_cst (type
) : NULL_TREE
;
1821 case IFN_GOMP_SIMT_VF
:
1822 rhs
= build_int_cst (type
, vf
);
1824 case IFN_GOMP_SIMT_ORDERED_PRED
:
1825 rhs
= vf
== 1 ? integer_zero_node
: NULL_TREE
;
1827 unlink_stmt_vdef (stmt
);
1829 case IFN_GOMP_SIMT_VOTE_ANY
:
1830 case IFN_GOMP_SIMT_XCHG_BFLY
:
1831 case IFN_GOMP_SIMT_XCHG_IDX
:
1832 rhs
= vf
== 1 ? gimple_call_arg (stmt
, 0) : NULL_TREE
;
1834 case IFN_GOMP_SIMD_LANE
:
1835 case IFN_GOMP_SIMD_LAST_LANE
:
1836 rhs
= vf
!= 1 ? build_zero_cst (type
) : NULL_TREE
;
1838 case IFN_GOMP_SIMD_VF
:
1839 rhs
= vf
!= 1 ? build_one_cst (type
) : NULL_TREE
;
1846 stmt
= lhs
? gimple_build_assign (lhs
, rhs
) : gimple_build_nop ();
1847 gsi_replace (&gsi
, stmt
, false);
1850 FOR_EACH_BB_REVERSE_FN (bb
, cfun
)
1851 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
); gsi_prev (&gsi
))
1852 if (walk_gimple_stmt (&gsi
, NULL
, find_simtpriv_var_op
, NULL
))
1854 if (gimple_clobber_p (gsi_stmt (gsi
)))
1855 gsi_remove (&gsi
, true);
1857 gimple_regimplify_operands (gsi_stmt (gsi
), &gsi
);
1860 cfun
->has_force_vectorize_loops
= false;
1866 const pass_data pass_data_omp_device_lower
=
1868 GIMPLE_PASS
, /* type */
1869 "ompdevlow", /* name */
1870 OPTGROUP_OMP
, /* optinfo_flags */
1871 TV_NONE
, /* tv_id */
1872 PROP_cfg
, /* properties_required */
1873 PROP_gimple_lomp_dev
, /* properties_provided */
1874 0, /* properties_destroyed */
1875 0, /* todo_flags_start */
1876 TODO_update_ssa
, /* todo_flags_finish */
1879 class pass_omp_device_lower
: public gimple_opt_pass
1882 pass_omp_device_lower (gcc::context
*ctxt
)
1883 : gimple_opt_pass (pass_data_omp_device_lower
, ctxt
)
1886 /* opt_pass methods: */
1887 virtual bool gate (function
*fun
)
1889 return !(fun
->curr_properties
& PROP_gimple_lomp_dev
);
1891 virtual unsigned int execute (function
*)
1893 return execute_omp_device_lower ();
1896 }; // class pass_expand_omp_ssa
1901 make_pass_omp_device_lower (gcc::context
*ctxt
)
1903 return new pass_omp_device_lower (ctxt
);
1906 /* "omp declare target link" handling pass. */
1910 const pass_data pass_data_omp_target_link
=
1912 GIMPLE_PASS
, /* type */
1913 "omptargetlink", /* name */
1914 OPTGROUP_OMP
, /* optinfo_flags */
1915 TV_NONE
, /* tv_id */
1916 PROP_ssa
, /* properties_required */
1917 0, /* properties_provided */
1918 0, /* properties_destroyed */
1919 0, /* todo_flags_start */
1920 TODO_update_ssa
, /* todo_flags_finish */
1923 class pass_omp_target_link
: public gimple_opt_pass
1926 pass_omp_target_link (gcc::context
*ctxt
)
1927 : gimple_opt_pass (pass_data_omp_target_link
, ctxt
)
1930 /* opt_pass methods: */
1931 virtual bool gate (function
*fun
)
1933 #ifdef ACCEL_COMPILER
1934 tree attrs
= DECL_ATTRIBUTES (fun
->decl
);
1935 return lookup_attribute ("omp declare target", attrs
)
1936 || lookup_attribute ("omp target entrypoint", attrs
);
1943 virtual unsigned execute (function
*);
1946 /* Callback for walk_gimple_stmt used to scan for link var operands. */
1949 find_link_var_op (tree
*tp
, int *walk_subtrees
, void *)
1954 && DECL_HAS_VALUE_EXPR_P (t
)
1955 && is_global_var (t
)
1956 && lookup_attribute ("omp declare target link", DECL_ATTRIBUTES (t
)))
1966 pass_omp_target_link::execute (function
*fun
)
1969 FOR_EACH_BB_FN (bb
, fun
)
1971 gimple_stmt_iterator gsi
;
1972 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1973 if (walk_gimple_stmt (&gsi
, NULL
, find_link_var_op
, NULL
))
1974 gimple_regimplify_operands (gsi_stmt (gsi
), &gsi
);
1983 make_pass_omp_target_link (gcc::context
*ctxt
)
1985 return new pass_omp_target_link (ctxt
);