Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
goiw111 opened this issue Oct 16, 2023 · 10 comments
Closed

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

goiw111 opened this issue Oct 16, 2023 · 10 comments
Labels
-Zbuild-std Unstable Cargo option: Compile the standard library yourself. A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@goiw111
Copy link

goiw111 commented Oct 16, 2023

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

@goiw111 goiw111 added the C-bug Category: This is a bug. label Oct 16, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 16, 2023
@saethlin
Copy link
Member

saethlin commented Oct 16, 2023

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
Copy link
Member

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
Copy link
Member

I opened #116832 for at least some of the panics that could be optimized out.

@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 17, 2023
@goiw111
Copy link
Author

goiw111 commented Oct 17, 2023

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 saethlin added O-AVR Target: AVR processors (ATtiny, ATmega, etc.) -Zbuild-std Unstable Cargo option: Compile the standard library yourself. A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. I-slow Issue: Problems and improvements with respect to performance of generated code. labels Oct 17, 2023
@saethlin
Copy link
Member

Thanks, that helps a lot.

#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 #116832

@saethlin saethlin changed the title linking with avr-gcc failed compiler-builtins doesn't link on AVR with debug assertions Oct 17, 2023
@DianQK
Copy link
Member

DianQK commented Oct 18, 2023

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
Copy link
Contributor

tmiasko commented Oct 18, 2023

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
Copy link
Author

goiw111 commented Oct 18, 2023

[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
Copy link
Author

goiw111 commented Oct 18, 2023

I used just

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

@tmiasko
Copy link
Contributor

tmiasko commented Oct 24, 2023

Thanks for checking.

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

@tmiasko tmiasko closed this as completed Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Zbuild-std Unstable Cargo option: Compile the standard library yourself. A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such I-slow Issue: Problems and improvements with respect to performance of generated code. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants