Skip to content

Commit 5cb8d34

Browse files
authored
Merge pull request #769 from rust-lang/fix/cross-lto
Add test for cross-language LTO
2 parents 97e1942 + 243ef31 commit 5cb8d34

File tree

5 files changed

+52
-1
lines changed

5 files changed

+52
-1
lines changed

.github/workflows/release.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ jobs:
8282
printf '[profile.release]\nlto = "fat"\n' >> build/build_sysroot/sysroot_src/library/Cargo.toml
8383
EMBED_LTO_BITCODE=1 ./y.sh test --release --clean --release-sysroot --build-sysroot --keep-lto-tests ${{ matrix.commands }}
8484
85-
- name: Run y.sh cargo build
85+
- name: LTO test
8686
run: |
8787
EMBED_LTO_BITCODE=1 CHANNEL="release" ./y.sh cargo build --release --manifest-path tests/hello-world/Cargo.toml
8888
call_found=$(objdump -dj .text tests/hello-world/target/release/hello_world | grep -c "call .*mylib.*my_func" ) ||:
@@ -92,6 +92,21 @@ jobs:
9292
exit 1
9393
fi
9494
95+
- name: Cross-language LTO test
96+
run: |
97+
pushd tests/cross_lang_lto
98+
gcc -c -flto add.c -masm=intel -fPIC -O3
99+
ar rcs libadd.a add.o
100+
popd
101+
102+
EMBED_LTO_BITCODE=1 CHANNEL="release" CG_RUSTFLAGS="-L native=. -Clinker-plugin-lto -Clinker=gcc" ./y.sh cargo build --release --manifest-path tests/cross_lang_lto/Cargo.toml
103+
call_found=$(objdump -dj .text tests/cross_lang_lto/target/release/cross_lang_lto | grep -c "call .*my_add" ) ||:
104+
if [ $call_found -gt 0 ]; then
105+
echo "ERROR: call my_add found in asm"
106+
echo "Test is done with cross-language LTO enabled, hence inlining should occur across object files"
107+
exit 1
108+
fi
109+
95110
# Summary job for the merge queue.
96111
# ALL THE PREVIOUS JOBS NEED TO BE ADDED TO THE `needs` SECTION OF THIS JOB!
97112
success_release:

tests/cross_lang_lto/Cargo.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# This file is automatically @generated by Cargo.
2+
# It is not intended for manual editing.
3+
version = 4
4+
5+
[[package]]
6+
name = "cross_lang_lto"
7+
version = "0.1.0"

tests/cross_lang_lto/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "cross_lang_lto"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

tests/cross_lang_lto/add.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <stdint.h>
2+
3+
uint32_t my_add(uint32_t a, uint32_t b) {
4+
return a + b;
5+
}

tests/cross_lang_lto/src/main.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Compile the C code with:
3+
* gcc -c -flto add.c -ffat-lto-objects
4+
* ar rcs libadd.a add.o
5+
*
6+
* Compile the Rust code with:
7+
* EMBED_LTO_BITCODE=1 CG_RUSTFLAGS="-L native=. -Clinker-plugin-lto -Clinker=gcc" y cargo run --release
8+
*/
9+
10+
#[link(name="add")]
11+
unsafe extern "C" {
12+
fn my_add(a: u32, b: u32) -> u32;
13+
}
14+
15+
fn main() {
16+
let res = unsafe { my_add(30, 12) };
17+
println!("{}", res);
18+
}

0 commit comments

Comments
 (0)