rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.81k stars 12.66k forks source link

compiler-builtins doesn't link on AVR with debug assertions #116817

Closed goiw111 closed 12 months ago

goiw111 commented 1 year ago

I tried this code:

let val= (5.0 / 1023.0) * i as f32;
let out = uFmt_f32::Zero(val);
uwriteln!(&mut serial, "A0 = {}V", out)

can you help me to find out what is the issue?

Meta

rustc --version --verbose:

rustc 1.73.0-nightly (03a119b0b 2023-08-07)
binary: rustc
commit-hash: 03a119b0b0e310d22d94399b24ed030056050f13
commit-date: 2023-08-07
host: x86_64-unknown-linux-gnu
release: 1.73.0-nightly
LLVM version: 16.0.5
Backtrace

``` error: linking with `avr-gcc` failed: exit status: 1 | = note: LC_ALL="C" PATH="/home/sohayl/.rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/sohayl/.cargo/bin:/home/sohayl/.nvm/versions/node/v18.18.0/bin:/home/sohayl/.local/bin:/home/sohayl/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin" VSLANG="1033" "avr-gcc" "-mmcu=atmega328p" "/tmp/rustcUdrntv/symbols.o" "/home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/example_1-90b57356f9eb72e2.ufmt_float-cc9c3e6b4f384f9b.ufmt_float.a9692a864ac7e113-cgu.0.rcgu.o.rcgu.o" "-Wl,--as-needed" "-L" "/home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps" "-L" "/home/sohayl/Projects/arduino_rust/test/target/debug/deps" "-L" "/home/sohayl/.rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-Wl,-Bstatic" "/home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/libcompiler_builtins-f6499a221392faf1.rlib" "-Wl,-Bdynamic" "-lgcc" "-Wl,-z,noexecstack" "-L" "/home/sohayl/.rustup/toolchains/nightly-2023-08-08-x86_64-unknown-linux-gnu/lib/rustlib/avr-atmega328p/lib" "-o" "/home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/example_1-90b57356f9eb72e2.elf" "-Wl,--gc-sections" "-no-pie" = note: /usr/lib/gcc/avr/13.2.0/../../../../avr/bin/ld: /home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/libcompiler_builtins-f6499a221392faf1.rlib(compiler_builtins-f6499a221392faf1.compiler_builtins.4319175d524586f5-cgu.1.rcgu.o): in function `_ZN17compiler_builtins5float4conv12int_to_float15u32_to_f32_bits17h3eb3f174af2218eeE': /home/sohayl/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.98/src/float/conv.rs:15:(.text._ZN17compiler_builtins5float4conv12int_to_float15u32_to_f32_bits17h3eb3f174af2218eeE+0x1d8): undefined reference to `_ZN4core9panicking5panic17h0faeb0977d538750E' /usr/lib/gcc/avr/13.2.0/../../../../avr/bin/ld: /home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/libcompiler_builtins-f6499a221392faf1.rlib(compiler_builtins-f6499a221392faf1.compiler_builtins.4319175d524586f5-cgu.1.rcgu.o): in function `_ZN17compiler_builtins5float3mul3mul17h999fa9acfedbde58E': /home/sohayl/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.98/src/float/mul.rs:(.text._ZN17compiler_builtins5float3mul8__mulsf317h418f26ddc414d3e4E+0x5e8): undefined reference to `_ZN4core9panicking5panic17h0faeb0977d538750E' /usr/lib/gcc/avr/13.2.0/../../../../avr/bin/ld: /home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/libcompiler_builtins-f6499a221392faf1.rlib(compiler_builtins-f6499a221392faf1.compiler_builtins.4319175d524586f5-cgu.1.rcgu.o): in function `_ZN55_$LT$f32$u20$as$u20$compiler_builtins..float..Float$GT$9normalize17hf0353452146143eaE': /home/sohayl/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.98/src/float/mod.rs:164:(.text._ZN55_$LT$f32$u20$as$u20$compiler_builtins..float..Float$GT$9normalize17hf0353452146143eaE+0x15e): undefined reference to `_ZN4core9panicking5panic17h0faeb0977d538750E' /usr/lib/gcc/avr/13.2.0/../../../../avr/bin/ld: /home/sohayl/Projects/arduino_rust/test/target/avr-atmega328p/debug/deps/libcompiler_builtins-f6499a221392faf1.rlib(compiler_builtins-f6499a221392faf1.compiler_builtins.4319175d524586f5-cgu.4.rcgu.o): in function `_ZN17compiler_builtins5float4conv9__fixsfsi17h7493de2ccc691cbaE': /home/sohayl/.cargo/registry/src/index.crates.io-6f17d22bba15001f/compiler_builtins-0.1.98/src/float/conv.rs:255:(.text._ZN17compiler_builtins5float4conv9__fixsfsi17h7493de2ccc691cbaE+0xe4): undefined reference to `_ZN4core9panicking5panic17h0faeb0977d538750E' collect2: error: ld returned 1 exit status = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified = note: use the `-l` flag to specify native libraries to link = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname) error: could not compile `test` (bin "example_1") due to previous error ```

saethlin commented 1 year ago

I think you managed to compile compiler-builtins with debug assertions enabled. The problem is not with the linker, but that compiler-builtins structurally cannot depend on core, except that if any overflow checks are inserted during codegen then not optimized out, the panic paths try to execute a function from core.

How exactly are you compiling this?

~I also need to write an LLVM issue. The reason you can't do this is just that LLVM doesn't have the optimizations required to remove the overflow checks.~

saethlin commented 1 year ago

Actually maybe LLVM has the optimizations required for this now. You're using a nightly that is rather old, it's the same version as current stable. Can you try out the latest nightly?

saethlin commented 1 year ago

I opened https://github.com/rust-lang/rust/issues/116832 for at least some of the panics that could be optimized out.

goiw111 commented 1 year ago

I'm using this config

cat .cargo/config.toml [build] target = "avr-specs/avr-atmega328p.json"

[target.'cfg(target_arch = "avr")'] runner = "ravedude uno -cb 57600"

[unstable] build-std = ["core"]

cat cargo.toml

. . .

[profile.dev] panic = "abort" lto = true opt-level = "s"

[profile.release] panic = "abort" codegen-units = 1 debug = true lto = true opt-level = "s"

I use cargo build

And I updated the compiler and I still have the same issue

saethlin commented 1 year ago

Thanks, that helps a lot.

https://github.com/rust-lang/rust/pull/113923 straightens out the behavior of LTO on the compiler-builtins crate. I think that fixes some of these linker errors. It doesn't get you to compiling with this config, but it removes one of the reasons I think.

If I turn off LTO, I get two linker errors. One of them is a legitimate panic path in <f32 as compiler_builtins::float::Float>::normalize, which I suspect becomes unreachable if that function is inlined. So I think the thing to do there is to make it be inlined in compiler-builtins.

The other has panic paths that can be optimized out but aren't. Here is the compiler-builtins code extracted: https://godbolt.org/z/vrMs7WWdE Here's alive2 saying removing the panics is correct: https://alive2.llvm.org/ce/z/ZCDSRy

I do not know if this is the same optimization that @DianQK is working on for https://github.com/rust-lang/rust/issues/116832

DianQK commented 1 year ago

The other has panic paths that can be optimized out but aren't. Here is the compiler-builtins code extracted: https://godbolt.org/z/vrMs7WWdE Here's alive2 saying removing the panics is correct: https://alive2.llvm.org/ce/z/ZCDSRy

I do not know if this is the same optimization that @DianQK is working on for #116832

Wow, so much panic can be removed. It looks like some different optimizations.

tmiasko commented 1 year ago

You could try building with following options (there might be spurious warnings that "profile package spec ... did not match any packages"):

[profile.dev.package.compiler_builtins]
debug-assertions = false
overflow-checks = false

[profile.release.package.compiler_builtins]
debug-assertions = false
overflow-checks = false

The compiler-builtins crate needs to be built in a specific way. Unfortunately this does not happen out of the box with -Z build-std at the moment.

goiw111 commented 1 year ago
[profile.dev.package.compiler_builtins]
debug-assertions = false
overflow-checks = false

[profile.release.package.compiler_builtins]
debug-assertions = false
overflow-checks = false

it's work, thank you but it's still giving me some warnings

> warning: profile package spec `compiler_builtins` in profile `release` did not match any packages
> warning: profile package spec `compiler_builtins` in profile `dev` did not match any packages
goiw111 commented 1 year ago

I used just

[profile.dev.package.compiler_builtins] overflow-checks = false

tmiasko commented 12 months ago

Thanks for checking.

The issue that "compiler-builtins" should be built with special flags is already tracked in https://github.com/rust-lang/wg-cargo-std-aware/issues/28.