111
|
1 /* brig-to-generic.h -- brig to gcc generic conversion
|
|
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_TO_GENERIC_H
|
|
23 #define BRIG_TO_GENERIC_H
|
|
24
|
|
25 #include <string>
|
|
26 #include <map>
|
|
27 #include <vector>
|
|
28
|
|
29 #include "config.h"
|
|
30 #include "system.h"
|
|
31 #include "ansidecl.h"
|
|
32 #include "coretypes.h"
|
|
33 #include "opts.h"
|
|
34 #include "tree.h"
|
|
35 #include "tree-iterator.h"
|
|
36 #include "hsa-brig-format.h"
|
|
37 #include "brig-function.h"
|
|
38
|
|
39 struct reg_decl_index_entry;
|
|
40
|
|
41 /* Converts an HSAIL BRIG input to GENERIC. This class holds global state
|
|
42 for the translation process. Handling of the smaller pieces of BRIG data
|
|
43 is delegated to various handler classes declared in
|
|
44 brig-code-entry-handlers.h. */
|
|
45
|
|
46 class brig_to_generic
|
|
47 {
|
|
48 public:
|
|
49 typedef std::map<const BrigDirectiveVariable *, tree> variable_index;
|
|
50
|
|
51 private:
|
|
52 typedef std::map<std::string, size_t> var_offset_table;
|
|
53 typedef std::map<const BrigBase *, std::string> name_index;
|
|
54
|
|
55 public:
|
|
56 brig_to_generic ();
|
|
57
|
|
58 void analyze (const char *brig_blob);
|
|
59 void parse (const char *brig_blob);
|
|
60
|
|
61 void write_globals ();
|
|
62
|
|
63 std::string get_string (size_t entry_offset) const;
|
|
64
|
|
65 const BrigData *get_brig_data_entry (size_t entry_offset) const;
|
|
66 const BrigBase *get_brig_operand_entry (size_t entry_offset) const;
|
|
67 const BrigBase *get_brig_code_entry (size_t entry_offset) const;
|
|
68
|
|
69 void append_global (tree g);
|
|
70
|
|
71 tree function_decl (const std::string &name);
|
|
72 void add_function_decl (const std::string &name, tree func_decl);
|
|
73
|
|
74 tree global_variable (const std::string &name) const;
|
|
75 void add_global_variable (const std::string &name, tree var_decl);
|
|
76 void add_host_def_var_ptr (const std::string &name, tree var_decl);
|
|
77
|
|
78 void start_function (tree f);
|
|
79 void finish_function ();
|
|
80
|
|
81 void append_private_variable (const std::string &name, size_t size,
|
|
82 size_t alignment);
|
|
83
|
|
84 size_t
|
|
85 private_variable_segment_offset (const std::string &name) const;
|
|
86
|
|
87 bool
|
|
88 has_private_variable (const std::string &name) const;
|
|
89
|
|
90 size_t private_variable_size (const std::string &name) const;
|
|
91
|
|
92 template <typename T>
|
|
93 std::string
|
|
94 get_mangled_name_tmpl (const T *brigVar) const;
|
|
95
|
|
96 std::string get_mangled_name (const BrigDirectiveFbarrier *fbar) const
|
|
97 { return get_mangled_name_tmpl (fbar); }
|
|
98 std::string get_mangled_name (const BrigDirectiveVariable *var) const
|
|
99 { return get_mangled_name_tmpl (var); }
|
|
100 std::string get_mangled_name (const BrigDirectiveExecutable *func) const;
|
|
101
|
|
102 size_t private_segment_size () const;
|
|
103
|
|
104 brig_function *get_finished_function (tree func_decl);
|
|
105
|
|
106 void add_group_variable (const std::string &name, size_t size,
|
|
107 size_t alignment, bool function_scope);
|
|
108
|
|
109 static tree s_fp16_type;
|
|
110 static tree s_fp32_type;
|
|
111 static tree s_fp64_type;
|
|
112
|
|
113 /* The default rounding mode that should be used for float instructions.
|
|
114 This can be set in each BRIG module header. */
|
|
115 BrigRound8_t m_default_float_rounding_mode;
|
|
116
|
|
117 /* The currently built function. */
|
|
118 brig_function *m_cf;
|
|
119
|
|
120 /* Stores the module and function scope group variable offsets. */
|
|
121 group_variable_offset_index m_module_group_variables;
|
|
122
|
|
123 /* The name of the currently handled BRIG module. */
|
|
124 std::string m_module_name;
|
|
125
|
|
126 /* Set to true if the compilation is in 'analyze' phase. */
|
|
127 bool m_analyzing;
|
|
128
|
|
129 /* Accumulates the total group segment usage. */
|
|
130 size_t m_total_group_segment_usage;
|
|
131
|
|
132 private:
|
|
133
|
|
134 void find_brig_sections ();
|
|
135 /* The BRIG blob and its different sections of the file currently being
|
|
136 parsed. */
|
|
137 const char *m_brig;
|
|
138 const char *m_data;
|
|
139 size_t m_data_size;
|
|
140 const char *m_operand;
|
|
141 size_t m_operand_size;
|
|
142 const char *m_code;
|
|
143 size_t m_code_size;
|
|
144
|
|
145 tree m_globals;
|
|
146
|
|
147 label_index m_global_variables;
|
|
148
|
|
149 /* The size of each private variable, including the alignment padding. */
|
|
150 std::map<std::string, size_t> m_private_data_sizes;
|
|
151
|
|
152 /* And private. */
|
|
153 size_t m_next_private_offset;
|
|
154 var_offset_table m_private_offsets;
|
|
155
|
|
156 /* Name index for declared functions. */
|
|
157 label_index m_function_index;
|
|
158
|
|
159 /* Stores all processed kernels in order. */
|
|
160 std::vector<brig_function *> m_kernels;
|
|
161
|
|
162 /* Stores all already processed functions from the translation unit
|
|
163 for some interprocedural analysis. */
|
|
164 std::map<std::string, brig_function *> m_finished_functions;
|
|
165
|
|
166 /* The original dump file. */
|
|
167 FILE *m_dump_file;
|
|
168
|
|
169 /* The original dump file flags. */
|
|
170 dump_flags_t m_dump_flags;
|
|
171 };
|
|
172
|
|
173 /* Produce a "mangled name" for the given brig variable. The mangling is used
|
|
174 to make unique global symbol names for module and function scope variables.
|
|
175 The templated version is suitable for most of the variable types. Functions
|
|
176 and kernels (BrigDirectiveExecutable) are handled with a specialized
|
|
177 get_mangled_name() version. */
|
|
178
|
|
179 template <typename T>
|
|
180 std::string
|
|
181 brig_to_generic::get_mangled_name_tmpl (const T *brigVar) const
|
|
182 {
|
|
183 std::string var_name = get_string (brigVar->name).substr (1);
|
|
184
|
|
185 /* Mangle the variable name using the function name and the module name
|
|
186 in case of a function scope variable. */
|
|
187 if (m_cf != NULL
|
|
188 && m_cf->has_function_scope_var (&brigVar->base))
|
|
189 var_name = m_cf->m_name + "." + var_name;
|
|
190
|
|
191 if (brigVar->linkage == BRIG_LINKAGE_MODULE)
|
|
192 var_name = "gccbrig." + m_module_name + "." + var_name;
|
|
193 return var_name;
|
|
194 }
|
|
195
|
|
196 /* An interface to organize the different types of BRIG element handlers. */
|
|
197
|
|
198 class brig_entry_handler
|
|
199 {
|
|
200 public:
|
|
201 brig_entry_handler (brig_to_generic &parent) : m_parent (parent)
|
|
202 {
|
|
203 }
|
|
204
|
|
205 /* Handles the brig_code data at the given pointer and adds it to the
|
|
206 currently built tree. Returns the number of consumed bytes; */
|
|
207 virtual size_t operator () (const BrigBase *base) = 0;
|
|
208
|
|
209 protected:
|
|
210 brig_to_generic &m_parent;
|
|
211 };
|
|
212
|
|
213 tree call_builtin (tree pdecl, int nargs, tree rettype, ...);
|
|
214
|
|
215 tree build_reinterpret_cast (tree destination_type, tree source);
|
|
216
|
|
217 tree build_stmt (enum tree_code code, ...);
|
|
218
|
|
219 tree get_unsigned_int_type (tree type);
|
|
220
|
|
221 void dump_function (FILE *dump_file, brig_function *f);
|
|
222
|
|
223 #endif
|