glthread: compile marshal_generated.c faster by breaking it up into 8 files
[mesa.git] / src / mapi / glapi / gen / gl_marshal.py
1
2 # Copyright (C) 2012 Intel Corporation
3 #
4 # Permission is hereby granted, free of charge, to any person obtaining a
5 # copy of this software and associated documentation files (the "Software"),
6 # to deal in the Software without restriction, including without limitation
7 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 # and/or sell copies of the Software, and to permit persons to whom the
9 # Software is furnished to do so, subject to the following conditions:
10 #
11 # The above copyright notice and this permission notice (including the next
12 # paragraph) shall be included in all copies or substantial portions of the
13 # Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 # IN THE SOFTWARE.
22
23 from __future__ import print_function
24
25 import contextlib
26 import getopt
27 import gl_XML
28 import license
29 import marshal_XML
30 import sys
31
32 header = """
33 #include "api_exec.h"
34 #include "glthread_marshal.h"
35 #include "dispatch.h"
36
37 #define COMPAT (ctx->API != API_OPENGL_CORE)
38
39 static inline int safe_mul(int a, int b)
40 {
41 if (a < 0 || b < 0) return -1;
42 if (a == 0 || b == 0) return 0;
43 if (a > INT_MAX / b) return -1;
44 return a * b;
45 }
46 """
47
48
49 file_index = 0
50 file_count = 1
51 current_indent = 0
52
53
54 def out(str):
55 if str:
56 print(' '*current_indent + str)
57 else:
58 print('')
59
60
61 @contextlib.contextmanager
62 def indent(delta = 3):
63 global current_indent
64 current_indent += delta
65 yield
66 current_indent -= delta
67
68
69 class PrintCode(gl_XML.gl_print_base):
70 def __init__(self):
71 super(PrintCode, self).__init__()
72
73 self.name = 'gl_marshal.py'
74 self.license = license.bsd_license_template % (
75 'Copyright (C) 2012 Intel Corporation', 'INTEL CORPORATION')
76
77 def printRealHeader(self):
78 print(header)
79
80 def printRealFooter(self):
81 pass
82
83 def print_sync_call(self, func, unmarshal = 0):
84 call = 'CALL_{0}(ctx->CurrentServerDispatch, ({1}))'.format(
85 func.name, func.get_called_parameter_string())
86 if func.return_type == 'void':
87 out('{0};'.format(call))
88 if func.marshal_call_after and not unmarshal:
89 out(func.marshal_call_after);
90 else:
91 out('return {0};'.format(call))
92 assert not func.marshal_call_after
93
94 def print_sync_dispatch(self, func):
95 self.print_sync_call(func)
96
97 def print_sync_body(self, func):
98 out('/* {0}: marshalled synchronously */'.format(func.name))
99 out('{0} GLAPIENTRY'.format(func.return_type))
100 out('_mesa_marshal_{0}({1})'.format(func.name, func.get_parameter_string()))
101 out('{')
102 with indent():
103 out('GET_CURRENT_CONTEXT(ctx);')
104 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
105 self.print_sync_call(func)
106 out('}')
107 out('')
108 out('')
109
110 def print_async_dispatch(self, func):
111 out('cmd = _mesa_glthread_allocate_command(ctx, '
112 'DISPATCH_CMD_{0}, cmd_size);'.format(func.name))
113 for p in func.fixed_params:
114 if p.count:
115 out('memcpy(cmd->{0}, {0}, {1});'.format(
116 p.name, p.size_string()))
117 else:
118 out('cmd->{0} = {0};'.format(p.name))
119 if func.variable_params:
120 out('char *variable_data = (char *) (cmd + 1);')
121 i = 1
122 for p in func.variable_params:
123 if p.img_null_flag:
124 out('cmd->{0}_null = !{0};'.format(p.name))
125 out('if (!cmd->{0}_null) {{'.format(p.name))
126 with indent():
127 out(('memcpy(variable_data, {0}, {0}_size);').format(p.name))
128 if i < len(func.variable_params):
129 out('variable_data += {0}_size;'.format(p.name))
130 out('}')
131 else:
132 out(('memcpy(variable_data, {0}, {0}_size);').format(p.name))
133 if i < len(func.variable_params):
134 out('variable_data += {0}_size;'.format(p.name))
135 i += 1
136
137 if not func.fixed_params and not func.variable_params:
138 out('(void) cmd;')
139
140 if func.marshal_call_after:
141 out(func.marshal_call_after);
142
143 # Uncomment this if you want to call _mesa_glthread_finish for debugging
144 #out('_mesa_glthread_finish(ctx);')
145
146 def print_async_struct(self, func):
147 out('struct marshal_cmd_{0}'.format(func.name))
148 out('{')
149 with indent():
150 out('struct marshal_cmd_base cmd_base;')
151 for p in func.fixed_params:
152 if p.count:
153 out('{0} {1}[{2}];'.format(
154 p.get_base_type_string(), p.name, p.count))
155 else:
156 out('{0} {1};'.format(p.type_string(), p.name))
157
158 for p in func.variable_params:
159 if p.img_null_flag:
160 out('bool {0}_null; /* If set, no data follows '
161 'for "{0}" */'.format(p.name))
162
163 for p in func.variable_params:
164 if p.count_scale != 1:
165 out(('/* Next {0} bytes are '
166 '{1} {2}[{3}][{4}] */').format(
167 p.size_string(marshal = 1), p.get_base_type_string(),
168 p.name, p.counter, p.count_scale))
169 else:
170 out(('/* Next {0} bytes are '
171 '{1} {2}[{3}] */').format(
172 p.size_string(marshal = 1), p.get_base_type_string(),
173 p.name, p.counter))
174 out('};')
175
176 def print_async_unmarshal(self, func):
177 out('void')
178 out(('_mesa_unmarshal_{0}(struct gl_context *ctx, '
179 'const struct marshal_cmd_{0} *cmd)').format(func.name))
180 out('{')
181 with indent():
182 for p in func.fixed_params:
183 if p.count:
184 p_decl = '{0} * {1} = cmd->{1};'.format(
185 p.get_base_type_string(), p.name)
186 else:
187 p_decl = '{0} {1} = cmd->{1};'.format(
188 p.type_string(), p.name)
189
190 if not p_decl.startswith('const '):
191 # Declare all local function variables as const, even if
192 # the original parameter is not const.
193 p_decl = 'const ' + p_decl
194
195 out(p_decl)
196
197 if func.variable_params:
198 for p in func.variable_params:
199 out('{0} * {1};'.format(
200 p.get_base_type_string(), p.name))
201 out('const char *variable_data = (const char *) (cmd + 1);')
202 i = 1
203 for p in func.variable_params:
204 out('{0} = ({1} *) variable_data;'.format(
205 p.name, p.get_base_type_string()))
206
207 if p.img_null_flag:
208 out('if (cmd->{0}_null)'.format(p.name))
209 with indent():
210 out('{0} = NULL;'.format(p.name))
211 if i < len(func.variable_params):
212 out('else')
213 with indent():
214 out('variable_data += {0};'.format(p.size_string(False, marshal = 1)))
215 elif i < len(func.variable_params):
216 out('variable_data += {0};'.format(p.size_string(False, marshal = 1)))
217 i += 1
218
219 self.print_sync_call(func, unmarshal = 1)
220 out('}')
221
222 def validate_count_or_fallback(self, func):
223 # Check that any counts for variable-length arguments might be < 0, in
224 # which case the command alloc or the memcpy would blow up before we
225 # get to the validation in Mesa core.
226 list = []
227 for p in func.parameters:
228 if p.is_variable_length():
229 list.append('{0}_size < 0'.format(p.name))
230 list.append('({0}_size > 0 && !{0})'.format(p.name))
231
232 if len(list) == 0:
233 return
234
235 list.append('(unsigned)cmd_size > MARSHAL_MAX_CMD_SIZE')
236
237 out('if (unlikely({0})) {{'.format(' || '.join(list)))
238 with indent():
239 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
240 self.print_sync_dispatch(func)
241 out('return;')
242 out('}')
243
244 def print_async_marshal(self, func):
245 out('void GLAPIENTRY')
246 out('_mesa_marshal_{0}({1})'.format(
247 func.name, func.get_parameter_string()))
248 out('{')
249 with indent():
250 out('GET_CURRENT_CONTEXT(ctx);')
251 for p in func.variable_params:
252 out('int {0}_size = {1};'.format(p.name, p.size_string(marshal = 1)))
253
254 struct = 'struct marshal_cmd_{0}'.format(func.name)
255 size_terms = ['sizeof({0})'.format(struct)]
256 for p in func.variable_params:
257 if p.img_null_flag:
258 size_terms.append('({0} ? {0}_size : 0)'.format(p.name))
259 else:
260 size_terms.append('{0}_size'.format(p.name))
261 out('int cmd_size = {0};'.format(' + '.join(size_terms)))
262 out('{0} *cmd;'.format(struct))
263
264 self.validate_count_or_fallback(func)
265
266 if func.marshal_sync:
267 out('if ({0}) {{'.format(func.marshal_sync))
268 with indent():
269 out('_mesa_glthread_finish_before(ctx, "{0}");'.format(func.name))
270 self.print_sync_dispatch(func)
271 out('return;')
272 out('}')
273
274 with indent():
275 self.print_async_dispatch(func)
276 out('}')
277
278 def print_async_body(self, func):
279 out('/* {0}: marshalled asynchronously */'.format(func.name))
280 self.print_async_struct(func)
281 self.print_async_unmarshal(func)
282 self.print_async_marshal(func)
283 out('')
284 out('')
285
286 def print_unmarshal_dispatch_cmd(self, api):
287 out('const _mesa_unmarshal_func _mesa_unmarshal_dispatch[NUM_DISPATCH_CMD] = {')
288 with indent():
289 for func in api.functionIterateAll():
290 flavor = func.marshal_flavor()
291 if flavor in ('skip', 'sync'):
292 continue
293 out('[DISPATCH_CMD_{0}] = (_mesa_unmarshal_func)_mesa_unmarshal_{0},'.format(func.name))
294 out('};')
295 out('')
296 out('')
297
298 def print_create_marshal_table(self, api):
299 out('/* _mesa_create_marshal_table takes a long time to compile with -O2 */')
300 out('#ifdef __GNUC__')
301 out('__attribute__((optimize("O1")))')
302 out('#endif')
303 out('struct _glapi_table *')
304 out('_mesa_create_marshal_table(const struct gl_context *ctx)')
305 out('{')
306 with indent():
307 out('struct _glapi_table *table;')
308 out('')
309 out('table = _mesa_alloc_dispatch_table();')
310 out('if (table == NULL)')
311 with indent():
312 out('return NULL;')
313 out('')
314 for func in api.functionIterateAll():
315 if func.marshal_flavor() == 'skip':
316 continue
317 # Don't use the SET_* functions, because they increase compile time
318 # by 20 seconds (on Ryzen 1700X).
319 out('if (_gloffset_{0} >= 0)'.format(func.name))
320 out(' ((_glapi_proc *)(table))[_gloffset_{0}] = (_glapi_proc)_mesa_marshal_{0};'
321 .format(func.name))
322 out('')
323 out('return table;')
324 out('}')
325 out('')
326 out('')
327
328 def printBody(self, api):
329 # The first file only contains the dispatch tables
330 if file_index == 0:
331 self.print_unmarshal_dispatch_cmd(api)
332 self.print_create_marshal_table(api)
333 return
334
335 # The remaining files contain the marshal and unmarshal functions
336 func_per_file = (len(api.functionIterateAll()) // (file_count - 1)) + 1
337 i = -1
338 for func in api.functionIterateAll():
339 i += 1
340 if i // func_per_file != (file_index - 1):
341 continue
342
343 flavor = func.marshal_flavor()
344 if flavor in ('skip', 'custom'):
345 continue
346 elif flavor == 'async':
347 self.print_async_body(func)
348 elif flavor == 'sync':
349 self.print_sync_body(func)
350
351
352 def show_usage():
353 print('Usage: %s [-f input_file_name]' % sys.argv[0])
354 sys.exit(1)
355
356
357 if __name__ == '__main__':
358 file_name = 'gl_API.xml'
359
360 try:
361 (args, trail) = getopt.getopt(sys.argv[1:], 'm:f:i:n:')
362 except Exception:
363 show_usage()
364
365 for (arg,val) in args:
366 if arg == '-f':
367 file_name = val
368 elif arg == '-i':
369 file_index = int(val)
370 elif arg == '-n':
371 file_count = int(val)
372
373 assert file_index < file_count
374 printer = PrintCode()
375
376 api = gl_XML.parse_GL_API(file_name, marshal_XML.marshal_item_factory())
377 printer.Print(api)