]> git.proxmox.com Git - rustc.git/blob - compiler/rustc_codegen_gcc/doc/gimple.md
New upstream version 1.75.0+dfsg1
[rustc.git] / compiler / rustc_codegen_gcc / doc / gimple.md
1 # GIMPLE
2
3 You can see the full documentation about what GIMPLE is [here](https://gcc.gnu.org/onlinedocs/gccint/GIMPLE.html). In this document we will explain how to generate it.
4
5 First, we'll copy the content from `gcc/gcc/testsuite/jit.dg/test-const-attribute.c` into a
6 file named `local.c` and remove the content we're not interested into:
7
8 ```diff
9 - /* { dg-do compile { target x86_64-*-* } } */
10 ...
11 - /* We don't want set_options() in harness.h to set -O3 to see that the const
12 - attribute affects the optimizations. */
13 - #define TEST_ESCHEWS_SET_OPTIONS
14 - static void set_options (gcc_jit_context *ctxt, const char *argv0)
15 - {
16 - // Set "-O3".
17 - gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
18 - }
19 -
20 - #define TEST_COMPILING_TO_FILE
21 - #define OUTPUT_KIND GCC_JIT_OUTPUT_KIND_ASSEMBLER
22 - #define OUTPUT_FILENAME "output-of-test-const-attribute.c.s"
23 - #include "harness.h"
24 ...
25 - /* { dg-final { jit-verify-output-file-was-created "" } } */
26 - /* Check that the loop was optimized away */
27 - /* { dg-final { jit-verify-assembler-output-not "jne" } } */
28 ```
29
30 Then we'll add a `main` function which will call the `create_code` function but
31 also add the calls we need to generate the GIMPLE:
32
33 ```C
34 int main() {
35 gcc_jit_context *ctxt = gcc_jit_context_acquire();
36 // To set `-O3`, update it depending on your needs.
37 gcc_jit_context_set_int_option(ctxt, GCC_JIT_INT_OPTION_OPTIMIZATION_LEVEL, 3);
38 // Very important option to generate the gimple format.
39 gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 1);
40 create_code(ctxt, NULL);
41
42 gcc_jit_context_compile(ctxt);
43 // If you want to compile to assembly (or any other format) directly, you can
44 // use the following call instead:
45 // gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_ASSEMBLER, "out.s");
46
47 return 0;
48 }
49 ```
50
51 Then we can compile it by using:
52
53 ```console
54 gcc local.c -I `pwd`/gcc/gcc/jit/ -L `pwd`/gcc-build/gcc -lgccjit -o out
55 ```
56
57 And finally when you run it:
58
59 ```console
60 LD_LIBRARY_PATH=`pwd`/gcc-build/gcc LIBRARY_PATH=`pwd`/gcc-build/gcc ./out
61 ```
62
63 It should display:
64
65 ```c
66 __attribute__((const))
67 int xxx ()
68 {
69 int D.3394;
70 int sum;
71 int x;
72
73 <D.3377>:
74 x = 45;
75 sum = 0;
76 goto loop_cond;
77 loop_cond:
78 x = x >> 1;
79 if (x != 0) goto after_loop; else goto loop_body;
80 loop_body:
81 _1 = foo (x);
82 _2 = _1 * 2;
83 x = x + _2;
84 goto loop_cond;
85 after_loop:
86 D.3394 = sum;
87 return D.3394;
88 }
89 ```
90
91 An alternative way to generate the GIMPLE is to replace:
92
93 ```c
94 gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 1);
95 ```
96
97 with:
98
99 ```c
100 gcc_jit_context_add_command_line_option(ctxt, "-fdump-tree-gimple");
101 ```
102
103 (although you can have both at the same time too). Then you can compile it like previously. Only one difference: before executing it, I recommend to run:
104
105 ```console
106 rm -rf /tmp/libgccjit-*
107 ```
108
109 to make it easier for you to know which folder to look into.
110
111 Once the execution is done, you should now have a file with path looking like `/tmp/libgccjit-9OFqkD/fake.c.006t.gimple` which contains the GIMPLE format.