Skip to content

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

Open
@imihajlow

Description

@imihajlow

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-feature-requestCategory: A feature request, i.e: not implemented / a PR.O-AVRTarget: AVR processors (ATtiny, ATmega, etc.)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions