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

"LLVM ERROR: Not supported instr" on avr-unknown-gnu-atmega328 #400

Closed
sobolevn opened this issue Jan 9, 2021 · 43 comments · Fixed by rust-lang/rust#96845
Closed

"LLVM ERROR: Not supported instr" on avr-unknown-gnu-atmega328 #400

sobolevn opened this issue Jan 9, 2021 · 43 comments · Fixed by rust-lang/rust#96845

Comments

@sobolevn
Copy link

sobolevn commented Jan 9, 2021

I am experiencing a bug when trying to compile my application for Arduino Uno.

Probably, there's a regression somewhere.
Because, it used to work 1 month ago. CI: https://travis-ci.com/github/Rahix/avr-hal/builds/207260821

Original issue: Rahix/avr-hal#124

Output

Step 6/6 : RUN cargo +nightly build --example uno-blink
 ---> Running in fde0189b88d8
    Updating git repository `https://github.com/Rahix/ufmt.git`
    Updating crates.io index
 Downloading crates ...
  Downloaded nb v0.1.3
  Downloaded paste v1.0.4
  Downloaded rustc_version v0.2.3
  Downloaded semver-parser v0.7.0
  Downloaded quote v1.0.8
  Downloaded proc-macro2 v1.0.24
  Downloaded proc-macro-hack v0.5.19
  Downloaded vcell v0.1.3
  Downloaded void v1.0.2
  Downloaded cfg-if v0.1.10
  Downloaded unicode-xid v0.2.1
  Downloaded syn v1.0.58
  Downloaded avr-device-macros v0.2.3
  Downloaded avr-device v0.2.3
  Downloaded semver v0.9.0
  Downloaded panic-halt v0.2.0
  Downloaded nb v1.0.0
  Downloaded embedded-hal v0.2.4
  Downloaded bare-metal v0.2.5
 Downloading crates ...
  Downloaded cc v1.0.60
  Downloaded compiler_builtins v0.1.39
  Downloaded getopts v0.2.21
  Downloaded hashbrown v0.9.0
  Downloaded addr2line v0.14.0
  Downloaded gimli v0.23.0
  Downloaded miniz_oxide v0.4.0
  Downloaded adler v0.2.3
  Downloaded object v0.22.0
  Downloaded unicode-width v0.1.8
  Downloaded rustc-demangle v0.1.18
  Downloaded libc v0.2.79
   Compiling compiler_builtins v0.1.39
   Compiling core v0.0.0 (/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
   Compiling proc-macro2 v1.0.24
   Compiling unicode-xid v0.2.1
   Compiling syn v1.0.58
   Compiling semver-parser v0.7.0
   Compiling proc-macro-hack v0.5.19
   Compiling paste v1.0.4
   Compiling semver v0.9.0
   Compiling rustc_version v0.2.3
   Compiling quote v1.0.8
   Compiling bare-metal v0.2.5
   Compiling rustc-std-workspace-core v1.99.0 (/usr/local/rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
LLVM ERROR: Not supported instr: <MCInst 258 <MCOperand Reg:1> <MCOperand Imm:15> <MCOperand Reg:40>>
error: could not compile `compiler_builtins`

To learn more, run the command again with --verbose.
warning: build failed, waiting for other jobs to finish...
error: build failed
The command '/bin/sh -c cargo +nightly build --example uno-blink' returned a non-zero code: 101

With --release and --verbose:

  Compiling embedded-hal v0.2.4
     Running `rustc --crate-name embedded_hal /Users/sobolev/.cargo/registry/src/github.com-1ecc6299db9ec823/embedded-hal-0.2.4/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=s -C panic=abort -C linker-plugin-lto -C codegen-units=1 -C debuginfo=2 --cfg 'feature="unproven"' -C metadata=503a59343e486e01 -C extra-filename=-503a59343e486e01 --out-dir /Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps --target /Users/sobolev/Desktop/avr-hal/avr-specs/avr-atmega328p.json -L dependency=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps -L dependency=/Users/sobolev/Desktop/avr-hal/target/release/deps --extern 'noprelude:compiler_builtins=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps/libcompiler_builtins-afa03ed941a63b4f.rmeta' --extern 'noprelude:core=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps/libcore-30308749cc1695e3.rmeta' --extern nb=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps/libnb-e03ff2173c96de6e.rmeta --extern void=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps/libvoid-4cf9d65885018701.rmeta -Z unstable-options --cap-lints allow`
LLVM ERROR: Not supported instr: <MCInst 258 <MCOperand Reg:1> <MCOperand Imm:15> <MCOperand Reg:40>>
error: could not compile `compiler_builtins`

Caused by:
  process didn't exit successfully: `rustc --crate-name compiler_builtins /Users/sobolev/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.39/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=s -C panic=abort -C linker-plugin-lto -C codegen-units=1 -C debuginfo=2 --cfg 'feature="compiler-builtins"' --cfg 'feature="core"' --cfg 'feature="default"' --cfg 'feature="rustc-dep-of-std"' -C metadata=afa03ed941a63b4f -C extra-filename=-afa03ed941a63b4f --out-dir /Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps --target /Users/sobolev/Desktop/avr-hal/avr-specs/avr-atmega328p.json -Z force-unstable-if-unmarked -L dependency=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps -L dependency=/Users/sobolev/Desktop/avr-hal/target/release/deps --extern core=/Users/sobolev/Desktop/avr-hal/target/avr-atmega328p/release/deps/librustc_std_workspace_core-2709fdbb3f7a5de3.rmeta --cap-lints allow --cfg 'feature="unstable"'` (exit code: 101)
warning: build failed, waiting for other jobs to finish...
error: build failed

Reproduction

  1. Clone https://github.com/Rahix/avr-hal
  2. Add this Dockefile:
FROM rustlang/rust:nightly-slim

RUN apt-get update \
  && apt-get install --no-install-recommends -y \
    avr-libc \
    binutils-avr \
    gcc-avr \
  && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
  && apt-get clean -y && rm -rf /var/lib/apt/lists/*

COPY . /code

WORKDIR /code/boards/arduino-uno

RUN rustup component add rust-src
RUN cargo +nightly build --example uno-blink
  1. Run docker build --tag=ard .

Versions

info: The currently active `rustc` version is `rustc 1.51.0-nightly (c8915eebe 2021-01-07)`
» avr-gcc --version
avr-gcc  9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
@MabezDev
Copy link
Contributor

Possibly related, but I have started seeing this behavior after updating my xtensa fork from 1.48 -> 1.49. I created an issue on the llvm fork's tracker but perhaps this is actually a Rust issue.

@ysblokje
Copy link

I ran into this or closely related issue while trying to get https://github.com/avr-rust/blink to compile. What I have noticed is that it compiles with the nightlies of 2021-01-07 but not after that date. The only thing I noticed between 2021-01-08 is that the version of compiler-builtins got updated from v0.1.36 to v0.1.39.

with nightly 2012-01-08

   Compiling bitflags v0.7.0
   Compiling lazy_static v0.2.11
   Compiling compiler_builtins v0.1.36
   Compiling core v0.0.0 (/home/ysblokje/.rustup/toolchains/nightly-2021-01-07-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
     Running `rustc --crate-name bitflags /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/bitflags-0.7.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debug-assertions=off -C metadata=819c50c018486aec -C extra-filename=-819c50c018486aec --out-dir /home/ysblokje/projecten/rust/blink/target/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --cap-lints allow`
     Running `rustc --crate-name lazy_static /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/lazy_static-0.2.11/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debug-assertions=off -C metadata=9e197ba8dea1699b -C extra-filename=-9e197ba8dea1699b --out-dir /home/ysblokje/projecten/rust/blink/target/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --cap-lints allow`
     Running `rustc --crate-name build_script_build /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.36/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off --cfg 'feature="compiler-builtins"' --cfg 'feature="core"' --cfg 'feature="default"' --cfg 'feature="rustc-dep-of-std"' -C metadata=844e68c26a6a57ce -C extra-filename=-844e68c26a6a57ce --out-dir /home/ysblokje/projecten/rust/blink/target/release/build/compiler_builtins-844e68c26a6a57ce -Z force-unstable-if-unmarked -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --cap-lints allow`
     Running `rustc --crate-name core --edition=2018 /home/ysblokje/.rustup/toolchains/nightly-2021-01-07-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=ffcc0c8e20d5d179 -C extra-filename=-ffcc0c8e20d5d179 --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -Z force-unstable-if-unmarked -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --cap-lints allow`
   Compiling xml-rs v0.4.1
     Running `rustc --crate-name xml /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/xml-rs-0.4.1/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debug-assertions=off -C metadata=b4c0432b26e20892 -C extra-filename=-b4c0432b26e20892 --out-dir /home/ysblokje/projecten/rust/blink/target/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern bitflags=/home/ysblokje/projecten/rust/blink/target/release/deps/libbitflags-819c50c018486aec.rmeta --cap-lints allow`
     Running `/home/ysblokje/projecten/rust/blink/target/release/build/compiler_builtins-844e68c26a6a57ce/build-script-build`
   Compiling xmltree v0.4.0
     Running `rustc --crate-name xmltree /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/xmltree-0.4.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debug-assertions=off -C metadata=8db81bdf93bace3e -C extra-filename=-8db81bdf93bace3e --out-dir /home/ysblokje/projecten/rust/blink/target/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern xml=/home/ysblokje/projecten/rust/blink/target/release/deps/libxml-b4c0432b26e20892.rmeta --cap-lints allow`
   Compiling avr-mcu v0.2.6
     Running `rustc --crate-name avr_mcu /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/avr-mcu-0.2.6/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debug-assertions=off -C metadata=b852a77c84ba31c9 -C extra-filename=-b852a77c84ba31c9 --out-dir /home/ysblokje/projecten/rust/blink/target/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern lazy_static=/home/ysblokje/projecten/rust/blink/target/release/deps/liblazy_static-9e197ba8dea1699b.rmeta --extern xmltree=/home/ysblokje/projecten/rust/blink/target/release/deps/libxmltree-8db81bdf93bace3e.rmeta --cap-lints allow`
   Compiling ruduino v0.2.0
     Running `rustc --crate-name build_script_build --edition=2018 /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/ruduino-0.2.0/core_generator/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debug-assertions=off -C metadata=4760d8c36b441576 -C extra-filename=-4760d8c36b441576 --out-dir /home/ysblokje/projecten/rust/blink/target/release/build/ruduino-4760d8c36b441576 -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern avr_mcu=/home/ysblokje/projecten/rust/blink/target/release/deps/libavr_mcu-b852a77c84ba31c9.rlib --cap-lints allow`
     Running `/home/ysblokje/projecten/rust/blink/target/release/build/ruduino-4760d8c36b441576/build-script-build`
   Compiling rustc-std-workspace-core v1.99.0 (/home/ysblokje/.rustup/toolchains/nightly-2021-01-07-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
     Running `rustc --crate-name rustc_std_workspace_core --edition=2018 /home/ysblokje/.rustup/toolchains/nightly-2021-01-07-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=720e5134eae211e9 -C extra-filename=-720e5134eae211e9 --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -Z force-unstable-if-unmarked -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern core=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcore-ffcc0c8e20d5d179.rmeta --cap-lints allow`
     Running `rustc --crate-name compiler_builtins /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.36/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no --cfg 'feature="compiler-builtins"' --cfg 'feature="core"' --cfg 'feature="default"' --cfg 'feature="rustc-dep-of-std"' -C metadata=75f365d68b064d0e -C extra-filename=-75f365d68b064d0e --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -Z force-unstable-if-unmarked -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern core=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/librustc_std_workspace_core-720e5134eae211e9.rmeta --cap-lints allow --cfg 'feature="unstable"'`
     Running `rustc --crate-name ruduino --edition=2018 /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/ruduino-0.2.0/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no -C metadata=333d03c3c0aad4c4 -C extra-filename=-333d03c3c0aad4c4 --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern 'noprelude:compiler_builtins=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcompiler_builtins-75f365d68b064d0e.rmeta' --extern 'noprelude:core=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcore-ffcc0c8e20d5d179.rmeta' -Z unstable-options --cap-lints allow`
   Compiling blink v0.1.0 (/home/ysblokje/projecten/rust/blink)
     Running `rustc --crate-name blink --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C opt-level=3 -C embed-bitcode=no -C metadata=1f47a526ca0061c6 -C extra-filename=-1f47a526ca0061c6 --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern 'noprelude:compiler_builtins=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcompiler_builtins-75f365d68b064d0e.rlib' --extern 'noprelude:core=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcore-ffcc0c8e20d5d179.rlib' --extern ruduino=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libruduino-333d03c3c0aad4c4.rlib -Z unstable-options`
    Finished release [optimized] target(s) in 8.88s

with nightly 2012-01-08

       Fresh bitflags v0.7.0
       Fresh lazy_static v0.2.11
       Fresh core v0.0.0 (/home/ysblokje/.rustup/toolchains/nightly-2021-01-07-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core)
       Fresh xml-rs v0.4.1
       Fresh xmltree v0.4.0
       Fresh rustc-std-workspace-core v1.99.0 (/home/ysblokje/.rustup/toolchains/nightly-2021-01-07-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/rustc-std-workspace-core)
       Fresh avr-mcu v0.2.6
       Fresh compiler_builtins v0.1.36
   Compiling compiler_builtins v0.1.39
       Fresh ruduino v0.2.0
     Running `rustc --crate-name compiler_builtins /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.39/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no --cfg 'feature="compiler-builtins"' --cfg 'feature="default"' -C metadata=58d3ac8f36249c98 -C extra-filename=-58d3ac8f36249c98 --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern 'noprelude:compiler_builtins=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcompiler_builtins-75f365d68b064d0e.rmeta' --extern 'noprelude:core=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcore-ffcc0c8e20d5d179.rmeta' -Z unstable-options --cap-lints allow --cfg 'feature="unstable"'`
LLVM ERROR: Not supported instr: <MCInst 258 <MCOperand Reg:1> <MCOperand Imm:15> <MCOperand Reg:45>>
error: could not compile `compiler_builtins`

Caused by:
  process didn't exit successfully: `rustc --crate-name compiler_builtins /home/ysblokje/.cargo/registry/src/github.com-1ecc6299db9ec823/compiler_builtins-0.1.39/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C opt-level=3 -C embed-bitcode=no --cfg 'feature="compiler-builtins"' --cfg 'feature="default"' -C metadata=58d3ac8f36249c98 -C extra-filename=-58d3ac8f36249c98 --out-dir /home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps --target /home/ysblokje/projecten/rust/blink/avr-atmega328p.json -L dependency=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps -L dependency=/home/ysblokje/projecten/rust/blink/target/release/deps --extern 'noprelude:compiler_builtins=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcompiler_builtins-75f365d68b064d0e.rmeta' --extern 'noprelude:core=/home/ysblokje/projecten/rust/blink/target/avr-atmega328p/release/deps/libcore-ffcc0c8e20d5d179.rmeta' -Z unstable-options --cap-lints allow --cfg 'feature="unstable"'` (exit code: 101)

I hope this helps.

@Rahix
Copy link

Rahix commented Jan 21, 2021

If someone has the patience, I think it would help to bisect the exact commit of compiler-builtins which leads to this regression.

I would assume this is a multi-layer problem: A change in compiler-builtins makes rustc emit instructions which the LLVM AVR backend cannot (yet?) lower correctly.

@stappersg
Copy link

I feel challenged to do git bisect.

However I have yet no clue how to tell cargo it should use the compiler-builtins that is being git bisected. So pointers to a rough direction are very welcome. (The direction I start going is tweaking cargo.toml.)

IJsblokje: Dank je wel voor de versie 0.1.36 werkt, v0.1.39 niet meer

@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2021

I think running cargo build --release --target <TARGET_TRIPLE> in a checkout of this repo will reproduce the bug. If this is the case you can simply use git bisect and run that build command at every step.

@stappersg
Copy link

What I found with tab completion:

$ cargo build --release --target 
thumbv6m-none-eabi        x86_64-unknown-linux-gnu

That lacks the target_triple avr-unknown-gnu-atmega328 this issue is about.

What should I do make cargo aware of an AVR microcontroller target?

@catcatmu
Copy link

catcatmu commented Jan 22, 2021

After running a git bisect, it looks like the commit which causes the issue is: 1d15e4e5043edcde12b8a2412f084c387f07ecc6 - Construct signed division functions differently

And the last good commit is:
e9688c68a5dd3ebe98f27386c6459afaf87c0468 - Remove unneeded code from asymmetric.rs

Bisect was run using:
cargo +nightly-2021-01-07 build -Z build-std=core --release --target boards/avr-atmega328p.json

The target config was from here: https://github.com/Rahix/avr-hal/blob/master/avr-specs/avr-atmega328p.json

@Rahix
Copy link

Rahix commented Jan 22, 2021

Nice work @catcatmu! I can't do it myself right now unfortunately, but maybe someone can generate the LLVM IR from before and after this commit? E.g. using

cargo +nightly-2021-01-07 rustc -Z build-std=core --target boards/avr-atmega328p.json -- --emit=llvm-ir -o /path/to/output-directory

Otherwise I will try taking a look later this weekend.

@catcatmu
Copy link

catcatmu commented Jan 22, 2021

Tar of different llvm-ir output (minus the build libs)
rust-avr-400.tar.gz

Folders:

  1. bad-debug: The broken commit in debug mode: Works
  2. bad-release: The broken commit in release mode: Fails LLVM ERROR: Not supported instr: <MCInst 25...
  3. good-debug: The good commit in debug mode: Works
  4. good-release: The good commit in release mode: Works

Notes:

  1. Works in debug mode, fails in release mode.

@stappersg
Copy link

Thanks

(I'm having a look at it

gs0604@trancilo:~/src/rust/RustAVR
$ tar tf rust-avr-400.tar.gz 
bad-debug/
bad-debug/ir-97326bb640629c90.d
bad-debug/ir-97326bb640629c90.ll
bad-release/
bad-release/ir-abab196ba785c327.d
bad-release/ir-abab196ba785c327.compiler_builtins.a06dgpxu-cgu.0.rcgu.ll
bad-release/error
good-debug/
good-debug/ir-97326bb640629c90.d
good-debug/ir-97326bb640629c90.ll
good-release/
good-release/ir-abab196ba785c327.d
good-release/ir-abab196ba785c327.ll
gs0604@trancilo:~/src/rust/RustAVR
$ mkdir issue400
gs0604@trancilo:~/src/rust/RustAVR
$ cd issue400/
gs0604@trancilo:~/src/rust/RustAVR/issue400
$ tar xf ../rust-avr-400.tar.gz 
gs0604@trancilo:~/src/rust/RustAVR/issue400
$ 

to express more apprication then a simple thanks 😄

@stappersg
Copy link

And I admit that I lack knowledge about what I'm looking at.

@Rahix
Copy link

Rahix commented Jan 23, 2021

Okay, I got closer ... A minimal reproducer (llc -filetype=obj broken.ll):

source_filename = "compiler_builtins.27zd1rau-cgu.0"
target datalayout = "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"
target triple = "avr-unknown-unknown"

declare dso_local fastcc { i128, i128 } @specialized_div_rem_u128(i128, i128) unnamed_addr addrspace(1) #0

define hidden i128 @__divmodti4(i128 %a, i128 %b, i128* %rem) unnamed_addr addrspace(1) #0 {
start:
  %b_neg = icmp slt i128 %b, 0
  %b2.0 = select i1 %b_neg, i128 0, i128 %b
  %0 = tail call fastcc addrspace(1) { i128, i128 } @specialized_div_rem_u128(i128 undef, i128 %b2.0) #4
  unreachable
}

attributes #0 = { "target-cpu"="atmega328" }
attributes #1 = { nounwind readnone speculatable willreturn }
attributes #2 = { cold noreturn nounwind }
attributes #3 = { nounwind willreturn }
attributes #4 = { nounwind }

!llvm.module.flags = !{!0}

!0 = !{i32 7, !"PIC Level", i32 2}

This produces invalid assembly containing the stdwstk/AVR::STDWSPQRr pseudo-instruction (that's the MCInst 258 from the error message) which for some reason is not lowered into real instructions.

This definitely needs to be fixed on LLVM side but I wonder if we could, in the meantime, disable [iu]128 intrinsics for AVR in compiler-builtins? I doubt anyone is using them right now and as it is broken either way, this will at least make other programs compile again...

@stappersg
Copy link

make other programs compile again...

Yes, that would be great

@bjorn3
Copy link
Member

bjorn3 commented Jan 23, 2021

The 128bit intrinsics need to be present to be able to link when not using LTO I think.

@stappersg
Copy link

needs to be fixed on LLVM side

My check on LLVM bugtracker ( https://bugs.llvm.org/buglist.cgi?quicksearch=AVR ) did return only 36 bugreports. None of them has Not supported instr in the title.

I think I found a way to help.

@stappersg
Copy link

I have requested an account at the LLVM bugtracker.

I have the minimal reproducer from Rahix

Do I have a good backtrace?
Actual: Should the hex offsets in #6 upto #9 be replaced with labels?
(If so, please provide hints and/or instructions on how to get such labels)

$ llc -filetype=obj broken.ll
LLVM ERROR: Not supported instr: <MCInst 258 <MCOperand Reg:1> <MCOperand Imm:15> <MCOperand Reg:55>>
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.	Program arguments: llc -filetype=obj broken.ll 
1.	Running pass 'Function Pass Manager' on module 'broken.ll'.
2.	Running pass 'AVR Assembly Printer' on function '@__divmodti4'
 #0 0x00007f869060ef8f llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xbd0f8f)
 #1 0x00007f869060d2f0 llvm::sys::RunSignalHandlers() (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xbcf2f0)
 #2 0x00007f869060f465 (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xbd1465)
 #3 0x00007f868fa30140 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14140)
 #4 0x00007f868f567ce1 raise ./signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007f868f551537 abort ./stdlib/abort.c:81:7
 #6 0x00007f869055ed68 (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xb20d68)
 #7 0x00007f869055ed87 (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xb20d87)
 #8 0x00007f869209b6f5 (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x265d6f5)
 #9 0x00007f869209abd2 (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x265cbd2)
#10 0x00007f86916d3030 llvm::MCELFStreamer::emitInstToData(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x1c95030)
#11 0x00007f86916e062e llvm::MCObjectStreamer::emitInstruction(llvm::MCInst const&, llvm::MCSubtargetInfo const&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x1ca262e)
#12 0x00007f869207b49f (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x263d49f)
#13 0x00007f8690cc8b91 llvm::AsmPrinter::emitFunctionBody() (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x128ab91)
#14 0x00007f8690cd23a5 (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0x12943a5)
#15 0x00007f86908e21ae llvm::MachineFunctionPass::runOnFunction(llvm::Function&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xea41ae)
#16 0x00007f869071e059 llvm::FPPassManager::runOnFunction(llvm::Function&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xce0059)
#17 0x00007f8690723603 llvm::FPPassManager::runOnModule(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xce5603)
#18 0x00007f869071e670 llvm::legacy::PassManagerImpl::run(llvm::Module&) (/lib/x86_64-linux-gnu/libLLVM-11.so.1+0xce0670)
#19 0x000000000040e0df main (/usr/lib/llvm-11/bin/llc+0x40e0df)
#20 0x00007f868f552d0a __libc_start_main ./csu/../csu/libc-start.c:308:16
#21 0x000000000040ba2a _start (/usr/lib/llvm-11/bin/llc+0x40ba2a)
Aborted

@Rahix
Copy link

Rahix commented Jan 24, 2021

@stappersg, I do not think the backtrace is relevant here. The issue is much earlier in the code-gen (where AVR::STDWSPQRr should be lowered into real instructions which an AVR MCU can understand) but we're only observing a crash very late, during translation into machine code (because it cannot translate this pseudo-instruction).

@Rahix
Copy link

Rahix commented Jan 24, 2021

The 128bit intrinsics need to be present to be able to link when not using LTO I think.

@bjorn3, can you elaborate on this? So far my experience with missing intrinsics was that they would lead to a linker error if referenced but everything works fine otherwise.

@bjorn3
Copy link
Member

bjorn3 commented Jan 24, 2021

I may be possible that without LTO it isn't possible to remove the references. It may also be possible that it can will be removed just fine.

@stappersg
Copy link

Acknowledge on "I do not think the backtrace is relevant here."

At https://acronyms.thefreedictionary.com/LTO I could not find a possible fit for LTO. Please elaborate the TLA. ( TLA?? Yes, Three Letter Acronym ;-)

@bjorn3
Copy link
Member

bjorn3 commented Jan 24, 2021

LTO means link time optimization. It allows optimizations to see all code in an executable, rather than only in a single object file.

@stappersg
Copy link

Reported upstream as https://bugs.llvm.org/show_bug.cgi?id=48863

Rahix added a commit to Rahix/avr-hal that referenced this issue Jan 27, 2021
Due to a compiler regression [1], latest nightly builds of the compiler are
currently broken.  Instead, suggest using the last working build which was
`nightly-2021-01-07`.  This commit can hopefully be reverted once the compiler
is fixed again.

[1]: rust-lang/compiler-builtins#400
Ref: #124
@dylanmckay
Copy link

Here's a bugfix, with a description of what was going on: https://reviews.llvm.org/D95664

Once it is reviewed and merged into LLVM, we can cherry-pick it into upstream Rust.

@stappersg
Copy link

Before I accepted responsibilities slash privileges on project AVR Rust I had "out-of-github-band" contact with Dylan. I'll contact him again.

It will be something like

Hello Dylan,

I do wish that what keeps you busy is self chosen ( caring for family, study, sports career, work ) and does bring what you hope for (and more).
You added me to AVR Rust people, hence this writing.
Please advice the AVR Rust universum which way to go.
Thing is mismatch between LLVM and the AVR backend.
You already wrote a patch for it, but the acceptance traject got stuck.

Regards /me

Adding another @dylanmckay
@dylanmckay

@gui2maraes
Copy link

Great, thank you, let's hope for the best.

@roqvist
Copy link

roqvist commented Nov 22, 2021

Does anyone have any news to share on this?

@stappersg
Copy link

https://reviews.llvm.org/D114611

@dylanmckay
Copy link

Hello, long time no see, heh. I haven't engaged that with much open source for the better part of the year as you notice, I didn't really plan it that way. Let's get this D114611 PR through eh? It looks really good, I will finish the review this week, I doubt I will ask for any extra changes than the ones I already have because I think it's probably functionally correct already

Take care everybody. More updates surely to follow soon.

@stappersg
Copy link

For what it is worth, there is a custom Rust Toolchain for AVR.

@Dionysusnu
Copy link

Dionysusnu commented Apr 29, 2022

I switched to the nightly-2021-01-07 toolchain to fix this issue, but my Visual Studio Code extension kept trying to install the rls component, which seems to to be unavailable for that nightly build.
After some digging, I found that nightly-2020-12-25 has the rls component available, and all the other stuff still worked.
Just posting in case anyone else runs into the VSC extension error.

@chinoto
Copy link

chinoto commented May 1, 2022

@Dionysusnu If you're using the Rust (rust-lang.rust) extension instead of matklad.rust-analyzer, go into your settings and tell it to use the rust-analyzer backend instead of rls.

bors added a commit to rust-lang-ci/rust that referenced this issue May 9, 2022
Upgrade llvm-project (rustc + avr = lovelove back again!)

See:
rust-lang/llvm-project#139

tl;dr:
- closes rust-lang#83633,
- closes rust-lang#82104,
- closes rust-lang#79889,
- closes rust-lang/compiler-builtins#400.

🙂
@Patryk27
Copy link
Contributor

This has been fixed in rust-lang/rust#96845 - @Amanieu, would you mind closing this bug? 🙂

@abusch
Copy link

abusch commented May 10, 2022

Awesome work @Patryk27 ! Looks like this took a lot of effort, so thanks for pushing through this. Looking forward to digging up my little Arduino and playing with it again :)

@timwalls
Copy link

Indeed, fantastic stuff, thank you @Patryk27 ! I just compiled my Rust AVR OS using nightly instead of my custom-patched toolchain and first impressions are good - this is massive news.

Really really appreciated, thanks again :).

@Amanieu
Copy link
Member

Amanieu commented May 10, 2022

Great work @Patryk27! Finally closing this issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.