1 /* HSAIL and BRIG related macros and definitions.
2 Copyright (C) 2013-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
23 #include "hsa-brig-format.h"
28 #include "hash-table.h"
29 #include "basic-block.h"
32 /* Return true if the compiler should produce HSAIL. */
35 hsa_gen_requested_p (void)
40 return !flag_disable_hsa
;
43 /* Standard warning message if we failed to generate HSAIL for a function. */
45 #define HSA_SORRY_MSG "could not emit HSAIL for the function"
48 class hsa_op_cst_list
;
53 typedef hsa_insn_basic
*hsa_insn_basic_p
;
55 /* Class representing an input argument, output argument (result) or a
56 variable, that will eventually end up being a symbol directive. */
61 hsa_symbol (BrigType16_t type
, BrigSegment8_t segment
,
62 BrigLinkage8_t linkage
, bool global_scope_p
= false,
63 BrigAllocation allocation
= BRIG_ALLOCATION_AUTOMATIC
);
65 /* Return total size of the symbol. */
66 unsigned HOST_WIDE_INT
total_byte_size ();
68 /* Fill in those values into the symbol according to DECL, which are
69 determined independently from whether it is parameter, result,
70 or a variable, local or global. */
71 void fillup_for_decl (tree decl
);
73 /* Pointer to the original tree, which is PARM_DECL for input parameters and
74 RESULT_DECL for the output parameters. */
77 /* Name of the symbol, that will be written into output and dumps. Can be
78 NULL, see name_number below. */
81 /* If name is NULL, artificial name will be formed from the segment name and
85 /* Once written, this is the offset of the associated symbol directive. Zero
86 means the symbol has not been written yet. */
87 unsigned m_directive_offset
;
89 /* HSA type of the parameter. */
92 /* The HSA segment this will eventually end up in. */
93 BrigSegment8_t m_segment
;
95 /* The HSA kind of linkage. */
96 BrigLinkage8_t m_linkage
;
98 /* Array dimension, if non-zero. */
99 unsigned HOST_WIDE_INT m_dim
;
101 /* Constant value, used for string constants. */
102 hsa_op_immed
*m_cst_value
;
104 /* Is in global scope. */
105 bool m_global_scope_p
;
107 /* True if an error has been seen for the symbol. */
110 /* Symbol allocation. */
111 BrigAllocation m_allocation
;
114 /* Default constructor. */
118 /* Abstract class for HSA instruction operands. */
123 /* Next operand scheduled to be written when writing BRIG operand
127 /* Offset to which the associated operand structure will be written. Zero if
128 yet not scheduled for writing. */
129 unsigned m_brig_op_offset
;
131 /* The type of a particular operand. */
135 hsa_op_base (BrigKind16_t k
);
137 /* Make the default constructor inaccessible. */
141 /* Common abstract ancestor for operands which have a type. */
143 class hsa_op_with_type
: public hsa_op_base
149 /* Convert an operand to a destination type DTYPE and attach insns
151 hsa_op_with_type
*get_in_type (BrigType16_t dtype
, hsa_bb
*hbb
);
154 hsa_op_with_type (BrigKind16_t k
, BrigType16_t t
);
156 /* Make the default constructor inaccessible. */
157 hsa_op_with_type () : hsa_op_base (BRIG_KIND_NONE
) {}
160 /* An immediate HSA operand. */
162 class hsa_op_immed
: public hsa_op_with_type
165 hsa_op_immed (tree tree_val
, bool min32int
= true);
166 hsa_op_immed (HOST_WIDE_INT int_value
, BrigType16_t type
);
167 void *operator new (size_t);
169 void set_type (BrigKind16_t t
);
171 /* Value as represented by middle end. */
174 /* Integer value representation. */
175 HOST_WIDE_INT m_int_value
;
177 /* Brig data representation. */
180 /* Brig data representation size in bytes. */
181 unsigned m_brig_repr_size
;
184 /* Make the default constructor inaccessible. */
186 /* All objects are deallocated by destroying their pool, so make delete
188 void operator delete (void *) {}
189 void emit_to_buffer (tree value
);
192 /* Report whether or not P is a an immediate operand. */
197 is_a_helper
<hsa_op_immed
*>::test (hsa_op_base
*p
)
199 return p
->m_kind
== BRIG_KIND_OPERAND_CONSTANT_BYTES
;
202 /* HSA register operand. */
204 class hsa_op_reg
: public hsa_op_with_type
206 friend class hsa_insn_basic
;
207 friend class hsa_insn_phi
;
209 hsa_op_reg (BrigType16_t t
);
210 void *operator new (size_t);
212 /* Verify register operand. */
215 /* If NON-NULL, gimple SSA that we come from. NULL if none. */
218 /* Defining instruction while still in the SSA. */
219 hsa_insn_basic
*m_def_insn
;
221 /* If the register allocator decides to spill the register, this is the
222 appropriate spill symbol. */
223 hsa_symbol
*m_spill_sym
;
225 /* Number of this register structure in the order in which they were
228 int m_lr_begin
, m_lr_end
;
230 /* Zero if the register is not yet allocated. After, allocation, this must
231 be 'c', 's', 'd' or 'q'. */
233 /* If allocated, the number of the HW register (within its HSA register
238 /* Make the default constructor inaccessible. */
239 hsa_op_reg () : hsa_op_with_type (BRIG_KIND_NONE
, BRIG_TYPE_NONE
) {}
240 /* All objects are deallocated by destroying their pool, so make delete
242 void operator delete (void *) {}
243 /* Set definition where the register is defined. */
244 void set_definition (hsa_insn_basic
*insn
);
245 /* Uses of the value while still in SSA. */
246 auto_vec
<hsa_insn_basic_p
> m_uses
;
249 typedef class hsa_op_reg
*hsa_op_reg_p
;
251 /* Report whether or not P is a register operand. */
256 is_a_helper
<hsa_op_reg
*>::test (hsa_op_base
*p
)
258 return p
->m_kind
== BRIG_KIND_OPERAND_REGISTER
;
261 /* Report whether or not P is a register operand. */
266 is_a_helper
<hsa_op_reg
*>::test (hsa_op_with_type
*p
)
268 return p
->m_kind
== BRIG_KIND_OPERAND_REGISTER
;
271 /* An address HSA operand. */
273 class hsa_op_address
: public hsa_op_base
276 /* set up a new address operand consisting of base symbol SYM, register R and
277 immediate OFFSET. If the machine model is not large and offset is 64 bit,
278 the upper, 32 bits have to be zero. */
279 hsa_op_address (hsa_symbol
*sym
, hsa_op_reg
*reg
,
280 HOST_WIDE_INT offset
= 0);
282 void *operator new (size_t);
284 /* Set up a new address operand consisting of base symbol SYM and
285 immediate OFFSET. If the machine model is not large and offset is 64 bit,
286 the upper, 32 bits have to be zero. */
287 hsa_op_address (hsa_symbol
*sym
, HOST_WIDE_INT offset
= 0);
289 /* Set up a new address operand consisting of register R and
290 immediate OFFSET. If the machine model is not large and offset is 64 bit,
291 the upper, 32 bits have to be zero. */
292 hsa_op_address (hsa_op_reg
*reg
, HOST_WIDE_INT offset
= 0);
294 /* Symbol base of the address. Can be NULL if there is none. */
295 hsa_symbol
*m_symbol
;
297 /* Register offset. Can be NULL if there is none. */
300 /* Immediate byte offset. */
301 HOST_WIDE_INT m_imm_offset
;
304 /* Make the default constructor inaccessible. */
305 hsa_op_address () : hsa_op_base (BRIG_KIND_NONE
) {}
306 /* All objects are deallocated by destroying their pool, so make delete
308 void operator delete (void *) {}
311 /* Report whether or not P is an address operand. */
316 is_a_helper
<hsa_op_address
*>::test (hsa_op_base
*p
)
318 return p
->m_kind
== BRIG_KIND_OPERAND_ADDRESS
;
321 /* A reference to code HSA operand. It can be either reference
322 to a start of a BB or a start of a function. */
324 class hsa_op_code_ref
: public hsa_op_base
329 /* Offset in the code section that this refers to. */
330 unsigned m_directive_offset
;
333 /* Report whether or not P is a code reference operand. */
338 is_a_helper
<hsa_op_code_ref
*>::test (hsa_op_base
*p
)
340 return p
->m_kind
== BRIG_KIND_OPERAND_CODE_REF
;
343 /* Code list HSA operand. */
345 class hsa_op_code_list
: public hsa_op_base
348 hsa_op_code_list (unsigned elements
);
349 void *operator new (size_t);
351 /* Offset to variable-sized array in hsa_data section, where
352 are offsets to entries in the hsa_code section. */
353 auto_vec
<unsigned> m_offsets
;
355 /* Make the default constructor inaccessible. */
356 hsa_op_code_list () : hsa_op_base (BRIG_KIND_NONE
) {}
357 /* All objects are deallocated by destroying their pool, so make delete
359 void operator delete (void *) {}
362 /* Report whether or not P is a code list operand. */
367 is_a_helper
<hsa_op_code_list
*>::test (hsa_op_base
*p
)
369 return p
->m_kind
== BRIG_KIND_OPERAND_CODE_LIST
;
372 /* Operand list HSA operand. */
374 class hsa_op_operand_list
: public hsa_op_base
377 hsa_op_operand_list (unsigned elements
);
378 ~hsa_op_operand_list ();
379 void *operator new (size_t);
381 /* Offset to variable-sized array in hsa_data section, where
382 are offsets to entries in the hsa_code section. */
383 auto_vec
<unsigned> m_offsets
;
385 /* Make the default constructor inaccessible. */
386 hsa_op_operand_list () : hsa_op_base (BRIG_KIND_NONE
) {}
387 /* All objects are deallocated by destroying their pool, so make delete
389 void operator delete (void *) {}
392 /* Report whether or not P is a code list operand. */
397 is_a_helper
<hsa_op_operand_list
*>::test (hsa_op_base
*p
)
399 return p
->m_kind
== BRIG_KIND_OPERAND_OPERAND_LIST
;
402 /* Opcodes of instructions that are not part of HSA but that we use to
403 represent it nevertheless. */
405 #define HSA_OPCODE_PHI (-1)
406 #define HSA_OPCODE_ARG_BLOCK (-2)
408 /* The number of operand pointers we can directly in an instruction. */
409 #define HSA_BRIG_INT_STORAGE_OPERANDS 5
411 /* Class representing an HSA instruction. Unlike typical ancestors for
412 specialized classes, this one is also directly used for all instructions
413 that are then represented as BrigInstBasic. */
418 hsa_insn_basic (unsigned nops
, int opc
);
419 hsa_insn_basic (unsigned nops
, int opc
, BrigType16_t t
,
420 hsa_op_base
*arg0
= NULL
,
421 hsa_op_base
*arg1
= NULL
,
422 hsa_op_base
*arg2
= NULL
,
423 hsa_op_base
*arg3
= NULL
);
425 void *operator new (size_t);
426 void set_op (int index
, hsa_op_base
*op
);
427 hsa_op_base
*get_op (int index
);
428 hsa_op_base
**get_op_addr (int index
);
429 unsigned int operand_count ();
431 unsigned input_count ();
432 unsigned num_used_ops ();
433 void set_output_in_type (hsa_op_reg
*dest
, unsigned op_index
, hsa_bb
*hbb
);
434 bool op_output_p (unsigned opnum
);
436 /* The previous and next instruction in the basic block. */
437 hsa_insn_basic
*m_prev
, *m_next
;
439 /* Basic block this instruction belongs to. */
442 /* Operand code distinguishing different types of instructions. Eventually
443 these should only be BRIG_INST_* values from the BrigOpcode16_t range but
444 initially we use negative values for PHI nodes and such. */
447 /* Linearized number assigned to the instruction by HSA RA. */
450 /* Type of the destination of the operations. */
453 /* BRIG offset of the instruction in code section. */
454 unsigned int m_brig_offset
;
457 /* Make the default constructor inaccessible. */
459 /* All objects are deallocated by destroying their pool, so make delete
461 void operator delete (void *) {}
462 /* The individual operands. All instructions but PHI nodes have five or
463 fewer instructions and so will fit the internal storage. */
464 /* TODO: Vast majority of instructions have three or fewer operands, so we
465 may actually try reducing it. */
466 auto_vec
<hsa_op_base
*, HSA_BRIG_INT_STORAGE_OPERANDS
> m_operands
;
469 /* Class representing a PHI node of the SSA form of HSA virtual
472 class hsa_insn_phi
: public hsa_insn_basic
475 hsa_insn_phi (unsigned nops
, hsa_op_reg
*dst
);
477 void *operator new (size_t);
483 /* Make the default constructor inaccessible. */
484 hsa_insn_phi () : hsa_insn_basic (1, HSA_OPCODE_PHI
) {}
485 /* All objects are deallocated by destroying their pool, so make delete
487 void operator delete (void *) {}
490 /* Report whether or not P is a PHI node. */
495 is_a_helper
<hsa_insn_phi
*>::test (hsa_insn_basic
*p
)
497 return p
->m_opcode
== HSA_OPCODE_PHI
;
500 /* HSA instruction for branches. Currently we explicitely represent only
501 conditional branches. */
503 class hsa_insn_br
: public hsa_insn_basic
506 hsa_insn_br (hsa_op_reg
*ctrl
);
508 void *operator new (size_t);
510 /* Width as described in HSA documentation. */
511 BrigWidth8_t m_width
;
513 /* Make the default constructor inaccessible. */
514 hsa_insn_br () : hsa_insn_basic (1, BRIG_OPCODE_CBR
) {}
515 /* All objects are deallocated by destroying their pool, so make delete
517 void operator delete (void *) {}
520 /* Report whether P is a branching instruction. */
525 is_a_helper
<hsa_insn_br
*>::test (hsa_insn_basic
*p
)
527 return p
->m_opcode
== BRIG_OPCODE_BR
528 || p
->m_opcode
== BRIG_OPCODE_CBR
;
531 /* HSA instruction for switch branches. */
533 class hsa_insn_sbr
: public hsa_insn_basic
536 hsa_insn_sbr (hsa_op_reg
*index
, unsigned jump_count
);
538 /* Default destructor. */
541 void *operator new (size_t);
543 void replace_all_labels (basic_block old_bb
, basic_block new_bb
);
545 /* Width as described in HSA documentation. */
546 BrigWidth8_t m_width
;
549 vec
<basic_block
> m_jump_table
;
551 /* Default label basic block. */
552 basic_block m_default_bb
;
554 /* Code list for label references. */
555 hsa_op_code_list
*m_label_code_list
;
558 /* Make the default constructor inaccessible. */
559 hsa_insn_sbr () : hsa_insn_basic (1, BRIG_OPCODE_SBR
) {}
560 /* All objects are deallocated by destroying their pool, so make delete
562 void operator delete (void *) {}
565 /* Report whether P is a switch branching instruction. */
570 is_a_helper
<hsa_insn_sbr
*>::test (hsa_insn_basic
*p
)
572 return p
->m_opcode
== BRIG_OPCODE_SBR
;
575 /* HSA instruction for comparisons. */
577 class hsa_insn_cmp
: public hsa_insn_basic
580 hsa_insn_cmp (BrigCompareOperation8_t cmp
, BrigType16_t t
,
581 hsa_op_base
*arg0
= NULL
, hsa_op_base
*arg1
= NULL
,
582 hsa_op_base
*arg2
= NULL
);
584 void *operator new (size_t);
586 /* Source type should be derived from operand types. */
588 /* The comparison operation. */
589 BrigCompareOperation8_t m_compare
;
591 /* TODO: Modifiers and packing control are missing but so are everywhere
594 /* Make the default constructor inaccessible. */
595 hsa_insn_cmp () : hsa_insn_basic (1, BRIG_OPCODE_CMP
) {}
596 /* All objects are deallocated by destroying their pool, so make delete
598 void operator delete (void *) {}
601 /* Report whether or not P is a comparison instruction. */
606 is_a_helper
<hsa_insn_cmp
*>::test (hsa_insn_basic
*p
)
608 return p
->m_opcode
== BRIG_OPCODE_CMP
;
611 /* HSA instruction for memory operations. */
613 class hsa_insn_mem
: public hsa_insn_basic
616 hsa_insn_mem (int opc
, BrigType16_t t
, hsa_op_base
*arg0
, hsa_op_base
*arg1
);
618 void *operator new (size_t);
620 /* Set alignment to VALUE. */
622 void set_align (BrigAlignment8_t value
);
624 /* The segment is of the memory access is either the segment of the symbol in
625 the address operand or flat address is there is no symbol there. */
627 /* Required alignment of the memory operation. */
628 BrigAlignment8_t m_align
;
630 /* HSA equiv class, basically an alias set number. */
631 uint8_t m_equiv_class
;
633 /* TODO: Add width modifier, perhaps also other things. */
635 hsa_insn_mem (unsigned nops
, int opc
, BrigType16_t t
,
636 hsa_op_base
*arg0
= NULL
, hsa_op_base
*arg1
= NULL
,
637 hsa_op_base
*arg2
= NULL
, hsa_op_base
*arg3
= NULL
);
640 /* Make the default constructor inaccessible. */
641 hsa_insn_mem () : hsa_insn_basic (1, BRIG_OPCODE_LD
) {}
642 /* All objects are deallocated by destroying their pool, so make delete
644 void operator delete (void *) {}
647 /* Report whether or not P is a memory instruction. */
652 is_a_helper
<hsa_insn_mem
*>::test (hsa_insn_basic
*p
)
654 return (p
->m_opcode
== BRIG_OPCODE_LD
655 || p
->m_opcode
== BRIG_OPCODE_ST
);
658 /* HSA instruction for atomic operations. */
660 class hsa_insn_atomic
: public hsa_insn_mem
663 hsa_insn_atomic (int nops
, int opc
, enum BrigAtomicOperation aop
,
664 BrigType16_t t
, BrigMemoryOrder memorder
,
665 hsa_op_base
*arg0
= NULL
, hsa_op_base
*arg1
= NULL
,
666 hsa_op_base
*arg2
= NULL
, hsa_op_base
*arg3
= NULL
);
667 void *operator new (size_t);
669 /* The operation itself. */
670 enum BrigAtomicOperation m_atomicop
;
672 /* Things like acquire/release/aligned. */
673 enum BrigMemoryOrder m_memoryorder
;
675 /* Scope of the atomic operation. */
676 enum BrigMemoryScope m_memoryscope
;
679 /* Make the default constructor inaccessible. */
680 hsa_insn_atomic () : hsa_insn_mem (1, BRIG_KIND_NONE
, BRIG_TYPE_NONE
) {}
681 /* All objects are deallocated by destroying their pool, so make delete
683 void operator delete (void *) {}
686 /* Report whether or not P is an atomic instruction. */
691 is_a_helper
<hsa_insn_atomic
*>::test (hsa_insn_basic
*p
)
693 return (p
->m_opcode
== BRIG_OPCODE_ATOMIC
694 || p
->m_opcode
== BRIG_OPCODE_ATOMICNORET
);
697 /* HSA instruction for signal operations. */
699 class hsa_insn_signal
: public hsa_insn_atomic
702 hsa_insn_signal (int nops
, int opc
, enum BrigAtomicOperation sop
,
703 BrigType16_t t
, hsa_op_base
*arg0
= NULL
,
704 hsa_op_base
*arg1
= NULL
,
705 hsa_op_base
*arg2
= NULL
, hsa_op_base
*arg3
= NULL
);
707 void *operator new (size_t);
710 /* All objects are deallocated by destroying their pool, so make delete
712 void operator delete (void *) {}
715 /* Report whether or not P is a signal instruction. */
720 is_a_helper
<hsa_insn_signal
*>::test (hsa_insn_basic
*p
)
722 return (p
->m_opcode
== BRIG_OPCODE_SIGNAL
723 || p
->m_opcode
== BRIG_OPCODE_SIGNALNORET
);
726 /* HSA instruction to convert between flat addressing and segments. */
728 class hsa_insn_seg
: public hsa_insn_basic
731 hsa_insn_seg (int opc
, BrigType16_t destt
, BrigType16_t srct
,
732 BrigSegment8_t seg
, hsa_op_base
*arg0
, hsa_op_base
*arg1
);
734 void *operator new (size_t);
736 /* Source type. Depends on the source addressing/segment. */
737 BrigType16_t m_src_type
;
738 /* The segment we are converting from or to. */
739 BrigSegment8_t m_segment
;
741 /* Make the default constructor inaccessible. */
742 hsa_insn_seg () : hsa_insn_basic (1, BRIG_OPCODE_STOF
) {}
743 /* All objects are deallocated by destroying their pool, so make delete
745 void operator delete (void *) {}
748 /* Report whether or not P is a segment conversion instruction. */
753 is_a_helper
<hsa_insn_seg
*>::test (hsa_insn_basic
*p
)
755 return (p
->m_opcode
== BRIG_OPCODE_STOF
756 || p
->m_opcode
== BRIG_OPCODE_FTOS
);
759 /* Class for internal functions for purpose of HSA emission. */
761 class hsa_internal_fn
764 hsa_internal_fn (enum internal_fn fn
, unsigned type_bit_size
):
765 m_fn (fn
), m_type_bit_size (type_bit_size
), m_offset (0) {}
767 hsa_internal_fn (const hsa_internal_fn
*f
):
768 m_fn (f
->m_fn
), m_type_bit_size (f
->m_type_bit_size
),
769 m_offset (f
->m_offset
) {}
771 /* Return arity of the internal function. */
772 unsigned get_arity ();
774 /* Return BRIG type of N-th argument, if -1 is passed, return value type
776 BrigType16_t
get_argument_type (int n
);
778 /* Return function name. The memory must be released by a caller. */
781 /* Internal function. */
782 enum internal_fn m_fn
;
784 /* Bit width of return type. */
785 unsigned m_type_bit_size
;
787 /* BRIG offset of declaration of the function. */
788 BrigCodeOffset32_t m_offset
;
791 /* HSA instruction for function call. */
793 class hsa_insn_call
: public hsa_insn_basic
796 hsa_insn_call (tree callee
);
797 hsa_insn_call (hsa_internal_fn
*fn
);
799 /* Default destructor. */
802 void *operator new (size_t);
804 /* Called function. */
805 tree m_called_function
;
807 /* Called internal function. */
808 hsa_internal_fn
*m_called_internal_fn
;
810 /* Input formal arguments. */
811 auto_vec
<hsa_symbol
*> m_input_args
;
813 /* Input arguments store instructions. */
814 auto_vec
<hsa_insn_mem
*> m_input_arg_insns
;
816 /* Output argument, can be NULL for void functions. */
817 hsa_symbol
*m_output_arg
;
819 /* Called function code reference. */
820 hsa_op_code_ref m_func
;
822 /* Code list for arguments of the function. */
823 hsa_op_code_list
*m_args_code_list
;
825 /* Code list for result of the function. */
826 hsa_op_code_list
*m_result_code_list
;
828 /* Make the default constructor inaccessible. */
829 hsa_insn_call () : hsa_insn_basic (0, BRIG_OPCODE_CALL
) {}
830 /* All objects are deallocated by destroying their pool, so make delete
832 void operator delete (void *) {}
835 /* Report whether or not P is a call instruction. */
840 is_a_helper
<hsa_insn_call
*>::test (hsa_insn_basic
*p
)
842 return (p
->m_opcode
== BRIG_OPCODE_CALL
);
845 /* HSA call instruction block encapsulates definition of arguments,
846 result type, corresponding loads and a possible store.
847 Moreover, it contains a single call instruction.
848 Emission of the instruction will produce multiple
849 HSAIL instructions. */
851 class hsa_insn_arg_block
: public hsa_insn_basic
854 hsa_insn_arg_block (BrigKind brig_kind
, hsa_insn_call
* call
);
856 void *operator new (size_t);
858 /* Kind of argument block. */
861 /* Call instruction. */
862 hsa_insn_call
*m_call_insn
;
864 /* All objects are deallocated by destroying their pool, so make delete
866 void operator delete (void *) {}
869 /* Report whether or not P is a call block instruction. */
874 is_a_helper
<hsa_insn_arg_block
*>::test (hsa_insn_basic
*p
)
876 return (p
->m_opcode
== HSA_OPCODE_ARG_BLOCK
);
879 /* HSA comment instruction. */
881 class hsa_insn_comment
: public hsa_insn_basic
884 /* Constructor of class representing the comment in HSAIL. */
885 hsa_insn_comment (const char *s
);
887 /* Default destructor. */
888 ~hsa_insn_comment ();
890 void *operator new (size_t);
895 /* Report whether or not P is a call block instruction. */
900 is_a_helper
<hsa_insn_comment
*>::test (hsa_insn_basic
*p
)
902 return (p
->m_opcode
== BRIG_KIND_DIRECTIVE_COMMENT
);
905 /* HSA queue instruction. */
907 class hsa_insn_queue
: public hsa_insn_basic
910 hsa_insn_queue (int nops
, BrigOpcode opcode
);
916 /* Report whether or not P is a queue instruction. */
921 is_a_helper
<hsa_insn_queue
*>::test (hsa_insn_basic
*p
)
923 return (p
->m_opcode
== BRIG_OPCODE_ADDQUEUEWRITEINDEX
);
926 /* HSA source type instruction. */
928 class hsa_insn_srctype
: public hsa_insn_basic
931 hsa_insn_srctype (int nops
, BrigOpcode opcode
, BrigType16_t destt
,
932 BrigType16_t srct
, hsa_op_base
*arg0
, hsa_op_base
*arg1
,
935 /* Pool allocator. */
936 void *operator new (size_t);
939 BrigType16_t m_source_type
;
942 ~hsa_insn_srctype ();
945 /* Report whether or not P is a source type instruction. */
950 is_a_helper
<hsa_insn_srctype
*>::test (hsa_insn_basic
*p
)
952 return (p
->m_opcode
== BRIG_OPCODE_POPCOUNT
953 || p
->m_opcode
== BRIG_OPCODE_FIRSTBIT
954 || p
->m_opcode
== BRIG_OPCODE_LASTBIT
);
957 /* HSA packed instruction. */
959 class hsa_insn_packed
: public hsa_insn_srctype
962 hsa_insn_packed (int nops
, BrigOpcode opcode
, BrigType16_t destt
,
963 BrigType16_t srct
, hsa_op_base
*arg0
, hsa_op_base
*arg1
,
966 /* Pool allocator. */
967 void *operator new (size_t);
969 /* Operand list for an operand of the instruction. */
970 hsa_op_operand_list
*m_operand_list
;
976 /* Report whether or not P is a combine instruction. */
981 is_a_helper
<hsa_insn_packed
*>::test (hsa_insn_basic
*p
)
983 return (p
->m_opcode
== BRIG_OPCODE_COMBINE
984 || p
->m_opcode
== BRIG_OPCODE_EXPAND
);
987 /* HSA convert instruction. */
989 class hsa_insn_cvt
: public hsa_insn_basic
992 hsa_insn_cvt (hsa_op_with_type
*dest
, hsa_op_with_type
*src
);
994 /* Pool allocator. */
995 void *operator new (size_t);
998 /* Report whether or not P is a convert instruction. */
1003 is_a_helper
<hsa_insn_cvt
*>::test (hsa_insn_basic
*p
)
1005 return (p
->m_opcode
== BRIG_OPCODE_CVT
);
1008 /* HSA alloca instruction. */
1010 class hsa_insn_alloca
: public hsa_insn_basic
1013 hsa_insn_alloca (hsa_op_with_type
*dest
, hsa_op_with_type
*size
,
1014 unsigned alignment
= 0);
1016 /* Required alignment of the allocation. */
1017 BrigAlignment8_t m_align
;
1019 /* Pool allocator. */
1020 void *operator new (size_t);
1023 /* Report whether or not P is an alloca instruction. */
1028 is_a_helper
<hsa_insn_alloca
*>::test (hsa_insn_basic
*p
)
1030 return (p
->m_opcode
== BRIG_OPCODE_ALLOCA
);
1033 /* Basic block of HSA instructions. */
1038 hsa_bb (basic_block cfg_bb
);
1039 hsa_bb (basic_block cfg_bb
, int idx
);
1042 /* Append an instruction INSN into the basic block. */
1043 void append_insn (hsa_insn_basic
*insn
);
1045 /* The real CFG BB that this HBB belongs to. */
1048 /* The operand that refers to the label to this BB. */
1049 hsa_op_code_ref m_label_ref
;
1051 /* The first and last instruction. */
1052 hsa_insn_basic
*m_first_insn
, *m_last_insn
;
1053 /* The first and last phi node. */
1054 hsa_insn_phi
*m_first_phi
, *m_last_phi
;
1056 /* Just a number to construct names from. */
1059 bitmap m_liveout
, m_livein
;
1061 /* Make the default constructor inaccessible. */
1063 /* All objects are deallocated by destroying their pool, so make delete
1064 inaccessible too. */
1065 void operator delete (void *) {}
1068 /* Return the corresponding HSA basic block structure for the given control
1069 flow basic_block BB. */
1071 static inline hsa_bb
*
1072 hsa_bb_for_bb (basic_block bb
)
1074 return (struct hsa_bb
*) bb
->aux
;
1077 /* Class for hashing local hsa_symbols. */
1079 struct hsa_noop_symbol_hasher
: nofree_ptr_hash
<hsa_symbol
>
1081 static inline hashval_t
hash (const value_type
);
1082 static inline bool equal (const value_type
, const compare_type
);
1085 /* Hash hsa_symbol. */
1088 hsa_noop_symbol_hasher::hash (const value_type item
)
1090 return DECL_UID (item
->m_decl
);
1093 /* Return true if the DECL_UIDs of decls both symbols refer to are equal. */
1096 hsa_noop_symbol_hasher::equal (const value_type a
, const compare_type b
)
1098 return (DECL_UID (a
->m_decl
) == DECL_UID (b
->m_decl
));
1101 /* Structure that encapsulates intermediate representation of a HSA
1104 class hsa_function_representation
1107 hsa_function_representation (tree fdecl
, bool kernel_p
,
1108 unsigned ssa_names_count
);
1109 hsa_function_representation (hsa_internal_fn
*fn
);
1110 ~hsa_function_representation ();
1112 /* Builds a shadow register that is utilized to a kernel dispatch. */
1113 hsa_op_reg
*get_shadow_reg ();
1115 /* Return true if we are in a function that has kernel dispatch
1117 bool has_shadow_reg_p ();
1119 /* The entry/exit blocks don't contain incoming code,
1120 but the HSA generator might use them to put code into,
1121 so we need hsa_bb instances of them. */
1122 void init_extra_bbs ();
1124 /* Return linkage of the representation. */
1125 BrigLinkage8_t
get_linkage ();
1127 /* Create a private symbol of requested TYPE. */
1128 hsa_symbol
*create_hsa_temporary (BrigType16_t type
);
1130 /* Lookup or create a HSA pseudo register for a given gimple SSA name. */
1131 hsa_op_reg
*reg_for_gimple_ssa (tree ssa
);
1133 /* Name of the function. */
1136 /* Number of allocated register structures. */
1139 /* Input arguments. */
1140 vec
<hsa_symbol
*> m_input_args
;
1142 /* Output argument or NULL if there is none. */
1143 hsa_symbol
*m_output_arg
;
1145 /* Hash table of local variable symbols. */
1146 hash_table
<hsa_noop_symbol_hasher
> *m_local_symbols
;
1148 /* Hash map for string constants. */
1149 hash_map
<tree
, hsa_symbol
*> m_string_constants_map
;
1151 /* Vector of pointers to spill symbols. */
1152 vec
<struct hsa_symbol
*> m_spill_symbols
;
1154 /* Vector of pointers to global variables and transformed string constants
1155 that are used by the function. */
1156 vec
<struct hsa_symbol
*> m_global_symbols
;
1158 /* Private function artificial variables. */
1159 vec
<struct hsa_symbol
*> m_private_variables
;
1161 /* Vector of called function declarations. */
1162 vec
<tree
> m_called_functions
;
1164 /* Vector of used internal functions. */
1165 vec
<hsa_internal_fn
*> m_called_internal_fns
;
1167 /* Number of HBB BBs. */
1170 /* Whether or not we could check and enforce SSA properties. */
1173 /* True if the function is kernel function. */
1176 /* True if the function representation is a declaration. */
1177 bool m_declaration_p
;
1179 /* Function declaration tree. */
1182 /* Internal function info is used for declarations of internal functions. */
1183 hsa_internal_fn
*m_internal_fn
;
1185 /* Runtime shadow register. */
1186 hsa_op_reg
*m_shadow_reg
;
1188 /* Number of kernel dispatched which take place in the function. */
1189 unsigned m_kernel_dispatch_count
;
1191 /* If the function representation contains a kernel dispatch,
1192 OMP data size is necessary memory that is used for copying before
1193 a kernel dispatch. */
1194 unsigned m_maximum_omp_data_size
;
1196 /* Return true if there's an HSA-specific warning already seen. */
1199 /* Counter for temporary symbols created in the function representation. */
1200 unsigned m_temp_symbol_count
;
1202 /* SSA names mapping. */
1203 vec
<hsa_op_reg_p
> m_ssa_map
;
1206 enum hsa_function_kind
1213 struct hsa_function_summary
1215 /* Default constructor. */
1216 hsa_function_summary ();
1218 /* Kind of GPU/host function. */
1219 hsa_function_kind m_kind
;
1221 /* Pointer to a cgraph node which is a HSA implementation of the function.
1222 In case of the function is a HSA function, the binded function points
1223 to the host function. */
1224 cgraph_node
*m_binded_function
;
1226 /* Identifies if the function is an HSA function or a host function. */
1227 bool m_gpu_implementation_p
;
1229 /* True if the function is a gridified kernel. */
1230 bool m_gridified_kernel_p
;
1234 hsa_function_summary::hsa_function_summary (): m_kind (HSA_NONE
),
1235 m_binded_function (NULL
), m_gpu_implementation_p (false)
1239 /* Function summary for HSA functions. */
1240 class hsa_summary_t
: public function_summary
<hsa_function_summary
*>
1243 hsa_summary_t (symbol_table
*table
):
1244 function_summary
<hsa_function_summary
*> (table
) { }
1246 /* Couple GPU and HOST as gpu-specific and host-specific implementation of
1247 the same function. KIND determines whether GPU is a host-invokable kernel
1248 or gpu-callable function and GRIDIFIED_KERNEL_P is set if the function was
1249 gridified in OMP. */
1251 void link_functions (cgraph_node
*gpu
, cgraph_node
*host
,
1252 hsa_function_kind kind
, bool gridified_kernel_p
);
1255 /* OMP simple builtin describes behavior that should be done for
1257 class omp_simple_builtin
1260 omp_simple_builtin (const char *name
, const char *warning_message
,
1261 bool sorry
, hsa_op_immed
*return_value
= NULL
):
1262 m_name (name
), m_warning_message (warning_message
), m_sorry (sorry
),
1263 m_return_value (return_value
)
1266 /* Generate HSAIL instructions for the builtin or produce warning message. */
1267 void generate (gimple
*stmt
, hsa_bb
*hbb
);
1269 /* Name of function. */
1272 /* Warning message. */
1273 const char *m_warning_message
;
1275 /* Flag if we should sorry after the warning message is printed. */
1278 /* Return value of the function. */
1279 hsa_op_immed
*m_return_value
;
1281 /* Emission function. */
1282 void (*m_emit_func
) (gimple
*stmt
, hsa_bb
*);
1285 /* Class for hashing hsa_internal_fn. */
1287 struct hsa_internal_fn_hasher
: free_ptr_hash
<hsa_internal_fn
>
1289 static inline hashval_t
hash (const value_type
);
1290 static inline bool equal (const value_type
, const compare_type
);
1293 /* Hash hsa_symbol. */
1296 hsa_internal_fn_hasher::hash (const value_type item
)
1301 /* Return true if the DECL_UIDs of decls both symbols refer to are equal. */
1304 hsa_internal_fn_hasher::equal (const value_type a
, const compare_type b
)
1306 return a
->m_fn
== b
->m_fn
&& a
->m_type_bit_size
== b
->m_type_bit_size
;
1310 extern struct hsa_function_representation
*hsa_cfun
;
1311 extern hash_map
<tree
, vec
<const char *> *> *hsa_decl_kernel_dependencies
;
1312 extern hsa_summary_t
*hsa_summaries
;
1313 extern hsa_symbol
*hsa_num_threads
;
1314 extern unsigned hsa_kernel_calls_counter
;
1315 extern hash_set
<tree
> *hsa_failed_functions
;
1316 extern hash_table
<hsa_noop_symbol_hasher
> *hsa_global_variable_symbols
;
1318 bool hsa_callable_function_p (tree fndecl
);
1319 void hsa_init_compilation_unit_data (void);
1320 void hsa_deinit_compilation_unit_data (void);
1321 bool hsa_machine_large_p (void);
1322 bool hsa_full_profile_p (void);
1323 bool hsa_opcode_floating_bit_insn_p (BrigOpcode16_t
);
1324 unsigned hsa_type_bit_size (BrigType16_t t
);
1325 BrigType16_t
hsa_bittype_for_bitsize (unsigned bitsize
);
1326 BrigType16_t
hsa_uint_for_bitsize (unsigned bitsize
);
1327 BrigType16_t
hsa_float_for_bitsize (unsigned bitsize
);
1328 BrigType16_t
hsa_bittype_for_type (BrigType16_t t
);
1329 bool hsa_type_float_p (BrigType16_t type
);
1330 bool hsa_type_integer_p (BrigType16_t type
);
1331 bool hsa_btype_p (BrigType16_t type
);
1332 BrigAlignment8_t
hsa_alignment_encoding (unsigned n
);
1333 BrigAlignment8_t
hsa_natural_alignment (BrigType16_t type
);
1334 void hsa_destroy_operand (hsa_op_base
*op
);
1335 void hsa_destroy_insn (hsa_insn_basic
*insn
);
1336 void hsa_add_kern_decl_mapping (tree decl
, char *name
, unsigned, bool);
1337 unsigned hsa_get_number_decl_kernel_mappings (void);
1338 tree
hsa_get_decl_kernel_mapping_decl (unsigned i
);
1339 char *hsa_get_decl_kernel_mapping_name (unsigned i
);
1340 unsigned hsa_get_decl_kernel_mapping_omp_size (unsigned i
);
1341 bool hsa_get_decl_kernel_mapping_gridified (unsigned i
);
1342 void hsa_free_decl_kernel_mapping (void);
1343 void hsa_add_kernel_dependency (tree caller
, const char *called_function
);
1344 void hsa_sanitize_name (char *p
);
1345 char *hsa_brig_function_name (const char *p
);
1346 const char *hsa_get_declaration_name (tree decl
);
1347 void hsa_register_kernel (cgraph_node
*host
);
1348 void hsa_register_kernel (cgraph_node
*gpu
, cgraph_node
*host
);
1349 bool hsa_seen_error (void);
1350 void hsa_fail_cfun (void);
1353 void hsa_build_append_simple_mov (hsa_op_reg
*, hsa_op_base
*, hsa_bb
*);
1354 hsa_symbol
*hsa_get_spill_symbol (BrigType16_t
);
1355 hsa_symbol
*hsa_get_string_cst_symbol (BrigType16_t
);
1356 hsa_op_reg
*hsa_spill_in (hsa_insn_basic
*, hsa_op_reg
*, hsa_op_reg
**);
1357 hsa_op_reg
*hsa_spill_out (hsa_insn_basic
*, hsa_op_reg
*, hsa_op_reg
**);
1358 hsa_bb
*hsa_init_new_bb (basic_block
);
1359 hsa_function_representation
*hsa_generate_function_declaration (tree decl
);
1360 hsa_function_representation
*hsa_generate_internal_fn_decl (hsa_internal_fn
*);
1361 tree
hsa_get_host_function (tree decl
);
1363 /* In hsa-regalloc.c. */
1364 void hsa_regalloc (void);
1366 /* In hsa-brig.c. */
1367 extern hash_table
<hsa_internal_fn_hasher
> *hsa_emitted_internal_decls
;
1368 void hsa_brig_emit_function (void);
1369 void hsa_output_brig (void);
1370 unsigned hsa_get_imm_brig_type_len (BrigType16_t type
);
1371 void hsa_brig_emit_omp_symbols (void);
1373 /* In hsa-dump.c. */
1374 const char *hsa_seg_name (BrigSegment8_t
);
1375 void dump_hsa_insn (FILE *f
, hsa_insn_basic
*insn
);
1376 void dump_hsa_bb (FILE *, hsa_bb
*);
1377 void dump_hsa_cfun (FILE *);
1378 DEBUG_FUNCTION
void debug_hsa_operand (hsa_op_base
*opc
);
1379 DEBUG_FUNCTION
void debug_hsa_insn (hsa_insn_basic
*insn
);
1389 /* Return true if a function DECL is an HSA implementation. */
1392 hsa_gpu_implementation_p (tree decl
)
1394 if (hsa_summaries
== NULL
)
1397 hsa_function_summary
*s
= hsa_summaries
->get (cgraph_node::get_create (decl
));
1399 return s
->m_gpu_implementation_p
;