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

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

Open
imihajlow opened this issue Dec 24, 2024 · 3 comments
Open

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

imihajlow opened this issue Dec 24, 2024 · 3 comments
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-AVR Target: AVR processors (ATtiny, ATmega, etc.)

Comments

@imihajlow
Copy link

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

@imihajlow imihajlow added the C-bug Category: This is a bug. label Dec 24, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 24, 2024
@imihajlow
Copy link
Author

Same happens if I use a huge match block instead of an array.

@jieyouxu jieyouxu added the O-AVR Target: AVR processors (ATtiny, ATmega, etc.) label Dec 24, 2024
@workingjubilee
Copy link
Member

@imihajlow does clang support compiling this table into ROM?

@imihajlow
Copy link
Author

@workingjubilee it looks like clang doesn't have __attribute__((__progmem__)) support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. O-AVR Target: AVR processors (ATtiny, ATmega, etc.)
Projects
None yet
Development

No branches or pull requests

4 participants