| Issue |
240184
|
| Summary |
[AVR] Incoming stack arguments are read one byte low on 3-byte-PC devices (avr6, e.g. atmega2560)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
kaidokert
|
On AVR devices with a 3-byte program counter (`avr6`, for example `atmega2560`), a function that sets up a frame reads its stack-passed arguments one byte below where the caller stored them. `call` pushes a 3-byte return address on these devices, but the offset of incoming arguments still assumes 2 bytes. That assumption seems to come from `AVRFrameLowering`'s `TargetFrameLowering(StackGrowsDown, Align(1), -2)`.
**Reproducer** (`stack_arg.ll`):
```llvm
target datalayout = "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8"
target triple = "avr-unknown-unknown"
declare void @use(ptr)
; %a and %b fill r10-r25, %c takes r8-r9, so %d is passed on the stack.
define i16 @stack_arg(i64 %a, i64 %b, i16 %c, i16 %d) {
%buf = alloca [4 x i8]
call void @use(ptr %buf)
ret i16 %d
}
```
`llc -O2 -mtriple=avr -mcpu=atmega328p` and `-mcpu=atmega2560` emit the same load:
```
ldd r24, Y+9
ldd r25, Y+10
```
That's correct for `atmega328p`: Y+1..4 is `buf`, Y+5..6 is the saved Y, Y+7..8 is the return address, so `%d` is at Y+9. On `atmega2560` the return address occupies Y+7..9, so `%d` is at Y+10. avr-gcc 5.4.0 agrees on the equivalent C (`short f(long long a, long long b, short c, short d)`): it emits `ldd r24,Y+9` for `-mmcu=atmega328p` and `ldd r24,Y+10` for `-mmcu=atmega2560`.
**Impact.** Any non-inlined callee with stack arguments gets them shifted by a byte. One example is any function with more than 18 bytes of arguments. The `i128` division helpers are another: `__udivti3`, `__divti3`, `__umodti3` and `__modti3` take their second operand on the stack. Under simavr on `atmega2560`, Rust's `u128 / u128` returned garbage that changed between builds, while the same program on `atmega1284p` (avr5, 2-byte PC) was correct.
**Versions.** Seen with LLVM 23.1.0 (rustc nightly-2026-08-14) and LLVM 23.1.5 (rustc 1.102.0-nightly). The `-2` local-area offset is still in `AVRFrameLowering.cpp` on main.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs