anv/entrypoints: Add dispatch support for haswell
[mesa.git] / src / vulkan / anv_entrypoints_gen.py
1 # coding=utf-8
2 #
3 # Copyright © 2015 Intel Corporation
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23 #
24
25 import fileinput, re, sys
26
27 # Each function typedef in the vulkan.h header is all on one line and matches
28 # this regepx. We hope that won't change.
29
30 p = re.compile('typedef ([^ ]*) *\(VKAPI \*PFN_vk([^(]*)\)(.*);')
31
32 entrypoints = []
33
34 # We generate a static hash table for entry point lookup
35 # (vkGetProcAddress). We use a linear congruential generator for our hash
36 # function and a power-of-two size table. The prime numbers are determined
37 # experimentally.
38
39 none = 0xffff
40 hash_size = 256
41 u32_mask = 2**32 - 1
42 hash_mask = hash_size - 1
43
44 prime_factor = 5024183
45 prime_step = 19
46
47 def hash(name):
48 h = 0;
49 for c in name:
50 h = (h * prime_factor + ord(c)) & u32_mask
51
52 return h
53
54 opt_header = False
55 opt_code = False
56
57 if (sys.argv[1] == "header"):
58 opt_header = True
59 sys.argv.pop()
60 elif (sys.argv[1] == "code"):
61 opt_code = True
62 sys.argv.pop()
63
64 # Parse the entry points in the header
65
66 i = 0
67 for line in fileinput.input():
68 m = p.match(line)
69 if (m):
70 if m.group(2) == 'VoidFunction':
71 continue
72 fullname = "vk" + m.group(2)
73 h = hash(fullname)
74 entrypoints.append((m.group(1), m.group(2), m.group(3), i, h))
75 i = i + 1
76
77 # For outputting entrypoints.h we generate a anv_EntryPoint() prototype
78 # per entry point.
79
80 if opt_header:
81 print "/* This file generated from vk_gen.py, don't edit directly. */\n"
82
83 print "struct anv_dispatch_table {"
84 print " union {"
85 print " void *entrypoints[%d];" % len(entrypoints)
86 print " struct {"
87
88 for type, name, args, num, h in entrypoints:
89 print " %s (*%s)%s;" % (type, name, args)
90 print " };\n"
91 print " };\n"
92 print "};\n"
93
94 print "void anv_set_dispatch_devinfo(const struct brw_device_info *info);\n"
95
96 for type, name, args, num, h in entrypoints:
97 print "%s anv_%s%s;" % (type, name, args)
98 print "%s gen7_%s%s;" % (type, name, args)
99 print "%s gen75_%s%s;" % (type, name, args)
100 print "%s gen8_%s%s;" % (type, name, args)
101 print "%s anv_validate_%s%s;" % (type, name, args)
102 exit()
103
104
105
106 print """/*
107 * Copyright © 2015 Intel Corporation
108 *
109 * Permission is hereby granted, free of charge, to any person obtaining a
110 * copy of this software and associated documentation files (the "Software"),
111 * to deal in the Software without restriction, including without limitation
112 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
113 * and/or sell copies of the Software, and to permit persons to whom the
114 * Software is furnished to do so, subject to the following conditions:
115 *
116 * The above copyright notice and this permission notice (including the next
117 * paragraph) shall be included in all copies or substantial portions of the
118 * Software.
119 *
120 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
121 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
122 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
123 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
124 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
125 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
126 * IN THE SOFTWARE.
127 */
128
129 /* DO NOT EDIT! This is a generated file. */
130
131 #include "anv_private.h"
132
133 struct anv_entrypoint {
134 uint32_t name;
135 uint32_t hash;
136 };
137
138 /* We use a big string constant to avoid lots of reloctions from the entry
139 * point table to lots of little strings. The entries in the entry point table
140 * store the index into this big string.
141 */
142
143 static const char strings[] ="""
144
145 offsets = []
146 i = 0;
147 for type, name, args, num, h in entrypoints:
148 print " \"vk%s\\0\"" % name
149 offsets.append(i)
150 i += 2 + len(name) + 1
151 print """ ;
152
153 /* Weak aliases for all potential validate functions. These will resolve to
154 * NULL if they're not defined, which lets the resolve_entrypoint() function
155 * either pick a validate wrapper if available or just plug in the actual
156 * entry point.
157 */
158 """
159
160 # Now generate the table of all entry points and their validation functions
161
162 print "\nstatic const struct anv_entrypoint entrypoints[] = {"
163 for type, name, args, num, h in entrypoints:
164 print " { %5d, 0x%08x }," % (offsets[num], h)
165 print "};\n"
166
167 for layer in [ "anv", "validate", "gen7", "gen75", "gen8" ]:
168 for type, name, args, num, h in entrypoints:
169 print "%s %s_%s%s __attribute__ ((weak));" % (type, layer, name, args)
170 print "\nconst struct anv_dispatch_table %s_layer = {" % layer
171 for type, name, args, num, h in entrypoints:
172 print " .%s = %s_%s," % (name, layer, name)
173 print "};\n"
174
175 print """
176 #ifdef DEBUG
177 static bool enable_validate = true;
178 #else
179 static bool enable_validate = false;
180 #endif
181
182 /* We can't use symbols that need resolving (like, oh, getenv) in the resolve
183 * function. This means that we have to determine whether or not to use the
184 * validation layer sometime before that. The constructor function attribute asks
185 * the dynamic linker to invoke determine_validate() at dlopen() time which
186 * works.
187 */
188 static void __attribute__ ((constructor))
189 determine_validate(void)
190 {
191 const char *s = getenv("ANV_VALIDATE");
192
193 if (s)
194 enable_validate = atoi(s);
195 }
196
197 static const struct brw_device_info *dispatch_devinfo;
198
199 void
200 anv_set_dispatch_devinfo(const struct brw_device_info *devinfo)
201 {
202 dispatch_devinfo = devinfo;
203 }
204
205 void * __attribute__ ((noinline))
206 anv_resolve_entrypoint(uint32_t index)
207 {
208 if (enable_validate && validate_layer.entrypoints[index])
209 return validate_layer.entrypoints[index];
210
211 if (dispatch_devinfo == NULL) {
212 assert(anv_layer.entrypoints[index]);
213 return anv_layer.entrypoints[index];
214 }
215
216 switch (dispatch_devinfo->gen) {
217 case 8:
218 if (gen8_layer.entrypoints[index])
219 return gen8_layer.entrypoints[index];
220 /* fall through */
221 case 7:
222 if (dispatch_devinfo->is_haswell && gen75_layer.entrypoints[index])
223 return gen75_layer.entrypoints[index];
224
225 if (gen7_layer.entrypoints[index])
226 return gen7_layer.entrypoints[index];
227 /* fall through */
228 case 0:
229 return anv_layer.entrypoints[index];
230 default:
231 unreachable("unsupported gen\\n");
232 }
233 }
234 """
235
236 # Now output ifuncs and their resolve helpers for all entry points. The
237 # resolve helper calls resolve_entrypoint() with the entry point index, which
238 # lets the resolver look it up in the table.
239
240 for type, name, args, num, h in entrypoints:
241 print "static void *resolve_%s(void) { return anv_resolve_entrypoint(%d); }" % (name, num)
242 print "%s vk%s%s\n __attribute__ ((ifunc (\"resolve_%s\"), visibility (\"default\")));\n" % (type, name, args, name)
243
244
245 # Now generate the hash table used for entry point look up. This is a
246 # uint16_t table of entry point indices. We use 0xffff to indicate an entry
247 # in the hash table is empty.
248
249 map = [none for f in xrange(hash_size)]
250 collisions = [0 for f in xrange(10)]
251 for type, name, args, num, h in entrypoints:
252 level = 0
253 while map[h & hash_mask] != none:
254 h = h + prime_step
255 level = level + 1
256 if level > 9:
257 collisions[9] += 1
258 else:
259 collisions[level] += 1
260 map[h & hash_mask] = num
261
262 print "/* Hash table stats:"
263 print " * size %d entries" % hash_size
264 print " * collisions entries"
265 for i in xrange(10):
266 if (i == 9):
267 plus = "+"
268 else:
269 plus = " "
270
271 print " * %2d%s %4d" % (i, plus, collisions[i])
272 print " */\n"
273
274 print "#define none 0x%04x\n" % none
275
276 print "static const uint16_t map[] = {"
277 for i in xrange(0, hash_size, 8):
278 print " ",
279 for j in xrange(i, i + 8):
280 if map[j] & 0xffff == 0xffff:
281 print " none,",
282 else:
283 print "0x%04x," % (map[j] & 0xffff),
284 print
285
286 print "};"
287
288 # Finally we generate the hash table lookup function. The hash function and
289 # linear probing algorithm matches the hash table generated above.
290
291 print """
292 void *
293 anv_lookup_entrypoint(const char *name)
294 {
295 static const uint32_t prime_factor = %d;
296 static const uint32_t prime_step = %d;
297 const struct anv_entrypoint *e;
298 uint32_t hash, h, i;
299 const char *p;
300
301 hash = 0;
302 for (p = name; *p; p++)
303 hash = hash * prime_factor + *p;
304
305 h = hash;
306 do {
307 i = map[h & %d];
308 if (i == none)
309 return NULL;
310 e = &entrypoints[i];
311 h += prime_step;
312 } while (e->hash != hash);
313
314 if (strcmp(name, strings + e->name) != 0)
315 return NULL;
316
317 return anv_resolve_entrypoint(i);
318 }
319 """ % (prime_factor, prime_step, hash_mask)