1 /* brig-function.h -- declaration of brig_function class. 2 Copyright (C) 2016-2017 Free Software Foundation, Inc. 3 Contributed by Pekka Jaaskelainen <pekka.jaaskelainen@parmance.com> 4 for General Processor Tech. 5 6 This file is part of GCC. 7 8 GCC is free software; you can redistribute it and/or modify it under 9 the terms of the GNU General Public License as published by the Free 10 Software Foundation; either version 3, or (at your option) any later 11 version. 12 13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY 14 WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with GCC; see the file COPYING3. If not see 20 <http://www.gnu.org/licenses/>. */ 21 22 #ifndef BRIG_FUNCTION_H 23 #define BRIG_FUNCTION_H 24 25 #include "config.h" 26 #include "system.h" 27 #include "ansidecl.h" 28 #include "coretypes.h" 29 #include "opts.h" 30 #include "tree.h" 31 #include "tree-iterator.h" 32 #include "hsa-brig-format.h" 33 34 class brig_to_generic; 35 36 #include <map> 37 #include <string> 38 #include <vector> 39 #include <set> 40 41 #include "phsa.h" 42 43 typedef std::map<std::string, tree> label_index; 44 typedef std::map<const BrigDirectiveVariable *, tree> variable_index; 45 typedef std::vector<tree> tree_stl_vec; 46 47 /* There are 128 c regs and 2048 s/d/q regs each in the HSAIL. */ 48 #define BRIG_2_TREE_HSAIL_C_REG_COUNT (128) 49 #define BRIG_2_TREE_HSAIL_S_REG_COUNT (2048) 50 #define BRIG_2_TREE_HSAIL_D_REG_COUNT (2048) 51 #define BRIG_2_TREE_HSAIL_Q_REG_COUNT (2048) 52 #define BRIG_2_TREE_HSAIL_TOTAL_REG_COUNT \ 53 (BRIG_2_TREE_HSAIL_C_REG_COUNT + BRIG_2_TREE_HSAIL_S_REG_COUNT \ 54 + BRIG_2_TREE_HSAIL_D_REG_COUNT + BRIG_2_TREE_HSAIL_Q_REG_COUNT) 55 56 /* Holds data for the currently built GENERIC function. */ 57 58 class brig_function 59 { 60 public: 61 typedef std::map<const BrigDirectiveVariable *, size_t> var_offset_table; 62 63 private: 64 struct reg_decl_index_entry 65 { 66 tree m_var_decl; 67 }; 68 69 public: 70 brig_function (const BrigDirectiveExecutable *exec, brig_to_generic *parent); 71 ~brig_function (); 72 73 tree arg_variable (const BrigDirectiveVariable *var) const; 74 void add_arg_variable (const BrigDirectiveVariable *brigVar, tree treeDecl); 75 76 void append_kernel_arg (const BrigDirectiveVariable *var, size_t size, 77 size_t alignment); 78 79 size_t kernel_arg_offset (const BrigDirectiveVariable *var) const; 80 81 void add_id_variables (); 82 83 tree label (const std::string &name); 84 85 tree add_local_variable (std::string name, tree type); 86 87 tree get_m_var_declfor_reg (const BrigOperandRegister *reg); 88 89 bool convert_to_wg_function (); 90 91 void add_wi_loop (int dim, tree_stmt_iterator *header_entry, 92 tree_stmt_iterator *branch_after); 93 94 tree emit_metadata (tree stmt_list); 95 tree emit_launcher_and_metadata (); 96 97 tree append_statement (tree stmt); 98 99 void create_alloca_frame (); 100 101 void finish (); 102 void finish_kernel (); 103 104 void append_return_stmt (); 105 106 bool has_function_scope_var (const BrigBase* var) const; 107 108 void analyze_calls (); 109 110 const BrigDirectiveExecutable *m_brig_def; 111 112 bool m_is_kernel; 113 bool m_is_finished; 114 std::string m_name; 115 tree m_current_bind_expr; 116 tree m_func_decl; 117 tree m_entry_label_stmt; 118 tree m_exit_label; 119 120 /* The __context function argument. */ 121 tree m_context_arg; 122 /* The __group_base_ptr argument in the current function. 123 Points to the start of the group segment for the kernel 124 instance. */ 125 tree m_group_base_arg; 126 /* The __private_base_ptr argument in the current function. 127 Points to the start of the private segment. */ 128 tree m_private_base_arg; 129 130 /* The return value variable for the current function. */ 131 tree m_ret_value; 132 133 /* The offsets of the kernel arguments in the __arg blob 134 pointing to the kernel argument space. */ 135 size_t m_next_kernarg_offset; 136 137 /* The largest kernel argument variable alignment. */ 138 size_t m_kernarg_max_align; 139 140 var_offset_table m_kernarg_offsets; 141 142 /* Argument variables in the currently handled binding expression 143 (argument segment). */ 144 variable_index m_arg_variables; 145 146 /* The brig variable for the function return value. */ 147 const BrigDirectiveVariable *m_ret_value_brig_var; 148 149 /* The function local temporary variable for the return value. */ 150 tree m_ret_temp; 151 152 /* Labels in the current function are collected here so we can refer 153 to them from jumps before they have been placed to the function. */ 154 label_index m_label_index; 155 156 /* If the kernel contains at least one barrier, this is set to true. */ 157 bool m_has_barriers; 158 159 /* True if the function has at least one alloca instruction. */ 160 bool m_has_allocas; 161 162 /* If the kernel containts at least one function call that _may_ 163 contain a barrier call, this is set to true. */ 164 bool m_has_function_calls_with_barriers; 165 166 /* Set to true after this function has been analyzed for barrier and 167 dispatch packet instruction usage in the final call graph analysis. */ 168 bool m_calls_analyzed; 169 170 /* True in case the function was successfully converted to a WG function. */ 171 bool m_is_wg_function; 172 173 /* Work-item ID related variables are cached in the entry of the kernel 174 function in order to use them directly in address computations, leading 175 to more efficient optimizations. The references to the local variables 176 are stored here. */ 177 tree m_local_id_vars[3]; 178 tree m_cur_wg_size_vars[3]; 179 tree m_wg_id_vars[3]; 180 tree m_wg_size_vars[3]; 181 tree m_grid_size_vars[3]; 182 183 /* Set to true in case the kernel contains at least one dispatch packet 184 (work-item ID-related) builtin call that could not be expanded to 185 tree nodes. */ 186 bool m_has_unexpanded_dp_builtins; 187 188 /* Points to the instruction after which the real kernel code starts. 189 Usually points to the last WI ID variable initialization statement. */ 190 tree_stmt_iterator m_kernel_entry; 191 192 /* True if we are currently generating the contents of an arg block. */ 193 bool m_generating_arg_block; 194 195 /* A collection of function scope variables seen so far for resolving 196 variable references vs. module scope declarations. */ 197 std::set<const BrigBase*> m_function_scope_vars; 198 199 /* The functions called by this function. */ 200 std::vector<tree> m_called_functions; 201 202 brig_to_generic *m_parent; 203 /* The metadata of the function that should be stored with the binary and 204 passed to the HSA runtime: */ 205 phsa_descriptor m_descriptor; 206 207 private: 208 /* Bookkeeping for the different HSA registers and their tree declarations 209 for the currently generated function. */ 210 reg_decl_index_entry *m_regs[BRIG_2_TREE_HSAIL_TOTAL_REG_COUNT]; 211 }; 212 213 #endif 214