]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/jit/docs/topics/contexts.rst
New jit API entrypoint: gcc_jit_context_set_logfile
[thirdparty/gcc.git] / gcc / jit / docs / topics / contexts.rst
1 .. Copyright (C) 2014 Free Software Foundation, Inc.
2 Originally contributed by David Malcolm <dmalcolm@redhat.com>
3
4 This is free software: you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful, but
10 WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see
16 <http://www.gnu.org/licenses/>.
17
18 .. default-domain:: c
19
20 Compilation contexts
21 ====================
22
23 .. type:: gcc_jit_context
24
25 The top-level of the API is the :c:type:`gcc_jit_context` type.
26
27 A :c:type:`gcc_jit_context` instance encapsulates the state of a
28 compilation.
29
30 You can set up options on it, and add types, functions and code.
31 Invoking :c:func:`gcc_jit_context_compile` on it gives you a
32 :c:type:`gcc_jit_result`.
33
34 Lifetime-management
35 -------------------
36 Contexts are the unit of lifetime-management within the API: objects
37 have their lifetime bounded by the context they are created within, and
38 cleanup of such objects is done for you when the context is released.
39
40 .. function:: gcc_jit_context *gcc_jit_context_acquire (void)
41
42 This function acquires a new :c:type:`gcc_jit_object *` instance,
43 which is independent of any others that may be present within this
44 process.
45
46 .. function:: void gcc_jit_context_release (gcc_jit_context *ctxt)
47
48 This function releases all resources associated with the given context.
49 Both the context itself and all of its :c:type:`gcc_jit_object *`
50 instances are cleaned up. It should be called exactly once on a given
51 context.
52
53 It is invalid to use the context or any of its "contextual" objects
54 after calling this.
55
56 .. code-block:: c
57
58 gcc_jit_context_release (ctxt);
59
60 .. function:: gcc_jit_context * gcc_jit_context_new_child_context (gcc_jit_context *parent_ctxt)
61
62 Given an existing JIT context, create a child context.
63
64 The child inherits a copy of all option-settings from the parent.
65
66 The child can reference objects created within the parent, but not
67 vice-versa.
68
69 The lifetime of the child context must be bounded by that of the
70 parent: you should release a child context before releasing the parent
71 context.
72
73 If you use a function from a parent context within a child context,
74 you have to compile the parent context before you can compile the
75 child context, and the gcc_jit_result of the parent context must
76 outlive the gcc_jit_result of the child context.
77
78 This allows caching of shared initializations. For example, you could
79 create types and declarations of global functions in a parent context
80 once within a process, and then create child contexts whenever a
81 function or loop becomes hot. Each such child context can be used for
82 JIT-compiling just one function or loop, but can reference types
83 and helper functions created within the parent context.
84
85 Contexts can be arbitrarily nested, provided the above rules are
86 followed, but it's probably not worth going above 2 or 3 levels, and
87 there will likely be a performance hit for such nesting.
88
89
90 Thread-safety
91 -------------
92 Instances of :c:type:`gcc_jit_context *` created via
93 :c:func:`gcc_jit_context_acquire` are independent from each other:
94 only one thread may use a given context at once, but multiple threads
95 could each have their own contexts without needing locks.
96
97 Contexts created via :c:func:`gcc_jit_context_new_child_context` are
98 related to their parent context. They can be partitioned by their
99 ultimate ancestor into independent "family trees". Only one thread
100 within a process may use a given "family tree" of such contexts at once,
101 and if you're using multiple threads you should provide your own locking
102 around entire such context partitions.
103
104 .. _error-handling:
105
106 Error-handling
107 --------------
108 Various kinds of errors are possible when using the API, such as
109 mismatched types in an assignment. You can only compile and get code from
110 a context if no errors occur.
111
112 Errors are printed on stderr and can be queried using
113 :c:func:`gcc_jit_context_get_first_error`.
114
115 They typically contain the name of the API entrypoint where the error
116 occurred, and pertinent information on the problem:
117
118 .. code-block:: console
119
120 ./buggy-program: error: gcc_jit_block_add_assignment: mismatching types: assignment to i (type: int) from "hello world" (type: const char *)
121
122 In general, if an error occurs when using an API entrypoint, the
123 entrypoint returns NULL. You don't have to check everywhere for NULL
124 results, since the API handles a NULL being passed in for any
125 argument by issuing another error. This typically leads to a cascade of
126 followup error messages, but is safe (albeit verbose).
127
128 .. function:: const char *\
129 gcc_jit_context_get_first_error (gcc_jit_context *ctxt)
130
131 Returns the first error message that occurred on the context.
132
133 The returned string is valid for the rest of the lifetime of the
134 context.
135
136 If no errors occurred, this will be NULL.
137
138 Debugging
139 ---------
140
141 .. function:: void\
142 gcc_jit_context_dump_to_file (gcc_jit_context *ctxt,\
143 const char *path,\
144 int update_locations)
145
146 To help with debugging: dump a C-like representation to the given path,
147 describing what's been set up on the context.
148
149 If "update_locations" is true, then also set up :type:`gcc_jit_location`
150 information throughout the context, pointing at the dump file as if it
151 were a source file. This may be of use in conjunction with
152 :macro:`GCC_JIT_BOOL_OPTION_DEBUGINFO` to allow stepping through the
153 code in a debugger.
154
155 .. function:: void\
156 gcc_jit_context_set_logfile (gcc_jit_context *ctxt,\
157 FILE *logfile,\
158 int flags,\
159 int verbosity)
160
161 To help with debugging; enable ongoing logging of the context's
162 activity to the given file.
163
164 For example, the following will enable logging to stderr.
165
166 .. code-block:: c
167
168 gcc_jit_context_set_logfile (ctxt, stderr, 0, 0);
169
170 Examples of information logged include:
171
172 * API calls
173
174 * the various steps involved within compilation
175
176 * activity on any :c:type:`gcc_jit_result` instances created by
177 the context
178
179 * activity within any child contexts
180
181 An example of a log can be seen :ref:`here <example-of-log-file>`,
182 though the precise format and kinds of information logged is subject
183 to change.
184
185 The caller remains responsible for closing `logfile`, and it must not
186 be closed until all users are released. In particular, note that
187 child contexts and :c:type:`gcc_jit_result` instances created by
188 the context will use the logfile.
189
190 There may a performance cost for logging.
191
192 You can turn off logging on `ctxt` by passing `NULL` for `logfile`.
193 Doing so only affects the context; it does not affect child contexts
194 or :c:type:`gcc_jit_result` instances already created by
195 the context.
196
197 The parameters "flags" and "verbosity" are reserved for future
198 expansion, and must be zero for now.
199
200 To contrast the above: :c:func:`gcc_jit_context_dump_to_file` dumps the
201 current state of a context to the given path, whereas
202 :c:func:`gcc_jit_context_set_logfile` enables on-going logging of
203 future activies on a context to the given `FILE *`.
204
205 .. function:: void\
206 gcc_jit_context_enable_dump (gcc_jit_context *ctxt,\
207 const char *dumpname, \
208 char **out_ptr)
209
210 Enable the dumping of a specific set of internal state from the
211 compilation, capturing the result in-memory as a buffer.
212
213 Parameter "dumpname" corresponds to the equivalent gcc command-line
214 option, without the "-fdump-" prefix.
215 For example, to get the equivalent of :option:`-fdump-tree-vrp1`,
216 supply ``"tree-vrp1"``:
217
218 .. code-block:: c
219
220 static char *dump_vrp1;
221
222 void
223 create_code (gcc_jit_context *ctxt)
224 {
225 gcc_jit_context_enable_dump (ctxt, "tree-vrp1", &dump_vrp1);
226 /* (other API calls omitted for brevity) */
227 }
228
229 The context directly stores the dumpname as a ``(const char *)``, so
230 the passed string must outlive the context.
231
232 :func:`gcc_jit_context_compile` will capture the dump as a
233 dynamically-allocated buffer, writing it to ``*out_ptr``.
234
235 The caller becomes responsible for calling:
236
237 .. code-block:: c
238
239 free (*out_ptr)
240
241 each time that :func:`gcc_jit_context_compile` is called.
242 ``*out_ptr`` will be written to, either with the address of a buffer,
243 or with ``NULL`` if an error occurred.
244
245 .. warning::
246
247 This API entrypoint is likely to be less stable than the others.
248 In particular, both the precise dumpnames, and the format and content
249 of the dumps are subject to change.
250
251 It exists primarily for writing the library's own test suite.
252
253 Options
254 -------
255
256 String Options
257 **************
258
259 .. function:: void gcc_jit_context_set_str_option(gcc_jit_context *ctxt, \
260 enum gcc_jit_str_option opt, \
261 const char *value)
262
263 Set a string option of the context.
264
265 .. type:: enum gcc_jit_str_option
266
267 There is currently just one string option:
268
269 .. macro:: GCC_JIT_STR_OPTION_PROGNAME
270
271 The name of the program, for use as a prefix when printing error
272 messages to stderr. If `NULL`, or default, "libgccjit.so" is used.
273
274 Boolean options
275 ***************
276
277 .. function:: void gcc_jit_context_set_bool_option(gcc_jit_context *ctxt, \
278 enum gcc_jit_bool_option opt, \
279 int value)
280
281 Set a boolean option of the context.
282 Zero is "false" (the default), non-zero is "true".
283
284 .. type:: enum gcc_jit_bool_option
285
286 .. macro:: GCC_JIT_BOOL_OPTION_DEBUGINFO
287
288 If true, :func:`gcc_jit_context_compile` will attempt to do the right
289 thing so that if you attach a debugger to the process, it will
290 be able to inspect variables and step through your code.
291
292 Note that you can't step through code unless you set up source
293 location information for the code (by creating and passing in
294 :type:`gcc_jit_location` instances).
295
296 .. macro:: GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE
297
298 If true, :func:`gcc_jit_context_compile` will dump its initial
299 "tree" representation of your code to stderr (before any
300 optimizations).
301
302 Here's some sample output (from the `square` example)::
303
304 <statement_list 0x7f4875a62cc0
305 type <void_type 0x7f4875a64bd0 VOID
306 align 8 symtab 0 alias set -1 canonical type 0x7f4875a64bd0
307 pointer_to_this <pointer_type 0x7f4875a64c78>>
308 side-effects head 0x7f4875a761e0 tail 0x7f4875a761f8 stmts 0x7f4875a62d20 0x7f4875a62d00
309
310 stmt <label_expr 0x7f4875a62d20 type <void_type 0x7f4875a64bd0>
311 side-effects
312 arg 0 <label_decl 0x7f4875a79080 entry type <void_type 0x7f4875a64bd0>
313 VOID file (null) line 0 col 0
314 align 1 context <function_decl 0x7f4875a77500 square>>>
315 stmt <return_expr 0x7f4875a62d00
316 type <integer_type 0x7f4875a645e8 public SI
317 size <integer_cst 0x7f4875a623a0 constant 32>
318 unit size <integer_cst 0x7f4875a623c0 constant 4>
319 align 32 symtab 0 alias set -1 canonical type 0x7f4875a645e8 precision 32 min <integer_cst 0x7f4875a62340 -2147483648> max <integer_cst 0x7f4875a62360 2147483647>
320 pointer_to_this <pointer_type 0x7f4875a6b348>>
321 side-effects
322 arg 0 <modify_expr 0x7f4875a72a78 type <integer_type 0x7f4875a645e8>
323 side-effects arg 0 <result_decl 0x7f4875a7a000 D.54>
324 arg 1 <mult_expr 0x7f4875a72a50 type <integer_type 0x7f4875a645e8>
325 arg 0 <parm_decl 0x7f4875a79000 i> arg 1 <parm_decl 0x7f4875a79000 i>>>>>
326
327 .. macro:: GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE
328
329 If true, :func:`gcc_jit_context_compile` will dump the "gimple"
330 representation of your code to stderr, before any optimizations
331 are performed. The dump resembles C code:
332
333 .. code-block:: c
334
335 square (signed int i)
336 {
337 signed int D.56;
338
339 entry:
340 D.56 = i * i;
341 return D.56;
342 }
343
344 .. macro:: GCC_JIT_BOOL_OPTION_DUMP_GENERATED_CODE
345
346 If true, :func:`gcc_jit_context_compile` will dump the final
347 generated code to stderr, in the form of assembly language:
348
349 .. code-block:: gas
350
351 .file "fake.c"
352 .text
353 .globl square
354 .type square, @function
355 square:
356 .LFB0:
357 .cfi_startproc
358 pushq %rbp
359 .cfi_def_cfa_offset 16
360 .cfi_offset 6, -16
361 movq %rsp, %rbp
362 .cfi_def_cfa_register 6
363 movl %edi, -4(%rbp)
364 .L2:
365 movl -4(%rbp), %eax
366 imull -4(%rbp), %eax
367 popq %rbp
368 .cfi_def_cfa 7, 8
369 ret
370 .cfi_endproc
371 .LFE0:
372 .size square, .-square
373 .ident "GCC: (GNU) 4.9.0 20131023 (Red Hat 0.1-%{gcc_release})"
374 .section .note.GNU-stack,"",@progbits
375
376
377 .. macro:: GCC_JIT_BOOL_OPTION_DUMP_SUMMARY
378
379 If true, :func:`gcc_jit_context_compile` will print information to stderr
380 on the actions it is performing, followed by a profile showing
381 the time taken and memory usage of each phase.
382
383 .. macro:: GCC_JIT_BOOL_OPTION_DUMP_EVERYTHING
384
385 If true, :func:`gcc_jit_context_compile` will dump copious
386 amount of information on what it's doing to various
387 files within a temporary directory. Use
388 :macro:`GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES` (see below) to
389 see the results. The files are intended to be human-readable,
390 but the exact files and their formats are subject to change.
391
392 .. macro:: GCC_JIT_BOOL_OPTION_SELFCHECK_GC
393
394 If true, libgccjit will aggressively run its garbage collector, to
395 shake out bugs (greatly slowing down the compile). This is likely
396 to only be of interest to developers *of* the library. It is
397 used when running the selftest suite.
398
399 .. macro:: GCC_JIT_BOOL_OPTION_KEEP_INTERMEDIATES
400
401 If true, the :type:`gcc_jit_context` will not clean up intermediate files
402 written to the filesystem, and will display their location on stderr.
403
404 Integer options
405 ***************
406
407 .. function:: void gcc_jit_context_set_int_option (gcc_jit_context *ctxt, \
408 enum gcc_jit_int_option opt, \
409 int value)
410
411 Set an integer option of the context.
412
413 .. type:: enum gcc_jit_int_option
414
415 There is currently just one integer option:
416
417 .. macro:: GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL
418
419 How much to optimize the code.
420
421 Valid values are 0-3, corresponding to GCC's command-line options
422 -O0 through -O3.
423
424 The default value is 0 (unoptimized).