rust-lang / rust-lang/rust

[AVR] Large const lookup table is placed in data section

Open
#134,731 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request O-AVR T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

When compiling for AVR, large const lookup table is placed in data section, wasting precious RAM.

Code in question:

const CURRENT_CALIBRATION_TABLE: [u16; 800] = [ /* ... */ ];

pub const fn get_current_by_adc(adc: u16) -> Fixed {
    let i = if (adc as usize) < CURRENT_CALIBRATION_TABLE.len() {
        adc as usize
    } else {
        CURRENT_CALIBRATION_TABLE.len() - 1
    };
    Fixed::from_bits(CURRENT_CALIBRATION_TABLE[i] as u32)
}

Target spec:

{
  "arch": "avr",
  "atomic-cas": false,
  "cpu": "atmega328p",
  "crt-objects-fallback": "false",
  "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",
  "late-link-args": {
    "gnu-cc": [
      "-lgcc"
    ],
    "gnu-lld-cc": [
      "-lgcc"
    ]
  },
  "linker": "avr-gcc",
  "linker-flavor": "gnu-cc",
  "llvm-target": "avr-unknown-unknown",
  "max-atomic-width": 8,
  "metadata": {
    "description": null,
    "host_tools": null,
    "std": null,
    "tier": null
  },
  "no-default-libraries": false,
  "pre-link-args": {
    "gnu-cc": [
      "-mmcu=atmega328p",
      "-Wl,--as-needed,--print-memory-usage"
    ],
    "gnu-lld-cc": [
      "-mmcu=atmega328p",
      "-Wl,--as-needed,--print-memory-usage"
    ]
  },
  "relocation-model": "static",
  "target-c-int-width": "16",
  "target-pointer-width": "16"
}

I expected to see this happen: Table is placed into Flash ROM, flash read instructions are used to access data.

Instead, this happened: Table is placed into RAM.

Meta

rustc --version --verbose:

rustc 1.85.0-nightly (426d17342 2024-12-21)
binary: rustc
commit-hash: 426d1734238e3c5f52e935ba4f617f3a9f43b59d
commit-date: 2024-12-21
host: aarch64-apple-darwin
release: 1.85.0-nightly
LLVM version: 19.1.6

Contributor guide

Open the contributing guide

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 by reproducing the const-table example for the provided AVR target and inspect the generated placement and access instructions. The issue names no repository files or tests; done means the table resides in Flash ROM and accesses use AVR flash-read instructions without placing the table in RAM.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers, embedded-iot
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.