Skip to content

Commit cd57c6e

Browse files
Add instructions on how to generate GIMPLE format
1 parent 57d2ecb commit cd57c6e

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

Readme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ https://rust-lang.zulipchat.com/#narrow/stream/301329-t-devtools/topic/subtree.2
227227

228228
`rustc` needs to be built without `jemalloc` so that `mem-trace` can overload `malloc` since `jemalloc` is linked statically, so a `LD_PRELOAD`-ed library won't a chance to intercept the calls to `malloc`.
229229

230+
### How to generate GIMPLE
231+
232+
If you need to check what gccjit is generating (GIMPLE), then take a look at how to
233+
generate it in [gimple.md](./doc/gimple.md).
234+
230235
### How to build a cross-compiling libgccjit
231236

232237
#### Building libgccjit

doc/gimple.md

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
create_code(ctxt, NULL);
37+
gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_DYNAMIC_LIBRARY, "tmp");
38+
return 0;
39+
}
40+
```
41+
42+
Then we can compile it by using:
43+
44+
```console
45+
gcc const.c -I `pwd`/gcc/gcc/jit/ -L `pwd`/gcc-build/gcc -lgccjit -o out
46+
```
47+
48+
And finally when you run it:
49+
50+
```console
51+
LD_LIBRARY_PATH=`pwd`/gcc-build/gcc ./out
52+
```
53+
54+
It should display:
55+
56+
```c
57+
__attribute__((const))
58+
int xxx ()
59+
{
60+
int D.3394;
61+
int sum;
62+
int x;
63+
64+
<D.3377>:
65+
x = 45;
66+
sum = 0;
67+
goto loop_cond;
68+
loop_cond:
69+
x = x >> 1;
70+
if (x != 0) goto after_loop; else goto loop_body;
71+
loop_body:
72+
_1 = foo (x);
73+
_2 = _1 * 2;
74+
x = x + _2;
75+
goto loop_cond;
76+
after_loop:
77+
D.3394 = sum;
78+
return D.3394;
79+
}
80+
```

0 commit comments

Comments
 (0)