Skip to content

Commit 4e41a8a

Browse files
authored
Merge pull request rust-lang#278 from rust-lang/feature/lto_2023-05-12
Add support for Link-Time Optimization
2 parents 2b956f5 + 62867dc commit 4e41a8a

23 files changed

+713
-174
lines changed

.github/workflows/ci.yml

+7-5
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ jobs:
5757
uses: dawidd6/action-download-artifact@v2
5858
with:
5959
workflow: main.yml
60-
name: ${{ matrix.libgccjit_version.gcc }}
61-
path: gcc-build
60+
name: gcc-13
61+
path: gcc-13
6262
repo: antoyo/gcc
6363
branch: ${{ matrix.libgccjit_version.artifacts_branch }}
6464
event: push
@@ -71,9 +71,8 @@ jobs:
7171
- name: Setup path to libgccjit
7272
if: matrix.libgccjit_version.gcc != 'libgccjit12.so'
7373
run: |
74-
echo $(readlink -f gcc-build) > gcc_path
75-
# NOTE: the filename is still libgccjit.so even when the artifact name is different.
76-
ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0
74+
sudo dpkg --force-overwrite -i gcc-13/gcc-13.deb
75+
echo /usr/lib/ > gcc_path
7776
7877
- name: Set env
7978
run: |
@@ -141,6 +140,9 @@ jobs:
141140
if: ${{ matrix.libgccjit_version.gcc == 'libgccjit12.so' }}
142141
run: cat failing-ui-tests12.txt >> failing-ui-tests.txt
143142

143+
- name: Add more failing tests because the sysroot is not compiled with LTO
144+
run: cat failing-non-lto-tests.txt >> failing-ui-tests.txt
145+
144146
- name: Run tests
145147
run: |
146148
${{ matrix.libgccjit_version.env_extra }} ./test.sh --release --clean --build-sysroot ${{ matrix.commands }} ${{ matrix.libgccjit_version.extra }}

.github/workflows/release.yml

