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

no_mangle on DEVICE_PERIPHERALS prevents optimizing write away #833

Open
bauen1 opened this issue Apr 1, 2024 · 0 comments
Open

no_mangle on DEVICE_PERIPHERALS prevents optimizing write away #833

bauen1 opened this issue Apr 1, 2024 · 0 comments

Comments

@bauen1
Copy link

bauen1 commented Apr 1, 2024

For fun (and profit) I'm trying to reduce the .bss usage of a rust embedded program down to zero, relying only on the stack for memory.

Actually the only users of .bss apart from the stack are the singleton guards of the stm32l4r5::CorePeripherals and stm32l4r5::Peripherals structs.

And by replacing take with steal, since I'm sure that main will only be called "once", these should have been eliminated, however for some weird reason DEVICE_PERIPHERALS persisted.

The reason for that is outlined in the short snippet below:

// Type your code here, or load an example.
#![no_std]
#![no_main]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_panic: &PanicInfo<'_>) -> ! {
    loop {}
}

// As of Rust 1.75, small functions are automatically
// marked as `#[inline]` so they will not show up in
// the output when compiling with optimisations. Use
// `#[no_mangle]` or `#[inline(never)]` to work around
// this issue.
// See https://github.com/compiler-explorer/compiler-explorer/issues/5939

// If you use `main()`, declare it as `pub` to see it in the output:
// pub fn main() { ... }

#[no_mangle]
static mut USED1: bool = false;

static mut USED2: bool = false;

#[inline(never)]
pub fn main() -> () {

  unsafe { USED1 = true };

  unsafe { USED2 = true };

  loop {

  }
}

godbolt

Only USED1 appears in the output, USED2 is optimized away.
Maybe you could also use no_mangle in the same way cortex-m does ?
https://docs.rs/cortex-m/latest/src/cortex_m/peripheral/mod.rs.html#156-162

When using the steal method, LLVM is then actually smart enough to completely eliminate the variable that's written only once, like #151 (comment) thought.

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

No branches or pull requests

1 participant