Skip to content

Commit 3c6a265

Browse files
authored
Merge pull request #444 from GuillaumeGomez/improve-docs
Improve docs
2 parents e69f125 + 79316d4 commit 3c6a265

File tree

6 files changed

+257
-223
lines changed

6 files changed

+257
-223
lines changed

Readme.md

+29-223
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ A secondary goal is to check if using the gcc backend will provide any run-time
1717
**This requires a patched libgccjit in order to work.
1818
You need to use my [fork of gcc](https://github.com/antoyo/gcc) which already includes these patches.**
1919

20+
```bash
21+
$ cp config.example.toml config.toml
22+
```
23+
24+
If don't need to test GCC patches you wrote in our GCC fork, then the default configuration should
25+
be all you need. You can update the `rustc_codegen_gcc` without worrying about GCC.
26+
27+
### Building with your own GCC version
28+
29+
If you wrote a patch for GCC and want to test it without this backend, you will need
30+
to do a few more things.
31+
2032
To build it (most of these instructions come from [here](https://gcc.gnu.org/onlinedocs/jit/internals/index.html), so don't hesitate to take a look there if you encounter an issue):
2133

2234
```bash
@@ -51,18 +63,17 @@ $ make check-jit RUNTESTFLAGS="-v -v -v jit.exp=jit.dg/test-asm.cc"
5163

5264
**Put the path to your custom build of libgccjit in the file `config.toml`.**
5365

54-
If you followed the instructions exactly as written (ie, you have created a `gcc-build` folder
55-
where gcc is built), the only thing you need to do is:
66+
You now need to set the `gcc-path` value in `config.toml` with the result of this command:
5667

5768
```bash
58-
$ cp config.example.toml config.toml
69+
$ dirname $(readlink -f `find . -name libgccjit.so`)
5970
```
6071

61-
But if you did something different, you also need to set the `gcc-path` value in `config.toml` with
62-
the result of this command:
72+
and to comment the `download-gccjit` setting:
6373

64-
```bash
65-
$ dirname $(readlink -f `find . -name libgccjit.so`)
74+
```toml
75+
gcc-path = "[MY PATH]"
76+
# download-gccjit = true
6677
```
6778

6879
Then you can run commands like this:
@@ -128,224 +139,19 @@ $ LIBRARY_PATH="[gcc-path value]" LD_LIBRARY_PATH="[gcc-path value]" rustc +$(ca
128139
<dd>Dump a C-like representation to /tmp/gccjit_dumps and enable debug info in order to debug this C-like representation.</dd>
129140
</dl>
130141

131-
## Licensing
132-
133-
While this crate is licensed under a dual Apache/MIT license, it links to `libgccjit` which is under the GPLv3+ and thus, the resulting toolchain (rustc + GCC codegen) will need to be released under the GPL license.
134-
135-
However, programs compiled with `rustc_codegen_gcc` do not need to be released under a GPL license.
136-
137-
## Debugging
138-
139-
Sometimes, libgccjit will crash and output an error like this:
140-
141-
```
142-
during RTL pass: expand
143-
libgccjit.so: error: in expmed_mode_index, at expmed.h:249
144-
0x7f0da2e61a35 expmed_mode_index
145-
../../../gcc/gcc/expmed.h:249
146-
0x7f0da2e61aa4 expmed_op_cost_ptr
147-
../../../gcc/gcc/expmed.h:271
148-
0x7f0da2e620dc sdiv_cost_ptr
149-
../../../gcc/gcc/expmed.h:540
150-
0x7f0da2e62129 sdiv_cost
151-
../../../gcc/gcc/expmed.h:558
152-
0x7f0da2e73c12 expand_divmod(int, tree_code, machine_mode, rtx_def*, rtx_def*, rtx_def*, int)
153-
../../../gcc/gcc/expmed.c:4335
154-
0x7f0da2ea1423 expand_expr_real_2(separate_ops*, rtx_def*, machine_mode, expand_modifier)
155-
../../../gcc/gcc/expr.c:9240
156-
0x7f0da2cd1a1e expand_gimple_stmt_1
157-
../../../gcc/gcc/cfgexpand.c:3796
158-
0x7f0da2cd1c30 expand_gimple_stmt
159-
../../../gcc/gcc/cfgexpand.c:3857
160-
0x7f0da2cd90a9 expand_gimple_basic_block
161-
../../../gcc/gcc/cfgexpand.c:5898
162-
0x7f0da2cdade8 execute
163-
../../../gcc/gcc/cfgexpand.c:6582
164-
```
165-
166-
To see the code which causes this error, call the following function:
167-
168-
```c
169-
gcc_jit_context_dump_to_file(ctxt, "/tmp/output.c", 1 /* update_locations */)
170-
```
171-
172-
This will create a C-like file and add the locations into the IR pointing to this C file.
173-
Then, rerun the program and it will output the location in the second line:
174-
175-
```
176-
libgccjit.so: /tmp/something.c:61322:0: error: in expmed_mode_index, at expmed.h:249
177-
```
178-
179-
Or add a breakpoint to `add_error` in gdb and print the line number using:
180-
181-
```
182-
p loc->m_line
183-
p loc->m_filename->m_buffer
184-
```
185-
186-
To print a debug representation of a tree:
187-
188-
```c
189-
debug_tree(expr);
190-
```
191-
192-
(defined in print-tree.h)
193-
194-
To print a debug representation of a gimple struct:
195-
196-
```c
197-
debug_gimple_stmt(gimple_struct)
198-
```
199-
200-
To get the `rustc` command to run in `gdb`, add the `--verbose` flag to `cargo build`.
201-
202-
To have the correct file paths in `gdb` instead of `/usr/src/debug/gcc/libstdc++-v3/libsupc++/eh_personality.cc`:
203-
204-
Maybe by calling the following at the beginning of gdb:
205-
206-
```
207-
set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
208-
```
209-
210-
TODO(antoyo): but that's not what I remember I was doing.
211-
212-
### `failed to build archive` error
213-
214-
When you get this error:
215-
216-
```
217-
error: failed to build archive: failed to open object file: No such file or directory (os error 2)
218-
```
219-
220-
That can be caused by the fact that you try to compile with `lto = "fat"`, but you didn't compile the sysroot with LTO.
221-
(Not sure if that's the reason since I cannot reproduce anymore. Maybe it happened when forgetting setting `FAT_LTO`.)
222-
223-
### ld: cannot find crtbegin.o
224-
225-
When compiling an executable with libgccijt, if setting the `*LIBRARY_PATH` variables to the install directory, you will get the following errors:
142+
## Extra documentation
226143

227-
```
228-
ld: cannot find crtbegin.o: No such file or directory
229-
ld: cannot find -lgcc: No such file or directory
230-
ld: cannot find -lgcc: No such file or directory
231-
libgccjit.so: error: error invoking gcc driver
232-
```
233-
234-
To fix this, set the variables to `gcc-build/build/gcc`.
235-
236-
### How to debug GCC LTO
237-
238-
Run do the command with `-v -save-temps` and then extract the `lto1` line from the output and run that under the debugger.
239-
240-
### How to send arguments to the GCC linker
241-
242-
```
243-
CG_RUSTFLAGS="-Clink-args=-save-temps -v" ../y.sh cargo build
244-
```
245-
246-
### How to see the personality functions in the asm dump
247-
248-
```
249-
CG_RUSTFLAGS="-Clink-arg=-save-temps -v -Clink-arg=-dA" ../y.sh cargo build
250-
```
251-
252-
### How to see the LLVM IR for a sysroot crate
253-
254-
```
255-
cargo build -v --target x86_64-unknown-linux-gnu -Zbuild-std
256-
# Take the command from the output and add --emit=llvm-ir
257-
```
258-
259-
### To prevent the linker from unmangling symbols
260-
261-
Run with:
262-
263-
```
264-
COLLECT_NO_DEMANGLE=1
265-
```
266-
267-
### How to use a custom-build rustc
268-
269-
* Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).
270-
* Clean and rebuild the codegen with `debug-current` in the file `rust-toolchain`.
271-
272-
### How to install a forked git-subtree
273-
274-
Using git-subtree with `rustc` requires a patched git to make it work.
275-
The PR that is needed is [here](https://github.com/gitgitgadget/git/pull/493).
276-
Use the following instructions to install it:
277-
278-
```bash
279-
git clone [email protected]:tqc/git.git
280-
cd git
281-
git checkout tqc/subtree
282-
make
283-
make install
284-
cd contrib/subtree
285-
make
286-
cp git-subtree ~/bin
287-
```
288-
289-
Then, do a sync with this command:
290-
291-
```bash
292-
PATH="$HOME/bin:$PATH" ~/bin/git-subtree push -P compiler/rustc_codegen_gcc/ ../rustc_codegen_gcc/ sync_branch_name
293-
cd ../rustc_codegen_gcc
294-
git checkout master
295-
git pull
296-
git checkout sync_branch_name
297-
git merge master
298-
```
299-
300-
To send the changes to the rust repo:
301-
302-
```bash
303-
cd ../rust
304-
git pull origin master
305-
git checkout -b subtree-update_cg_gcc_YYYY-MM-DD
306-
PATH="$HOME/bin:$PATH" ~/bin/git-subtree pull --prefix=compiler/rustc_codegen_gcc/ https://github.com/rust-lang/rustc_codegen_gcc.git master
307-
git push
308-
309-
# Immediately merge the merge commit into cg_gcc to prevent merge conflicts when syncing from rust-lang/rust later.
310-
PATH="$HOME/bin:$PATH" ~/bin/git-subtree push -P compiler/rustc_codegen_gcc/ ../rustc_codegen_gcc/ sync_branch_name
311-
```
312-
313-
TODO: write a script that does the above.
314-
315-
https://rust-lang.zulipchat.com/#narrow/stream/301329-t-devtools/topic/subtree.20madness/near/258877725
316-
317-
### How to use [mem-trace](https://github.com/antoyo/mem-trace)
318-
319-
`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`.
144+
More specific documentation is available in the [`doc`](./doc) folder:
320145

321-
### How to generate GIMPLE
146+
* [Common errors](./doc/errors.md)
147+
* [Debugging GCC LTO](./doc/debugging-gcc-lto.md)
148+
* [Debugging libgccjit](./doc/debugging-libgccjit.md)
149+
* [Git subtree sync](./doc/subtree.md)
150+
* [List of useful commands](./doc/tips.md)
151+
* [Send a patch to GCC](./doc/sending-gcc-patch.md)
322152

323-
If you need to check what gccjit is generating (GIMPLE), then take a look at how to
324-
generate it in [gimple.md](./doc/gimple.md).
325-
326-
### How to build a cross-compiling libgccjit
327-
328-
#### Building libgccjit
329-
330-
* Follow the instructions on [this repo](https://github.com/cross-cg-gcc-tools/cross-gcc).
331-
332-
#### Configuring rustc_codegen_gcc
333-
334-
* Run `./y.sh prepare --cross` so that the sysroot is patched for the cross-compiling case.
335-
* Set the path to the cross-compiling libgccjit in `gcc-path` (in `config.toml`).
336-
* Make sure you have the linker for your target (for instance `m68k-unknown-linux-gnu-gcc`) in your `$PATH`. Currently, the linker name is hardcoded as being `$TARGET-gcc`. Specify the target when building the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu`.
337-
* Build your project by specifying the target: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../y.sh cargo build --target m68k-unknown-linux-gnu`.
338-
339-
If the target is not yet supported by the Rust compiler, create a [target specification file](https://docs.rust-embedded.org/embedonomicon/custom-target.html) (note that the `arch` specified in this file must be supported by the rust compiler).
340-
Then, you can use it the following way:
341-
342-
* Add the target specification file using `--target` as an **absolute** path to build the sysroot: `./y.sh build --target-triple m68k-unknown-linux-gnu --target $(pwd)/m68k-unknown-linux-gnu.json`
343-
* Build your project by specifying the target specification file: `OVERWRITE_TARGET_TRIPLE=m68k-unknown-linux-gnu ../y.sh cargo build --target path/to/m68k-unknown-linux-gnu.json`.
344-
345-
If you get the following error:
153+
## Licensing
346154

347-
```
348-
/usr/bin/ld: unrecognised emulation mode: m68kelf
349-
```
155+
While this crate is licensed under a dual Apache/MIT license, it links to `libgccjit` which is under the GPLv3+ and thus, the resulting toolchain (rustc + GCC codegen) will need to be released under the GPL license.
350156

351-
Make sure you set `gcc-path` (in `config.toml`) to the install directory.
157+
However, programs compiled with `rustc_codegen_gcc` do not need to be released under a GPL license.

doc/debugging-gcc-lto.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How to debug GCC LTO
2+
3+
Run do the command with `-v -save-temps` and then extract the `lto1` line from the output and run that under the debugger.

doc/debugging-libgccjit.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Debugging libgccjit
2+
3+
Sometimes, libgccjit will crash and output an error like this:
4+
5+
```
6+
during RTL pass: expand
7+
libgccjit.so: error: in expmed_mode_index, at expmed.h:249
8+
0x7f0da2e61a35 expmed_mode_index
9+
../../../gcc/gcc/expmed.h:249
10+
0x7f0da2e61aa4 expmed_op_cost_ptr
11+
../../../gcc/gcc/expmed.h:271
12+
0x7f0da2e620dc sdiv_cost_ptr
13+
../../../gcc/gcc/expmed.h:540
14+
0x7f0da2e62129 sdiv_cost
15+
../../../gcc/gcc/expmed.h:558
16+
0x7f0da2e73c12 expand_divmod(int, tree_code, machine_mode, rtx_def*, rtx_def*, rtx_def*, int)
17+
../../../gcc/gcc/expmed.c:4335
18+
0x7f0da2ea1423 expand_expr_real_2(separate_ops*, rtx_def*, machine_mode, expand_modifier)
19+
../../../gcc/gcc/expr.c:9240
20+
0x7f0da2cd1a1e expand_gimple_stmt_1
21+
../../../gcc/gcc/cfgexpand.c:3796
22+
0x7f0da2cd1c30 expand_gimple_stmt
23+
../../../gcc/gcc/cfgexpand.c:3857
24+
0x7f0da2cd90a9 expand_gimple_basic_block
25+
../../../gcc/gcc/cfgexpand.c:5898
26+
0x7f0da2cdade8 execute
27+
../../../gcc/gcc/cfgexpand.c:6582
28+
```
29+
30+
To see the code which causes this error, call the following function:
31+
32+
```c
33+
gcc_jit_context_dump_to_file(ctxt, "/tmp/output.c", 1 /* update_locations */)
34+
```
35+
36+
This will create a C-like file and add the locations into the IR pointing to this C file.
37+
Then, rerun the program and it will output the location in the second line:
38+
39+
```
40+
libgccjit.so: /tmp/something.c:61322:0: error: in expmed_mode_index, at expmed.h:249
41+
```
42+
43+
Or add a breakpoint to `add_error` in gdb and print the line number using:
44+
45+
```
46+
p loc->m_line
47+
p loc->m_filename->m_buffer
48+
```
49+
50+
To print a debug representation of a tree:
51+
52+
```c
53+
debug_tree(expr);
54+
```
55+
56+
(defined in print-tree.h)
57+
58+
To print a debug representation of a gimple struct:
59+
60+
```c
61+
debug_gimple_stmt(gimple_struct)
62+
```
63+
64+
To get the `rustc` command to run in `gdb`, add the `--verbose` flag to `cargo build`.
65+
66+
To have the correct file paths in `gdb` instead of `/usr/src/debug/gcc/libstdc++-v3/libsupc++/eh_personality.cc`:
67+
68+
Maybe by calling the following at the beginning of gdb:
69+
70+
```
71+
set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
72+
```
73+
74+
TODO(antoyo): but that's not what I remember I was doing.

doc/errors.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Common errors
2+
3+
This file lists errors that were encountered and how to fix them.
4+
5+
### `failed to build archive` error
6+
7+
When you get this error:
8+
9+
```
10+
error: failed to build archive: failed to open object file: No such file or directory (os error 2)
11+
```
12+
13+
That can be caused by the fact that you try to compile with `lto = "fat"`, but you didn't compile the sysroot with LTO.
14+
(Not sure if that's the reason since I cannot reproduce anymore. Maybe it happened when forgetting setting `FAT_LTO`.)
15+
16+
### ld: cannot find crtbegin.o
17+
18+
When compiling an executable with libgccijt, if setting the `*LIBRARY_PATH` variables to the install directory, you will get the following errors:
19+
20+
```
21+
ld: cannot find crtbegin.o: No such file or directory
22+
ld: cannot find -lgcc: No such file or directory
23+
ld: cannot find -lgcc: No such file or directory
24+
libgccjit.so: error: error invoking gcc driver
25+
```
26+
27+
To fix this, set the variables to `gcc-build/build/gcc`.

0 commit comments

Comments
 (0)