+12-10
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
libgccjit_version:
22-
- { gcc: "libgccjit.so", artifacts_branch: "master" }
2321
commands: [
2422
"--test-successful-rustc --nb-parts 2 --current-part 0",
2523
"--test-successful-rustc --nb-parts 2 --current-part 1",
@@ -40,18 +38,17 @@ jobs:
4038
uses: dawidd6/action-download-artifact@v2
4139
with:
4240
workflow: main.yml
43-
name: ${{ matrix.libgccjit_version.gcc }}
44-
path: gcc-build
41+
name: gcc-13
42+
path: gcc-13
4543
repo: antoyo/gcc
46-
branch: ${{ matrix.libgccjit_version.artifacts_branch }}
44+
branch: "master"
4745
event: push
4846
search_artifacts: true # Because, instead, the action only check the last job ran and that won't work since we want multiple artifacts.
4947

5048
- name: Setup path to libgccjit
5149
run: |
52-
echo $(readlink -f gcc-build) > gcc_path
53-
# NOTE: the filename is still libgccjit.so even when the artifact name is different.
54-
ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0
50+
sudo dpkg --force-overwrite -i gcc-13/gcc-13.deb
51+
echo /usr/lib/ > gcc_path
5552
5653
- name: Set env
5754
run: |
@@ -89,7 +86,7 @@ jobs:
8986
- name: Build
9087
run: |
9188
./y.sh prepare --only-libcore
92-
./build.sh --release --release-sysroot
89+
EMBED_LTO_BITCODE=1 ./build.sh --release --release-sysroot
9390
cargo test
9491
./clean_all.sh
9592
@@ -98,6 +95,8 @@ jobs:
9895
git config --global user.email "[email protected]"
9996
git config --global user.name "User"
10097
./y.sh prepare
98+
# FIXME(antoyo): we cannot enable LTO for stdarch tests currently because of some failing LTO tests using proc-macros.
99+
echo -n 'lto = "fat"' >> build_sysroot/Cargo.toml
101100
102101
# Compile is a separate step, as the actions-rs/cargo action supports error annotations
103102
- name: Compile
@@ -106,6 +105,9 @@ jobs:
106105
command: build
107106
args: --release
108107

108+
- name: Add more failing tests because of undefined symbol errors (FIXME)
109+
run: cat failing-lto-tests.txt >> failing-ui-tests.txt
110+
109111
- name: Run tests
110112
run: |
111-
./test.sh --release --clean --release-sysroot --build-sysroot ${{ matrix.commands }}
113+
EMBED_LTO_BITCODE=1 ./test.sh --release --clean --release-sysroot --build-sysroot ${{ matrix.commands }}

.github/workflows/stdarch.yml

+6-8
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ jobs:
1818
strategy:
1919
fail-fast: false
2020
matrix:
21-
libgccjit_version:
22-
- { gcc: "libgccjit.so", artifacts_branch: "master" }
2321
cargo_runner: [
2422
"sde -future -rtm_mode full --",
2523
"",
@@ -54,18 +52,17 @@ jobs:
5452
uses: dawidd6/action-download-artifact@v2
5553
with:
5654
workflow: main.yml
57-
name: ${{ matrix.libgccjit_version.gcc }}
58-
path: gcc-build
55+
name: gcc-13
56+
path: gcc-13
5957
repo: antoyo/gcc
60-
branch: ${{ matrix.libgccjit_version.artifacts_branch }}
58+
branch: "master"
6159
event: push
6260
search_artifacts: true # Because, instead, the action only check the last job ran and that won't work since we want multiple artifacts.
6361

6462
- name: Setup path to libgccjit
6563
run: |
66-
echo $(readlink -f gcc-build) > gcc_path
67-
# NOTE: the filename is still libgccjit.so even when the artifact name is different.
68-
ln gcc-build/libgccjit.so gcc-build/libgccjit.so.0
64+
sudo dpkg --force-overwrite -i gcc-13/gcc-13.deb
65+
echo /usr/lib/ > gcc_path
6966
7067
- name: Set env
7168
run: |
@@ -139,4 +136,5 @@ jobs:
139136
if: ${{ matrix.cargo_runner }}
140137
run: |
141138
cd build_sysroot/sysroot_src/library/stdarch/
139+
# FIXME: these tests fail when the sysroot is compiled with LTO because of a missing symbol in proc-macro.
142140
STDARCH_TEST_EVERYTHING=1 CHANNEL=release CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_RUNNER="${{ matrix.cargo_runner }}" TARGET=x86_64-unknown-linux-gnu CG_RUSTFLAGS="-Ainternal_features" ../../../../cargo.sh test -- --skip rtm --skip tbm --skip sse4a

Cargo.lock

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ gccjit = { git = "https://github.com/antoyo/gccjit.rs" }
2727
# Local copy.
2828
#gccjit = { path = "../gccjit.rs" }
2929

30+
object = { version = "0.30.1", default-features = false, features = [
31+
"std",
32+
"read",
33+
] }
3034
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
3135
# TODO(antoyo): make tempfile optional.
3236
tempfile = "3.7.1"

Readme.md

+46-1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ $ CHANNEL="release" $CG_GCCJIT_DIR/cargo.sh run
9191

9292
If you compiled cg_gccjit in debug mode (aka you didn't pass `--release` to `./test.sh`) you should use `CHANNEL="debug"` instead or omit `CHANNEL="release"` completely.
9393

94+
To use LTO, you need to set the variable `FAT_LTO=1` and `EMBED_LTO_BITCODE=1` in addition to setting `lto = "fat"` in the `Cargo.toml`.
95+
Don't set `FAT_LTO` when compiling the sysroot, though: only set `EMBED_LTO_BITCODE=1`.
96+
9497
### Rustc
9598

9699
> You should prefer using the Cargo method.
@@ -191,6 +194,48 @@ set substitute-path /usr/src/debug/gcc /path/to/gcc-repo/gcc
191194
192195
TODO(antoyo): but that's not what I remember I was doing.
193196
197+
### `failed to build archive` error
198+
199+
When you get this error:
200+
201+
```
202+
error: failed to build archive: failed to open object file: No such file or directory (os error 2)
203+
```
204+
205+
That can be caused by the fact that you try to compile with `lto = "fat"`, but you didn't compile the sysroot with LTO.
206+
(Not sure if that's the reason since I cannot reproduce anymore. Maybe it happened when forgetting setting `FAT_LTO`.)
207+
208+
### How to debug GCC LTO
209+
210+
Run do the command with `-v -save-temps` and then extract the `lto1` line from the output and run that under the debugger.
211+
212+
### How to send arguments to the GCC linker
213+
214+
```
215+
CG_RUSTFLAGS="-Clink-args=-save-temps -v" ../cargo.sh build
216+
```
217+
218+
### How to see the personality functions in the asm dump
219+
220+
```
221+
CG_RUSTFLAGS="-Clink-arg=-save-temps -v -Clink-arg=-dA" ../cargo.sh build
222+
```
223+
224+
### How to see the LLVM IR for a sysroot crate
225+
226+
```
227+
cargo build -v --target x86_64-unknown-linux-gnu -Zbuild-std
228+
# Take the command from the output and add --emit=llvm-ir
229+
```
230+
231+
### To prevent the linker from unmangling symbols
232+
233+
Run with:
234+
235+
```
236+
COLLECT_NO_DEMANGLE=1
237+
```
238+
194239
### How to use a custom-build rustc
195240
196241
* Build the stage2 compiler (`rustup toolchain link debug-current build/x86_64-unknown-linux-gnu/stage2`).
@@ -253,4 +298,4 @@ generate it in [gimple.md](./doc/gimple.md).
253298
* Set `linker='-Clinker=m68k-linux-gcc'`.
254299
* Set the path to the cross-compiling libgccjit in `gcc_path`.
255300
* Comment the line: `context.add_command_line_option("-masm=intel");` in src/base.rs.
256-
* (might not be necessary) Disable the compilation of libstd.so (and possibly libcore.so?).
301+
* (might not be necessary) Disable the compilation of libstd.so (and possibly libcore.so?): Remove dylib from build_sysroot/sysroot_src/library/std/Cargo.toml.

build_sysroot/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ rustc-std-workspace-std = { path = "./sysroot_src/library/rustc-std-workspace-st
1919

2020
[profile.release]
2121
debug = true
22+
#lto = "fat" # TODO(antoyo): re-enable when the failing LTO tests regarding proc-macros are fixed.

config.sh

+12-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,14 @@ if [[ "$HOST_TRIPLE" != "$TARGET_TRIPLE" ]]; then
3838
fi
3939
fi
4040

41-
export RUSTFLAGS="$CG_RUSTFLAGS $linker -Csymbol-mangling-version=v0 -Cdebuginfo=2 -Clto=off -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot $TEST_FLAGS"
41+
# Since we don't support ThinLTO, disable LTO completely when not trying to do LTO.
42+
# TODO(antoyo): remove when we can handle ThinLTO.
43+
disable_lto_flags=''
44+
if [[ ! -v FAT_LTO ]]; then
45+
disable_lto_flags='-Clto=off'
46+
fi
47+
48+
export RUSTFLAGS="$CG_RUSTFLAGS $linker -Csymbol-mangling-version=v0 -Cdebuginfo=2 $disable_lto_flags -Zcodegen-backend=$(pwd)/target/${CHANNEL:-debug}/librustc_codegen_gcc.$dylib_ext --sysroot $(pwd)/build_sysroot/sysroot $TEST_FLAGS"
4249

4350
# FIXME(antoyo): remove once the atomic shim is gone
4451
if [[ `uname` == 'Darwin' ]]; then
@@ -50,3 +57,7 @@ export RUSTC_LOG=warn # display metadata load errors
5057

5158
export LD_LIBRARY_PATH="$(pwd)/target/out:$(pwd)/build_sysroot/sysroot/lib/rustlib/$TARGET_TRIPLE/lib:$GCC_PATH"
5259
export DYLD_LIBRARY_PATH=$LD_LIBRARY_PATH
60+
# NOTE: To avoid the -fno-inline errors, use /opt/gcc/bin/gcc instead of cc.
61+
# To do so, add a symlink for cc to /opt/gcc/bin/gcc in our PATH.
62+
# Another option would be to add the following Rust flag: -Clinker=/opt/gcc/bin/gcc
63+
export PATH="/opt/gcc/bin:$PATH"

failing-lto-tests.txt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
tests/ui/lint/unsafe_code/forge_unsafe_block.rs
2+
tests/ui/lint/unused-qualification-in-derive-expansion.rs
3+
tests/ui/macro-quote-test.rs
4+
tests/ui/macros/proc_macro.rs
5+
tests/ui/panic-runtime/lto-unwind.rs
6+
tests/ui/resolve/derive-macro-1.rs
7+
tests/ui/resolve/derive-macro-2.rs
8+
tests/ui/rfcs/rfc-2565-param-attrs/param-attrs-pretty.rs
9+
tests/ui/rfcs/rfc-2565-param-attrs/issue-64682-dropping-first-attrs-in-impl-fns.rs
10+
tests/ui/rfcs/rfc-3348-c-string-literals/edition-spans.rs
11+
tests/ui/rust-2018/suggestions-not-always-applicable.rs
12+
tests/ui/rust-2021/reserved-prefixes-via-macro.rs
13+
tests/ui/underscore-imports/duplicate.rs
14+
tests/ui/async-await/issues/issue-60674.rs
15+
tests/ui/attributes/main-removed-2/main.rs
16+
tests/ui/cfg/assume-incomplete-release/assume-incomplete.rs
17+
tests/ui/crate-loading/cross-compiled-proc-macro.rs
18+
tests/ui/derives/derive-marker-tricky.rs
19+
tests/ui/diagnostic_namespace/existing_proc_macros.rs
20+
tests/ui/fmt/format-args-capture-issue-106408.rs
21+
tests/ui/fmt/indoc-issue-106408.rs
22+
tests/ui/hygiene/issue-77523-def-site-async-await.rs
23+
tests/ui/inherent-impls-overlap-check/no-overlap.rs

failing-non-lto-tests.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/ui/issues/issue-44056.rs
2+
tests/ui/lto/fat-lto.rs
3+
tests/ui/lto/debuginfo-lto.rs
4+
tests/ui/lto/lto-many-codegen-units.rs
5+
tests/ui/lto/issue-100772.rs
6+
tests/ui/lto/lto-rustc-loads-linker-plugin.rs
7+
tests/ui/panic-runtime/lto-unwind.rs
8+
tests/ui/sanitize/issue-111184-generator-witness.rs
9+
tests/ui/sepcomp/sepcomp-lib-lto.rs
10+
tests/ui/lto/lto-opt-level-s.rs
11+
tests/ui/lto/lto-opt-level-z.rs

failing-ui-tests.txt

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
tests/ui/allocator/custom-in-block.rs
2-
tests/ui/allocator/custom-in-submodule.rs
3-
tests/ui/allocator/custom.rs
4-
tests/ui/allocator/hygiene.rs
51
tests/ui/allocator/no_std-alloc-error-handler-custom.rs
62
tests/ui/allocator/no_std-alloc-error-handler-default.rs
7-
tests/ui/allocator/xcrate-use.rs
8-
tests/ui/allocator/xcrate-use2.rs
93
tests/ui/asm/may_unwind.rs
104
tests/ui/asm/x86_64/multiple-clobber-abi.rs
115
tests/ui/debuginfo/debuginfo-emit-llvm-ir-and-split-debuginfo.rs
@@ -14,7 +8,6 @@ tests/ui/linkage-attr/linkage1.rs
148
tests/ui/lto/dylib-works.rs
159
tests/ui/numbers-arithmetic/saturating-float-casts.rs
1610
tests/ui/polymorphization/promoted-function.rs
17-
tests/ui/process/nofile-limit.rs
1811
tests/ui/sepcomp/sepcomp-cci.rs
1912
tests/ui/sepcomp/sepcomp-extern.rs
2013
tests/ui/sepcomp/sepcomp-fns-backwards.rs
@@ -53,8 +46,8 @@ tests/ui/issues/issue-40883.rs
5346
tests/ui/issues/issue-43853.rs
5447
tests/ui/issues/issue-47364.rs
5548
tests/ui/macros/rfc-2011-nicer-assert-messages/assert-without-captures-does-not-create-unnecessary-code.rs
56-
tests/ui/rfcs/rfc-2091-track-caller/std-panic-locations.rs
5749
tests/ui/rfcs/rfc-1857-stabilize-drop-order/drop-order.rs
50+
tests/ui/rfcs/rfc-2091-track-caller/std-panic-locations.rs
5851
tests/ui/simd/issue-17170.rs
5952
tests/ui/simd/issue-39720.rs
6053
tests/ui/simd/issue-89193.rs
@@ -64,9 +57,15 @@ tests/ui/alloc-error/default-alloc-error-hook.rs
6457
tests/ui/generator/panic-safe.rs
6558
tests/ui/issues/issue-14875.rs
6659
tests/ui/issues/issue-29948.rs
67-
tests/ui/panic-while-printing.rs
68-
tests/ui/enum-discriminant/get_discr.rs
6960
tests/ui/panics/nested_panic_caught.rs
7061
tests/ui/simd/intrinsic/generic-bswap-byte.rs
7162
tests/ui/const_prop/ice-issue-111353.rs
7263
tests/ui/process/println-with-broken-pipe.rs
64+
tests/ui/panic-runtime/lto-abort.rs
65+
tests/ui/lto/thin-lto-inlines2.rs
66+
tests/ui/lto/weak-works.rs
67+
tests/ui/lto/thin-lto-inlines.rs
68+
tests/ui/lto/thin-lto-global-allocator.rs
69+
tests/ui/lto/msvc-imp-present.rs
70+
tests/ui/lto/lto-thin-rustc-loads-linker-plugin.rs
71+
tests/ui/lto/all-crates.rs

0 commit comments

Comments
 (0)