annotate gcc/omp-grid.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Lowering and expansion of OpenMP directives for HSA GPU agents.
kono
parents:
diff changeset
2
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
3 Copyright (C) 2013-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 #include "config.h"
kono
parents:
diff changeset
22 #include "system.h"
kono
parents:
diff changeset
23 #include "coretypes.h"
kono
parents:
diff changeset
24 #include "backend.h"
kono
parents:
diff changeset
25 #include "tree.h"
kono
parents:
diff changeset
26 #include "gimple.h"
kono
parents:
diff changeset
27 #include "tree-pass.h"
kono
parents:
diff changeset
28 #include "ssa.h"
kono
parents:
diff changeset
29 #include "cgraph.h"
kono
parents:
diff changeset
30 #include "pretty-print.h"
kono
parents:
diff changeset
31 #include "fold-const.h"
kono
parents:
diff changeset
32 #include "gimplify.h"
kono
parents:
diff changeset
33 #include "gimple-iterator.h"
kono
parents:
diff changeset
34 #include "gimple-walk.h"
kono
parents:
diff changeset
35 #include "tree-inline.h"
kono
parents:
diff changeset
36 #include "langhooks.h"
kono
parents:
diff changeset
37 #include "omp-general.h"
kono
parents:
diff changeset
38 #include "omp-low.h"
kono
parents:
diff changeset
39 #include "omp-grid.h"
kono
parents:
diff changeset
40 #include "gimple-pretty-print.h"
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 /* Return the lastprivate predicate for a given gridified loop described by
kono
parents:
diff changeset
43 FD). */
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45 tree
kono
parents:
diff changeset
46 omp_grid_lastprivate_predicate (struct omp_for_data *fd)
kono
parents:
diff changeset
47 {
kono
parents:
diff changeset
48 /* When dealing with a gridified loop, we need to check up to three collapsed
kono
parents:
diff changeset
49 iteration variables but they are not actually captured in this fd.
kono
parents:
diff changeset
50 Fortunately, we can easily rely on HSA builtins to get this
kono
parents:
diff changeset
51 information. */
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 tree id, size;
kono
parents:
diff changeset
54 if (gimple_omp_for_kind (fd->for_stmt) == GF_OMP_FOR_KIND_GRID_LOOP
kono
parents:
diff changeset
55 && gimple_omp_for_grid_intra_group (fd->for_stmt))
kono
parents:
diff changeset
56 {
kono
parents:
diff changeset
57 id = builtin_decl_explicit (BUILT_IN_HSA_WORKITEMID);
kono
parents:
diff changeset
58 size = builtin_decl_explicit (BUILT_IN_HSA_CURRENTWORKGROUPSIZE);
kono
parents:
diff changeset
59 }
kono
parents:
diff changeset
60 else
kono
parents:
diff changeset
61 {
kono
parents:
diff changeset
62 id = builtin_decl_explicit (BUILT_IN_HSA_WORKITEMABSID);
kono
parents:
diff changeset
63 size = builtin_decl_explicit (BUILT_IN_HSA_GRIDSIZE);
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65 tree cond = NULL;
kono
parents:
diff changeset
66 for (int dim = 0; dim < fd->collapse; dim++)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 tree dim_tree = build_int_cstu (unsigned_type_node, dim);
kono
parents:
diff changeset
69 tree u1 = build_int_cstu (unsigned_type_node, 1);
kono
parents:
diff changeset
70 tree c2
kono
parents:
diff changeset
71 = build2 (EQ_EXPR, boolean_type_node,
kono
parents:
diff changeset
72 build2 (PLUS_EXPR, unsigned_type_node,
kono
parents:
diff changeset
73 build_call_expr (id, 1, dim_tree), u1),
kono
parents:
diff changeset
74 build_call_expr (size, 1, dim_tree));
kono
parents:
diff changeset
75 if (cond)
kono
parents:
diff changeset
76 cond = build2 (TRUTH_AND_EXPR, boolean_type_node, cond, c2);
kono
parents:
diff changeset
77 else
kono
parents:
diff changeset
78 cond = c2;
kono
parents:
diff changeset
79 }
kono
parents:
diff changeset
80 return cond;
kono
parents:
diff changeset
81 }
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 /* Structure describing the basic properties of the loop we ara analyzing
kono
parents:
diff changeset
84 whether it can be gridified and when it is gridified. */
kono
parents:
diff changeset
85
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
86 class grid_prop
111
kono
parents:
diff changeset
87 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
88 public:
111
kono
parents:
diff changeset
89 /* True when we are doing tiling gridification, i.e. when there is a distinct
kono
parents:
diff changeset
90 distribute loop over groups and a loop construct over work-items. False
kono
parents:
diff changeset
91 when distribute and parallel for loops form a combined construct. */
kono
parents:
diff changeset
92 bool tiling;
kono
parents:
diff changeset
93 /* Location of the target construct for optimization information
kono
parents:
diff changeset
94 messages. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
95 dump_user_location_t target_loc;
111
kono
parents:
diff changeset
96 /* The collapse clause of the involved loops. Collapse value of all of them
kono
parents:
diff changeset
97 must be the same for gridification to take place. */
kono
parents:
diff changeset
98 size_t collapse;
kono
parents:
diff changeset
99 /* Group sizes, if requested by the user or NULL if not requested. */
kono
parents:
diff changeset
100 tree group_sizes[3];
kono
parents:
diff changeset
101 };
kono
parents:
diff changeset
102
kono
parents:
diff changeset
103 #define GRID_MISSED_MSG_PREFIX "Will not turn target construct into a " \
kono
parents:
diff changeset
104 "gridified HSA kernel because "
kono
parents:
diff changeset
105
kono
parents:
diff changeset
106 /* Return true if STMT is an assignment of a register-type into a local
kono
parents:
diff changeset
107 VAR_DECL. If GRID is non-NULL, the assignment additionally must not be to
kono
parents:
diff changeset
108 any of the trees specifying group sizes there. */
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 static bool
kono
parents:
diff changeset
111 grid_safe_assignment_p (gimple *stmt, grid_prop *grid)
kono
parents:
diff changeset
112 {
kono
parents:
diff changeset
113 gassign *assign = dyn_cast <gassign *> (stmt);
kono
parents:
diff changeset
114 if (!assign)
kono
parents:
diff changeset
115 return false;
kono
parents:
diff changeset
116 if (gimple_clobber_p (assign))
kono
parents:
diff changeset
117 return true;
kono
parents:
diff changeset
118 tree lhs = gimple_assign_lhs (assign);
kono
parents:
diff changeset
119 if (!VAR_P (lhs)
kono
parents:
diff changeset
120 || !is_gimple_reg_type (TREE_TYPE (lhs))
kono
parents:
diff changeset
121 || is_global_var (lhs))
kono
parents:
diff changeset
122 return false;
kono
parents:
diff changeset
123 if (grid)
kono
parents:
diff changeset
124 for (unsigned i = 0; i < grid->collapse; i++)
kono
parents:
diff changeset
125 if (lhs == grid->group_sizes[i])
kono
parents:
diff changeset
126 return false;
kono
parents:
diff changeset
127 return true;
kono
parents:
diff changeset
128 }
kono
parents:
diff changeset
129
kono
parents:
diff changeset
130 /* Return true if all statements in SEQ are assignments to local register-type
kono
parents:
diff changeset
131 variables that do not hold group size information. */
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 static bool
kono
parents:
diff changeset
134 grid_seq_only_contains_local_assignments (gimple_seq seq, grid_prop *grid)
kono
parents:
diff changeset
135 {
kono
parents:
diff changeset
136 if (!seq)
kono
parents:
diff changeset
137 return true;
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
140 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
141 if (!grid_safe_assignment_p (gsi_stmt (gsi), grid))
kono
parents:
diff changeset
142 return false;
kono
parents:
diff changeset
143 return true;
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 /* Scan statements in SEQ and call itself recursively on any bind. GRID
kono
parents:
diff changeset
147 describes hitherto discovered properties of the loop that is evaluated for
kono
parents:
diff changeset
148 possible gridification. If during whole search only assignments to
kono
parents:
diff changeset
149 register-type local variables (that do not overwrite group size information)
kono
parents:
diff changeset
150 and one single OMP statement is encountered, return true, otherwise return
kono
parents:
diff changeset
151 false. RET is where we store any OMP statement encountered. */
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 static bool
kono
parents:
diff changeset
154 grid_find_single_omp_among_assignments_1 (gimple_seq seq, grid_prop *grid,
kono
parents:
diff changeset
155 const char *name, gimple **ret)
kono
parents:
diff changeset
156 {
kono
parents:
diff changeset
157 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
158 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
159 {
kono
parents:
diff changeset
160 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 if (grid_safe_assignment_p (stmt, grid))
kono
parents:
diff changeset
163 continue;
kono
parents:
diff changeset
164 if (gbind *bind = dyn_cast <gbind *> (stmt))
kono
parents:
diff changeset
165 {
kono
parents:
diff changeset
166 gimple_seq bind_body = gimple_bind_body (bind);
kono
parents:
diff changeset
167 if (!grid_find_single_omp_among_assignments_1 (bind_body, grid, name,
kono
parents:
diff changeset
168 ret))
kono
parents:
diff changeset
169 return false;
kono
parents:
diff changeset
170 }
kono
parents:
diff changeset
171 else if (is_gimple_omp (stmt))
kono
parents:
diff changeset
172 {
kono
parents:
diff changeset
173 if (*ret)
kono
parents:
diff changeset
174 {
kono
parents:
diff changeset
175 if (dump_enabled_p ())
kono
parents:
diff changeset
176 {
kono
parents:
diff changeset
177 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
178 GRID_MISSED_MSG_PREFIX "%s construct "
kono
parents:
diff changeset
179 "contains multiple OpenMP constructs\n",
kono
parents:
diff changeset
180 name);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 dump_printf_loc (MSG_NOTE, *ret,
111
kono
parents:
diff changeset
182 "The first OpenMP construct within "
kono
parents:
diff changeset
183 "a parallel\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
184 dump_printf_loc (MSG_NOTE, stmt,
111
kono
parents:
diff changeset
185 "The second OpenMP construct within "
kono
parents:
diff changeset
186 "a parallel\n");
kono
parents:
diff changeset
187 }
kono
parents:
diff changeset
188 return false;
kono
parents:
diff changeset
189 }
kono
parents:
diff changeset
190 *ret = stmt;
kono
parents:
diff changeset
191 }
kono
parents:
diff changeset
192 else
kono
parents:
diff changeset
193 {
kono
parents:
diff changeset
194 if (dump_enabled_p ())
kono
parents:
diff changeset
195 {
kono
parents:
diff changeset
196 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
197 GRID_MISSED_MSG_PREFIX "%s construct contains "
kono
parents:
diff changeset
198 "a complex statement\n", name);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
199 dump_printf_loc (MSG_NOTE, stmt,
111
kono
parents:
diff changeset
200 "This statement cannot be analyzed for "
kono
parents:
diff changeset
201 "gridification\n");
kono
parents:
diff changeset
202 }
kono
parents:
diff changeset
203 return false;
kono
parents:
diff changeset
204 }
kono
parents:
diff changeset
205 }
kono
parents:
diff changeset
206 return true;
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 /* Scan statements in SEQ and make sure that it and any binds in it contain
kono
parents:
diff changeset
210 only assignments to local register-type variables (that do not overwrite
kono
parents:
diff changeset
211 group size information) and one OMP construct. If so, return that
kono
parents:
diff changeset
212 construct, otherwise return NULL. GRID describes hitherto discovered
kono
parents:
diff changeset
213 properties of the loop that is evaluated for possible gridification. If
kono
parents:
diff changeset
214 dumping is enabled and function fails, use NAME to dump a note with the
kono
parents:
diff changeset
215 reason for failure. */
kono
parents:
diff changeset
216
kono
parents:
diff changeset
217 static gimple *
kono
parents:
diff changeset
218 grid_find_single_omp_among_assignments (gimple_seq seq, grid_prop *grid,
kono
parents:
diff changeset
219 const char *name)
kono
parents:
diff changeset
220 {
kono
parents:
diff changeset
221 if (!seq)
kono
parents:
diff changeset
222 {
kono
parents:
diff changeset
223 if (dump_enabled_p ())
kono
parents:
diff changeset
224 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
225 GRID_MISSED_MSG_PREFIX "%s construct has empty body\n",
kono
parents:
diff changeset
226 name);
kono
parents:
diff changeset
227 return NULL;
kono
parents:
diff changeset
228 }
kono
parents:
diff changeset
229
kono
parents:
diff changeset
230 gimple *ret = NULL;
kono
parents:
diff changeset
231 if (grid_find_single_omp_among_assignments_1 (seq, grid, name, &ret))
kono
parents:
diff changeset
232 {
kono
parents:
diff changeset
233 if (!ret && dump_enabled_p ())
kono
parents:
diff changeset
234 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
235 GRID_MISSED_MSG_PREFIX "%s construct does not contain"
kono
parents:
diff changeset
236 " any other OpenMP construct\n", name);
kono
parents:
diff changeset
237 return ret;
kono
parents:
diff changeset
238 }
kono
parents:
diff changeset
239 else
kono
parents:
diff changeset
240 return NULL;
kono
parents:
diff changeset
241 }
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 /* Walker function looking for statements there is no point gridifying (and for
kono
parents:
diff changeset
244 noreturn function calls which we cannot do). Return non-NULL if such a
kono
parents:
diff changeset
245 function is found. */
kono
parents:
diff changeset
246
kono
parents:
diff changeset
247 static tree
kono
parents:
diff changeset
248 grid_find_ungridifiable_statement (gimple_stmt_iterator *gsi,
kono
parents:
diff changeset
249 bool *handled_ops_p,
kono
parents:
diff changeset
250 struct walk_stmt_info *wi)
kono
parents:
diff changeset
251 {
kono
parents:
diff changeset
252 *handled_ops_p = false;
kono
parents:
diff changeset
253 gimple *stmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
254 switch (gimple_code (stmt))
kono
parents:
diff changeset
255 {
kono
parents:
diff changeset
256 case GIMPLE_CALL:
kono
parents:
diff changeset
257 if (gimple_call_noreturn_p (as_a <gcall *> (stmt)))
kono
parents:
diff changeset
258 {
kono
parents:
diff changeset
259 *handled_ops_p = true;
kono
parents:
diff changeset
260 wi->info = stmt;
kono
parents:
diff changeset
261 return error_mark_node;
kono
parents:
diff changeset
262 }
kono
parents:
diff changeset
263 break;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 /* We may reduce the following list if we find a way to implement the
kono
parents:
diff changeset
266 clauses, but now there is no point trying further. */
kono
parents:
diff changeset
267 case GIMPLE_OMP_CRITICAL:
kono
parents:
diff changeset
268 case GIMPLE_OMP_TASKGROUP:
kono
parents:
diff changeset
269 case GIMPLE_OMP_TASK:
kono
parents:
diff changeset
270 case GIMPLE_OMP_SECTION:
kono
parents:
diff changeset
271 case GIMPLE_OMP_SECTIONS:
kono
parents:
diff changeset
272 case GIMPLE_OMP_SECTIONS_SWITCH:
kono
parents:
diff changeset
273 case GIMPLE_OMP_TARGET:
kono
parents:
diff changeset
274 case GIMPLE_OMP_ORDERED:
kono
parents:
diff changeset
275 *handled_ops_p = true;
kono
parents:
diff changeset
276 wi->info = stmt;
kono
parents:
diff changeset
277 return error_mark_node;
kono
parents:
diff changeset
278 default:
kono
parents:
diff changeset
279 break;
kono
parents:
diff changeset
280 }
kono
parents:
diff changeset
281 return NULL;
kono
parents:
diff changeset
282 }
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 /* Examine clauses of omp parallel statement PAR and if any prevents
kono
parents:
diff changeset
285 gridification, issue a missed-optimization diagnostics and return false,
kono
parents:
diff changeset
286 otherwise return true. GRID describes hitherto discovered properties of the
kono
parents:
diff changeset
287 loop that is evaluated for possible gridification. */
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 static bool
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
290 grid_parallel_clauses_gridifiable (gomp_parallel *par, dump_user_location_t tloc)
111
kono
parents:
diff changeset
291 {
kono
parents:
diff changeset
292 tree clauses = gimple_omp_parallel_clauses (par);
kono
parents:
diff changeset
293 while (clauses)
kono
parents:
diff changeset
294 {
kono
parents:
diff changeset
295 switch (OMP_CLAUSE_CODE (clauses))
kono
parents:
diff changeset
296 {
kono
parents:
diff changeset
297 case OMP_CLAUSE_NUM_THREADS:
kono
parents:
diff changeset
298 if (dump_enabled_p ())
kono
parents:
diff changeset
299 {
kono
parents:
diff changeset
300 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
301 GRID_MISSED_MSG_PREFIX "because there is "
kono
parents:
diff changeset
302 "a num_threads clause of the parallel "
kono
parents:
diff changeset
303 "construct\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
304 dump_printf_loc (MSG_NOTE, par,
111
kono
parents:
diff changeset
305 "Parallel construct has a num_threads clause\n");
kono
parents:
diff changeset
306 }
kono
parents:
diff changeset
307 return false;
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 case OMP_CLAUSE_REDUCTION:
kono
parents:
diff changeset
310 if (dump_enabled_p ())
kono
parents:
diff changeset
311 {
kono
parents:
diff changeset
312 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
313 GRID_MISSED_MSG_PREFIX "a reduction clause "
kono
parents:
diff changeset
314 "is present\n ");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
315 dump_printf_loc (MSG_NOTE, par,
111
kono
parents:
diff changeset
316 "Parallel construct has a reduction clause\n");
kono
parents:
diff changeset
317 }
kono
parents:
diff changeset
318 return false;
kono
parents:
diff changeset
319
kono
parents:
diff changeset
320 default:
kono
parents:
diff changeset
321 break;
kono
parents:
diff changeset
322 }
kono
parents:
diff changeset
323 clauses = OMP_CLAUSE_CHAIN (clauses);
kono
parents:
diff changeset
324 }
kono
parents:
diff changeset
325 return true;
kono
parents:
diff changeset
326 }
kono
parents:
diff changeset
327
kono
parents:
diff changeset
328 /* Examine clauses and the body of omp loop statement GFOR and if something
kono
parents:
diff changeset
329 prevents gridification, issue a missed-optimization diagnostics and return
kono
parents:
diff changeset
330 false, otherwise return true. GRID describes hitherto discovered properties
kono
parents:
diff changeset
331 of the loop that is evaluated for possible gridification. */
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 static bool
kono
parents:
diff changeset
334 grid_inner_loop_gridifiable_p (gomp_for *gfor, grid_prop *grid)
kono
parents:
diff changeset
335 {
kono
parents:
diff changeset
336 if (!grid_seq_only_contains_local_assignments (gimple_omp_for_pre_body (gfor),
kono
parents:
diff changeset
337 grid))
kono
parents:
diff changeset
338 {
kono
parents:
diff changeset
339 if (dump_enabled_p ())
kono
parents:
diff changeset
340 {
kono
parents:
diff changeset
341 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
342 GRID_MISSED_MSG_PREFIX "the inner loop "
kono
parents:
diff changeset
343 "loop bounds computation contains a complex "
kono
parents:
diff changeset
344 "statement\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
345 dump_printf_loc (MSG_NOTE, gfor,
111
kono
parents:
diff changeset
346 "Loop construct cannot be analyzed for "
kono
parents:
diff changeset
347 "gridification\n");
kono
parents:
diff changeset
348 }
kono
parents:
diff changeset
349 return false;
kono
parents:
diff changeset
350 }
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 tree clauses = gimple_omp_for_clauses (gfor);
kono
parents:
diff changeset
353 while (clauses)
kono
parents:
diff changeset
354 {
kono
parents:
diff changeset
355 switch (OMP_CLAUSE_CODE (clauses))
kono
parents:
diff changeset
356 {
kono
parents:
diff changeset
357 case OMP_CLAUSE_SCHEDULE:
kono
parents:
diff changeset
358 if (OMP_CLAUSE_SCHEDULE_KIND (clauses) != OMP_CLAUSE_SCHEDULE_AUTO)
kono
parents:
diff changeset
359 {
kono
parents:
diff changeset
360 if (dump_enabled_p ())
kono
parents:
diff changeset
361 {
kono
parents:
diff changeset
362 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
363 GRID_MISSED_MSG_PREFIX "the inner loop "
kono
parents:
diff changeset
364 "has a non-automatic schedule clause\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
365 dump_printf_loc (MSG_NOTE, gfor,
111
kono
parents:
diff changeset
366 "Loop construct has a non automatic "
kono
parents:
diff changeset
367 "schedule clause\n");
kono
parents:
diff changeset
368 }
kono
parents:
diff changeset
369 return false;
kono
parents:
diff changeset
370 }
kono
parents:
diff changeset
371 break;
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 case OMP_CLAUSE_REDUCTION:
kono
parents:
diff changeset
374 if (dump_enabled_p ())
kono
parents:
diff changeset
375 {
kono
parents:
diff changeset
376 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
377 GRID_MISSED_MSG_PREFIX "a reduction "
kono
parents:
diff changeset
378 "clause is present\n ");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
379 dump_printf_loc (MSG_NOTE, gfor,
111
kono
parents:
diff changeset
380 "Loop construct has a reduction schedule "
kono
parents:
diff changeset
381 "clause\n");
kono
parents:
diff changeset
382 }
kono
parents:
diff changeset
383 return false;
kono
parents:
diff changeset
384
kono
parents:
diff changeset
385 default:
kono
parents:
diff changeset
386 break;
kono
parents:
diff changeset
387 }
kono
parents:
diff changeset
388 clauses = OMP_CLAUSE_CHAIN (clauses);
kono
parents:
diff changeset
389 }
kono
parents:
diff changeset
390 struct walk_stmt_info wi;
kono
parents:
diff changeset
391 memset (&wi, 0, sizeof (wi));
kono
parents:
diff changeset
392 if (walk_gimple_seq (gimple_omp_body (gfor),
kono
parents:
diff changeset
393 grid_find_ungridifiable_statement,
kono
parents:
diff changeset
394 NULL, &wi))
kono
parents:
diff changeset
395 {
kono
parents:
diff changeset
396 gimple *bad = (gimple *) wi.info;
kono
parents:
diff changeset
397 if (dump_enabled_p ())
kono
parents:
diff changeset
398 {
kono
parents:
diff changeset
399 if (is_gimple_call (bad))
kono
parents:
diff changeset
400 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
401 GRID_MISSED_MSG_PREFIX "the inner loop contains "
kono
parents:
diff changeset
402 "call to a noreturn function\n");
kono
parents:
diff changeset
403 else
kono
parents:
diff changeset
404 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
405 GRID_MISSED_MSG_PREFIX "the inner loop contains "
kono
parents:
diff changeset
406 "statement %s which cannot be transformed\n",
kono
parents:
diff changeset
407 gimple_code_name[(int) gimple_code (bad)]);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
408 dump_printf_loc (MSG_NOTE, bad,
111
kono
parents:
diff changeset
409 "This statement cannot be analyzed for "
kono
parents:
diff changeset
410 "gridification\n");
kono
parents:
diff changeset
411 }
kono
parents:
diff changeset
412 return false;
kono
parents:
diff changeset
413 }
kono
parents:
diff changeset
414 return true;
kono
parents:
diff changeset
415 }
kono
parents:
diff changeset
416
kono
parents:
diff changeset
417 /* Given distribute omp construct represented by DIST, which in the original
kono
parents:
diff changeset
418 source forms a compound construct with a looping construct, return true if it
kono
parents:
diff changeset
419 can be turned into a gridified HSA kernel. Otherwise return false. GRID
kono
parents:
diff changeset
420 describes hitherto discovered properties of the loop that is evaluated for
kono
parents:
diff changeset
421 possible gridification. */
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 static bool
kono
parents:
diff changeset
424 grid_dist_follows_simple_pattern (gomp_for *dist, grid_prop *grid)
kono
parents:
diff changeset
425 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
426 dump_user_location_t tloc = grid->target_loc;
111
kono
parents:
diff changeset
427 gimple *stmt = grid_find_single_omp_among_assignments (gimple_omp_body (dist),
kono
parents:
diff changeset
428 grid, "distribute");
kono
parents:
diff changeset
429 gomp_parallel *par;
kono
parents:
diff changeset
430 if (!stmt
kono
parents:
diff changeset
431 || !(par = dyn_cast <gomp_parallel *> (stmt))
kono
parents:
diff changeset
432 || !grid_parallel_clauses_gridifiable (par, tloc))
kono
parents:
diff changeset
433 return false;
kono
parents:
diff changeset
434
kono
parents:
diff changeset
435 stmt = grid_find_single_omp_among_assignments (gimple_omp_body (par), grid,
kono
parents:
diff changeset
436 "parallel");
kono
parents:
diff changeset
437 gomp_for *gfor;
kono
parents:
diff changeset
438 if (!stmt || !(gfor = dyn_cast <gomp_for *> (stmt)))
kono
parents:
diff changeset
439 return false;
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 if (gimple_omp_for_kind (gfor) != GF_OMP_FOR_KIND_FOR)
kono
parents:
diff changeset
442 {
kono
parents:
diff changeset
443 if (dump_enabled_p ())
kono
parents:
diff changeset
444 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
445 GRID_MISSED_MSG_PREFIX "the inner loop is not "
kono
parents:
diff changeset
446 "a simple for loop\n");
kono
parents:
diff changeset
447 return false;
kono
parents:
diff changeset
448 }
kono
parents:
diff changeset
449 gcc_assert (gimple_omp_for_collapse (gfor) == grid->collapse);
kono
parents:
diff changeset
450
kono
parents:
diff changeset
451 if (!grid_inner_loop_gridifiable_p (gfor, grid))
kono
parents:
diff changeset
452 return false;
kono
parents:
diff changeset
453
kono
parents:
diff changeset
454 return true;
kono
parents:
diff changeset
455 }
kono
parents:
diff changeset
456
kono
parents:
diff changeset
457 /* Given an omp loop statement GFOR, return true if it can participate in
kono
parents:
diff changeset
458 tiling gridification, i.e. in one where the distribute and parallel for
kono
parents:
diff changeset
459 loops do not form a compound statement. GRID describes hitherto discovered
kono
parents:
diff changeset
460 properties of the loop that is evaluated for possible gridification. */
kono
parents:
diff changeset
461
kono
parents:
diff changeset
462 static bool
kono
parents:
diff changeset
463 grid_gfor_follows_tiling_pattern (gomp_for *gfor, grid_prop *grid)
kono
parents:
diff changeset
464 {
kono
parents:
diff changeset
465 if (gimple_omp_for_kind (gfor) != GF_OMP_FOR_KIND_FOR)
kono
parents:
diff changeset
466 {
kono
parents:
diff changeset
467 if (dump_enabled_p ())
kono
parents:
diff changeset
468 {
kono
parents:
diff changeset
469 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
470 GRID_MISSED_MSG_PREFIX "an inner loop is not "
kono
parents:
diff changeset
471 "a simple for loop\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
472 dump_printf_loc (MSG_NOTE, gfor,
111
kono
parents:
diff changeset
473 "This statement is not a simple for loop\n");
kono
parents:
diff changeset
474 }
kono
parents:
diff changeset
475 return false;
kono
parents:
diff changeset
476 }
kono
parents:
diff changeset
477
kono
parents:
diff changeset
478 if (!grid_inner_loop_gridifiable_p (gfor, grid))
kono
parents:
diff changeset
479 return false;
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 if (gimple_omp_for_collapse (gfor) != grid->collapse)
kono
parents:
diff changeset
482 {
kono
parents:
diff changeset
483 if (dump_enabled_p ())
kono
parents:
diff changeset
484 {
kono
parents:
diff changeset
485 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
486 GRID_MISSED_MSG_PREFIX "an inner loop does not "
kono
parents:
diff changeset
487 "have use the same collapse clause\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
488 dump_printf_loc (MSG_NOTE, gfor,
111
kono
parents:
diff changeset
489 "Loop construct uses a different collapse clause\n");
kono
parents:
diff changeset
490 }
kono
parents:
diff changeset
491 return false;
kono
parents:
diff changeset
492 }
kono
parents:
diff changeset
493
kono
parents:
diff changeset
494 struct omp_for_data fd;
kono
parents:
diff changeset
495 struct omp_for_data_loop *loops
kono
parents:
diff changeset
496 = (struct omp_for_data_loop *)alloca (grid->collapse
kono
parents:
diff changeset
497 * sizeof (struct omp_for_data_loop));
kono
parents:
diff changeset
498 omp_extract_for_data (gfor, &fd, loops);
kono
parents:
diff changeset
499 for (unsigned i = 0; i < grid->collapse; i++)
kono
parents:
diff changeset
500 {
kono
parents:
diff changeset
501 tree itype, type = TREE_TYPE (fd.loops[i].v);
kono
parents:
diff changeset
502 if (POINTER_TYPE_P (type))
kono
parents:
diff changeset
503 itype = signed_type_for (type);
kono
parents:
diff changeset
504 else
kono
parents:
diff changeset
505 itype = type;
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 tree n1 = fold_convert (itype, fd.loops[i].n1);
kono
parents:
diff changeset
508 tree n2 = fold_convert (itype, fd.loops[i].n2);
kono
parents:
diff changeset
509 tree t = build_int_cst (itype,
kono
parents:
diff changeset
510 (fd.loops[i].cond_code == LT_EXPR ? -1 : 1));
kono
parents:
diff changeset
511 t = fold_build2 (PLUS_EXPR, itype, fd.loops[i].step, t);
kono
parents:
diff changeset
512 t = fold_build2 (PLUS_EXPR, itype, t, n2);
kono
parents:
diff changeset
513 t = fold_build2 (MINUS_EXPR, itype, t, n1);
kono
parents:
diff changeset
514 if (TYPE_UNSIGNED (itype) && fd.loops[i].cond_code == GT_EXPR)
kono
parents:
diff changeset
515 t = fold_build2 (TRUNC_DIV_EXPR, itype,
kono
parents:
diff changeset
516 fold_build1 (NEGATE_EXPR, itype, t),
kono
parents:
diff changeset
517 fold_build1 (NEGATE_EXPR, itype, fd.loops[i].step));
kono
parents:
diff changeset
518 else
kono
parents:
diff changeset
519 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, fd.loops[i].step);
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 if (!operand_equal_p (grid->group_sizes[i], t, 0))
kono
parents:
diff changeset
522 {
kono
parents:
diff changeset
523 if (dump_enabled_p ())
kono
parents:
diff changeset
524 {
kono
parents:
diff changeset
525 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
526 GRID_MISSED_MSG_PREFIX "the distribute and "
kono
parents:
diff changeset
527 "an internal loop do not agree on tile size\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
528 dump_printf_loc (MSG_NOTE, gfor,
111
kono
parents:
diff changeset
529 "Loop construct does not seem to loop over "
kono
parents:
diff changeset
530 "a tile size\n");
kono
parents:
diff changeset
531 }
kono
parents:
diff changeset
532 return false;
kono
parents:
diff changeset
533 }
kono
parents:
diff changeset
534 }
kono
parents:
diff changeset
535 return true;
kono
parents:
diff changeset
536 }
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 /* Facing a call to FNDECL in the body of a distribute construct, return true
kono
parents:
diff changeset
539 if we can handle it or false if it precludes gridification. */
kono
parents:
diff changeset
540
kono
parents:
diff changeset
541 static bool
kono
parents:
diff changeset
542 grid_call_permissible_in_distribute_p (tree fndecl)
kono
parents:
diff changeset
543 {
kono
parents:
diff changeset
544 if (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl))
kono
parents:
diff changeset
545 return true;
kono
parents:
diff changeset
546
kono
parents:
diff changeset
547 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
kono
parents:
diff changeset
548 if (strstr (name, "omp_") != name)
kono
parents:
diff changeset
549 return false;
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 if ((strcmp (name, "omp_get_thread_num") == 0)
kono
parents:
diff changeset
552 || (strcmp (name, "omp_get_num_threads") == 0)
kono
parents:
diff changeset
553 || (strcmp (name, "omp_get_num_teams") == 0)
kono
parents:
diff changeset
554 || (strcmp (name, "omp_get_team_num") == 0)
kono
parents:
diff changeset
555 || (strcmp (name, "omp_get_level") == 0)
kono
parents:
diff changeset
556 || (strcmp (name, "omp_get_active_level") == 0)
kono
parents:
diff changeset
557 || (strcmp (name, "omp_in_parallel") == 0))
kono
parents:
diff changeset
558 return true;
kono
parents:
diff changeset
559
kono
parents:
diff changeset
560 return false;
kono
parents:
diff changeset
561 }
kono
parents:
diff changeset
562
kono
parents:
diff changeset
563 /* Facing a call satisfying grid_call_permissible_in_distribute_p in the body
kono
parents:
diff changeset
564 of a distribute construct that is pointed at by GSI, modify it as necessary
kono
parents:
diff changeset
565 for gridification. If the statement itself got removed, return true. */
kono
parents:
diff changeset
566
kono
parents:
diff changeset
567 static bool
kono
parents:
diff changeset
568 grid_handle_call_in_distribute (gimple_stmt_iterator *gsi)
kono
parents:
diff changeset
569 {
kono
parents:
diff changeset
570 gimple *stmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
571 tree fndecl = gimple_call_fndecl (stmt);
kono
parents:
diff changeset
572 gcc_checking_assert (stmt);
kono
parents:
diff changeset
573 if (DECL_PURE_P (fndecl) || TREE_READONLY (fndecl))
kono
parents:
diff changeset
574 return false;
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 const char *name = IDENTIFIER_POINTER (DECL_NAME (fndecl));
kono
parents:
diff changeset
577 if ((strcmp (name, "omp_get_thread_num") == 0)
kono
parents:
diff changeset
578 || (strcmp (name, "omp_get_level") == 0)
kono
parents:
diff changeset
579 || (strcmp (name, "omp_get_active_level") == 0)
kono
parents:
diff changeset
580 || (strcmp (name, "omp_in_parallel") == 0))
kono
parents:
diff changeset
581 {
kono
parents:
diff changeset
582 tree lhs = gimple_call_lhs (stmt);
kono
parents:
diff changeset
583 if (lhs)
kono
parents:
diff changeset
584 {
kono
parents:
diff changeset
585 gassign *assign
kono
parents:
diff changeset
586 = gimple_build_assign (lhs, build_zero_cst (TREE_TYPE (lhs)));
kono
parents:
diff changeset
587 gsi_insert_before (gsi, assign, GSI_SAME_STMT);
kono
parents:
diff changeset
588 }
kono
parents:
diff changeset
589 gsi_remove (gsi, true);
kono
parents:
diff changeset
590 return true;
kono
parents:
diff changeset
591 }
kono
parents:
diff changeset
592
kono
parents:
diff changeset
593 /* The rest of the omp functions can stay as they are, HSA back-end will
kono
parents:
diff changeset
594 handle them correctly. */
kono
parents:
diff changeset
595 gcc_checking_assert ((strcmp (name, "omp_get_num_threads") == 0)
kono
parents:
diff changeset
596 || (strcmp (name, "omp_get_num_teams") == 0)
kono
parents:
diff changeset
597 || (strcmp (name, "omp_get_team_num") == 0));
kono
parents:
diff changeset
598 return false;
kono
parents:
diff changeset
599 }
kono
parents:
diff changeset
600
kono
parents:
diff changeset
601 /* Given a sequence of statements within a distribute omp construct or a
kono
parents:
diff changeset
602 parallel construct, which in the original source does not form a compound
kono
parents:
diff changeset
603 construct with a looping construct, return true if it does not prevent us
kono
parents:
diff changeset
604 from turning it into a gridified HSA kernel. Otherwise return false. GRID
kono
parents:
diff changeset
605 describes hitherto discovered properties of the loop that is evaluated for
kono
parents:
diff changeset
606 possible gridification. IN_PARALLEL must be true if seq is within a
kono
parents:
diff changeset
607 parallel construct and flase if it is only within a distribute
kono
parents:
diff changeset
608 construct. */
kono
parents:
diff changeset
609
kono
parents:
diff changeset
610 static bool
kono
parents:
diff changeset
611 grid_dist_follows_tiling_pattern (gimple_seq seq, grid_prop *grid,
kono
parents:
diff changeset
612 bool in_parallel)
kono
parents:
diff changeset
613 {
kono
parents:
diff changeset
614 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
615 for (gsi = gsi_start (seq); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
616 {
kono
parents:
diff changeset
617 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
618
kono
parents:
diff changeset
619 if (grid_safe_assignment_p (stmt, grid)
kono
parents:
diff changeset
620 || gimple_code (stmt) == GIMPLE_GOTO
kono
parents:
diff changeset
621 || gimple_code (stmt) == GIMPLE_LABEL
kono
parents:
diff changeset
622 || gimple_code (stmt) == GIMPLE_COND)
kono
parents:
diff changeset
623 continue;
kono
parents:
diff changeset
624 else if (gbind *bind = dyn_cast <gbind *> (stmt))
kono
parents:
diff changeset
625 {
kono
parents:
diff changeset
626 if (!grid_dist_follows_tiling_pattern (gimple_bind_body (bind),
kono
parents:
diff changeset
627 grid, in_parallel))
kono
parents:
diff changeset
628 return false;
kono
parents:
diff changeset
629 continue;
kono
parents:
diff changeset
630 }
kono
parents:
diff changeset
631 else if (gtry *try_stmt = dyn_cast <gtry *> (stmt))
kono
parents:
diff changeset
632 {
kono
parents:
diff changeset
633 if (gimple_try_kind (try_stmt) == GIMPLE_TRY_CATCH)
kono
parents:
diff changeset
634 {
kono
parents:
diff changeset
635 if (dump_enabled_p ())
kono
parents:
diff changeset
636 {
kono
parents:
diff changeset
637 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
638 GRID_MISSED_MSG_PREFIX "the distribute "
kono
parents:
diff changeset
639 "construct contains a try..catch region\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
640 dump_printf_loc (MSG_NOTE, try_stmt,
111
kono
parents:
diff changeset
641 "This statement cannot be analyzed for "
kono
parents:
diff changeset
642 "tiled gridification\n");
kono
parents:
diff changeset
643 }
kono
parents:
diff changeset
644 return false;
kono
parents:
diff changeset
645 }
kono
parents:
diff changeset
646 if (!grid_dist_follows_tiling_pattern (gimple_try_eval (try_stmt),
kono
parents:
diff changeset
647 grid, in_parallel))
kono
parents:
diff changeset
648 return false;
kono
parents:
diff changeset
649 if (!grid_dist_follows_tiling_pattern (gimple_try_cleanup (try_stmt),
kono
parents:
diff changeset
650 grid, in_parallel))
kono
parents:
diff changeset
651 return false;
kono
parents:
diff changeset
652 continue;
kono
parents:
diff changeset
653 }
kono
parents:
diff changeset
654 else if (is_gimple_call (stmt))
kono
parents:
diff changeset
655 {
kono
parents:
diff changeset
656 tree fndecl = gimple_call_fndecl (stmt);
kono
parents:
diff changeset
657 if (fndecl && grid_call_permissible_in_distribute_p (fndecl))
kono
parents:
diff changeset
658 continue;
kono
parents:
diff changeset
659
kono
parents:
diff changeset
660 if (dump_enabled_p ())
kono
parents:
diff changeset
661 {
kono
parents:
diff changeset
662 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
663 GRID_MISSED_MSG_PREFIX "the distribute "
kono
parents:
diff changeset
664 "construct contains a call\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
665 dump_printf_loc (MSG_NOTE, stmt,
111
kono
parents:
diff changeset
666 "This statement cannot be analyzed for "
kono
parents:
diff changeset
667 "tiled gridification\n");
kono
parents:
diff changeset
668 }
kono
parents:
diff changeset
669 return false;
kono
parents:
diff changeset
670 }
kono
parents:
diff changeset
671 else if (gomp_parallel *par = dyn_cast <gomp_parallel *> (stmt))
kono
parents:
diff changeset
672 {
kono
parents:
diff changeset
673 if (in_parallel)
kono
parents:
diff changeset
674 {
kono
parents:
diff changeset
675 if (dump_enabled_p ())
kono
parents:
diff changeset
676 {
kono
parents:
diff changeset
677 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
678 GRID_MISSED_MSG_PREFIX "a parallel "
kono
parents:
diff changeset
679 "construct contains another parallel "
kono
parents:
diff changeset
680 "construct\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
681 dump_printf_loc (MSG_NOTE, stmt,
111
kono
parents:
diff changeset
682 "This parallel construct is nested in "
kono
parents:
diff changeset
683 "another one\n");
kono
parents:
diff changeset
684 }
kono
parents:
diff changeset
685 return false;
kono
parents:
diff changeset
686 }
kono
parents:
diff changeset
687 if (!grid_parallel_clauses_gridifiable (par, grid->target_loc)
kono
parents:
diff changeset
688 || !grid_dist_follows_tiling_pattern (gimple_omp_body (par),
kono
parents:
diff changeset
689 grid, true))
kono
parents:
diff changeset
690 return false;
kono
parents:
diff changeset
691 }
kono
parents:
diff changeset
692 else if (gomp_for *gfor = dyn_cast <gomp_for *> (stmt))
kono
parents:
diff changeset
693 {
kono
parents:
diff changeset
694 if (!in_parallel)
kono
parents:
diff changeset
695 {
kono
parents:
diff changeset
696 if (dump_enabled_p ())
kono
parents:
diff changeset
697 {
kono
parents:
diff changeset
698 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
699 GRID_MISSED_MSG_PREFIX "a loop "
kono
parents:
diff changeset
700 "construct is not nested within a parallel "
kono
parents:
diff changeset
701 "construct\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
702 dump_printf_loc (MSG_NOTE, stmt,
111
kono
parents:
diff changeset
703 "This loop construct is not nested in "
kono
parents:
diff changeset
704 "a parallel construct\n");
kono
parents:
diff changeset
705 }
kono
parents:
diff changeset
706 return false;
kono
parents:
diff changeset
707 }
kono
parents:
diff changeset
708 if (!grid_gfor_follows_tiling_pattern (gfor, grid))
kono
parents:
diff changeset
709 return false;
kono
parents:
diff changeset
710 }
kono
parents:
diff changeset
711 else
kono
parents:
diff changeset
712 {
kono
parents:
diff changeset
713 if (dump_enabled_p ())
kono
parents:
diff changeset
714 {
kono
parents:
diff changeset
715 dump_printf_loc (MSG_MISSED_OPTIMIZATION, grid->target_loc,
kono
parents:
diff changeset
716 GRID_MISSED_MSG_PREFIX "the distribute "
kono
parents:
diff changeset
717 "construct contains a complex statement\n");
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
718 dump_printf_loc (MSG_NOTE, stmt,
111
kono
parents:
diff changeset
719 "This statement cannot be analyzed for "
kono
parents:
diff changeset
720 "tiled gridification\n");
kono
parents:
diff changeset
721 }
kono
parents:
diff changeset
722 return false;
kono
parents:
diff changeset
723 }
kono
parents:
diff changeset
724 }
kono
parents:
diff changeset
725 return true;
kono
parents:
diff changeset
726 }
kono
parents:
diff changeset
727
kono
parents:
diff changeset
728 /* If TARGET follows a pattern that can be turned into a gridified HSA kernel,
kono
parents:
diff changeset
729 return true, otherwise return false. In the case of success, also fill in
kono
parents:
diff changeset
730 GRID with information describing the kernel grid. */
kono
parents:
diff changeset
731
kono
parents:
diff changeset
732 static bool
kono
parents:
diff changeset
733 grid_target_follows_gridifiable_pattern (gomp_target *target, grid_prop *grid)
kono
parents:
diff changeset
734 {
kono
parents:
diff changeset
735 if (gimple_omp_target_kind (target) != GF_OMP_TARGET_KIND_REGION)
kono
parents:
diff changeset
736 return false;
kono
parents:
diff changeset
737
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
738 dump_user_location_t tloc = target;
111
kono
parents:
diff changeset
739 grid->target_loc = tloc;
kono
parents:
diff changeset
740 gimple *stmt
kono
parents:
diff changeset
741 = grid_find_single_omp_among_assignments (gimple_omp_body (target),
kono
parents:
diff changeset
742 grid, "target");
kono
parents:
diff changeset
743 if (!stmt)
kono
parents:
diff changeset
744 return false;
kono
parents:
diff changeset
745 gomp_teams *teams = dyn_cast <gomp_teams *> (stmt);
kono
parents:
diff changeset
746 tree group_size = NULL;
kono
parents:
diff changeset
747 if (!teams)
kono
parents:
diff changeset
748 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
749 if (dump_enabled_p ())
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
750 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
751 GRID_MISSED_MSG_PREFIX "it does not have a sole "
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
752 "teams construct in it.\n");
111
kono
parents:
diff changeset
753 return false;
kono
parents:
diff changeset
754 }
kono
parents:
diff changeset
755
kono
parents:
diff changeset
756 tree clauses = gimple_omp_teams_clauses (teams);
kono
parents:
diff changeset
757 while (clauses)
kono
parents:
diff changeset
758 {
kono
parents:
diff changeset
759 switch (OMP_CLAUSE_CODE (clauses))
kono
parents:
diff changeset
760 {
kono
parents:
diff changeset
761 case OMP_CLAUSE_NUM_TEAMS:
kono
parents:
diff changeset
762 if (dump_enabled_p ())
kono
parents:
diff changeset
763 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
764 GRID_MISSED_MSG_PREFIX "the teams construct "
kono
parents:
diff changeset
765 "contains a num_teams clause\n ");
kono
parents:
diff changeset
766 return false;
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 case OMP_CLAUSE_REDUCTION:
kono
parents:
diff changeset
769 if (dump_enabled_p ())
kono
parents:
diff changeset
770 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
771 GRID_MISSED_MSG_PREFIX "a reduction "
kono
parents:
diff changeset
772 "clause is present\n ");
kono
parents:
diff changeset
773 return false;
kono
parents:
diff changeset
774
kono
parents:
diff changeset
775 case OMP_CLAUSE_THREAD_LIMIT:
kono
parents:
diff changeset
776 if (!integer_zerop (OMP_CLAUSE_OPERAND (clauses, 0)))
kono
parents:
diff changeset
777 group_size = OMP_CLAUSE_OPERAND (clauses, 0);
kono
parents:
diff changeset
778 break;
kono
parents:
diff changeset
779
kono
parents:
diff changeset
780 default:
kono
parents:
diff changeset
781 break;
kono
parents:
diff changeset
782 }
kono
parents:
diff changeset
783 clauses = OMP_CLAUSE_CHAIN (clauses);
kono
parents:
diff changeset
784 }
kono
parents:
diff changeset
785
kono
parents:
diff changeset
786 stmt = grid_find_single_omp_among_assignments (gimple_omp_body (teams), grid,
kono
parents:
diff changeset
787 "teams");
kono
parents:
diff changeset
788 if (!stmt)
kono
parents:
diff changeset
789 return false;
kono
parents:
diff changeset
790 gomp_for *dist = dyn_cast <gomp_for *> (stmt);
kono
parents:
diff changeset
791 if (!dist)
kono
parents:
diff changeset
792 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
793 if (dump_enabled_p ())
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
794 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
795 GRID_MISSED_MSG_PREFIX "the teams construct does not "
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
796 "have a single distribute construct in it.\n");
111
kono
parents:
diff changeset
797 return false;
kono
parents:
diff changeset
798 }
kono
parents:
diff changeset
799
kono
parents:
diff changeset
800 gcc_assert (gimple_omp_for_kind (dist) == GF_OMP_FOR_KIND_DISTRIBUTE);
kono
parents:
diff changeset
801
kono
parents:
diff changeset
802 grid->collapse = gimple_omp_for_collapse (dist);
kono
parents:
diff changeset
803 if (grid->collapse > 3)
kono
parents:
diff changeset
804 {
kono
parents:
diff changeset
805 if (dump_enabled_p ())
kono
parents:
diff changeset
806 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
807 GRID_MISSED_MSG_PREFIX "the distribute construct "
kono
parents:
diff changeset
808 "contains collapse clause with parameter greater "
kono
parents:
diff changeset
809 "than 3\n");
kono
parents:
diff changeset
810 return false;
kono
parents:
diff changeset
811 }
kono
parents:
diff changeset
812
kono
parents:
diff changeset
813 struct omp_for_data fd;
kono
parents:
diff changeset
814 struct omp_for_data_loop *dist_loops
kono
parents:
diff changeset
815 = (struct omp_for_data_loop *)alloca (grid->collapse
kono
parents:
diff changeset
816 * sizeof (struct omp_for_data_loop));
kono
parents:
diff changeset
817 omp_extract_for_data (dist, &fd, dist_loops);
kono
parents:
diff changeset
818 if (fd.chunk_size)
kono
parents:
diff changeset
819 {
kono
parents:
diff changeset
820 if (group_size && !operand_equal_p (group_size, fd.chunk_size, 0))
kono
parents:
diff changeset
821 {
kono
parents:
diff changeset
822 if (dump_enabled_p ())
kono
parents:
diff changeset
823 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
824 GRID_MISSED_MSG_PREFIX "the teams "
kono
parents:
diff changeset
825 "thread limit is different from distribute "
kono
parents:
diff changeset
826 "schedule chunk\n");
kono
parents:
diff changeset
827 return false;
kono
parents:
diff changeset
828 }
kono
parents:
diff changeset
829 group_size = fd.chunk_size;
kono
parents:
diff changeset
830 }
kono
parents:
diff changeset
831 if (group_size && grid->collapse > 1)
kono
parents:
diff changeset
832 {
kono
parents:
diff changeset
833 if (dump_enabled_p ())
kono
parents:
diff changeset
834 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
835 GRID_MISSED_MSG_PREFIX "group size cannot be "
kono
parents:
diff changeset
836 "set using thread_limit or schedule clauses "
kono
parents:
diff changeset
837 "when also using a collapse clause greater than 1\n");
kono
parents:
diff changeset
838 return false;
kono
parents:
diff changeset
839 }
kono
parents:
diff changeset
840
kono
parents:
diff changeset
841 if (gimple_omp_for_combined_p (dist))
kono
parents:
diff changeset
842 {
kono
parents:
diff changeset
843 grid->tiling = false;
kono
parents:
diff changeset
844 grid->group_sizes[0] = group_size;
kono
parents:
diff changeset
845 for (unsigned i = 1; i < grid->collapse; i++)
kono
parents:
diff changeset
846 grid->group_sizes[i] = NULL;
kono
parents:
diff changeset
847 return grid_dist_follows_simple_pattern (dist, grid);
kono
parents:
diff changeset
848 }
kono
parents:
diff changeset
849 else
kono
parents:
diff changeset
850 {
kono
parents:
diff changeset
851 grid->tiling = true;
kono
parents:
diff changeset
852 if (group_size)
kono
parents:
diff changeset
853 {
kono
parents:
diff changeset
854 if (dump_enabled_p ())
kono
parents:
diff changeset
855 dump_printf_loc (MSG_MISSED_OPTIMIZATION, tloc,
kono
parents:
diff changeset
856 GRID_MISSED_MSG_PREFIX "group size cannot be set "
kono
parents:
diff changeset
857 "using thread_limit or schedule clauses when "
kono
parents:
diff changeset
858 "distribute and loop constructs do not form "
kono
parents:
diff changeset
859 "one combined construct\n");
kono
parents:
diff changeset
860 return false;
kono
parents:
diff changeset
861 }
kono
parents:
diff changeset
862 for (unsigned i = 0; i < grid->collapse; i++)
kono
parents:
diff changeset
863 {
kono
parents:
diff changeset
864 if (fd.loops[i].cond_code == GT_EXPR)
kono
parents:
diff changeset
865 grid->group_sizes[i] = fold_build1 (NEGATE_EXPR,
kono
parents:
diff changeset
866 TREE_TYPE (fd.loops[i].step),
kono
parents:
diff changeset
867 fd.loops[i].step);
kono
parents:
diff changeset
868 else
kono
parents:
diff changeset
869 grid->group_sizes[i] = fd.loops[i].step;
kono
parents:
diff changeset
870 }
kono
parents:
diff changeset
871 return grid_dist_follows_tiling_pattern (gimple_omp_body (dist), grid,
kono
parents:
diff changeset
872 false);
kono
parents:
diff changeset
873 }
kono
parents:
diff changeset
874 }
kono
parents:
diff changeset
875
kono
parents:
diff changeset
876 /* Operand walker, used to remap pre-body declarations according to a hash map
kono
parents:
diff changeset
877 provided in DATA. */
kono
parents:
diff changeset
878
kono
parents:
diff changeset
879 static tree
kono
parents:
diff changeset
880 grid_remap_prebody_decls (tree *tp, int *walk_subtrees, void *data)
kono
parents:
diff changeset
881 {
kono
parents:
diff changeset
882 tree t = *tp;
kono
parents:
diff changeset
883
kono
parents:
diff changeset
884 if (DECL_P (t) || TYPE_P (t))
kono
parents:
diff changeset
885 *walk_subtrees = 0;
kono
parents:
diff changeset
886 else
kono
parents:
diff changeset
887 *walk_subtrees = 1;
kono
parents:
diff changeset
888
kono
parents:
diff changeset
889 if (VAR_P (t))
kono
parents:
diff changeset
890 {
kono
parents:
diff changeset
891 struct walk_stmt_info *wi = (struct walk_stmt_info *) data;
kono
parents:
diff changeset
892 hash_map<tree, tree> *declmap = (hash_map<tree, tree> *) wi->info;
kono
parents:
diff changeset
893 tree *repl = declmap->get (t);
kono
parents:
diff changeset
894 if (repl)
kono
parents:
diff changeset
895 *tp = *repl;
kono
parents:
diff changeset
896 }
kono
parents:
diff changeset
897 return NULL_TREE;
kono
parents:
diff changeset
898 }
kono
parents:
diff changeset
899
kono
parents:
diff changeset
900 /* Identifiers of segments into which a particular variable should be places
kono
parents:
diff changeset
901 when gridifying. */
kono
parents:
diff changeset
902
kono
parents:
diff changeset
903 enum grid_var_segment {GRID_SEGMENT_PRIVATE, GRID_SEGMENT_GROUP,
kono
parents:
diff changeset
904 GRID_SEGMENT_GLOBAL};
kono
parents:
diff changeset
905
kono
parents:
diff changeset
906 /* Mark VAR so that it is eventually placed into SEGMENT. Place an artificial
kono
parents:
diff changeset
907 builtin call into SEQ that will make sure the variable is always considered
kono
parents:
diff changeset
908 address taken. */
kono
parents:
diff changeset
909
kono
parents:
diff changeset
910 static void
kono
parents:
diff changeset
911 grid_mark_variable_segment (tree var, enum grid_var_segment segment)
kono
parents:
diff changeset
912 {
kono
parents:
diff changeset
913 /* Making a non-addressable variables would require that we re-gimplify all
kono
parents:
diff changeset
914 their uses. Fortunately, we do not have to do this because if they are
kono
parents:
diff changeset
915 not addressable, it means they are not used in atomic or parallel
kono
parents:
diff changeset
916 statements and so relaxed GPU consistency rules mean we can just keep them
kono
parents:
diff changeset
917 private. */
kono
parents:
diff changeset
918 if (!TREE_ADDRESSABLE (var))
kono
parents:
diff changeset
919 return;
kono
parents:
diff changeset
920
kono
parents:
diff changeset
921 switch (segment)
kono
parents:
diff changeset
922 {
kono
parents:
diff changeset
923 case GRID_SEGMENT_GROUP:
kono
parents:
diff changeset
924 DECL_ATTRIBUTES (var) = tree_cons (get_identifier ("hsa_group_segment"),
kono
parents:
diff changeset
925 NULL, DECL_ATTRIBUTES (var));
kono
parents:
diff changeset
926 break;
kono
parents:
diff changeset
927 case GRID_SEGMENT_GLOBAL:
kono
parents:
diff changeset
928 DECL_ATTRIBUTES (var) = tree_cons (get_identifier ("hsa_global_segment"),
kono
parents:
diff changeset
929 NULL, DECL_ATTRIBUTES (var));
kono
parents:
diff changeset
930 break;
kono
parents:
diff changeset
931 default:
kono
parents:
diff changeset
932 gcc_unreachable ();
kono
parents:
diff changeset
933 }
kono
parents:
diff changeset
934
kono
parents:
diff changeset
935 if (!TREE_STATIC (var))
kono
parents:
diff changeset
936 {
kono
parents:
diff changeset
937 TREE_STATIC (var) = 1;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
938 const char *prefix = IDENTIFIER_POINTER (DECL_NAME (var));
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
939 SET_DECL_ASSEMBLER_NAME (var, create_tmp_var_name (prefix));
111
kono
parents:
diff changeset
940 varpool_node::finalize_decl (var);
kono
parents:
diff changeset
941 }
kono
parents:
diff changeset
942
kono
parents:
diff changeset
943 }
kono
parents:
diff changeset
944
kono
parents:
diff changeset
945 /* Copy leading register-type assignments to local variables in SRC to just
kono
parents:
diff changeset
946 before DST, Creating temporaries, adjusting mapping of operands in WI and
kono
parents:
diff changeset
947 remapping operands as necessary. Add any new temporaries to TGT_BIND.
kono
parents:
diff changeset
948 Return the first statement that does not conform to grid_safe_assignment_p
kono
parents:
diff changeset
949 or NULL. If VAR_SEGMENT is not GRID_SEGMENT_PRIVATE, also mark all
kono
parents:
diff changeset
950 variables in traversed bind statements so that they are put into the
kono
parents:
diff changeset
951 appropriate segment. */
kono
parents:
diff changeset
952
kono
parents:
diff changeset
953 static gimple *
kono
parents:
diff changeset
954 grid_copy_leading_local_assignments (gimple_seq src, gimple_stmt_iterator *dst,
kono
parents:
diff changeset
955 gbind *tgt_bind,
kono
parents:
diff changeset
956 enum grid_var_segment var_segment,
kono
parents:
diff changeset
957 struct walk_stmt_info *wi)
kono
parents:
diff changeset
958 {
kono
parents:
diff changeset
959 hash_map<tree, tree> *declmap = (hash_map<tree, tree> *) wi->info;
kono
parents:
diff changeset
960 gimple_stmt_iterator gsi;
kono
parents:
diff changeset
961 for (gsi = gsi_start (src); !gsi_end_p (gsi); gsi_next (&gsi))
kono
parents:
diff changeset
962 {
kono
parents:
diff changeset
963 gimple *stmt = gsi_stmt (gsi);
kono
parents:
diff changeset
964 if (gbind *bind = dyn_cast <gbind *> (stmt))
kono
parents:
diff changeset
965 {
kono
parents:
diff changeset
966 gimple *r = grid_copy_leading_local_assignments
kono
parents:
diff changeset
967 (gimple_bind_body (bind), dst, tgt_bind, var_segment, wi);
kono
parents:
diff changeset
968
kono
parents:
diff changeset
969 if (var_segment != GRID_SEGMENT_PRIVATE)
kono
parents:
diff changeset
970 for (tree var = gimple_bind_vars (bind);
kono
parents:
diff changeset
971 var;
kono
parents:
diff changeset
972 var = DECL_CHAIN (var))
kono
parents:
diff changeset
973 grid_mark_variable_segment (var, var_segment);
kono
parents:
diff changeset
974 if (r)
kono
parents:
diff changeset
975 return r;
kono
parents:
diff changeset
976 else
kono
parents:
diff changeset
977 continue;
kono
parents:
diff changeset
978 }
kono
parents:
diff changeset
979 if (!grid_safe_assignment_p (stmt, NULL))
kono
parents:
diff changeset
980 return stmt;
kono
parents:
diff changeset
981 tree lhs = gimple_assign_lhs (as_a <gassign *> (stmt));
kono
parents:
diff changeset
982 tree repl = copy_var_decl (lhs, create_tmp_var_name (NULL),
kono
parents:
diff changeset
983 TREE_TYPE (lhs));
kono
parents:
diff changeset
984 DECL_CONTEXT (repl) = current_function_decl;
kono
parents:
diff changeset
985 gimple_bind_append_vars (tgt_bind, repl);
kono
parents:
diff changeset
986
kono
parents:
diff changeset
987 declmap->put (lhs, repl);
kono
parents:
diff changeset
988 gassign *copy = as_a <gassign *> (gimple_copy (stmt));
kono
parents:
diff changeset
989 walk_gimple_op (copy, grid_remap_prebody_decls, wi);
kono
parents:
diff changeset
990 gsi_insert_before (dst, copy, GSI_SAME_STMT);
kono
parents:
diff changeset
991 }
kono
parents:
diff changeset
992 return NULL;
kono
parents:
diff changeset
993 }
kono
parents:
diff changeset
994
kono
parents:
diff changeset
995 /* Statement walker function to make adjustments to statements within the
kono
parents:
diff changeset
996 gridifed kernel copy. */
kono
parents:
diff changeset
997
kono
parents:
diff changeset
998 static tree
kono
parents:
diff changeset
999 grid_process_grid_body (gimple_stmt_iterator *gsi, bool *handled_ops_p,
kono
parents:
diff changeset
1000 struct walk_stmt_info *)
kono
parents:
diff changeset
1001 {
kono
parents:
diff changeset
1002 *handled_ops_p = false;
kono
parents:
diff changeset
1003 gimple *stmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
1004 if (gimple_code (stmt) == GIMPLE_OMP_FOR
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1005 && gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD)
111
kono
parents:
diff changeset
1006 {
kono
parents:
diff changeset
1007 gomp_for *loop = as_a <gomp_for *> (stmt);
kono
parents:
diff changeset
1008 tree clauses = gimple_omp_for_clauses (loop);
kono
parents:
diff changeset
1009 tree cl = omp_find_clause (clauses, OMP_CLAUSE_SAFELEN);
kono
parents:
diff changeset
1010 if (cl)
kono
parents:
diff changeset
1011 OMP_CLAUSE_SAFELEN_EXPR (cl) = integer_one_node;
kono
parents:
diff changeset
1012 else
kono
parents:
diff changeset
1013 {
kono
parents:
diff changeset
1014 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_SAFELEN);
kono
parents:
diff changeset
1015 OMP_CLAUSE_SAFELEN_EXPR (c) = integer_one_node;
kono
parents:
diff changeset
1016 OMP_CLAUSE_CHAIN (c) = clauses;
kono
parents:
diff changeset
1017 gimple_omp_for_set_clauses (loop, c);
kono
parents:
diff changeset
1018 }
kono
parents:
diff changeset
1019 }
kono
parents:
diff changeset
1020 return NULL_TREE;
kono
parents:
diff changeset
1021 }
kono
parents:
diff changeset
1022
kono
parents:
diff changeset
1023 /* Given a PARLOOP that is a normal for looping construct but also a part of a
kono
parents:
diff changeset
1024 combined construct with a simd loop, eliminate the simd loop. */
kono
parents:
diff changeset
1025
kono
parents:
diff changeset
1026 static void
kono
parents:
diff changeset
1027 grid_eliminate_combined_simd_part (gomp_for *parloop)
kono
parents:
diff changeset
1028 {
kono
parents:
diff changeset
1029 struct walk_stmt_info wi;
kono
parents:
diff changeset
1030
kono
parents:
diff changeset
1031 memset (&wi, 0, sizeof (wi));
kono
parents:
diff changeset
1032 wi.val_only = true;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1033 enum gf_mask msk = GF_OMP_FOR_KIND_SIMD;
111
kono
parents:
diff changeset
1034 wi.info = (void *) &msk;
kono
parents:
diff changeset
1035 walk_gimple_seq (gimple_omp_body (parloop), omp_find_combined_for, NULL, &wi);
kono
parents:
diff changeset
1036 gimple *stmt = (gimple *) wi.info;
kono
parents:
diff changeset
1037 /* We expect that the SIMD id the only statement in the parallel loop. */
kono
parents:
diff changeset
1038 gcc_assert (stmt
kono
parents:
diff changeset
1039 && gimple_code (stmt) == GIMPLE_OMP_FOR
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1040 && (gimple_omp_for_kind (stmt) == GF_OMP_FOR_KIND_SIMD)
111
kono
parents:
diff changeset
1041 && gimple_omp_for_combined_into_p (stmt)
kono
parents:
diff changeset
1042 && !gimple_omp_for_combined_p (stmt));
kono
parents:
diff changeset
1043 gomp_for *simd = as_a <gomp_for *> (stmt);
kono
parents:
diff changeset
1044
kono
parents:
diff changeset
1045 /* Copy over the iteration properties because the body refers to the index in
kono
parents:
diff changeset
1046 the bottmom-most loop. */
kono
parents:
diff changeset
1047 unsigned i, collapse = gimple_omp_for_collapse (parloop);
kono
parents:
diff changeset
1048 gcc_checking_assert (collapse == gimple_omp_for_collapse (simd));
kono
parents:
diff changeset
1049 for (i = 0; i < collapse; i++)
kono
parents:
diff changeset
1050 {
kono
parents:
diff changeset
1051 gimple_omp_for_set_index (parloop, i, gimple_omp_for_index (simd, i));
kono
parents:
diff changeset
1052 gimple_omp_for_set_initial (parloop, i, gimple_omp_for_initial (simd, i));
kono
parents:
diff changeset
1053 gimple_omp_for_set_final (parloop, i, gimple_omp_for_final (simd, i));
kono
parents:
diff changeset
1054 gimple_omp_for_set_incr (parloop, i, gimple_omp_for_incr (simd, i));
kono
parents:
diff changeset
1055 }
kono
parents:
diff changeset
1056
kono
parents:
diff changeset
1057 tree *tgt= gimple_omp_for_clauses_ptr (parloop);
kono
parents:
diff changeset
1058 while (*tgt)
kono
parents:
diff changeset
1059 tgt = &OMP_CLAUSE_CHAIN (*tgt);
kono
parents:
diff changeset
1060
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1061 /* Copy over all clauses, except for linear clauses, which are turned into
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1062 private clauses, and all other simd-specific clauses, which are
111
kono
parents:
diff changeset
1063 ignored. */
kono
parents:
diff changeset
1064 tree *pc = gimple_omp_for_clauses_ptr (simd);
kono
parents:
diff changeset
1065 while (*pc)
kono
parents:
diff changeset
1066 {
kono
parents:
diff changeset
1067 tree c = *pc;
kono
parents:
diff changeset
1068 switch (TREE_CODE (c))
kono
parents:
diff changeset
1069 {
kono
parents:
diff changeset
1070 case OMP_CLAUSE_LINEAR:
kono
parents:
diff changeset
1071 {
kono
parents:
diff changeset
1072 tree priv = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE_PRIVATE);
kono
parents:
diff changeset
1073 OMP_CLAUSE_DECL (priv) = OMP_CLAUSE_DECL (c);
kono
parents:
diff changeset
1074 OMP_CLAUSE_CHAIN (priv) = NULL;
kono
parents:
diff changeset
1075 *tgt = priv;
kono
parents:
diff changeset
1076 tgt = &OMP_CLAUSE_CHAIN (priv);
kono
parents:
diff changeset
1077 pc = &OMP_CLAUSE_CHAIN (c);
kono
parents:
diff changeset
1078 break;
kono
parents:
diff changeset
1079 }
kono
parents:
diff changeset
1080
kono
parents:
diff changeset
1081 case OMP_CLAUSE_SAFELEN:
kono
parents:
diff changeset
1082 case OMP_CLAUSE_SIMDLEN:
kono
parents:
diff changeset
1083 case OMP_CLAUSE_ALIGNED:
kono
parents:
diff changeset
1084 pc = &OMP_CLAUSE_CHAIN (c);
kono
parents:
diff changeset
1085 break;
kono
parents:
diff changeset
1086
kono
parents:
diff changeset
1087 default:
kono
parents:
diff changeset
1088 *pc = OMP_CLAUSE_CHAIN (c);
kono
parents:
diff changeset
1089 OMP_CLAUSE_CHAIN (c) = NULL;
kono
parents:
diff changeset
1090 *tgt = c;
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1091 tgt = &OMP_CLAUSE_CHAIN (c);
111
kono
parents:
diff changeset
1092 break;
kono
parents:
diff changeset
1093 }
kono
parents:
diff changeset
1094 }
kono
parents:
diff changeset
1095
kono
parents:
diff changeset
1096 /* Finally, throw away the simd and mark the parallel loop as not
kono
parents:
diff changeset
1097 combined. */
kono
parents:
diff changeset
1098 gimple_omp_set_body (parloop, gimple_omp_body (simd));
kono
parents:
diff changeset
1099 gimple_omp_for_set_combined_p (parloop, false);
kono
parents:
diff changeset
1100 }
kono
parents:
diff changeset
1101
kono
parents:
diff changeset
1102 /* Statement walker function marking all parallels as grid_phony and loops as
kono
parents:
diff changeset
1103 grid ones representing threads of a particular thread group. */
kono
parents:
diff changeset
1104
kono
parents:
diff changeset
1105 static tree
kono
parents:
diff changeset
1106 grid_mark_tiling_loops (gimple_stmt_iterator *gsi, bool *handled_ops_p,
kono
parents:
diff changeset
1107 struct walk_stmt_info *wi_in)
kono
parents:
diff changeset
1108 {
kono
parents:
diff changeset
1109 *handled_ops_p = false;
kono
parents:
diff changeset
1110 if (gomp_for *loop = dyn_cast <gomp_for *> (gsi_stmt (*gsi)))
kono
parents:
diff changeset
1111 {
kono
parents:
diff changeset
1112 *handled_ops_p = true;
kono
parents:
diff changeset
1113 gimple_omp_for_set_kind (loop, GF_OMP_FOR_KIND_GRID_LOOP);
kono
parents:
diff changeset
1114 gimple_omp_for_set_grid_intra_group (loop, true);
kono
parents:
diff changeset
1115 if (gimple_omp_for_combined_p (loop))
kono
parents:
diff changeset
1116 grid_eliminate_combined_simd_part (loop);
kono
parents:
diff changeset
1117
kono
parents:
diff changeset
1118 struct walk_stmt_info body_wi;
kono
parents:
diff changeset
1119 memset (&body_wi, 0, sizeof (body_wi));
kono
parents:
diff changeset
1120 walk_gimple_seq_mod (gimple_omp_body_ptr (loop),
kono
parents:
diff changeset
1121 grid_process_grid_body, NULL, &body_wi);
kono
parents:
diff changeset
1122
kono
parents:
diff changeset
1123 gbind *bind = (gbind *) wi_in->info;
kono
parents:
diff changeset
1124 tree c;
kono
parents:
diff changeset
1125 for (c = gimple_omp_for_clauses (loop); c; c = OMP_CLAUSE_CHAIN (c))
kono
parents:
diff changeset
1126 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
kono
parents:
diff changeset
1127 {
kono
parents:
diff changeset
1128 push_gimplify_context ();
kono
parents:
diff changeset
1129 tree ov = OMP_CLAUSE_DECL (c);
kono
parents:
diff changeset
1130 tree gv = copy_var_decl (ov, create_tmp_var_name (NULL),
kono
parents:
diff changeset
1131 TREE_TYPE (ov));
kono
parents:
diff changeset
1132
kono
parents:
diff changeset
1133 grid_mark_variable_segment (gv, GRID_SEGMENT_GROUP);
kono
parents:
diff changeset
1134 DECL_CONTEXT (gv) = current_function_decl;
kono
parents:
diff changeset
1135 gimple_bind_append_vars (bind, gv);
kono
parents:
diff changeset
1136 tree x = lang_hooks.decls.omp_clause_assign_op (c, gv, ov);
kono
parents:
diff changeset
1137 gimplify_and_add (x, &OMP_CLAUSE_LASTPRIVATE_GIMPLE_SEQ (c));
kono
parents:
diff changeset
1138 x = lang_hooks.decls.omp_clause_copy_ctor (c, ov, gv);
kono
parents:
diff changeset
1139 gimple_seq l = NULL;
kono
parents:
diff changeset
1140 gimplify_and_add (x, &l);
kono
parents:
diff changeset
1141 gsi_insert_seq_after (gsi, l, GSI_SAME_STMT);
kono
parents:
diff changeset
1142 pop_gimplify_context (bind);
kono
parents:
diff changeset
1143 }
kono
parents:
diff changeset
1144 }
kono
parents:
diff changeset
1145 return NULL_TREE;
kono
parents:
diff changeset
1146 }
kono
parents:
diff changeset
1147
kono
parents:
diff changeset
1148 /* Statement walker function marking all parallels as grid_phony and loops as
kono
parents:
diff changeset
1149 grid ones representing threads of a particular thread group. */
kono
parents:
diff changeset
1150
kono
parents:
diff changeset
1151 static tree
kono
parents:
diff changeset
1152 grid_mark_tiling_parallels_and_loops (gimple_stmt_iterator *gsi,
kono
parents:
diff changeset
1153 bool *handled_ops_p,
kono
parents:
diff changeset
1154 struct walk_stmt_info *wi_in)
kono
parents:
diff changeset
1155 {
kono
parents:
diff changeset
1156 *handled_ops_p = false;
kono
parents:
diff changeset
1157 wi_in->removed_stmt = false;
kono
parents:
diff changeset
1158 gimple *stmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
1159 if (gbind *bind = dyn_cast <gbind *> (stmt))
kono
parents:
diff changeset
1160 {
kono
parents:
diff changeset
1161 for (tree var = gimple_bind_vars (bind); var; var = DECL_CHAIN (var))
kono
parents:
diff changeset
1162 grid_mark_variable_segment (var, GRID_SEGMENT_GROUP);
kono
parents:
diff changeset
1163 }
kono
parents:
diff changeset
1164 else if (gomp_parallel *parallel = dyn_cast <gomp_parallel *> (stmt))
kono
parents:
diff changeset
1165 {
kono
parents:
diff changeset
1166 *handled_ops_p = true;
kono
parents:
diff changeset
1167 gimple_omp_parallel_set_grid_phony (parallel, true);
kono
parents:
diff changeset
1168
kono
parents:
diff changeset
1169 gbind *new_bind = gimple_build_bind (NULL, NULL, make_node (BLOCK));
kono
parents:
diff changeset
1170 gimple_bind_set_body (new_bind, gimple_omp_body (parallel));
kono
parents:
diff changeset
1171 gimple_seq s = NULL;
kono
parents:
diff changeset
1172 gimple_seq_add_stmt (&s, new_bind);
kono
parents:
diff changeset
1173 gimple_omp_set_body (parallel, s);
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 struct walk_stmt_info wi_par;
kono
parents:
diff changeset
1176 memset (&wi_par, 0, sizeof (wi_par));
kono
parents:
diff changeset
1177 wi_par.info = new_bind;
kono
parents:
diff changeset
1178 walk_gimple_seq_mod (gimple_bind_body_ptr (new_bind),
kono
parents:
diff changeset
1179 grid_mark_tiling_loops, NULL, &wi_par);
kono
parents:
diff changeset
1180 }
kono
parents:
diff changeset
1181 else if (is_a <gcall *> (stmt))
kono
parents:
diff changeset
1182 wi_in->removed_stmt = grid_handle_call_in_distribute (gsi);
kono
parents:
diff changeset
1183 return NULL_TREE;
kono
parents:
diff changeset
1184 }
kono
parents:
diff changeset
1185
kono
parents:
diff changeset
1186 /* Given freshly copied top level kernel SEQ, identify the individual OMP
kono
parents:
diff changeset
1187 components, mark them as part of kernel, copy assignment leading to them
kono
parents:
diff changeset
1188 just before DST, remapping them using WI and adding new temporaries to
kono
parents:
diff changeset
1189 TGT_BIND, and and return the loop that will be used for kernel dispatch. */
kono
parents:
diff changeset
1190
kono
parents:
diff changeset
1191 static gomp_for *
kono
parents:
diff changeset
1192 grid_process_kernel_body_copy (grid_prop *grid, gimple_seq seq,
kono
parents:
diff changeset
1193 gimple_stmt_iterator *dst,
kono
parents:
diff changeset
1194 gbind *tgt_bind, struct walk_stmt_info *wi)
kono
parents:
diff changeset
1195 {
kono
parents:
diff changeset
1196 gimple *stmt = grid_copy_leading_local_assignments (seq, dst, tgt_bind,
kono
parents:
diff changeset
1197 GRID_SEGMENT_GLOBAL, wi);
kono
parents:
diff changeset
1198 gomp_teams *teams = dyn_cast <gomp_teams *> (stmt);
kono
parents:
diff changeset
1199 gcc_assert (teams);
kono
parents:
diff changeset
1200 gimple_omp_teams_set_grid_phony (teams, true);
kono
parents:
diff changeset
1201 stmt = grid_copy_leading_local_assignments (gimple_omp_body (teams), dst,
kono
parents:
diff changeset
1202 tgt_bind, GRID_SEGMENT_GLOBAL,
kono
parents:
diff changeset
1203 wi);
kono
parents:
diff changeset
1204 gcc_checking_assert (stmt);
kono
parents:
diff changeset
1205 gomp_for *dist = dyn_cast <gomp_for *> (stmt);
kono
parents:
diff changeset
1206 gcc_assert (dist);
kono
parents:
diff changeset
1207 gimple_seq prebody = gimple_omp_for_pre_body (dist);
kono
parents:
diff changeset
1208 if (prebody)
kono
parents:
diff changeset
1209 grid_copy_leading_local_assignments (prebody, dst, tgt_bind,
kono
parents:
diff changeset
1210 GRID_SEGMENT_GROUP, wi);
kono
parents:
diff changeset
1211
kono
parents:
diff changeset
1212 if (grid->tiling)
kono
parents:
diff changeset
1213 {
kono
parents:
diff changeset
1214 gimple_omp_for_set_kind (dist, GF_OMP_FOR_KIND_GRID_LOOP);
kono
parents:
diff changeset
1215 gimple_omp_for_set_grid_group_iter (dist, true);
kono
parents:
diff changeset
1216
kono
parents:
diff changeset
1217 struct walk_stmt_info wi_tiled;
kono
parents:
diff changeset
1218 memset (&wi_tiled, 0, sizeof (wi_tiled));
kono
parents:
diff changeset
1219 walk_gimple_seq_mod (gimple_omp_body_ptr (dist),
kono
parents:
diff changeset
1220 grid_mark_tiling_parallels_and_loops, NULL,
kono
parents:
diff changeset
1221 &wi_tiled);
kono
parents:
diff changeset
1222 return dist;
kono
parents:
diff changeset
1223 }
kono
parents:
diff changeset
1224 else
kono
parents:
diff changeset
1225 {
kono
parents:
diff changeset
1226 gimple_omp_for_set_grid_phony (dist, true);
kono
parents:
diff changeset
1227 stmt = grid_copy_leading_local_assignments (gimple_omp_body (dist), dst,
kono
parents:
diff changeset
1228 tgt_bind,
kono
parents:
diff changeset
1229 GRID_SEGMENT_PRIVATE, wi);
kono
parents:
diff changeset
1230 gcc_checking_assert (stmt);
kono
parents:
diff changeset
1231 gomp_parallel *parallel = as_a <gomp_parallel *> (stmt);
kono
parents:
diff changeset
1232 gimple_omp_parallel_set_grid_phony (parallel, true);
kono
parents:
diff changeset
1233 stmt = grid_copy_leading_local_assignments (gimple_omp_body (parallel),
kono
parents:
diff changeset
1234 dst, tgt_bind,
kono
parents:
diff changeset
1235 GRID_SEGMENT_PRIVATE, wi);
kono
parents:
diff changeset
1236 gomp_for *inner_loop = as_a <gomp_for *> (stmt);
kono
parents:
diff changeset
1237 gimple_omp_for_set_kind (inner_loop, GF_OMP_FOR_KIND_GRID_LOOP);
kono
parents:
diff changeset
1238 prebody = gimple_omp_for_pre_body (inner_loop);
kono
parents:
diff changeset
1239 if (prebody)
kono
parents:
diff changeset
1240 grid_copy_leading_local_assignments (prebody, dst, tgt_bind,
kono
parents:
diff changeset
1241 GRID_SEGMENT_PRIVATE, wi);
kono
parents:
diff changeset
1242
kono
parents:
diff changeset
1243 if (gimple_omp_for_combined_p (inner_loop))
kono
parents:
diff changeset
1244 grid_eliminate_combined_simd_part (inner_loop);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1245 struct walk_stmt_info body_wi;
111
kono
parents:
diff changeset
1246 memset (&body_wi, 0, sizeof (body_wi));
kono
parents:
diff changeset
1247 walk_gimple_seq_mod (gimple_omp_body_ptr (inner_loop),
kono
parents:
diff changeset
1248 grid_process_grid_body, NULL, &body_wi);
kono
parents:
diff changeset
1249
kono
parents:
diff changeset
1250 return inner_loop;
kono
parents:
diff changeset
1251 }
kono
parents:
diff changeset
1252 }
kono
parents:
diff changeset
1253
kono
parents:
diff changeset
1254 /* If TARGET points to a GOMP_TARGET which follows a gridifiable pattern,
kono
parents:
diff changeset
1255 create a GPU kernel for it. GSI must point to the same statement, TGT_BIND
kono
parents:
diff changeset
1256 is the bind into which temporaries inserted before TARGET should be
kono
parents:
diff changeset
1257 added. */
kono
parents:
diff changeset
1258
kono
parents:
diff changeset
1259 static void
kono
parents:
diff changeset
1260 grid_attempt_target_gridification (gomp_target *target,
kono
parents:
diff changeset
1261 gimple_stmt_iterator *gsi,
kono
parents:
diff changeset
1262 gbind *tgt_bind)
kono
parents:
diff changeset
1263 {
kono
parents:
diff changeset
1264 /* removed group_size */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1265 grid_prop grid = {};
111
kono
parents:
diff changeset
1266 if (!target || !grid_target_follows_gridifiable_pattern (target, &grid))
kono
parents:
diff changeset
1267 return;
kono
parents:
diff changeset
1268
kono
parents:
diff changeset
1269 location_t loc = gimple_location (target);
kono
parents:
diff changeset
1270 if (dump_enabled_p ())
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1271 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, target,
111
kono
parents:
diff changeset
1272 "Target construct will be turned into a gridified HSA "
kono
parents:
diff changeset
1273 "kernel\n");
kono
parents:
diff changeset
1274
kono
parents:
diff changeset
1275 /* Copy target body to a GPUKERNEL construct: */
kono
parents:
diff changeset
1276 gimple_seq kernel_seq = copy_gimple_seq_and_replace_locals
kono
parents:
diff changeset
1277 (gimple_omp_body (target));
kono
parents:
diff changeset
1278
kono
parents:
diff changeset
1279 hash_map<tree, tree> *declmap = new hash_map<tree, tree>;
kono
parents:
diff changeset
1280 struct walk_stmt_info wi;
kono
parents:
diff changeset
1281 memset (&wi, 0, sizeof (struct walk_stmt_info));
kono
parents:
diff changeset
1282 wi.info = declmap;
kono
parents:
diff changeset
1283
kono
parents:
diff changeset
1284 /* Copy assignments in between OMP statements before target, mark OMP
kono
parents:
diff changeset
1285 statements within copy appropriately. */
kono
parents:
diff changeset
1286 gomp_for *inner_loop = grid_process_kernel_body_copy (&grid, kernel_seq, gsi,
kono
parents:
diff changeset
1287 tgt_bind, &wi);
kono
parents:
diff changeset
1288
kono
parents:
diff changeset
1289 gbind *old_bind
kono
parents:
diff changeset
1290 = as_a <gbind *> (gimple_seq_first (gimple_omp_body (target)));
kono
parents:
diff changeset
1291 gbind *new_bind = as_a <gbind *> (gimple_seq_first (kernel_seq));
kono
parents:
diff changeset
1292 tree new_block = gimple_bind_block (new_bind);
kono
parents:
diff changeset
1293 tree enc_block = BLOCK_SUPERCONTEXT (gimple_bind_block (old_bind));
kono
parents:
diff changeset
1294 BLOCK_CHAIN (new_block) = BLOCK_SUBBLOCKS (enc_block);
kono
parents:
diff changeset
1295 BLOCK_SUBBLOCKS (enc_block) = new_block;
kono
parents:
diff changeset
1296 BLOCK_SUPERCONTEXT (new_block) = enc_block;
kono
parents:
diff changeset
1297 gimple *gpukernel = gimple_build_omp_grid_body (kernel_seq);
kono
parents:
diff changeset
1298 gimple_seq_add_stmt
kono
parents:
diff changeset
1299 (gimple_bind_body_ptr (as_a <gbind *> (gimple_omp_body (target))),
kono
parents:
diff changeset
1300 gpukernel);
kono
parents:
diff changeset
1301
kono
parents:
diff changeset
1302 for (size_t i = 0; i < grid.collapse; i++)
kono
parents:
diff changeset
1303 walk_tree (&grid.group_sizes[i], grid_remap_prebody_decls, &wi, NULL);
kono
parents:
diff changeset
1304 push_gimplify_context ();
kono
parents:
diff changeset
1305 for (size_t i = 0; i < grid.collapse; i++)
kono
parents:
diff changeset
1306 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1307 tree index_var = gimple_omp_for_index (inner_loop, i);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1308 tree itype, type = TREE_TYPE (index_var);
111
kono
parents:
diff changeset
1309 if (POINTER_TYPE_P (type))
kono
parents:
diff changeset
1310 itype = signed_type_for (type);
kono
parents:
diff changeset
1311 else
kono
parents:
diff changeset
1312 itype = type;
kono
parents:
diff changeset
1313
kono
parents:
diff changeset
1314 enum tree_code cond_code = gimple_omp_for_cond (inner_loop, i);
kono
parents:
diff changeset
1315 tree n1 = unshare_expr (gimple_omp_for_initial (inner_loop, i));
kono
parents:
diff changeset
1316 walk_tree (&n1, grid_remap_prebody_decls, &wi, NULL);
kono
parents:
diff changeset
1317 tree n2 = unshare_expr (gimple_omp_for_final (inner_loop, i));
kono
parents:
diff changeset
1318 walk_tree (&n2, grid_remap_prebody_decls, &wi, NULL);
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1319 tree step
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1320 = omp_get_for_step_from_incr (loc, gimple_omp_for_incr (inner_loop, i));
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
1321 omp_adjust_for_condition (loc, &cond_code, &n2, index_var, step);
111
kono
parents:
diff changeset
1322 n1 = fold_convert (itype, n1);
kono
parents:
diff changeset
1323 n2 = fold_convert (itype, n2);
kono
parents:
diff changeset
1324
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1325 tree cond = fold_build2 (cond_code, boolean_type_node, n1, n2);
111
kono
parents:
diff changeset
1326
kono
parents:
diff changeset
1327 tree t = build_int_cst (itype, (cond_code == LT_EXPR ? -1 : 1));
kono
parents:
diff changeset
1328 t = fold_build2 (PLUS_EXPR, itype, step, t);
kono
parents:
diff changeset
1329 t = fold_build2 (PLUS_EXPR, itype, t, n2);
kono
parents:
diff changeset
1330 t = fold_build2 (MINUS_EXPR, itype, t, n1);
kono
parents:
diff changeset
1331 if (TYPE_UNSIGNED (itype) && cond_code == GT_EXPR)
kono
parents:
diff changeset
1332 t = fold_build2 (TRUNC_DIV_EXPR, itype,
kono
parents:
diff changeset
1333 fold_build1 (NEGATE_EXPR, itype, t),
kono
parents:
diff changeset
1334 fold_build1 (NEGATE_EXPR, itype, step));
kono
parents:
diff changeset
1335 else
kono
parents:
diff changeset
1336 t = fold_build2 (TRUNC_DIV_EXPR, itype, t, step);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1337 t = fold_build3 (COND_EXPR, itype, cond, t, build_zero_cst (itype));
111
kono
parents:
diff changeset
1338 if (grid.tiling)
kono
parents:
diff changeset
1339 {
kono
parents:
diff changeset
1340 if (cond_code == GT_EXPR)
kono
parents:
diff changeset
1341 step = fold_build1 (NEGATE_EXPR, itype, step);
kono
parents:
diff changeset
1342 t = fold_build2 (MULT_EXPR, itype, t, step);
kono
parents:
diff changeset
1343 }
kono
parents:
diff changeset
1344
kono
parents:
diff changeset
1345 tree gs = fold_convert (uint32_type_node, t);
kono
parents:
diff changeset
1346 gimple_seq tmpseq = NULL;
kono
parents:
diff changeset
1347 gimplify_expr (&gs, &tmpseq, NULL, is_gimple_val, fb_rvalue);
kono
parents:
diff changeset
1348 if (!gimple_seq_empty_p (tmpseq))
kono
parents:
diff changeset
1349 gsi_insert_seq_before (gsi, tmpseq, GSI_SAME_STMT);
kono
parents:
diff changeset
1350
kono
parents:
diff changeset
1351 tree ws;
kono
parents:
diff changeset
1352 if (grid.group_sizes[i])
kono
parents:
diff changeset
1353 {
kono
parents:
diff changeset
1354 ws = fold_convert (uint32_type_node, grid.group_sizes[i]);
kono
parents:
diff changeset
1355 tmpseq = NULL;
kono
parents:
diff changeset
1356 gimplify_expr (&ws, &tmpseq, NULL, is_gimple_val, fb_rvalue);
kono
parents:
diff changeset
1357 if (!gimple_seq_empty_p (tmpseq))
kono
parents:
diff changeset
1358 gsi_insert_seq_before (gsi, tmpseq, GSI_SAME_STMT);
kono
parents:
diff changeset
1359 }
kono
parents:
diff changeset
1360 else
kono
parents:
diff changeset
1361 ws = build_zero_cst (uint32_type_node);
kono
parents:
diff changeset
1362
kono
parents:
diff changeset
1363 tree c = build_omp_clause (UNKNOWN_LOCATION, OMP_CLAUSE__GRIDDIM_);
kono
parents:
diff changeset
1364 OMP_CLAUSE__GRIDDIM__DIMENSION (c) = i;
kono
parents:
diff changeset
1365 OMP_CLAUSE__GRIDDIM__SIZE (c) = gs;
kono
parents:
diff changeset
1366 OMP_CLAUSE__GRIDDIM__GROUP (c) = ws;
kono
parents:
diff changeset
1367 OMP_CLAUSE_CHAIN (c) = gimple_omp_target_clauses (target);
kono
parents:
diff changeset
1368 gimple_omp_target_set_clauses (target, c);
kono
parents:
diff changeset
1369 }
kono
parents:
diff changeset
1370 pop_gimplify_context (tgt_bind);
kono
parents:
diff changeset
1371 delete declmap;
kono
parents:
diff changeset
1372 return;
kono
parents:
diff changeset
1373 }
kono
parents:
diff changeset
1374
kono
parents:
diff changeset
1375 /* Walker function doing all the work for create_target_kernels. */
kono
parents:
diff changeset
1376
kono
parents:
diff changeset
1377 static tree
kono
parents:
diff changeset
1378 grid_gridify_all_targets_stmt (gimple_stmt_iterator *gsi,
kono
parents:
diff changeset
1379 bool *handled_ops_p,
kono
parents:
diff changeset
1380 struct walk_stmt_info *incoming)
kono
parents:
diff changeset
1381 {
kono
parents:
diff changeset
1382 *handled_ops_p = false;
kono
parents:
diff changeset
1383
kono
parents:
diff changeset
1384 gimple *stmt = gsi_stmt (*gsi);
kono
parents:
diff changeset
1385 gomp_target *target = dyn_cast <gomp_target *> (stmt);
kono
parents:
diff changeset
1386 if (target)
kono
parents:
diff changeset
1387 {
kono
parents:
diff changeset
1388 gbind *tgt_bind = (gbind *) incoming->info;
kono
parents:
diff changeset
1389 gcc_checking_assert (tgt_bind);
kono
parents:
diff changeset
1390 grid_attempt_target_gridification (target, gsi, tgt_bind);
kono
parents:
diff changeset
1391 return NULL_TREE;
kono
parents:
diff changeset
1392 }
kono
parents:
diff changeset
1393 gbind *bind = dyn_cast <gbind *> (stmt);
kono
parents:
diff changeset
1394 if (bind)
kono
parents:
diff changeset
1395 {
kono
parents:
diff changeset
1396 *handled_ops_p = true;
kono
parents:
diff changeset
1397 struct walk_stmt_info wi;
kono
parents:
diff changeset
1398 memset (&wi, 0, sizeof (wi));
kono
parents:
diff changeset
1399 wi.info = bind;
kono
parents:
diff changeset
1400 walk_gimple_seq_mod (gimple_bind_body_ptr (bind),
kono
parents:
diff changeset
1401 grid_gridify_all_targets_stmt, NULL, &wi);
kono
parents:
diff changeset
1402 }
kono
parents:
diff changeset
1403 return NULL_TREE;
kono
parents:
diff changeset
1404 }
kono
parents:
diff changeset
1405
kono
parents:
diff changeset
1406 /* Attempt to gridify all target constructs in BODY_P. All such targets will
kono
parents:
diff changeset
1407 have their bodies duplicated, with the new copy being put into a
kono
parents:
diff changeset
1408 gimple_omp_grid_body statement. All kernel-related construct within the
kono
parents:
diff changeset
1409 grid_body will be marked with phony flags or kernel kinds. Moreover, some
kono
parents:
diff changeset
1410 re-structuring is often needed, such as copying pre-bodies before the target
kono
parents:
diff changeset
1411 construct so that kernel grid sizes can be computed. */
kono
parents:
diff changeset
1412
kono
parents:
diff changeset
1413 void
kono
parents:
diff changeset
1414 omp_grid_gridify_all_targets (gimple_seq *body_p)
kono
parents:
diff changeset
1415 {
kono
parents:
diff changeset
1416 struct walk_stmt_info wi;
kono
parents:
diff changeset
1417 memset (&wi, 0, sizeof (wi));
kono
parents:
diff changeset
1418 walk_gimple_seq_mod (body_p, grid_gridify_all_targets_stmt, NULL, &wi);
kono
parents:
diff changeset
1419 }