compiler/rustc_codegen_gcc/doc/gimple.md MARKDOWN 112 lines View on github.com → Search inside
1# GIMPLE23You 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.45First, we'll copy the content from `gcc/gcc/testsuite/jit.dg/test-const-attribute.c` into a6file named `local.c` and remove the content we're not interested in:78```diff9- /* { dg-do compile { target x86_64-*-* } } */10...11- /* We don't want set_options() in harness.h to set -O3 to see that the const12-    attribute affects the optimizations. */13- #define TEST_ESCHEWS_SET_OPTIONS14- 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_FILE21- #define OUTPUT_KIND      GCC_JIT_OUTPUT_KIND_ASSEMBLER22- #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```2930Then we'll add a `main` function which will call the `create_code` function but31also add the calls we need to generate the GIMPLE:3233```C34int 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);4142    gcc_jit_context_compile(ctxt);43    // If you want to compile to assembly (or any other format) directly, you can44    // use the following call instead:45    // gcc_jit_context_compile_to_file(ctxt, GCC_JIT_OUTPUT_KIND_ASSEMBLER, "out.s");4647    return 0;48}49```5051Then we can compile it by using:5253```console54gcc local.c -I `pwd`/gcc/gcc/jit/ -L `pwd`/gcc-build/gcc -lgccjit -o out55```5657And finally when you run it:5859```console60LD_LIBRARY_PATH=`pwd`/gcc-build/gcc LIBRARY_PATH=`pwd`/gcc-build/gcc ./out61```6263It should display:6465```c66__attribute__((const))67int xxx ()68{69  int D.3394;70  int sum;71  int x;7273  <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```9091An alternative way to generate the GIMPLE is to replace:9293```c94    gcc_jit_context_set_bool_option(ctxt, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 1);95```9697with:9899```c100    gcc_jit_context_add_command_line_option(ctxt, "-fdump-tree-gimple");101```102103(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:104105```console106rm -rf /tmp/libgccjit-*107```108109to make it easier for you to know which folder to look into.110111Once 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.

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.