]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ipa-hsa.c
re PR tree-optimization/91945 (ICE: tree check: expected integer_cst, have var_decl...
[thirdparty/gcc.git] / gcc / ipa-hsa.c
CommitLineData
b2b40051 1/* Callgraph based analysis of static variables.
a5544970 2 Copyright (C) 2015-2019 Free Software Foundation, Inc.
b2b40051
MJ
3 Contributed by Martin Liska <mliska@suse.cz>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21/* Interprocedural HSA pass is responsible for creation of HSA clones.
22 For all these HSA clones, we emit HSAIL instructions and pass processing
23 is terminated. */
24
25#include "config.h"
26#include "system.h"
27#include "coretypes.h"
28#include "tm.h"
29#include "is-a.h"
30#include "hash-set.h"
31#include "vec.h"
32#include "tree.h"
33#include "tree-pass.h"
34#include "function.h"
35#include "basic-block.h"
36#include "gimple.h"
37#include "dumpfile.h"
38#include "gimple-pretty-print.h"
39#include "tree-streamer.h"
40#include "stringpool.h"
41#include "cgraph.h"
42#include "print-tree.h"
43#include "symbol-summary.h"
13293add 44#include "hsa-common.h"
b2b40051
MJ
45
46namespace {
47
48/* If NODE is not versionable, warn about not emiting HSAIL and return false.
49 Otherwise return true. */
50
51static bool
52check_warn_node_versionable (cgraph_node *node)
53{
87f94429 54 if (!node->versionable)
b2b40051
MJ
55 {
56 warning_at (EXPR_LOCATION (node->decl), OPT_Whsa,
57 "could not emit HSAIL for function %s: function cannot be "
58 "cloned", node->name ());
59 return false;
60 }
61 return true;
62}
63
64/* The function creates HSA clones for all functions that were either
65 marked as HSA kernels or are callable HSA functions. Apart from that,
66 we redirect all edges that come from an HSA clone and end in another
67 HSA clone to connect these two functions. */
68
69static unsigned int
70process_hsa_functions (void)
71{
72 struct cgraph_node *node;
73
74 if (hsa_summaries == NULL)
75 hsa_summaries = new hsa_summary_t (symtab);
76
77 FOR_EACH_DEFINED_FUNCTION (node)
78 {
0148358a 79 hsa_function_summary *s = hsa_summaries->get (node);
b2b40051
MJ
80
81 /* A linked function is skipped. */
0148358a 82 if (s != NULL && s->m_bound_function != NULL)
b2b40051
MJ
83 continue;
84
0148358a 85 if (s != NULL)
b2b40051
MJ
86 {
87 if (!check_warn_node_versionable (node))
88 continue;
89 cgraph_node *clone
90 = node->create_virtual_clone (vec <cgraph_edge *> (),
53aedcce 91 NULL, NULL, "hsa", 0);
b2b40051 92 TREE_PUBLIC (clone->decl) = TREE_PUBLIC (node->decl);
56b1c60e 93 clone->externally_visible = node->externally_visible;
b2b40051
MJ
94
95 clone->force_output = true;
96 hsa_summaries->link_functions (clone, node, s->m_kind, false);
97
98 if (dump_file)
99 fprintf (dump_file, "Created a new HSA clone: %s, type: %s\n",
100 clone->name (),
101 s->m_kind == HSA_KERNEL ? "kernel" : "function");
102 }
51020892
MJ
103 else if (hsa_callable_function_p (node->decl)
104 /* At this point, this is enough to identify clones for
105 parallel, which for HSA would need to be kernels anyway. */
106 && !DECL_ARTIFICIAL (node->decl))
b2b40051
MJ
107 {
108 if (!check_warn_node_versionable (node))
109 continue;
110 cgraph_node *clone
111 = node->create_virtual_clone (vec <cgraph_edge *> (),
53aedcce 112 NULL, NULL, "hsa", 0);
b2b40051 113 TREE_PUBLIC (clone->decl) = TREE_PUBLIC (node->decl);
56b1c60e 114 clone->externally_visible = node->externally_visible;
b2b40051 115
87f94429 116 if (!node->local)
b2b40051
MJ
117 clone->force_output = true;
118 hsa_summaries->link_functions (clone, node, HSA_FUNCTION, false);
119
120 if (dump_file)
121 fprintf (dump_file, "Created a new HSA function clone: %s\n",
122 clone->name ());
123 }
124 }
125
126 /* Redirect all edges that are between HSA clones. */
127 FOR_EACH_DEFINED_FUNCTION (node)
128 {
129 cgraph_edge *e = node->callees;
130
131 while (e)
132 {
0148358a
ML
133 hsa_function_summary *src = hsa_summaries->get (node);
134 if (src != NULL && src->m_gpu_implementation_p)
b2b40051 135 {
0148358a
ML
136 hsa_function_summary *dst = hsa_summaries->get (e->callee);
137 if (dst != NULL && !dst->m_gpu_implementation_p)
b2b40051 138 {
56b1c60e 139 e->redirect_callee (dst->m_bound_function);
b2b40051
MJ
140 if (dump_file)
141 fprintf (dump_file,
142 "Redirecting edge to HSA function: %s->%s\n",
143 xstrdup_for_dump (e->caller->name ()),
144 xstrdup_for_dump (e->callee->name ()));
145 }
146 }
147
148 e = e->next_callee;
149 }
150 }
151
152 return 0;
153}
154
155/* Iterate all HSA functions and stream out HSA function summary. */
156
157static void
158ipa_hsa_write_summary (void)
159{
160 struct bitpack_d bp;
161 struct cgraph_node *node;
162 struct output_block *ob;
163 unsigned int count = 0;
164 lto_symtab_encoder_iterator lsei;
165 lto_symtab_encoder_t encoder;
166
167 if (!hsa_summaries)
168 return;
169
170 ob = create_output_block (LTO_section_ipa_hsa);
171 encoder = ob->decl_state->symtab_node_encoder;
172 ob->symbol = NULL;
173 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
174 lsei_next_function_in_partition (&lsei))
175 {
176 node = lsei_cgraph_node (lsei);
0148358a 177 hsa_function_summary *s = hsa_summaries->get (node);
b2b40051 178
0148358a 179 if (s != NULL)
b2b40051
MJ
180 count++;
181 }
182
183 streamer_write_uhwi (ob, count);
184
185 /* Process all of the functions. */
186 for (lsei = lsei_start_function_in_partition (encoder); !lsei_end_p (lsei);
187 lsei_next_function_in_partition (&lsei))
188 {
189 node = lsei_cgraph_node (lsei);
0148358a 190 hsa_function_summary *s = hsa_summaries->get (node);
b2b40051 191
0148358a 192 if (s != NULL)
b2b40051
MJ
193 {
194 encoder = ob->decl_state->symtab_node_encoder;
195 int node_ref = lto_symtab_encoder_encode (encoder, node);
196 streamer_write_uhwi (ob, node_ref);
197
198 bp = bitpack_create (ob->main_stream);
199 bp_pack_value (&bp, s->m_kind, 2);
200 bp_pack_value (&bp, s->m_gpu_implementation_p, 1);
56b1c60e 201 bp_pack_value (&bp, s->m_bound_function != NULL, 1);
b2b40051 202 streamer_write_bitpack (&bp);
56b1c60e
MJ
203 if (s->m_bound_function)
204 stream_write_tree (ob, s->m_bound_function->decl, true);
b2b40051
MJ
205 }
206 }
207
208 streamer_write_char_stream (ob->main_stream, 0);
209 produce_asm (ob, NULL);
210 destroy_output_block (ob);
211}
212
213/* Read section in file FILE_DATA of length LEN with data DATA. */
214
215static void
216ipa_hsa_read_section (struct lto_file_decl_data *file_data, const char *data,
217 size_t len)
218{
219 const struct lto_function_header *header
220 = (const struct lto_function_header *) data;
221 const int cfg_offset = sizeof (struct lto_function_header);
222 const int main_offset = cfg_offset + header->cfg_size;
223 const int string_offset = main_offset + header->main_size;
99b1c316 224 class data_in *data_in;
b2b40051
MJ
225 unsigned int i;
226 unsigned int count;
227
228 lto_input_block ib_main ((const char *) data + main_offset,
229 header->main_size, file_data->mode_table);
230
231 data_in
232 = lto_data_in_create (file_data, (const char *) data + string_offset,
233 header->string_size, vNULL);
234 count = streamer_read_uhwi (&ib_main);
235
236 for (i = 0; i < count; i++)
237 {
238 unsigned int index;
239 struct cgraph_node *node;
240 lto_symtab_encoder_t encoder;
241
242 index = streamer_read_uhwi (&ib_main);
243 encoder = file_data->symtab_node_encoder;
244 node = dyn_cast<cgraph_node *> (lto_symtab_encoder_deref (encoder,
245 index));
246 gcc_assert (node->definition);
99353fcf 247 hsa_function_summary *s = hsa_summaries->get_create (node);
b2b40051
MJ
248
249 struct bitpack_d bp = streamer_read_bitpack (&ib_main);
250 s->m_kind = (hsa_function_kind) bp_unpack_value (&bp, 2);
251 s->m_gpu_implementation_p = bp_unpack_value (&bp, 1);
252 bool has_tree = bp_unpack_value (&bp, 1);
253
254 if (has_tree)
255 {
256 tree decl = stream_read_tree (&ib_main, data_in);
56b1c60e 257 s->m_bound_function = cgraph_node::get_create (decl);
b2b40051
MJ
258 }
259 }
260 lto_free_section_data (file_data, LTO_section_ipa_hsa, NULL, data,
261 len);
262 lto_data_in_delete (data_in);
263}
264
265/* Load streamed HSA functions summary and assign the summary to a function. */
266
267static void
268ipa_hsa_read_summary (void)
269{
270 struct lto_file_decl_data **file_data_vec = lto_get_file_decl_data ();
271 struct lto_file_decl_data *file_data;
272 unsigned int j = 0;
273
274 if (hsa_summaries == NULL)
275 hsa_summaries = new hsa_summary_t (symtab);
276
277 while ((file_data = file_data_vec[j++]))
278 {
279 size_t len;
3c56d8d8
ML
280 const char *data
281 = lto_get_summary_section_data (file_data, LTO_section_ipa_hsa, &len);
b2b40051
MJ
282 if (data)
283 ipa_hsa_read_section (file_data, data, len);
284 }
285}
286
287const pass_data pass_data_ipa_hsa =
288{
289 IPA_PASS, /* type */
290 "hsa", /* name */
d03958cf 291 OPTGROUP_OMP, /* optinfo_flags */
b2b40051
MJ
292 TV_IPA_HSA, /* tv_id */
293 0, /* properties_required */
294 0, /* properties_provided */
295 0, /* properties_destroyed */
296 0, /* todo_flags_start */
297 TODO_dump_symtab, /* todo_flags_finish */
298};
299
300class pass_ipa_hsa : public ipa_opt_pass_d
301{
302public:
303 pass_ipa_hsa (gcc::context *ctxt)
304 : ipa_opt_pass_d (pass_data_ipa_hsa, ctxt,
305 NULL, /* generate_summary */
306 ipa_hsa_write_summary, /* write_summary */
307 ipa_hsa_read_summary, /* read_summary */
308 ipa_hsa_write_summary, /* write_optimization_summary */
309 ipa_hsa_read_summary, /* read_optimization_summary */
310 NULL, /* stmt_fixup */
311 0, /* function_transform_todo_flags_start */
312 NULL, /* function_transform */
313 NULL) /* variable_transform */
314 {}
315
316 /* opt_pass methods: */
317 virtual bool gate (function *);
318
319 virtual unsigned int execute (function *) { return process_hsa_functions (); }
320
321}; // class pass_ipa_reference
322
323bool
324pass_ipa_hsa::gate (function *)
325{
326 return hsa_gen_requested_p ();
327}
328
329} // anon namespace
330
331ipa_opt_pass_d *
332make_pass_ipa_hsa (gcc::context *ctxt)
333{
334 return new pass_ipa_hsa (ctxt);
335}