annotate gcc/brig/brigfrontend/brig-code-entry-handler.cc @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* brig-code-entry-handler.cc -- a gccbrig base class
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com>
kono
parents:
diff changeset
4 for General Processor Tech.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 This file is part of GCC.
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
9 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
10 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
11 version.
kono
parents:
diff changeset
12
kono
parents:
diff changeset
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
16 for more details.
kono
parents:
diff changeset
17
kono
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
19 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
20 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 #include "brig-code-entry-handler.h"
kono
parents:
diff changeset
23
kono
parents:
diff changeset
24 #include "stringpool.h"
kono
parents:
diff changeset
25 #include "tree-iterator.h"
kono
parents:
diff changeset
26 #include "toplev.h"
kono
parents:
diff changeset
27 #include "diagnostic.h"
kono
parents:
diff changeset
28 #include "brig-machine.h"
kono
parents:
diff changeset
29 #include "brig-util.h"
kono
parents:
diff changeset
30 #include "errors.h"
kono
parents:
diff changeset
31 #include "real.h"
kono
parents:
diff changeset
32 #include "print-tree.h"
kono
parents:
diff changeset
33 #include "tree-pretty-print.h"
kono
parents:
diff changeset
34 #include "target.h"
kono
parents:
diff changeset
35 #include "langhooks.h"
kono
parents:
diff changeset
36 #include "gimple-expr.h"
kono
parents:
diff changeset
37 #include "convert.h"
kono
parents:
diff changeset
38 #include "brig-util.h"
kono
parents:
diff changeset
39 #include "builtins.h"
kono
parents:
diff changeset
40 #include "phsa.h"
kono
parents:
diff changeset
41 #include "brig-builtins.h"
kono
parents:
diff changeset
42 #include "fold-const.h"
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 brig_code_entry_handler::brig_code_entry_handler (brig_to_generic &parent)
kono
parents:
diff changeset
45 : brig_entry_handler (parent)
kono
parents:
diff changeset
46 {
kono
parents:
diff changeset
47 }
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* Build a tree operand which is a reference to a piece of code. REF is the
kono
parents:
diff changeset
50 original reference as a BRIG object. */
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 tree
kono
parents:
diff changeset
53 brig_code_entry_handler::build_code_ref (const BrigBase &ref)
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 if (ref.kind == BRIG_KIND_DIRECTIVE_LABEL)
kono
parents:
diff changeset
56 {
kono
parents:
diff changeset
57 const BrigDirectiveLabel *brig_label = (const BrigDirectiveLabel *) &ref;
kono
parents:
diff changeset
58
kono
parents:
diff changeset
59 const BrigData *label_name
kono
parents:
diff changeset
60 = m_parent.get_brig_data_entry (brig_label->name);
kono
parents:
diff changeset
61
kono
parents:
diff changeset
62 std::string label_str ((const char *) (label_name->bytes),
kono
parents:
diff changeset
63 label_name->byteCount);
kono
parents:
diff changeset
64 return m_parent.m_cf->label (label_str);
kono
parents:
diff changeset
65 }
kono
parents:
diff changeset
66 else if (ref.kind == BRIG_KIND_DIRECTIVE_FUNCTION)
kono
parents:
diff changeset
67 {
kono
parents:
diff changeset
68 const BrigDirectiveExecutable *func
kono
parents:
diff changeset
69 = (const BrigDirectiveExecutable *) &ref;
kono
parents:
diff changeset
70 return m_parent.function_decl (m_parent.get_mangled_name (func));
kono
parents:
diff changeset
71 }
kono
parents:
diff changeset
72 else if (ref.kind == BRIG_KIND_DIRECTIVE_FBARRIER)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 const BrigDirectiveFbarrier* fbar = (const BrigDirectiveFbarrier*)&ref;
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 std::string var_name = m_parent.get_mangled_name (fbar);
kono
parents:
diff changeset
77 uint64_t offset
kono
parents:
diff changeset
78 = m_parent.m_cf->group_variable_segment_offset (var_name);
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 tree local_offset = build_int_cst (uint32_type_node, offset);
kono
parents:
diff changeset
81 if (m_parent.m_cf->m_local_group_variables.has_variable (var_name))
kono
parents:
diff changeset
82 local_offset
kono
parents:
diff changeset
83 = build2 (PLUS_EXPR, uint64_type_node, local_offset,
kono
parents:
diff changeset
84 convert (uint64_type_node,
kono
parents:
diff changeset
85 m_parent.m_cf->m_group_local_offset_arg));
kono
parents:
diff changeset
86 return local_offset;
kono
parents:
diff changeset
87 }
kono
parents:
diff changeset
88 else
kono
parents:
diff changeset
89 gcc_unreachable ();
kono
parents:
diff changeset
90 }
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 /* Produce a tree operand for the given BRIG_INST and its OPERAND.
kono
parents:
diff changeset
93 OPERAND_TYPE should be the operand type in case it should not
kono
parents:
diff changeset
94 be dictated by the BrigBase. IS_INPUT indicates if the operand
kono
parents:
diff changeset
95 is an input operand or a result. */
kono
parents:
diff changeset
96
kono
parents:
diff changeset
97 tree
kono
parents:
diff changeset
98 brig_code_entry_handler::build_tree_operand (const BrigInstBase &brig_inst,
kono
parents:
diff changeset
99 const BrigBase &operand,
kono
parents:
diff changeset
100 tree operand_type, bool is_input)
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 switch (operand.kind)
kono
parents:
diff changeset
103 {
kono
parents:
diff changeset
104 case BRIG_KIND_OPERAND_OPERAND_LIST:
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 vec<constructor_elt, va_gc> *constructor_vals = NULL;
kono
parents:
diff changeset
107 const BrigOperandOperandList &oplist
kono
parents:
diff changeset
108 = (const BrigOperandOperandList &) operand;
kono
parents:
diff changeset
109 const BrigData *data = m_parent.get_brig_data_entry (oplist.elements);
kono
parents:
diff changeset
110 size_t bytes = data->byteCount;
kono
parents:
diff changeset
111 const BrigOperandOffset32_t *operand_ptr
kono
parents:
diff changeset
112 = (const BrigOperandOffset32_t *) data->bytes;
kono
parents:
diff changeset
113 while (bytes > 0)
kono
parents:
diff changeset
114 {
kono
parents:
diff changeset
115 BrigOperandOffset32_t offset = *operand_ptr;
kono
parents:
diff changeset
116 const BrigBase *operand_element
kono
parents:
diff changeset
117 = m_parent.get_brig_operand_entry (offset);
kono
parents:
diff changeset
118 tree element
kono
parents:
diff changeset
119 = build_tree_operand (brig_inst, *operand_element, operand_type);
kono
parents:
diff changeset
120
kono
parents:
diff changeset
121 /* In case a vector is used an input, cast the elements to
kono
parents:
diff changeset
122 correct size here so we don't need a separate unpack/pack for it.
kono
parents:
diff changeset
123 fp16-fp32 conversion is done in build_operands (). */
kono
parents:
diff changeset
124 if (is_input && TREE_TYPE (element) != operand_type)
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
125 element = build_resize_convert_view (operand_type, element);
111
kono
parents:
diff changeset
126
kono
parents:
diff changeset
127 CONSTRUCTOR_APPEND_ELT (constructor_vals, NULL_TREE, element);
kono
parents:
diff changeset
128 ++operand_ptr;
kono
parents:
diff changeset
129 bytes -= 4;
kono
parents:
diff changeset
130 }
kono
parents:
diff changeset
131 size_t element_count = data->byteCount / 4;
kono
parents:
diff changeset
132 tree vec_type = build_vector_type (operand_type, element_count);
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 return build_constructor (vec_type, constructor_vals);
kono
parents:
diff changeset
135 }
kono
parents:
diff changeset
136 case BRIG_KIND_OPERAND_CODE_LIST:
kono
parents:
diff changeset
137 {
kono
parents:
diff changeset
138 /* Build a TREE_VEC of code expressions. */
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 const BrigOperandCodeList &oplist
kono
parents:
diff changeset
141 = (const BrigOperandCodeList &) operand;
kono
parents:
diff changeset
142 const BrigData *data = m_parent.get_brig_data_entry (oplist.elements);
kono
parents:
diff changeset
143 size_t bytes = data->byteCount;
kono
parents:
diff changeset
144 const BrigOperandOffset32_t *operand_ptr
kono
parents:
diff changeset
145 = (const BrigOperandOffset32_t *) data->bytes;
kono
parents:
diff changeset
146
kono
parents:
diff changeset
147 size_t case_index = 0;
kono
parents:
diff changeset
148 size_t element_count = data->byteCount / 4;
kono
parents:
diff changeset
149
kono
parents:
diff changeset
150 /* Create a TREE_VEC out of the labels in the list. */
kono
parents:
diff changeset
151 tree vec = make_tree_vec (element_count);
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 while (bytes > 0)
kono
parents:
diff changeset
154 {
kono
parents:
diff changeset
155 BrigOperandOffset32_t offset = *operand_ptr;
kono
parents:
diff changeset
156 const BrigBase *ref = m_parent.get_brig_code_entry (offset);
kono
parents:
diff changeset
157 tree element = build_code_ref (*ref);
kono
parents:
diff changeset
158
kono
parents:
diff changeset
159 gcc_assert (case_index < element_count);
kono
parents:
diff changeset
160 TREE_VEC_ELT (vec, case_index) = element;
kono
parents:
diff changeset
161 case_index++;
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 ++operand_ptr;
kono
parents:
diff changeset
164 bytes -= 4;
kono
parents:
diff changeset
165 }
kono
parents:
diff changeset
166 return vec;
kono
parents:
diff changeset
167 }
kono
parents:
diff changeset
168 case BRIG_KIND_OPERAND_REGISTER:
kono
parents:
diff changeset
169 {
kono
parents:
diff changeset
170 const BrigOperandRegister *brig_reg
kono
parents:
diff changeset
171 = (const BrigOperandRegister *) &operand;
kono
parents:
diff changeset
172 return m_parent.m_cf->get_m_var_declfor_reg (brig_reg);
kono
parents:
diff changeset
173 }
kono
parents:
diff changeset
174 case BRIG_KIND_OPERAND_CONSTANT_BYTES:
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 const BrigOperandConstantBytes *brigConst
kono
parents:
diff changeset
177 = (const BrigOperandConstantBytes *) &operand;
kono
parents:
diff changeset
178 /* The constants can be of different type than the instruction
kono
parents:
diff changeset
179 and are implicitly casted to the input operand. */
kono
parents:
diff changeset
180 return get_tree_cst_for_hsa_operand (brigConst, NULL_TREE);
kono
parents:
diff changeset
181 }
kono
parents:
diff changeset
182 case BRIG_KIND_OPERAND_WAVESIZE:
kono
parents:
diff changeset
183 {
kono
parents:
diff changeset
184 if (!INTEGRAL_TYPE_P (operand_type))
kono
parents:
diff changeset
185 {
kono
parents:
diff changeset
186 gcc_unreachable ();
kono
parents:
diff changeset
187 return NULL_TREE;
kono
parents:
diff changeset
188 }
kono
parents:
diff changeset
189 return build_int_cstu (operand_type, gccbrig_get_target_wavesize ());
kono
parents:
diff changeset
190 }
kono
parents:
diff changeset
191 case BRIG_KIND_OPERAND_CODE_REF:
kono
parents:
diff changeset
192 {
kono
parents:
diff changeset
193 const BrigOperandCodeRef *brig_code_ref
kono
parents:
diff changeset
194 = (const BrigOperandCodeRef *) &operand;
kono
parents:
diff changeset
195
kono
parents:
diff changeset
196 const BrigBase *ref = m_parent.get_brig_code_entry (brig_code_ref->ref);
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 return build_code_ref (*ref);
kono
parents:
diff changeset
199 }
kono
parents:
diff changeset
200 case BRIG_KIND_OPERAND_ADDRESS:
kono
parents:
diff changeset
201 {
kono
parents:
diff changeset
202 return build_address_operand (brig_inst,
kono
parents:
diff changeset
203 (const BrigOperandAddress &) operand);
kono
parents:
diff changeset
204 }
kono
parents:
diff changeset
205 default:
kono
parents:
diff changeset
206 gcc_unreachable ();
kono
parents:
diff changeset
207 }
kono
parents:
diff changeset
208 }
kono
parents:
diff changeset
209
kono
parents:
diff changeset
210 /* Build a tree node representing an address reference from a BRIG_INST and its
kono
parents:
diff changeset
211 ADDR_OPERAND. */
kono
parents:
diff changeset
212
kono
parents:
diff changeset
213 tree
kono
parents:
diff changeset
214 brig_code_entry_handler::build_address_operand
kono
parents:
diff changeset
215 (const BrigInstBase &brig_inst, const BrigOperandAddress &addr_operand)
kono
parents:
diff changeset
216 {
kono
parents:
diff changeset
217 tree instr_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 BrigSegment8_t segment = BRIG_SEGMENT_GLOBAL;
kono
parents:
diff changeset
220 if (brig_inst.opcode == BRIG_OPCODE_LDA)
kono
parents:
diff changeset
221 segment = ((const BrigInstAddr &) brig_inst).segment;
kono
parents:
diff changeset
222 else if (brig_inst.base.kind == BRIG_KIND_INST_MEM)
kono
parents:
diff changeset
223 segment = ((const BrigInstMem &) brig_inst).segment;
kono
parents:
diff changeset
224 else if (brig_inst.base.kind == BRIG_KIND_INST_ATOMIC)
kono
parents:
diff changeset
225 segment = ((const BrigInstAtomic &) brig_inst).segment;
kono
parents:
diff changeset
226
kono
parents:
diff changeset
227 tree var_offset = NULL_TREE;
kono
parents:
diff changeset
228 tree const_offset = NULL_TREE;
kono
parents:
diff changeset
229 tree symbol_base = NULL_TREE;
kono
parents:
diff changeset
230
kono
parents:
diff changeset
231 if (addr_operand.symbol != 0)
kono
parents:
diff changeset
232 {
kono
parents:
diff changeset
233 const BrigDirectiveVariable *arg_symbol
kono
parents:
diff changeset
234 = (const BrigDirectiveVariable *) m_parent.get_brig_code_entry
kono
parents:
diff changeset
235 (addr_operand.symbol);
kono
parents:
diff changeset
236
kono
parents:
diff changeset
237 std::string var_name = m_parent.get_mangled_name (arg_symbol);
kono
parents:
diff changeset
238
kono
parents:
diff changeset
239 if (segment == BRIG_SEGMENT_KERNARG)
kono
parents:
diff changeset
240 {
kono
parents:
diff changeset
241 /* Find the offset to the kernarg buffer for the given
kono
parents:
diff changeset
242 kernel argument variable. */
kono
parents:
diff changeset
243 tree func = m_parent.m_cf->m_func_decl;
kono
parents:
diff changeset
244 /* __args is the first parameter in kernel functions. */
kono
parents:
diff changeset
245 symbol_base = DECL_ARGUMENTS (func);
kono
parents:
diff changeset
246 uint64_t offset = m_parent.m_cf->kernel_arg_offset (arg_symbol);
kono
parents:
diff changeset
247 if (offset > 0)
kono
parents:
diff changeset
248 const_offset = build_int_cst (size_type_node, offset);
kono
parents:
diff changeset
249 }
kono
parents:
diff changeset
250 else if (segment == BRIG_SEGMENT_GROUP)
kono
parents:
diff changeset
251 {
kono
parents:
diff changeset
252 uint64_t offset
kono
parents:
diff changeset
253 = m_parent.m_cf->group_variable_segment_offset (var_name);
kono
parents:
diff changeset
254 const_offset = build_int_cst (size_type_node, offset);
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 /* If it's a local group variable reference, substract the local
kono
parents:
diff changeset
257 group segment offset to get the group base ptr offset. */
kono
parents:
diff changeset
258 if (m_parent.m_cf->m_local_group_variables.has_variable (var_name))
kono
parents:
diff changeset
259 const_offset
kono
parents:
diff changeset
260 = build2 (PLUS_EXPR, uint64_type_node, const_offset,
kono
parents:
diff changeset
261 convert (uint64_type_node,
kono
parents:
diff changeset
262 m_parent.m_cf->m_group_local_offset_arg));
kono
parents:
diff changeset
263
kono
parents:
diff changeset
264 }
kono
parents:
diff changeset
265 else if (segment == BRIG_SEGMENT_PRIVATE || segment == BRIG_SEGMENT_SPILL)
kono
parents:
diff changeset
266 {
kono
parents:
diff changeset
267 uint32_t offset = m_parent.private_variable_segment_offset (var_name);
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 /* Compute the offset to the work item's copy:
kono
parents:
diff changeset
270
kono
parents:
diff changeset
271 single-wi-offset * local_size + wiflatid * varsize
kono
parents:
diff changeset
272
kono
parents:
diff changeset
273 This way the work items have the same variable in
kono
parents:
diff changeset
274 successive elements to each other in the segment,
kono
parents:
diff changeset
275 helping to achieve autovectorization of loads/stores
kono
parents:
diff changeset
276 with stride 1. */
kono
parents:
diff changeset
277
kono
parents:
diff changeset
278 tree_stl_vec uint32_0
kono
parents:
diff changeset
279 = tree_stl_vec (1, build_int_cst (uint32_type_node, 0));
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 tree_stl_vec uint32_1
kono
parents:
diff changeset
282 = tree_stl_vec (1, build_int_cst (uint32_type_node, 1));
kono
parents:
diff changeset
283
kono
parents:
diff changeset
284 tree_stl_vec uint32_2
kono
parents:
diff changeset
285 = tree_stl_vec (1, build_int_cst (uint32_type_node, 2));
kono
parents:
diff changeset
286
kono
parents:
diff changeset
287 tree local_size
kono
parents:
diff changeset
288 = build2 (MULT_EXPR, uint32_type_node,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
289 m_parent.m_cf->expand_or_call_builtin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
290 (BRIG_OPCODE_WORKGROUPSIZE, BRIG_TYPE_U32,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
291 uint32_type_node, uint32_0),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
292 m_parent.m_cf->expand_or_call_builtin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
293 (BRIG_OPCODE_WORKGROUPSIZE, BRIG_TYPE_U32,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
294 uint32_type_node, uint32_1));
111
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 local_size
kono
parents:
diff changeset
297 = build2 (MULT_EXPR, uint32_type_node,
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
298 m_parent.m_cf->expand_or_call_builtin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
299 (BRIG_OPCODE_WORKGROUPSIZE, BRIG_TYPE_U32,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
300 uint32_type_node, uint32_2),
111
kono
parents:
diff changeset
301 local_size);
kono
parents:
diff changeset
302
kono
parents:
diff changeset
303 tree var_region
kono
parents:
diff changeset
304 = build2 (MULT_EXPR, uint32_type_node,
kono
parents:
diff changeset
305 build_int_cst (uint32_type_node, offset), local_size);
kono
parents:
diff changeset
306
kono
parents:
diff changeset
307 tree_stl_vec operands;
kono
parents:
diff changeset
308 tree pos
kono
parents:
diff changeset
309 = build2 (MULT_EXPR, uint32_type_node,
kono
parents:
diff changeset
310 build_int_cst (uint32_type_node,
kono
parents:
diff changeset
311 m_parent.private_variable_size (var_name)),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
312 m_parent.m_cf->expand_or_call_builtin
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
313 (BRIG_OPCODE_WORKITEMFLATID, BRIG_TYPE_U32,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
314 uint32_type_node, operands));
111
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 tree var_offset
kono
parents:
diff changeset
317 = build2 (PLUS_EXPR, uint32_type_node, var_region, pos);
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 /* In case of LDA this is returned directly as an integer value.
kono
parents:
diff changeset
320 For other mem-related instructions, we will convert this segment
kono
parents:
diff changeset
321 offset to a flat address by adding it as an offset to a (private
kono
parents:
diff changeset
322 or group) base pointer later on. Same applies to group_var_offset. */
kono
parents:
diff changeset
323 symbol_base
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
324 = m_parent.m_cf->add_temp_var ("priv_var_offset",
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
325 convert (size_type_node,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
326 var_offset));
111
kono
parents:
diff changeset
327 }
kono
parents:
diff changeset
328 else if (segment == BRIG_SEGMENT_ARG)
kono
parents:
diff changeset
329 {
kono
parents:
diff changeset
330 tree arg_var_decl;
kono
parents:
diff changeset
331 if (m_parent.m_cf->m_ret_value_brig_var == arg_symbol)
kono
parents:
diff changeset
332 arg_var_decl = m_parent.m_cf->m_ret_temp;
kono
parents:
diff changeset
333 else
kono
parents:
diff changeset
334 arg_var_decl = m_parent.m_cf->arg_variable (arg_symbol);
kono
parents:
diff changeset
335
kono
parents:
diff changeset
336 gcc_assert (arg_var_decl != NULL_TREE);
kono
parents:
diff changeset
337
kono
parents:
diff changeset
338 tree ptype = build_pointer_type (instr_type);
kono
parents:
diff changeset
339
kono
parents:
diff changeset
340 if (arg_symbol->type & BRIG_TYPE_ARRAY)
kono
parents:
diff changeset
341 {
kono
parents:
diff changeset
342
kono
parents:
diff changeset
343 /* Two different type of array references in case of arguments
kono
parents:
diff changeset
344 depending where they are referred at. In the caller (argument
kono
parents:
diff changeset
345 segment), the reference is to an array object and
kono
parents:
diff changeset
346 in the callee, the array object has been passed as a pointer
kono
parents:
diff changeset
347 to the array object. */
kono
parents:
diff changeset
348
kono
parents:
diff changeset
349 if (POINTER_TYPE_P (TREE_TYPE (arg_var_decl)))
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
350 symbol_base = build_resize_convert_view (ptype, arg_var_decl);
111
kono
parents:
diff changeset
351 else
kono
parents:
diff changeset
352 {
kono
parents:
diff changeset
353 /* In case we are referring to an array (the argument in
kono
parents:
diff changeset
354 call site), use its element zero as the base address. */
kono
parents:
diff changeset
355 tree element_zero
kono
parents:
diff changeset
356 = build4 (ARRAY_REF, TREE_TYPE (TREE_TYPE (arg_var_decl)),
kono
parents:
diff changeset
357 arg_var_decl, integer_zero_node, NULL_TREE,
kono
parents:
diff changeset
358 NULL_TREE);
kono
parents:
diff changeset
359 symbol_base = build1 (ADDR_EXPR, ptype, element_zero);
kono
parents:
diff changeset
360 }
kono
parents:
diff changeset
361 }
kono
parents:
diff changeset
362 else
kono
parents:
diff changeset
363 symbol_base = build1 (ADDR_EXPR, ptype, arg_var_decl);
kono
parents:
diff changeset
364 }
kono
parents:
diff changeset
365 else
kono
parents:
diff changeset
366 {
kono
parents:
diff changeset
367 tree global_var_decl = m_parent.global_variable (var_name);
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 /* In case the global variable hasn't been defined (yet),
kono
parents:
diff changeset
370 use the host def indirection ptr variable. */
kono
parents:
diff changeset
371 if (global_var_decl == NULL_TREE)
kono
parents:
diff changeset
372 {
kono
parents:
diff changeset
373 std::string host_ptr_name
kono
parents:
diff changeset
374 = std::string (PHSA_HOST_DEF_PTR_PREFIX) + var_name;
kono
parents:
diff changeset
375 tree host_defined_ptr = m_parent.global_variable (host_ptr_name);
kono
parents:
diff changeset
376 gcc_assert (host_defined_ptr != NULL_TREE);
kono
parents:
diff changeset
377 symbol_base = host_defined_ptr;
kono
parents:
diff changeset
378 }
kono
parents:
diff changeset
379 else
kono
parents:
diff changeset
380 {
kono
parents:
diff changeset
381 gcc_assert (global_var_decl != NULL_TREE);
kono
parents:
diff changeset
382
kono
parents:
diff changeset
383 tree ptype = build_pointer_type (instr_type);
kono
parents:
diff changeset
384 symbol_base = build1 (ADDR_EXPR, ptype, global_var_decl);
kono
parents:
diff changeset
385 }
kono
parents:
diff changeset
386 }
kono
parents:
diff changeset
387 }
kono
parents:
diff changeset
388
kono
parents:
diff changeset
389 if (brig_inst.opcode != BRIG_OPCODE_LDA)
kono
parents:
diff changeset
390 {
kono
parents:
diff changeset
391 /* In case of lda_* we want to return the segment address because it's
kono
parents:
diff changeset
392 used as a value, perhaps in address computation and later converted
kono
parents:
diff changeset
393 explicitly to a flat address.
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 In case of other instructions with memory operands we produce the flat
kono
parents:
diff changeset
396 address directly here (assuming the target does not have a separate
kono
parents:
diff changeset
397 address space for group/private segments for now). */
kono
parents:
diff changeset
398 if (segment == BRIG_SEGMENT_GROUP)
kono
parents:
diff changeset
399 symbol_base = m_parent.m_cf->m_group_base_arg;
kono
parents:
diff changeset
400 else if (segment == BRIG_SEGMENT_PRIVATE
kono
parents:
diff changeset
401 || segment == BRIG_SEGMENT_SPILL)
kono
parents:
diff changeset
402 {
kono
parents:
diff changeset
403 if (symbol_base != NULL_TREE)
kono
parents:
diff changeset
404 symbol_base = build2 (POINTER_PLUS_EXPR, ptr_type_node,
kono
parents:
diff changeset
405 m_parent.m_cf->m_private_base_arg,
kono
parents:
diff changeset
406 symbol_base);
kono
parents:
diff changeset
407 else
kono
parents:
diff changeset
408 symbol_base = m_parent.m_cf->m_private_base_arg;
kono
parents:
diff changeset
409 }
kono
parents:
diff changeset
410 }
kono
parents:
diff changeset
411
kono
parents:
diff changeset
412 if (addr_operand.reg != 0)
kono
parents:
diff changeset
413 {
kono
parents:
diff changeset
414 const BrigOperandRegister *mem_base_reg
kono
parents:
diff changeset
415 = (const BrigOperandRegister *) m_parent.get_brig_operand_entry
kono
parents:
diff changeset
416 (addr_operand.reg);
kono
parents:
diff changeset
417 tree base_reg_var = m_parent.m_cf->get_m_var_declfor_reg (mem_base_reg);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
418 tree as_uint = build_reinterpret_to_uint (base_reg_var);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
419 var_offset = convert_to_pointer (ptr_type_node, as_uint);
111
kono
parents:
diff changeset
420
kono
parents:
diff changeset
421 gcc_assert (var_offset != NULL_TREE);
kono
parents:
diff changeset
422 }
kono
parents:
diff changeset
423 /* The pointer type we use to access the memory. Should be of the
kono
parents:
diff changeset
424 width of the load/store instruction, not the target/data
kono
parents:
diff changeset
425 register. */
kono
parents:
diff changeset
426 tree ptype = build_pointer_type (instr_type);
kono
parents:
diff changeset
427
kono
parents:
diff changeset
428 gcc_assert (ptype != NULL_TREE);
kono
parents:
diff changeset
429
kono
parents:
diff changeset
430 tree addr = NULL_TREE;
kono
parents:
diff changeset
431 if (symbol_base != NULL_TREE && var_offset != NULL_TREE)
kono
parents:
diff changeset
432 /* The most complex addressing mode: symbol + reg [+ const offset]. */
kono
parents:
diff changeset
433 addr = build2 (POINTER_PLUS_EXPR, ptr_type_node,
kono
parents:
diff changeset
434 convert (ptr_type_node, symbol_base),
kono
parents:
diff changeset
435 convert (size_type_node, var_offset));
kono
parents:
diff changeset
436 else if (var_offset != NULL)
kono
parents:
diff changeset
437 addr = var_offset;
kono
parents:
diff changeset
438 else if (symbol_base != NULL)
kono
parents:
diff changeset
439 addr = symbol_base;
kono
parents:
diff changeset
440
kono
parents:
diff changeset
441 if (const_offset != NULL_TREE)
kono
parents:
diff changeset
442 {
kono
parents:
diff changeset
443 if (addr == NULL_TREE)
kono
parents:
diff changeset
444 /* At least direct module-scope global group symbol access with LDA
kono
parents:
diff changeset
445 has only the const_offset. Group base ptr is not added as LDA should
kono
parents:
diff changeset
446 return the segment address, not the flattened one. */
kono
parents:
diff changeset
447 addr = const_offset;
kono
parents:
diff changeset
448 else
kono
parents:
diff changeset
449 addr = build2 (POINTER_PLUS_EXPR, ptr_type_node,
kono
parents:
diff changeset
450 addr, convert (size_type_node, const_offset));
kono
parents:
diff changeset
451 }
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 /* We might have two const offsets in case of group or private arrays
kono
parents:
diff changeset
454 which have the first offset to the incoming group/private pointer
kono
parents:
diff changeset
455 arg, and the second one an offset to it. It's also legal to have
kono
parents:
diff changeset
456 a reference with a zero constant offset but no symbol. I've seen
kono
parents:
diff changeset
457 codes that reference kernarg segment like this. Thus, if at this
kono
parents:
diff changeset
458 point there is no address expression at all we assume it's an
kono
parents:
diff changeset
459 access to offset 0. */
kono
parents:
diff changeset
460 uint64_t offs = gccbrig_to_uint64_t (addr_operand.offset);
kono
parents:
diff changeset
461 if (offs > 0 || addr == NULL_TREE)
kono
parents:
diff changeset
462 {
kono
parents:
diff changeset
463 /* In large mode, the offset is treated as 32bits unless it's
kono
parents:
diff changeset
464 global, readonly or kernarg address space.
kono
parents:
diff changeset
465 See:
kono
parents:
diff changeset
466 http://www.hsafoundation.com/html_spec111/HSA_Library.htm
kono
parents:
diff changeset
467 #PRM/Topics/02_ProgModel/small_and_large_machine_models.htm
kono
parents:
diff changeset
468 #table_machine_model_data_sizes */
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 int is64b_offset = segment == BRIG_SEGMENT_GLOBAL
kono
parents:
diff changeset
471 || segment == BRIG_SEGMENT_READONLY
kono
parents:
diff changeset
472 || segment == BRIG_SEGMENT_KERNARG;
kono
parents:
diff changeset
473
kono
parents:
diff changeset
474 /* The original offset is signed and should be sign
kono
parents:
diff changeset
475 extended for the pointer arithmetics. */
kono
parents:
diff changeset
476 tree const_offset_2 = is64b_offset
kono
parents:
diff changeset
477 ? build_int_cst (size_type_node, offs)
kono
parents:
diff changeset
478 : convert (long_integer_type_node,
kono
parents:
diff changeset
479 build_int_cst (integer_type_node, offs));
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 if (addr == NULL_TREE)
kono
parents:
diff changeset
482 addr = const_offset_2;
kono
parents:
diff changeset
483 else
kono
parents:
diff changeset
484 addr = build2 (POINTER_PLUS_EXPR, ptr_type_node,
kono
parents:
diff changeset
485 /* Addr can be a constant offset in case this is
kono
parents:
diff changeset
486 a private array access. */
kono
parents:
diff changeset
487 convert (ptr_type_node, addr),
kono
parents:
diff changeset
488 convert (size_type_node, const_offset_2));
kono
parents:
diff changeset
489 }
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 gcc_assert (addr != NULL_TREE);
kono
parents:
diff changeset
492 return convert_to_pointer (ptype, addr);
kono
parents:
diff changeset
493 }
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 /* Builds a tree operand with the given OPERAND_INDEX for the given
kono
parents:
diff changeset
496 BRIG_INST with the desired tree OPERAND_TYPE. OPERAND_TYPE can
kono
parents:
diff changeset
497 be NULL in case the type is forced by the BRIG_INST type. */
kono
parents:
diff changeset
498
kono
parents:
diff changeset
499 tree
kono
parents:
diff changeset
500 brig_code_entry_handler::build_tree_operand_from_brig
kono
parents:
diff changeset
501 (const BrigInstBase *brig_inst, tree operand_type, size_t operand_index)
kono
parents:
diff changeset
502 {
kono
parents:
diff changeset
503 const BrigData *operand_entries
kono
parents:
diff changeset
504 = m_parent.get_brig_data_entry (brig_inst->operands);
kono
parents:
diff changeset
505
kono
parents:
diff changeset
506 uint32_t operand_offset
kono
parents:
diff changeset
507 = ((const uint32_t *) &operand_entries->bytes)[operand_index];
kono
parents:
diff changeset
508 const BrigBase *operand_data
kono
parents:
diff changeset
509 = m_parent.get_brig_operand_entry (operand_offset);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
510
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
511 bool inputp = !gccbrig_hsa_opcode_op_output_p (brig_inst->opcode,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
512 operand_index);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
513 return build_tree_operand (*brig_inst, *operand_data, operand_type, inputp);
111
kono
parents:
diff changeset
514 }
kono
parents:
diff changeset
515
kono
parents:
diff changeset
516 /* Builds a single (scalar) constant initialized element of type
kono
parents:
diff changeset
517 ELEMENT_TYPE from the buffer pointed to by NEXT_DATA. */
kono
parents:
diff changeset
518
kono
parents:
diff changeset
519 tree
kono
parents:
diff changeset
520 brig_code_entry_handler::build_tree_cst_element
kono
parents:
diff changeset
521 (BrigType16_t element_type, const unsigned char *next_data) const
kono
parents:
diff changeset
522 {
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 tree tree_element_type = gccbrig_tree_type_for_hsa_type (element_type);
kono
parents:
diff changeset
525
kono
parents:
diff changeset
526 tree cst;
kono
parents:
diff changeset
527 switch (element_type)
kono
parents:
diff changeset
528 {
kono
parents:
diff changeset
529 case BRIG_TYPE_F16:
kono
parents:
diff changeset
530 {
kono
parents:
diff changeset
531 HOST_WIDE_INT low = *(const uint16_t *) next_data;
kono
parents:
diff changeset
532 cst = build_int_cst (uint16_type_node, low);
kono
parents:
diff changeset
533 break;
kono
parents:
diff changeset
534 }
kono
parents:
diff changeset
535 case BRIG_TYPE_F32:
kono
parents:
diff changeset
536 {
kono
parents:
diff changeset
537 REAL_VALUE_TYPE val;
kono
parents:
diff changeset
538 ieee_single_format.decode (&ieee_single_format, &val,
kono
parents:
diff changeset
539 (const long *) next_data);
kono
parents:
diff changeset
540 cst = build_real (tree_element_type, val);
kono
parents:
diff changeset
541 break;
kono
parents:
diff changeset
542 }
kono
parents:
diff changeset
543 case BRIG_TYPE_F64:
kono
parents:
diff changeset
544 {
kono
parents:
diff changeset
545 long data[2];
kono
parents:
diff changeset
546 data[0] = *(const uint32_t *) next_data;
kono
parents:
diff changeset
547 data[1] = *(const uint32_t *) (next_data + 4);
kono
parents:
diff changeset
548 REAL_VALUE_TYPE val;
kono
parents:
diff changeset
549 ieee_double_format.decode (&ieee_double_format, &val, data);
kono
parents:
diff changeset
550 cst = build_real (tree_element_type, val);
kono
parents:
diff changeset
551 break;
kono
parents:
diff changeset
552 }
kono
parents:
diff changeset
553 case BRIG_TYPE_S8:
kono
parents:
diff changeset
554 case BRIG_TYPE_S16:
kono
parents:
diff changeset
555 case BRIG_TYPE_S32:
kono
parents:
diff changeset
556 case BRIG_TYPE_S64:
kono
parents:
diff changeset
557 {
kono
parents:
diff changeset
558 HOST_WIDE_INT low = *(const int64_t *) next_data;
kono
parents:
diff changeset
559 cst = build_int_cst (tree_element_type, low);
kono
parents:
diff changeset
560 break;
kono
parents:
diff changeset
561 }
kono
parents:
diff changeset
562 case BRIG_TYPE_U8:
kono
parents:
diff changeset
563 case BRIG_TYPE_U16:
kono
parents:
diff changeset
564 case BRIG_TYPE_U32:
kono
parents:
diff changeset
565 case BRIG_TYPE_U64:
kono
parents:
diff changeset
566 {
kono
parents:
diff changeset
567 unsigned HOST_WIDE_INT low = *(const uint64_t *) next_data;
kono
parents:
diff changeset
568 cst = build_int_cstu (tree_element_type, low);
kono
parents:
diff changeset
569 break;
kono
parents:
diff changeset
570 }
kono
parents:
diff changeset
571 case BRIG_TYPE_SIG64:
kono
parents:
diff changeset
572 {
kono
parents:
diff changeset
573 unsigned HOST_WIDE_INT low = *(const uint64_t *) next_data;
kono
parents:
diff changeset
574 cst = build_int_cstu (uint64_type_node, low);
kono
parents:
diff changeset
575 break;
kono
parents:
diff changeset
576 }
kono
parents:
diff changeset
577 case BRIG_TYPE_SIG32:
kono
parents:
diff changeset
578 {
kono
parents:
diff changeset
579 unsigned HOST_WIDE_INT low = *(const uint64_t *) next_data;
kono
parents:
diff changeset
580 cst = build_int_cstu (uint32_type_node, low);
kono
parents:
diff changeset
581 break;
kono
parents:
diff changeset
582 }
kono
parents:
diff changeset
583 default:
kono
parents:
diff changeset
584 gcc_unreachable ();
kono
parents:
diff changeset
585 return NULL_TREE;
kono
parents:
diff changeset
586 }
kono
parents:
diff changeset
587 return cst;
kono
parents:
diff changeset
588 }
kono
parents:
diff changeset
589
kono
parents:
diff changeset
590 /* Produce a tree constant type for the given BRIG constant (BRIG_CONST).
kono
parents:
diff changeset
591 TYPE should be the forced instruction type, otherwise the type is
kono
parents:
diff changeset
592 dictated by the BRIG_CONST. */
kono
parents:
diff changeset
593
kono
parents:
diff changeset
594 tree
kono
parents:
diff changeset
595 brig_code_entry_handler::get_tree_cst_for_hsa_operand
kono
parents:
diff changeset
596 (const BrigOperandConstantBytes *brig_const, tree type) const
kono
parents:
diff changeset
597 {
kono
parents:
diff changeset
598 const BrigData *data = m_parent.get_brig_data_entry (brig_const->bytes);
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 tree cst = NULL_TREE;
kono
parents:
diff changeset
601
kono
parents:
diff changeset
602 if (type == NULL_TREE)
kono
parents:
diff changeset
603 type = gccbrig_tree_type_for_hsa_type (brig_const->type);
kono
parents:
diff changeset
604
kono
parents:
diff changeset
605 /* The type of a single (scalar) element inside an array,
kono
parents:
diff changeset
606 vector or an array of vectors. */
kono
parents:
diff changeset
607 BrigType16_t scalar_element_type
kono
parents:
diff changeset
608 = brig_const->type & BRIG_TYPE_BASE_MASK;
kono
parents:
diff changeset
609 tree tree_element_type = type;
kono
parents:
diff changeset
610
kono
parents:
diff changeset
611 vec<constructor_elt, va_gc> *constructor_vals = NULL;
kono
parents:
diff changeset
612
kono
parents:
diff changeset
613 if (TREE_CODE (type) == ARRAY_TYPE)
kono
parents:
diff changeset
614 tree_element_type = TREE_TYPE (type);
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 size_t bytes_left = data->byteCount;
kono
parents:
diff changeset
617 const unsigned char *next_data = data->bytes;
kono
parents:
diff changeset
618 size_t scalar_element_size
kono
parents:
diff changeset
619 = gccbrig_hsa_type_bit_size (scalar_element_type) / BITS_PER_UNIT;
kono
parents:
diff changeset
620
kono
parents:
diff changeset
621 while (bytes_left > 0)
kono
parents:
diff changeset
622 {
kono
parents:
diff changeset
623 if (VECTOR_TYPE_P (tree_element_type))
kono
parents:
diff changeset
624 {
kono
parents:
diff changeset
625 /* In case of vector type elements (or sole vectors),
kono
parents:
diff changeset
626 create a vector ctor. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
627 size_t element_count
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
628 = gccbrig_type_vector_subparts (tree_element_type);
111
kono
parents:
diff changeset
629 if (bytes_left < scalar_element_size * element_count)
kono
parents:
diff changeset
630 fatal_error (UNKNOWN_LOCATION,
kono
parents:
diff changeset
631 "Not enough bytes left for the initializer "
kono
parents:
diff changeset
632 "(%lu need %lu).", (unsigned long) bytes_left,
kono
parents:
diff changeset
633 (unsigned long) (scalar_element_size
kono
parents:
diff changeset
634 * element_count));
kono
parents:
diff changeset
635
kono
parents:
diff changeset
636 vec<constructor_elt, va_gc> *vec_els = NULL;
kono
parents:
diff changeset
637 for (size_t i = 0; i < element_count; ++i)
kono
parents:
diff changeset
638 {
kono
parents:
diff changeset
639 tree element
kono
parents:
diff changeset
640 = build_tree_cst_element (scalar_element_type, next_data);
kono
parents:
diff changeset
641 CONSTRUCTOR_APPEND_ELT (vec_els, NULL_TREE, element);
kono
parents:
diff changeset
642 bytes_left -= scalar_element_size;
kono
parents:
diff changeset
643 next_data += scalar_element_size;
kono
parents:
diff changeset
644 }
kono
parents:
diff changeset
645 cst = build_vector_from_ctor (tree_element_type, vec_els);
kono
parents:
diff changeset
646 }
kono
parents:
diff changeset
647 else
kono
parents:
diff changeset
648 {
kono
parents:
diff changeset
649 if (bytes_left < scalar_element_size)
kono
parents:
diff changeset
650 fatal_error (UNKNOWN_LOCATION,
kono
parents:
diff changeset
651 "Not enough bytes left for the initializer "
kono
parents:
diff changeset
652 "(%lu need %lu).", (unsigned long) bytes_left,
kono
parents:
diff changeset
653 (unsigned long) scalar_element_size);
kono
parents:
diff changeset
654 cst = build_tree_cst_element (scalar_element_type, next_data);
kono
parents:
diff changeset
655 bytes_left -= scalar_element_size;
kono
parents:
diff changeset
656 next_data += scalar_element_size;
kono
parents:
diff changeset
657 }
kono
parents:
diff changeset
658 CONSTRUCTOR_APPEND_ELT (constructor_vals, NULL_TREE, cst);
kono
parents:
diff changeset
659 }
kono
parents:
diff changeset
660
kono
parents:
diff changeset
661 if (TREE_CODE (type) == ARRAY_TYPE)
kono
parents:
diff changeset
662 return build_constructor (type, constructor_vals);
kono
parents:
diff changeset
663 else
kono
parents:
diff changeset
664 return cst;
kono
parents:
diff changeset
665 }
kono
parents:
diff changeset
666
kono
parents:
diff changeset
667 /* Return the matching tree instruction arithmetics type for the
kono
parents:
diff changeset
668 given BRIG_TYPE. The aritmethics type is the one with which
kono
parents:
diff changeset
669 computation is done (in contrast to the storage type). F16
kono
parents:
diff changeset
670 arithmetics type is emulated using F32 for now. */
kono
parents:
diff changeset
671
kono
parents:
diff changeset
672 tree
kono
parents:
diff changeset
673 brig_code_entry_handler::get_tree_expr_type_for_hsa_type
kono
parents:
diff changeset
674 (BrigType16_t brig_type) const
kono
parents:
diff changeset
675 {
kono
parents:
diff changeset
676 BrigType16_t brig_inner_type = brig_type & BRIG_TYPE_BASE_MASK;
kono
parents:
diff changeset
677 if (brig_inner_type == BRIG_TYPE_F16)
kono
parents:
diff changeset
678 {
kono
parents:
diff changeset
679 if (brig_inner_type == brig_type)
kono
parents:
diff changeset
680 return m_parent.s_fp32_type;
kono
parents:
diff changeset
681 size_t element_count = gccbrig_hsa_type_bit_size (brig_type) / 16;
kono
parents:
diff changeset
682 return build_vector_type (m_parent.s_fp32_type, element_count);
kono
parents:
diff changeset
683 }
kono
parents:
diff changeset
684 else
kono
parents:
diff changeset
685 return gccbrig_tree_type_for_hsa_type (brig_type);
kono
parents:
diff changeset
686 }
kono
parents:
diff changeset
687
kono
parents:
diff changeset
688 /* Return the correct GENERIC type for storing comparison results
kono
parents:
diff changeset
689 of operand with the type given in SOURCE_TYPE. */
kono
parents:
diff changeset
690
kono
parents:
diff changeset
691 tree
kono
parents:
diff changeset
692 brig_code_entry_handler::get_comparison_result_type (tree source_type)
kono
parents:
diff changeset
693 {
kono
parents:
diff changeset
694 if (VECTOR_TYPE_P (source_type))
kono
parents:
diff changeset
695 {
kono
parents:
diff changeset
696 size_t element_size = int_size_in_bytes (TREE_TYPE (source_type));
kono
parents:
diff changeset
697 return build_vector_type
kono
parents:
diff changeset
698 (build_nonstandard_boolean_type (element_size * BITS_PER_UNIT),
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
699 gccbrig_type_vector_subparts (source_type));
111
kono
parents:
diff changeset
700 }
kono
parents:
diff changeset
701 else
kono
parents:
diff changeset
702 return gccbrig_tree_type_for_hsa_type (BRIG_TYPE_B1);
kono
parents:
diff changeset
703 }
kono
parents:
diff changeset
704
kono
parents:
diff changeset
705 /* Creates a FP32 to FP16 conversion call, assuming the source and destination
kono
parents:
diff changeset
706 are FP32 type variables. */
kono
parents:
diff changeset
707
kono
parents:
diff changeset
708 tree
kono
parents:
diff changeset
709 brig_code_entry_handler::build_f2h_conversion (tree source)
kono
parents:
diff changeset
710 {
kono
parents:
diff changeset
711 return float_to_half () (*this, source);
kono
parents:
diff changeset
712 }
kono
parents:
diff changeset
713
kono
parents:
diff changeset
714 /* Creates a FP16 to FP32 conversion call, assuming the source and destination
kono
parents:
diff changeset
715 are FP32 type variables. */
kono
parents:
diff changeset
716
kono
parents:
diff changeset
717 tree
kono
parents:
diff changeset
718 brig_code_entry_handler::build_h2f_conversion (tree source)
kono
parents:
diff changeset
719 {
kono
parents:
diff changeset
720 return half_to_float () (*this, source);
kono
parents:
diff changeset
721 }
kono
parents:
diff changeset
722
kono
parents:
diff changeset
723 /* Builds and "normalizes" the dest and source operands for the instruction
kono
parents:
diff changeset
724 execution; converts the input operands to the expected instruction type,
kono
parents:
diff changeset
725 performs half to float conversions, constant to correct type variable,
kono
parents:
diff changeset
726 and flush to zero (if applicable). */
kono
parents:
diff changeset
727
kono
parents:
diff changeset
728 tree_stl_vec
kono
parents:
diff changeset
729 brig_code_entry_handler::build_operands (const BrigInstBase &brig_inst)
kono
parents:
diff changeset
730 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
731 return build_or_analyze_operands (brig_inst, false);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
732 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
733
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
734 void
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
735 brig_code_entry_handler::analyze_operands (const BrigInstBase &brig_inst)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
736 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
737 build_or_analyze_operands (brig_inst, true);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
738 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
739
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
740 /* Implements both the build_operands () and analyze_operands () call
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
741 so changes go in tandem. Performs build_operands () when ANALYZE
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
742 is false. Otherwise, only analyze operands and return empty
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
743 list.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
744
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
745 If analyzing record each HSA register operand with the
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
746 corresponding resolved operand tree type to
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
747 brig_to_generic::m_fn_regs_use_index. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
748
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
749 tree_stl_vec
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
750 brig_code_entry_handler::
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
751 build_or_analyze_operands (const BrigInstBase &brig_inst, bool analyze)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
752 {
111
kono
parents:
diff changeset
753 /* Flush to zero. */
kono
parents:
diff changeset
754 bool ftz = false;
kono
parents:
diff changeset
755 const BrigBase *base = &brig_inst.base;
kono
parents:
diff changeset
756
kono
parents:
diff changeset
757 if (base->kind == BRIG_KIND_INST_MOD)
kono
parents:
diff changeset
758 {
kono
parents:
diff changeset
759 const BrigInstMod *mod = (const BrigInstMod *) base;
kono
parents:
diff changeset
760 ftz = mod->modifier & BRIG_ALU_FTZ;
kono
parents:
diff changeset
761 }
kono
parents:
diff changeset
762 else if (base->kind == BRIG_KIND_INST_CMP)
kono
parents:
diff changeset
763 {
kono
parents:
diff changeset
764 const BrigInstCmp *cmp = (const BrigInstCmp *) base;
kono
parents:
diff changeset
765 ftz = cmp->modifier & BRIG_ALU_FTZ;
kono
parents:
diff changeset
766 }
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 bool is_vec_instr = hsa_type_packed_p (brig_inst.type);
kono
parents:
diff changeset
769
kono
parents:
diff changeset
770 size_t element_count;
kono
parents:
diff changeset
771 if (is_vec_instr)
kono
parents:
diff changeset
772 {
kono
parents:
diff changeset
773 BrigType16_t brig_element_type = brig_inst.type & BRIG_TYPE_BASE_MASK;
kono
parents:
diff changeset
774 element_count = gccbrig_hsa_type_bit_size (brig_inst.type)
kono
parents:
diff changeset
775 / gccbrig_hsa_type_bit_size (brig_element_type);
kono
parents:
diff changeset
776 }
kono
parents:
diff changeset
777 else
kono
parents:
diff changeset
778 element_count = 1;
kono
parents:
diff changeset
779
kono
parents:
diff changeset
780 bool is_fp16_arith = false;
kono
parents:
diff changeset
781
kono
parents:
diff changeset
782 tree src_type;
kono
parents:
diff changeset
783 tree dest_type;
kono
parents:
diff changeset
784 if (base->kind == BRIG_KIND_INST_CMP)
kono
parents:
diff changeset
785 {
kono
parents:
diff changeset
786 const BrigInstCmp *cmp_inst = (const BrigInstCmp *) base;
kono
parents:
diff changeset
787 src_type = gccbrig_tree_type_for_hsa_type (cmp_inst->sourceType);
kono
parents:
diff changeset
788 dest_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
789 is_fp16_arith
kono
parents:
diff changeset
790 = (cmp_inst->sourceType & BRIG_TYPE_BASE_MASK) == BRIG_TYPE_F16;
kono
parents:
diff changeset
791 }
kono
parents:
diff changeset
792 else if (base->kind == BRIG_KIND_INST_SOURCE_TYPE)
kono
parents:
diff changeset
793 {
kono
parents:
diff changeset
794 const BrigInstSourceType *src_type_inst
kono
parents:
diff changeset
795 = (const BrigInstSourceType *) base;
kono
parents:
diff changeset
796 src_type = gccbrig_tree_type_for_hsa_type (src_type_inst->sourceType);
kono
parents:
diff changeset
797 dest_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
798 is_fp16_arith
kono
parents:
diff changeset
799 = (src_type_inst->sourceType & BRIG_TYPE_BASE_MASK) == BRIG_TYPE_F16
kono
parents:
diff changeset
800 && !gccbrig_is_bit_operation (brig_inst.opcode);
kono
parents:
diff changeset
801 }
kono
parents:
diff changeset
802 else if (base->kind == BRIG_KIND_INST_SEG_CVT)
kono
parents:
diff changeset
803 {
kono
parents:
diff changeset
804 const BrigInstSegCvt *seg_cvt_inst = (const BrigInstSegCvt *) base;
kono
parents:
diff changeset
805 src_type = gccbrig_tree_type_for_hsa_type (seg_cvt_inst->sourceType);
kono
parents:
diff changeset
806 dest_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
807 }
kono
parents:
diff changeset
808 else if (base->kind == BRIG_KIND_INST_MEM)
kono
parents:
diff changeset
809 {
kono
parents:
diff changeset
810 src_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
811 dest_type = src_type;
kono
parents:
diff changeset
812 /* With mem instructions we don't want to cast the fp16
kono
parents:
diff changeset
813 back and forth between fp32, because the load/stores
kono
parents:
diff changeset
814 are not specific to the data type. */
kono
parents:
diff changeset
815 is_fp16_arith = false;
kono
parents:
diff changeset
816 }
kono
parents:
diff changeset
817 else if (base->kind == BRIG_KIND_INST_CVT)
kono
parents:
diff changeset
818 {
kono
parents:
diff changeset
819 const BrigInstCvt *cvt_inst = (const BrigInstCvt *) base;
kono
parents:
diff changeset
820
kono
parents:
diff changeset
821 src_type = gccbrig_tree_type_for_hsa_type (cvt_inst->sourceType);
kono
parents:
diff changeset
822 dest_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
823 }
kono
parents:
diff changeset
824 else
kono
parents:
diff changeset
825 {
kono
parents:
diff changeset
826 switch (brig_inst.opcode)
kono
parents:
diff changeset
827 {
kono
parents:
diff changeset
828 case BRIG_OPCODE_INITFBAR:
kono
parents:
diff changeset
829 case BRIG_OPCODE_JOINFBAR:
kono
parents:
diff changeset
830 case BRIG_OPCODE_WAITFBAR:
kono
parents:
diff changeset
831 case BRIG_OPCODE_ARRIVEFBAR:
kono
parents:
diff changeset
832 case BRIG_OPCODE_LEAVEFBAR:
kono
parents:
diff changeset
833 case BRIG_OPCODE_RELEASEFBAR:
kono
parents:
diff changeset
834 src_type = uint32_type_node;
kono
parents:
diff changeset
835 break;
kono
parents:
diff changeset
836 default:
kono
parents:
diff changeset
837 src_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
838 break;
kono
parents:
diff changeset
839 }
kono
parents:
diff changeset
840 dest_type = src_type;
kono
parents:
diff changeset
841 is_fp16_arith
kono
parents:
diff changeset
842 = !gccbrig_is_bit_operation (brig_inst.opcode)
kono
parents:
diff changeset
843 && (brig_inst.type & BRIG_TYPE_BASE_MASK) == BRIG_TYPE_F16;
kono
parents:
diff changeset
844 }
kono
parents:
diff changeset
845
kono
parents:
diff changeset
846 /* Halfs are a tricky special case: their "storage format" is u16, but
kono
parents:
diff changeset
847 scalars are stored in 32b regs while packed f16 are... well packed. */
kono
parents:
diff changeset
848 tree half_storage_type = element_count > 1
kono
parents:
diff changeset
849 ? gccbrig_tree_type_for_hsa_type (brig_inst.type)
kono
parents:
diff changeset
850 : uint32_type_node;
kono
parents:
diff changeset
851
kono
parents:
diff changeset
852 const BrigData *operand_entries
kono
parents:
diff changeset
853 = m_parent.get_brig_data_entry (brig_inst.operands);
kono
parents:
diff changeset
854 std::vector<tree> operands;
kono
parents:
diff changeset
855 for (size_t i = 0; i < operand_entries->byteCount / 4; ++i)
kono
parents:
diff changeset
856 {
kono
parents:
diff changeset
857 uint32_t operand_offset = ((const uint32_t *) &operand_entries->bytes)[i];
kono
parents:
diff changeset
858 const BrigBase *operand_data
kono
parents:
diff changeset
859 = m_parent.get_brig_operand_entry (operand_offset);
kono
parents:
diff changeset
860
kono
parents:
diff changeset
861 const bool is_output
kono
parents:
diff changeset
862 = gccbrig_hsa_opcode_op_output_p (brig_inst.opcode, i);
kono
parents:
diff changeset
863
kono
parents:
diff changeset
864 tree operand_type = is_output ? dest_type : src_type;
kono
parents:
diff changeset
865
kono
parents:
diff changeset
866 bool half_to_float = is_fp16_arith;
kono
parents:
diff changeset
867
kono
parents:
diff changeset
868 /* Special cases for operand types. */
kono
parents:
diff changeset
869 if ((brig_inst.opcode == BRIG_OPCODE_SHL
kono
parents:
diff changeset
870 || brig_inst.opcode == BRIG_OPCODE_SHR)
kono
parents:
diff changeset
871 && i == 2)
kono
parents:
diff changeset
872 /* The shift amount is always a scalar. */
kono
parents:
diff changeset
873 operand_type
kono
parents:
diff changeset
874 = VECTOR_TYPE_P (src_type) ? TREE_TYPE (src_type) : src_type;
kono
parents:
diff changeset
875 else if (brig_inst.opcode == BRIG_OPCODE_SHUFFLE)
kono
parents:
diff changeset
876 {
kono
parents:
diff changeset
877 if (i == 3)
kono
parents:
diff changeset
878 /* HSAIL shuffle inputs the MASK vector as tightly packed bits
kono
parents:
diff changeset
879 while GENERIC VEC_PERM_EXPR expects the mask elements to be
kono
parents:
diff changeset
880 of the same size as the elements in the input vectors. Let's
kono
parents:
diff changeset
881 cast to a scalar type here and convert to the VEC_PERM_EXPR
kono
parents:
diff changeset
882 format in instruction handling. There are no arbitrary bit
kono
parents:
diff changeset
883 width int types in GENERIC so we cannot use the original
kono
parents:
diff changeset
884 vector type. */
kono
parents:
diff changeset
885 operand_type = uint32_type_node;
kono
parents:
diff changeset
886 else
kono
parents:
diff changeset
887 /* Always treat the element as unsigned ints to avoid
kono
parents:
diff changeset
888 sign extensions/negative offsets with masks, which
kono
parents:
diff changeset
889 are expected to be of the same element type as the
kono
parents:
diff changeset
890 data in VEC_PERM_EXPR. With shuffles the data type
kono
parents:
diff changeset
891 should not matter as it's a "raw operation". */
kono
parents:
diff changeset
892 operand_type = get_unsigned_int_type (operand_type);
kono
parents:
diff changeset
893 }
kono
parents:
diff changeset
894 else if (brig_inst.opcode == BRIG_OPCODE_PACK)
kono
parents:
diff changeset
895 {
kono
parents:
diff changeset
896 if (i == 1)
kono
parents:
diff changeset
897 operand_type = get_unsigned_int_type (dest_type);
kono
parents:
diff changeset
898 else if (i == 2)
kono
parents:
diff changeset
899 operand_type = get_unsigned_int_type (TREE_TYPE (dest_type));
kono
parents:
diff changeset
900 else if (i == 3)
kono
parents:
diff changeset
901 operand_type = uint32_type_node;
kono
parents:
diff changeset
902 }
kono
parents:
diff changeset
903 else if (brig_inst.opcode == BRIG_OPCODE_UNPACK && i == 2)
kono
parents:
diff changeset
904 operand_type = uint32_type_node;
kono
parents:
diff changeset
905 else if (brig_inst.opcode == BRIG_OPCODE_SAD && i == 3)
kono
parents:
diff changeset
906 operand_type = uint32_type_node;
kono
parents:
diff changeset
907 else if (brig_inst.opcode == BRIG_OPCODE_CLASS && i == 2)
kono
parents:
diff changeset
908 {
kono
parents:
diff changeset
909 operand_type = uint32_type_node;
kono
parents:
diff changeset
910 half_to_float = false;
kono
parents:
diff changeset
911 }
kono
parents:
diff changeset
912 else if (brig_inst.opcode == BRIG_OPCODE_ACTIVELANEPERMUTE && i == 4)
kono
parents:
diff changeset
913 {
kono
parents:
diff changeset
914 operand_type = uint32_type_node;
kono
parents:
diff changeset
915 }
kono
parents:
diff changeset
916 else if (half_to_float)
kono
parents:
diff changeset
917 /* Treat the operands as the storage type at this point. */
kono
parents:
diff changeset
918 operand_type = half_storage_type;
kono
parents:
diff changeset
919
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
920 if (analyze)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
921 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
922 if (operand_data->kind == BRIG_KIND_OPERAND_REGISTER)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
923 {
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
924 const BrigOperandRegister &brig_reg
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
925 = (const BrigOperandRegister &) *operand_data;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
926 m_parent.add_reg_used_as_type (brig_reg, operand_type);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
927 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
928 continue;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
929 }
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
930
111
kono
parents:
diff changeset
931 tree operand = build_tree_operand (brig_inst, *operand_data, operand_type,
kono
parents:
diff changeset
932 !is_output);
kono
parents:
diff changeset
933 gcc_assert (operand);
kono
parents:
diff changeset
934
kono
parents:
diff changeset
935 /* Cast/convert the inputs to correct types as expected by the GENERIC
kono
parents:
diff changeset
936 opcode instruction. */
kono
parents:
diff changeset
937 if (!is_output)
kono
parents:
diff changeset
938 {
kono
parents:
diff changeset
939 if (half_to_float)
kono
parents:
diff changeset
940 operand = build_h2f_conversion
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
941 (build_resize_convert_view (half_storage_type, operand));
111
kono
parents:
diff changeset
942 else if (TREE_CODE (operand) != LABEL_DECL
kono
parents:
diff changeset
943 && TREE_CODE (operand) != TREE_VEC
kono
parents:
diff changeset
944 && operand_data->kind != BRIG_KIND_OPERAND_ADDRESS
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
945 && operand_data->kind != BRIG_KIND_OPERAND_OPERAND_LIST)
111
kono
parents:
diff changeset
946 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
947 operand = build_resize_convert_view (operand_type, operand);
111
kono
parents:
diff changeset
948 }
kono
parents:
diff changeset
949 else if (brig_inst.opcode == BRIG_OPCODE_SHUFFLE)
kono
parents:
diff changeset
950 /* Force the operand type to be treated as the raw type. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
951 operand = build_resize_convert_view (operand_type, operand);
111
kono
parents:
diff changeset
952
kono
parents:
diff changeset
953 if (brig_inst.opcode == BRIG_OPCODE_CMOV && i == 1)
kono
parents:
diff changeset
954 {
kono
parents:
diff changeset
955 /* gcc expects the lower bit to be 1 (or all ones in case of
kono
parents:
diff changeset
956 vectors) while CMOV assumes false iff 0. Convert the input
kono
parents:
diff changeset
957 here to what gcc likes by generating
kono
parents:
diff changeset
958 'operand = operand != 0'. */
kono
parents:
diff changeset
959 tree cmp_res_type = get_comparison_result_type (operand_type);
kono
parents:
diff changeset
960 operand = build2 (NE_EXPR, cmp_res_type, operand,
kono
parents:
diff changeset
961 build_zero_cst (TREE_TYPE (operand)));
kono
parents:
diff changeset
962 }
kono
parents:
diff changeset
963
kono
parents:
diff changeset
964 if (ftz)
kono
parents:
diff changeset
965 operand = flush_to_zero (is_fp16_arith) (*this, operand);
kono
parents:
diff changeset
966 }
kono
parents:
diff changeset
967 operands.push_back (operand);
kono
parents:
diff changeset
968 }
kono
parents:
diff changeset
969 return operands;
kono
parents:
diff changeset
970 }
kono
parents:
diff changeset
971
kono
parents:
diff changeset
972 /* Build the GENERIC for assigning the result of an instruction to the result
kono
parents:
diff changeset
973 "register" (variable). BRIG_INST is the original brig instruction,
kono
parents:
diff changeset
974 OUTPUT the result variable/register, INST_EXPR the one producing the
kono
parents:
diff changeset
975 result. Required bitcasts and fp32 to fp16 conversions are added as
kono
parents:
diff changeset
976 well. */
kono
parents:
diff changeset
977
kono
parents:
diff changeset
978 tree
kono
parents:
diff changeset
979 brig_code_entry_handler::build_output_assignment (const BrigInstBase &brig_inst,
kono
parents:
diff changeset
980 tree output, tree inst_expr)
kono
parents:
diff changeset
981 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
982 /* The result/input type might be different from the output register
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
983 variable type (can be any type; see get_m_var_declfor_reg @
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
984 brig-function.cc). */
111
kono
parents:
diff changeset
985 tree output_type = TREE_TYPE (output);
kono
parents:
diff changeset
986 bool is_fp16 = (brig_inst.type & BRIG_TYPE_BASE_MASK) == BRIG_TYPE_F16
kono
parents:
diff changeset
987 && brig_inst.base.kind != BRIG_KIND_INST_MEM
kono
parents:
diff changeset
988 && !gccbrig_is_bit_operation (brig_inst.opcode);
kono
parents:
diff changeset
989
kono
parents:
diff changeset
990 /* Flush to zero. */
kono
parents:
diff changeset
991 bool ftz = false;
kono
parents:
diff changeset
992 const BrigBase *base = &brig_inst.base;
kono
parents:
diff changeset
993
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
994 if (m_parent.m_cf->is_id_val (inst_expr))
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
995 inst_expr = m_parent.m_cf->id_val (inst_expr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
996
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
997 tree input_type = TREE_TYPE (inst_expr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
998
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
999 m_parent.m_cf->add_reg_var_update (output, inst_expr);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1000
111
kono
parents:
diff changeset
1001 if (base->kind == BRIG_KIND_INST_MOD)
kono
parents:
diff changeset
1002 {
kono
parents:
diff changeset
1003 const BrigInstMod *mod = (const BrigInstMod *) base;
kono
parents:
diff changeset
1004 ftz = mod->modifier & BRIG_ALU_FTZ;
kono
parents:
diff changeset
1005 }
kono
parents:
diff changeset
1006 else if (base->kind == BRIG_KIND_INST_CMP)
kono
parents:
diff changeset
1007 {
kono
parents:
diff changeset
1008 const BrigInstCmp *cmp = (const BrigInstCmp *) base;
kono
parents:
diff changeset
1009 ftz = cmp->modifier & BRIG_ALU_FTZ;
kono
parents:
diff changeset
1010 }
kono
parents:
diff changeset
1011
kono
parents:
diff changeset
1012 if (TREE_CODE (inst_expr) == CALL_EXPR)
kono
parents:
diff changeset
1013 {
kono
parents:
diff changeset
1014 tree func_decl = TREE_OPERAND (TREE_OPERAND (inst_expr, 1), 0);
kono
parents:
diff changeset
1015 input_type = TREE_TYPE (TREE_TYPE (func_decl));
kono
parents:
diff changeset
1016 }
kono
parents:
diff changeset
1017
kono
parents:
diff changeset
1018 if (ftz && (VECTOR_FLOAT_TYPE_P (TREE_TYPE (inst_expr))
kono
parents:
diff changeset
1019 || SCALAR_FLOAT_TYPE_P (TREE_TYPE (inst_expr)) || is_fp16))
kono
parents:
diff changeset
1020 {
kono
parents:
diff changeset
1021 /* Ensure we don't duplicate the arithmetics to the arguments of the bit
kono
parents:
diff changeset
1022 field reference operators. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1023 inst_expr = m_parent.m_cf->add_temp_var ("before_ftz", inst_expr);
111
kono
parents:
diff changeset
1024 inst_expr = flush_to_zero (is_fp16) (*this, inst_expr);
kono
parents:
diff changeset
1025 }
kono
parents:
diff changeset
1026
kono
parents:
diff changeset
1027 if (is_fp16)
kono
parents:
diff changeset
1028 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1029 inst_expr = m_parent.m_cf->add_temp_var ("before_f2h", inst_expr);
111
kono
parents:
diff changeset
1030 tree f2h_output = build_f2h_conversion (inst_expr);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1031 tree conv = build_resize_convert_view (output_type, f2h_output);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1032 tree assign = build2 (MODIFY_EXPR, output_type, output, conv);
111
kono
parents:
diff changeset
1033 m_parent.m_cf->append_statement (assign);
kono
parents:
diff changeset
1034 return assign;
kono
parents:
diff changeset
1035 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1036 else if (VECTOR_TYPE_P (output_type) && TREE_CODE (output) == CONSTRUCTOR)
111
kono
parents:
diff changeset
1037 {
kono
parents:
diff changeset
1038 /* Expand/unpack the input value to the given vector elements. */
kono
parents:
diff changeset
1039 size_t i;
kono
parents:
diff changeset
1040 tree input = inst_expr;
kono
parents:
diff changeset
1041 tree element_type = gccbrig_tree_type_for_hsa_type (brig_inst.type);
kono
parents:
diff changeset
1042 tree element;
kono
parents:
diff changeset
1043 tree last_assign = NULL_TREE;
kono
parents:
diff changeset
1044 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (output), i, element)
kono
parents:
diff changeset
1045 {
kono
parents:
diff changeset
1046 tree element_ref
kono
parents:
diff changeset
1047 = build3 (BIT_FIELD_REF, element_type, input,
kono
parents:
diff changeset
1048 TYPE_SIZE (element_type),
kono
parents:
diff changeset
1049 bitsize_int (i * int_size_in_bytes (element_type)
kono
parents:
diff changeset
1050 * BITS_PER_UNIT));
kono
parents:
diff changeset
1051
kono
parents:
diff changeset
1052 last_assign
kono
parents:
diff changeset
1053 = build_output_assignment (brig_inst, element, element_ref);
kono
parents:
diff changeset
1054 }
kono
parents:
diff changeset
1055 return last_assign;
kono
parents:
diff changeset
1056 }
kono
parents:
diff changeset
1057 else
kono
parents:
diff changeset
1058 {
kono
parents:
diff changeset
1059 /* All we do here is to bitcast the result and store it to the
kono
parents:
diff changeset
1060 'register' (variable). Mainly need to take care of differing
kono
parents:
diff changeset
1061 bitwidths. */
kono
parents:
diff changeset
1062 size_t src_width = int_size_in_bytes (input_type);
kono
parents:
diff changeset
1063 size_t dst_width = int_size_in_bytes (output_type);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1064 tree input = inst_expr;
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1065 /* Integer results are extended to the target register width, using
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1066 the same sign as the inst_expr. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1067 if (INTEGRAL_TYPE_P (TREE_TYPE (input)) && src_width != dst_width)
111
kono
parents:
diff changeset
1068 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1069 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (input));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1070 tree resized_type
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1071 = build_nonstandard_integer_type (dst_width * BITS_PER_UNIT,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1072 unsigned_p);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1073 input = convert_to_integer (resized_type, input);
111
kono
parents:
diff changeset
1074 }
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1075 input = build_resize_convert_view (output_type, input);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1076 tree assign = build2 (MODIFY_EXPR, output_type, output, input);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1077 m_parent.m_cf->append_statement (assign);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1078 return assign;
111
kono
parents:
diff changeset
1079 }
kono
parents:
diff changeset
1080 return NULL_TREE;
kono
parents:
diff changeset
1081 }
kono
parents:
diff changeset
1082
kono
parents:
diff changeset
1083 /* Appends a GENERIC statement (STMT) to the currently constructed function. */
kono
parents:
diff changeset
1084
kono
parents:
diff changeset
1085 void
kono
parents:
diff changeset
1086 brig_code_entry_handler::append_statement (tree stmt)
kono
parents:
diff changeset
1087 {
kono
parents:
diff changeset
1088 m_parent.m_cf->append_statement (stmt);
kono
parents:
diff changeset
1089 }
kono
parents:
diff changeset
1090
kono
parents:
diff changeset
1091 /* Visits the element(s) in the OPERAND, calling HANDLER to each of them. */
kono
parents:
diff changeset
1092
kono
parents:
diff changeset
1093 tree
kono
parents:
diff changeset
1094 tree_element_unary_visitor::operator () (brig_code_entry_handler &handler,
kono
parents:
diff changeset
1095 tree operand)
kono
parents:
diff changeset
1096 {
kono
parents:
diff changeset
1097 if (VECTOR_TYPE_P (TREE_TYPE (operand)))
kono
parents:
diff changeset
1098 {
kono
parents:
diff changeset
1099 size_t vec_size = int_size_in_bytes (TREE_TYPE (operand));
kono
parents:
diff changeset
1100 size_t element_size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (operand)));
kono
parents:
diff changeset
1101 size_t element_count = vec_size / element_size;
kono
parents:
diff changeset
1102
kono
parents:
diff changeset
1103 tree input_element_type = TREE_TYPE (TREE_TYPE (operand));
kono
parents:
diff changeset
1104 tree output_element_type = NULL_TREE;
kono
parents:
diff changeset
1105
kono
parents:
diff changeset
1106 vec<constructor_elt, va_gc> *constructor_vals = NULL;
kono
parents:
diff changeset
1107 for (size_t i = 0; i < element_count; ++i)
kono
parents:
diff changeset
1108 {
kono
parents:
diff changeset
1109 tree element = build3 (BIT_FIELD_REF, input_element_type, operand,
kono
parents:
diff changeset
1110 TYPE_SIZE (input_element_type),
kono
parents:
diff changeset
1111 bitsize_int (i * element_size
kono
parents:
diff changeset
1112 * BITS_PER_UNIT));
kono
parents:
diff changeset
1113
kono
parents:
diff changeset
1114 tree output = visit_element (handler, element);
kono
parents:
diff changeset
1115 output_element_type = TREE_TYPE (output);
kono
parents:
diff changeset
1116
kono
parents:
diff changeset
1117 CONSTRUCTOR_APPEND_ELT (constructor_vals, NULL_TREE, output);
kono
parents:
diff changeset
1118 }
kono
parents:
diff changeset
1119
kono
parents:
diff changeset
1120 tree vec_type = build_vector_type (output_element_type, element_count);
kono
parents:
diff changeset
1121
kono
parents:
diff changeset
1122 /* build_constructor creates a vector type which is not a vector_cst
kono
parents:
diff changeset
1123 that requires compile time constant elements. */
kono
parents:
diff changeset
1124 tree vec = build_constructor (vec_type, constructor_vals);
kono
parents:
diff changeset
1125
kono
parents:
diff changeset
1126 /* Add a temp variable for readability. */
kono
parents:
diff changeset
1127 tree tmp_var = create_tmp_var (vec_type, "vec_out");
kono
parents:
diff changeset
1128 tree vec_tmp_assign
kono
parents:
diff changeset
1129 = build2 (MODIFY_EXPR, TREE_TYPE (tmp_var), tmp_var, vec);
kono
parents:
diff changeset
1130 handler.append_statement (vec_tmp_assign);
kono
parents:
diff changeset
1131 return tmp_var;
kono
parents:
diff changeset
1132 }
kono
parents:
diff changeset
1133 else
kono
parents:
diff changeset
1134 return visit_element (handler, operand);
kono
parents:
diff changeset
1135 }
kono
parents:
diff changeset
1136
kono
parents:
diff changeset
1137 /* Visits the element pair(s) in the OPERAND0 and OPERAND1, calling HANDLER
kono
parents:
diff changeset
1138 to each of them. */
kono
parents:
diff changeset
1139
kono
parents:
diff changeset
1140 tree
kono
parents:
diff changeset
1141 tree_element_binary_visitor::operator () (brig_code_entry_handler &handler,
kono
parents:
diff changeset
1142 tree operand0, tree operand1)
kono
parents:
diff changeset
1143 {
kono
parents:
diff changeset
1144 if (VECTOR_TYPE_P (TREE_TYPE (operand0)))
kono
parents:
diff changeset
1145 {
kono
parents:
diff changeset
1146 gcc_assert (VECTOR_TYPE_P (TREE_TYPE (operand1)));
kono
parents:
diff changeset
1147 size_t vec_size = int_size_in_bytes (TREE_TYPE (operand0));
kono
parents:
diff changeset
1148 size_t element_size
kono
parents:
diff changeset
1149 = int_size_in_bytes (TREE_TYPE (TREE_TYPE (operand0)));
kono
parents:
diff changeset
1150 size_t element_count = vec_size / element_size;
kono
parents:
diff changeset
1151
kono
parents:
diff changeset
1152 tree input_element_type = TREE_TYPE (TREE_TYPE (operand0));
kono
parents:
diff changeset
1153 tree output_element_type = NULL_TREE;
kono
parents:
diff changeset
1154
kono
parents:
diff changeset
1155 vec<constructor_elt, va_gc> *constructor_vals = NULL;
kono
parents:
diff changeset
1156 for (size_t i = 0; i < element_count; ++i)
kono
parents:
diff changeset
1157 {
kono
parents:
diff changeset
1158
kono
parents:
diff changeset
1159 tree element0 = build3 (BIT_FIELD_REF, input_element_type, operand0,
kono
parents:
diff changeset
1160 TYPE_SIZE (input_element_type),
kono
parents:
diff changeset
1161 bitsize_int (i * element_size
kono
parents:
diff changeset
1162 * BITS_PER_UNIT));
kono
parents:
diff changeset
1163
kono
parents:
diff changeset
1164 tree element1 = build3 (BIT_FIELD_REF, input_element_type, operand1,
kono
parents:
diff changeset
1165 TYPE_SIZE (input_element_type),
kono
parents:
diff changeset
1166 bitsize_int (i * element_size
kono
parents:
diff changeset
1167 * BITS_PER_UNIT));
kono
parents:
diff changeset
1168
kono
parents:
diff changeset
1169 tree output = visit_element (handler, element0, element1);
kono
parents:
diff changeset
1170 output_element_type = TREE_TYPE (output);
kono
parents:
diff changeset
1171
kono
parents:
diff changeset
1172 CONSTRUCTOR_APPEND_ELT (constructor_vals, NULL_TREE, output);
kono
parents:
diff changeset
1173 }
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 tree vec_type = build_vector_type (output_element_type, element_count);
kono
parents:
diff changeset
1176
kono
parents:
diff changeset
1177 /* build_constructor creates a vector type which is not a vector_cst
kono
parents:
diff changeset
1178 that requires compile time constant elements. */
kono
parents:
diff changeset
1179 tree vec = build_constructor (vec_type, constructor_vals);
kono
parents:
diff changeset
1180
kono
parents:
diff changeset
1181 /* Add a temp variable for readability. */
kono
parents:
diff changeset
1182 tree tmp_var = create_tmp_var (vec_type, "vec_out");
kono
parents:
diff changeset
1183 tree vec_tmp_assign
kono
parents:
diff changeset
1184 = build2 (MODIFY_EXPR, TREE_TYPE (tmp_var), tmp_var, vec);
kono
parents:
diff changeset
1185 handler.append_statement (vec_tmp_assign);
kono
parents:
diff changeset
1186 return tmp_var;
kono
parents:
diff changeset
1187 }
kono
parents:
diff changeset
1188 else
kono
parents:
diff changeset
1189 return visit_element (handler, operand0, operand1);
kono
parents:
diff changeset
1190 }
kono
parents:
diff changeset
1191
kono
parents:
diff changeset
1192 /* Generates GENERIC code that flushes the visited element to zero. */
kono
parents:
diff changeset
1193
kono
parents:
diff changeset
1194 tree
kono
parents:
diff changeset
1195 flush_to_zero::visit_element (brig_code_entry_handler &, tree operand)
kono
parents:
diff changeset
1196 {
kono
parents:
diff changeset
1197 size_t size = int_size_in_bytes (TREE_TYPE (operand));
kono
parents:
diff changeset
1198 if (size == 4)
kono
parents:
diff changeset
1199 {
kono
parents:
diff changeset
1200 tree built_in
kono
parents:
diff changeset
1201 = (m_fp16) ? builtin_decl_explicit (BUILT_IN_HSAIL_FTZ_F32_F16) :
kono
parents:
diff changeset
1202 builtin_decl_explicit (BUILT_IN_HSAIL_FTZ_F32);
kono
parents:
diff changeset
1203
kono
parents:
diff changeset
1204 return call_builtin (built_in, 1, float_type_node, float_type_node,
kono
parents:
diff changeset
1205 operand);
kono
parents:
diff changeset
1206 }
kono
parents:
diff changeset
1207 else if (size == 8)
kono
parents:
diff changeset
1208 {
kono
parents:
diff changeset
1209 return call_builtin (builtin_decl_explicit (BUILT_IN_HSAIL_FTZ_F64), 1,
kono
parents:
diff changeset
1210 double_type_node, double_type_node, operand);
kono
parents:
diff changeset
1211 }
kono
parents:
diff changeset
1212 else
kono
parents:
diff changeset
1213 gcc_unreachable ();
kono
parents:
diff changeset
1214 return NULL_TREE;
kono
parents:
diff changeset
1215 }
kono
parents:
diff changeset
1216
kono
parents:
diff changeset
1217 /* Generates GENERIC code that converts a single precision float to half
kono
parents:
diff changeset
1218 precision float. */
kono
parents:
diff changeset
1219
kono
parents:
diff changeset
1220 tree
kono
parents:
diff changeset
1221 float_to_half::visit_element (brig_code_entry_handler &caller, tree operand)
kono
parents:
diff changeset
1222 {
kono
parents:
diff changeset
1223 tree built_in = builtin_decl_explicit (BUILT_IN_HSAIL_F32_TO_F16);
kono
parents:
diff changeset
1224
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1225 tree casted_operand = build_resize_convert_view (uint32_type_node, operand);
111
kono
parents:
diff changeset
1226
kono
parents:
diff changeset
1227 tree call = call_builtin (built_in, 1, uint16_type_node, uint32_type_node,
kono
parents:
diff changeset
1228 casted_operand);
kono
parents:
diff changeset
1229 tree output
kono
parents:
diff changeset
1230 = create_tmp_var (TREE_TYPE (TREE_TYPE (built_in)), "fp16out");
kono
parents:
diff changeset
1231 tree assign = build2 (MODIFY_EXPR, TREE_TYPE (output), output, call);
kono
parents:
diff changeset
1232 caller.append_statement (assign);
kono
parents:
diff changeset
1233 return output;
kono
parents:
diff changeset
1234 }
kono
parents:
diff changeset
1235
kono
parents:
diff changeset
1236 /* Generates GENERIC code that converts a half precision float to single
kono
parents:
diff changeset
1237 precision float. */
kono
parents:
diff changeset
1238
kono
parents:
diff changeset
1239 tree
kono
parents:
diff changeset
1240 half_to_float::visit_element (brig_code_entry_handler &caller, tree operand)
kono
parents:
diff changeset
1241 {
kono
parents:
diff changeset
1242 tree built_in = builtin_decl_explicit (BUILT_IN_HSAIL_F16_TO_F32);
kono
parents:
diff changeset
1243 tree truncated_source = convert_to_integer (uint16_type_node, operand);
kono
parents:
diff changeset
1244
kono
parents:
diff changeset
1245 tree call
kono
parents:
diff changeset
1246 = call_builtin (built_in, 1, uint32_type_node, uint16_type_node,
kono
parents:
diff changeset
1247 truncated_source);
kono
parents:
diff changeset
1248
kono
parents:
diff changeset
1249 tree const_fp32_type
kono
parents:
diff changeset
1250 = build_type_variant (brig_to_generic::s_fp32_type, 1, 0);
kono
parents:
diff changeset
1251
kono
parents:
diff changeset
1252 tree output = create_tmp_var (const_fp32_type, "fp32out");
kono
parents:
diff changeset
1253 tree casted_result
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
1254 = build_resize_convert_view (brig_to_generic::s_fp32_type, call);
111
kono
parents:
diff changeset
1255
kono
parents:
diff changeset
1256 tree assign = build2 (MODIFY_EXPR, TREE_TYPE (output), output, casted_result);
kono
parents:
diff changeset
1257
kono
parents:
diff changeset
1258 caller.append_statement (assign);
kono
parents:
diff changeset
1259
kono
parents:
diff changeset
1260 return output;
kono
parents:
diff changeset
1261 }
kono
parents:
diff changeset
1262
kono
parents:
diff changeset
1263 /* Treats the INPUT as SRC_TYPE and sign or zero extends it to DEST_TYPE. */
kono
parents:
diff changeset
1264
kono
parents:
diff changeset
1265 tree
kono
parents:
diff changeset
1266 brig_code_entry_handler::extend_int (tree input, tree dest_type, tree src_type)
kono
parents:
diff changeset
1267 {
kono
parents:
diff changeset
1268 /* Extend integer conversions according to the destination's
kono
parents:
diff changeset
1269 ext mode. First we need to clip the input register to
kono
parents:
diff changeset
1270 the possible smaller integer size to ensure the correct sign
kono
parents:
diff changeset
1271 bit is extended. */
kono
parents:
diff changeset
1272 tree clipped_input = convert_to_integer (src_type, input);
kono
parents:
diff changeset
1273 tree conversion_result;
kono
parents:
diff changeset
1274
kono
parents:
diff changeset
1275 if (TYPE_UNSIGNED (src_type))
kono
parents:
diff changeset
1276 conversion_result
kono
parents:
diff changeset
1277 = convert_to_integer (unsigned_type_for (dest_type), clipped_input);
kono
parents:
diff changeset
1278 else
kono
parents:
diff changeset
1279 conversion_result
kono
parents:
diff changeset
1280 = convert_to_integer (signed_type_for (dest_type), clipped_input);
kono
parents:
diff changeset
1281
kono
parents:
diff changeset
1282 /* Treat the result as unsigned so we do not sign extend to the
kono
parents:
diff changeset
1283 register width. For some reason this GENERIC sequence sign
kono
parents:
diff changeset
1284 extends to the s register:
kono
parents:
diff changeset
1285
kono
parents:
diff changeset
1286 D.1541 = (signed char) s1;
kono
parents:
diff changeset
1287 D.1542 = (signed short) D.1541;
kono
parents:
diff changeset
1288 s0 = (unsigned int) D.1542
kono
parents:
diff changeset
1289 */
kono
parents:
diff changeset
1290
kono
parents:
diff changeset
1291 /* The converted result is then extended to the target register
kono
parents:
diff changeset
1292 width, using the same sign as the destination. */
kono
parents:
diff changeset
1293 return convert_to_integer (dest_type, conversion_result);
kono
parents:
diff changeset
1294 }
kono
parents:
diff changeset
1295
kono
parents:
diff changeset
1296 /* Returns the integer constant value of the given node.
kono
parents:
diff changeset
1297 If it's a cast, looks into the source of the cast. */
kono
parents:
diff changeset
1298 HOST_WIDE_INT
kono
parents:
diff changeset
1299 brig_code_entry_handler::int_constant_value (tree node)
kono
parents:
diff changeset
1300 {
kono
parents:
diff changeset
1301 tree n = node;
kono
parents:
diff changeset
1302 if (TREE_CODE (n) == VIEW_CONVERT_EXPR)
kono
parents:
diff changeset
1303 n = TREE_OPERAND (n, 0);
kono
parents:
diff changeset
1304 return int_cst_value (n);
kono
parents:
diff changeset
1305 }