rust-embedded / heapless

Heapless, `static` friendly data structures
Apache License 2.0
1.56k stars 185 forks source link

Misdetects arm_llsc and fails to build on thumbv6m-none-eabi with `-C linker-plugin-lto=y` #463

Open kevinmehall opened 8 months ago

kevinmehall commented 8 months ago

After updating heapless to 0.8 in my project (as a transitive dependency of usb-device 0.3.2), it fails to build with:

error[E0599]: no method named `fetch_add` found for struct `AtomicUsize` in the current scope
   --> src/pool/arc.rs:235:44
    |
235 |         let old_size = self.inner().strong.fetch_add(1, Ordering::Relaxed);
    |                                            ^^^^^^^^^ method not found in `AtomicUsize`

error[E0599]: no method named `fetch_sub` found for struct `AtomicUsize` in the current scope
   --> src/pool/arc.rs:282:32
    |
282 |         if self.inner().strong.fetch_sub(1, Ordering::Release) != 1 {
    |                                ^^^^^^^^^ method not found in `AtomicUsize`

For more information about this error, try `rustc --explain E0599`.
error: could not compile `heapless` (lib) due to 2 previous errors

I noticed in cargo build -v that it was building with --cfg arm_llsc, and tracked this down to my -C linker-plugin-lto=y in RUSTFLAGS. It seems that the asm in ARM_LLSC_PROBE in build.rs gets optimized out and successfully builds in this configuration, despite thumbv6m not supporting atomics.

This is reproducible in this repository with

cargo clean
RUSTFLAGS='-Clinker-plugin-lto=y' cargo build --target thumbv6m-none-eabi -v
$ cat target/thumbv6m-none-eabi/debug/build/heapless-*/output         
cargo:rustc-cfg=has_atomic_load_store
cargo:rustc-cfg=arm_llsc