rust-lang / rust

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

"error: ran out of registers during register allocation" for absolutely minimal attiny85 program #108232

Closed mgrunwald closed 1 year ago

mgrunwald commented 1 year ago

I tried this code …

#![no_std]
#![no_main]

#[no_mangle]
pub extern fn main() {
}

#[panic_handler]
fn my_panic(_info: &core::panic::PanicInfo) -> ! {
    loop {}
}

… with this Cargo.toml …

[package]
name = "avr01"
version = "0.1.0"
edition = "2021"

[dependencies]

[profile.dev]
panic = "abort"

[profile.release]
panic = "abort"

[dependencies.attiny-hal]
git = "https://github.com/rahix/avr-hal"
version = "0.1.0"
rev="4c9c44c314eb061ee20556ef10d45dea36e75ee4"
features = ["attiny85"]

… and this target avr-attiny85.json …

{
  "arch": "avr",
  "atomic-cas": false,
  "cpu": "attiny85",
  "data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
  "eh-frame-header": false,
  "exe-suffix": ".elf",
  "executables": true,
  "late-link-args": {
    "gcc": [
      "-lgcc"
    ]
  },
  "linker": "avr-gcc",
  "llvm-target": "avr-unknown-unknown",
  "max-atomic-width": 8,
  "no-default-libraries": false,
  "pre-link-args": {
    "gcc": [
      "-mmcu=attiny85"
    ]
  },
  "target-c-int-width": "16",
  "target-pointer-width": "16"
}

… using today's rust nightly (rustc 1.69.0-nightly (4507fdaaa 2023-02-18)) and this commandline:

cargo build -Z build-std=core --target avr-attiny85.json

I expected to see this happen: Given that I'm trying to compile rust for avr without success for a while, the only thing I expect is that the compiler doesn't crash.

Instead, this happened:

   Compiling compiler_builtins v0.1.87
   Compiling core v0.0.0 (/home/markus/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
   Compiling proc-macro2 v1.0.51
   Compiling quote v1.0.23
   Compiling unicode-ident v1.0.6
   Compiling proc-macro-hack v0.5.20+deprecated
   Compiling syn v1.0.107
   Compiling rustversion v1.0.11
   Compiling paste v1.0.11
   Compiling avr-hal-generic v0.1.0 (https://github.com/rahix/avr-hal#4c9c44c3)
   Compiling ufmt-macros v0.2.0
   Compiling rustc-std-workspace-core v1.99.0 (/home/markus/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
error: ran out of registers during register allocation

error: could not compile `core` due to previous error

This seems very much related to this recently closed issue: https://github.com/rust-lang/rust/issues/88252

Meta

rustc --version --verbose:

rustc --version --verbose
rustc 1.69.0-nightly (4507fdaaa 2023-02-18)
binary: rustc
commit-hash: 4507fdaaa27ea2fb59a41df2ce7d1f290da53dae
commit-date: 2023-02-18
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7

I wasn't able to create a backtrace using RUST_BACKTRACE=1 cargo build.

Background story in users.rust-lang.org: https://users.rust-lang.org/t/sigsegv-when-trying-to-build-minimal-program-for-attiny85/89282

Edit: I updated the version info and git revision for attiny-hal

workingjubilee commented 1 year ago

[dependencies.attiny-hal] git = "https://github.com/rahix/avr-hal" version = "*" features = ["attiny85"]

For an MCVE, in order for it to be reproducible later, it would help greatly if the crate version and git revision was specified, or better yet, a crate version on crates.io. Otherwise changes in a dependency's library code could affect repro, and this can be especially important in the embedded ecosystem, as such crates rely more on e.g. inline assembly which was only recently stabilized and only for certain architectures which have had more validation through well-known usage.

I believe you can find the version and revision that Cargo selected in the Cargo.lock file it generated.

Does this still happen if you include this in your Cargo.toml

[profile.release] lto = true

and use cargo build -Z build-std=core --target avr-attiny85.json --release?

mgrunwald commented 1 year ago

[dependencies.attiny-hal] git = "https://github.com/rahix/avr-hal" version = "*" features = ["attiny85"]

For an MCVE, in order for it to be reproducible later, it would help greatly if the crate version and git revision was specified

That makes a lot of sense, I updated my original post.

Does this still happen if you include this in your Cargo.toml

[profile.release] lto = true

and use cargo build -Z build-std=core --target avr-attiny85.json --release?

No it doesn't! In fact, It compiles without error now :D Thanks a lot!

workingjubilee commented 1 year ago

Thanks for reporting!

Hopefully we can figure out a way to make this at least not require manual configuration to work.