rust-embedded / rust-embedded/svd2rust

no_mangle on DEVICE_PERIPHERALS prevents optimizing write away

Open
#833 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
857
Forks
164
PR merge metrics
No merged PRs in 30d

Description

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 https://github.com/rust-embedded/svd2rust/issues/151#issuecomment-334947973 thought.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated CorePeripherals and Peripherals singleton definitions, then compare the approach in cortex_m/peripheral/mod.rs referenced by the issue. Use the linked Compiler Explorer example to check whether the generated DEVICE_PERIPHERALS guard can be optimized away after the change, while preserving the steal behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
embedded-iot
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.