rust-lang / rust

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

LLVM codegen backend error in stable embedded Rust project #74513

Closed luojia65 closed 2 years ago

luojia65 commented 4 years ago

Code

I don't know exactly where, but somewhere in my embedded Rust code resulted in compile error. The compiler is stable Rust 1.45.0.

I cannot reproduce if I take a part of the source code, maybe I need further help to get a minimum ICE example.

Here's the file:

gd32v-workspace.zip

Download this file, decompress and execute:

cd gd32vf103-example
cargo build --example vf103c-start-backup-data

Meta

rustc --version --verbose:

rustc 1.45.0 (5c1f21c3b 2020-07-13)
binary: rustc
commit-hash: 5c1f21c3b82297671ad3ae1e8c942d2ca92e84f2
commit-date: 2020-07-13
host: x86_64-apple-darwin
release: 1.45.0
LLVM version: 10.0

Error output

Backtrace

``` ❯ RUST_BACKTRACE=full cargo build --example vf103c-start-backup-data --verbose Fresh lazy_static v1.4.0 Fresh regex-syntax v0.6.18 Fresh semver-parser v0.7.0 Fresh unicode-xid v0.1.0 Fresh bit_field v0.10.0 Fresh rand_core v0.4.2 Fresh vcell v0.1.2 Fresh nb v1.0.0 Fresh r0 v1.0.0 Fresh nb v0.1.2 Fresh panic-halt v0.2.0 Fresh thread_local v1.0.1 Fresh semver v0.9.0 Fresh rand_core v0.3.1 Fresh embedded-hal v1.0.0-alpha.1 (https://github.com/rust-embedded/embedded-hal#83f5beac) Fresh memchr v2.3.3 Fresh rustc_version v0.2.3 Fresh proc-macro2 v0.4.30 Fresh rand v0.5.6 Fresh aho-corasick v0.7.10 Fresh quote v0.6.13 Fresh regex v1.3.9 Fresh syn v0.15.44 Fresh riscv-target v0.1.2 Fresh bare-metal v0.2.5 Fresh riscv-rt-macros v0.1.6 Fresh riscv v0.6.0 Fresh gd32vf103-pac v0.3.1 (/Users/mac/2020/RustProjects/gd32v-workspace/gd32vf103-pac) Fresh riscv-rt v0.8.0 warning: lint `legacy_directory_ownership` has been removed: `converted into hard error, see issue #37872 for more information` --> gd32vf103-pac/src/lib.rs:5:9 | 5 | #![deny(legacy_directory_ownership)] | ^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(renamed_and_removed_lints)]` on by default warning: lint `plugin_as_library` has been removed: `plugins have been deprecated and retired` --> gd32vf103-pac/src/lib.rs:12:9 | 12 | #![deny(plugin_as_library)] | ^^^^^^^^^^^^^^^^^ warning: lint `safe_extern_statics` has been removed: `converted into hard error, see issue #36247 for more information` --> gd32vf103-pac/src/lib.rs:14:9 | 14 | #![deny(safe_extern_statics)] | ^^^^^^^^^^^^^^^^^^^ warning: unknown lint: `unions_with_drop_fields` --> gd32vf103-pac/src/lib.rs:16:9 | 16 | #![deny(unions_with_drop_fields)] | ^^^^^^^^^^^^^^^^^^^^^^^ | = note: `#[warn(unknown_lints)]` on by default warning: 4 warnings emitted Compiling gd32vf103-hal v0.0.5 (/Users/mac/2020/RustProjects/gd32v-workspace/gd32vf103-hal) Running `rustc --crate-name gd32vf103_hal --edition=2018 gd32vf103-hal/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=7d55167f0e15211b -C extra-filename=-7d55167f0e15211b --out-dir /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps --target riscv32imac-unknown-none-elf -C incremental=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/incremental -L dependency=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps -L dependency=/Users/mac/2020/RustProjects/gd32v-workspace/target/debug/deps --extern embedded_hal=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libembedded_hal-019224d84e9de5c8.rmeta --extern gd32vf103_pac=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_pac-8dcf5f32b20d82e4.rmeta --extern nb=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libnb-7b5a980be221e162.rmeta --extern riscv=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libriscv-af815419a3534480.rmeta -C link-arg=-Tlink.x -C link-arg=-Tmemory.x -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-84316d11b058d843/out -l static=gd32vf103-hal -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/riscv-e1760fc6a9873660/out` warning: unused import: `bkp` --> gd32vf103-hal/src/backup.rs:3:18 | 3 | use crate::pac::{bkp, BKP, PMU}; | ^^^ | = note: `#[warn(unused_imports)]` on by default warning: associated function is never used: `cfg1` --> gd32vf103-hal/src/rcu.rs:127:19 | 127 | pub(crate) fn cfg1(&mut self) -> &rcu::CFG1 { | ^^^^ | = note: `#[warn(dead_code)]` on by default warning: associated function is never used: `bdctl` --> gd32vf103-hal/src/rcu.rs:510:19 | 510 | pub(crate) fn bdctl(&mut self) -> &rcu::BDCTL { | ^^^^^ warning: field is never read: `state` --> gd32vf103-hal/src/wdog.rs:44:5 | 44 | state: STATE, | ^^^^^^^^^^^^ warning: field is never read: `wwdgt` --> gd32vf103-hal/src/wdog.rs:190:5 | 190 | wwdgt: WWDGT, | ^^^^^^^^^^^^ Compiling gd32vf103-hal-example v0.1.0 (/Users/mac/2020/RustProjects/gd32v-workspace/gd32vf103-example) Running `rustc --crate-name gd32vf103_hal_example --edition=2018 gd32vf103-example/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=776c25045ed9234f -C extra-filename=-776c25045ed9234f --out-dir /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps --target riscv32imac-unknown-none-elf -C incremental=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/incremental -L dependency=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps -L dependency=/Users/mac/2020/RustProjects/gd32v-workspace/target/debug/deps --extern gd32vf103_hal=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_hal-7d55167f0e15211b.rmeta --extern nb=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libnb-053dad82f20964c3.rmeta --extern panic_halt=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libpanic_halt-c20813a18ecd9008.rmeta --extern riscv_rt=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libriscv_rt-78f019e90760d0fe.rmeta -C link-arg=-Tlink.x -C link-arg=-Tmemory.x -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-example-c7768ee9e23700f1/out -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-84316d11b058d843/out -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/riscv-e1760fc6a9873660/out -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-45d4352344ed4955/out -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/riscv-rt-45d4352344ed4955/out` thread 'rustc' panicked at 'called `Option::unwrap()` on a `None` value', src/librustc_codegen_llvm/back/archive.rs:274:33 stack backtrace: 0: 0x10fb22dbe - ::fmt::he3a6eecb2201efb0 1: 0x10fb5c51c - core::fmt::write::h8ef98027ac1df1be 2: 0x10fb14897 - std::io::Write::write_fmt::hed52d76b85d70756 3: 0x10fb278f5 - std::panicking::default_hook::{{closure}}::h013242fa408d10b7 4: 0x10fb27632 - std::panicking::default_hook::h9777ceea989331ac 5: 0x11417d2b8 - rustc_driver::report_ice::h3b4ef3e9c85f0bd8 6: 0x10fb27f45 - std::panicking::rust_panic_with_hook::h845f7f3fcb3a3323 7: 0x10fb27af2 - rust_begin_unwind 8: 0x10fb7f87f - core::panicking::panic_fmt::h60d70f2d73364aac 9: 0x10fb7f7d7 - core::panicking::panic::h3582cc3308948195 10: 0x1144475b3 - ::build::h4aad89d60bdd3c09 11: 0x1144bd564 - rustc_codegen_ssa::back::link::link_binary::hd1bf01b56348ffb1 12: 0x11457730f - rustc_session::utils::::time::h0f9de8a99a521001 13: 0x1144e271b - ::link::h193ab3317bc5ddb9 14: 0x1142e7d94 - rustc_interface::queries::Linker::link::hb730e2c76eb5ddbe 15: 0x1141451f7 - rustc_interface::interface::run_compiler_in_existing_thread_pool::h18c835dfaf7b6d80 16: 0x11418b635 - rustc_ast::attr::with_globals::h3bd9a4b2c3edf3dd 17: 0x11418fe71 - std::sys_common::backtrace::__rust_begin_short_backtrace::hb809cf68603b0e7f 18: 0x1141480ec - core::ops::function::FnOnce::call_once{{vtable.shim}}::ha95ea21f25d153e2 19: 0x10fb3611d - std::sys::unix::thread::Thread::new::thread_start::h0031c4f1bba2584f 20: 0x7fff6d962109 - _ZL12preoptimized error: internal compiler error: unexpected panic note: the compiler unexpectedly panicked. this is a bug. note: we would appreciate a bug report: https://github.com/rust-lang/rust/blob/master/CONTRIBUTING.md#bug-reports note: rustc 1.45.0 (5c1f21c3b 2020-07-13) running on x86_64-apple-darwin note: compiler flags: -C embed-bitcode=no -C debuginfo=2 -C incremental -C link-arg=-Tlink.x -C link-arg=-Tmemory.x --crate-type lib note: some of the compiler flags provided by cargo are hidden query stack during panic: end of query stack warning: 5 warnings emitted error: could not compile `gd32vf103-hal`. Caused by: process didn't exit successfully: `rustc --crate-name gd32vf103_hal --edition=2018 gd32vf103-hal/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -Cembed-bitcode=no -C debuginfo=2 -C metadata=7d55167f0e15211b -C extra-filename=-7d55167f0e15211b --out-dir /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps --target riscv32imac-unknown-none-elf -C incremental=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/incremental -L dependency=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps -L dependency=/Users/mac/2020/RustProjects/gd32v-workspace/target/debug/deps --extern embedded_hal=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libembedded_hal-019224d84e9de5c8.rmeta --extern gd32vf103_pac=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libgd32vf103_pac-8dcf5f32b20d82e4.rmeta --extern nb=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libnb-7b5a980be221e162.rmeta --extern riscv=/Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/deps/libriscv-af815419a3534480.rmeta -C link-arg=-Tlink.x -C link-arg=-Tmemory.x -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/gd32vf103-hal-84316d11b058d843/out -l static=gd32vf103-hal -L /Users/mac/2020/RustProjects/gd32v-workspace/target/riscv32imac-unknown-none-elf/debug/build/riscv-e1760fc6a9873660/out` (exit code: 101) ```

bjorn3 commented 4 years ago

The crash is at https://github.com/rust-lang/rust/blob/b52522ade1f6979a35b24087dadcf5ba5c981cbe/src/librustc_codegen_llvm/back/archive.rs#L274

Noticed that riscv32imac-unknown-none-elf.a contains the file riscv32imac-unknown-none-elf.o twice:

$ ar t /tmp/riscv32imac-unknown-none-elf.a
/
//
riscv32imac-unknown-none-elf.o
riscv32imac-unknown-none-elf.o

It is possible that LLVM couldn't handle this and returned a path without filename or a non-utf8 path.

luojia65 commented 4 years ago

Thanks! :) I may need to recompile the .a file and try again. It would be better if LLVM could handle this issue