Issue 169190
Summary [AVR] alignment of stack allocations gets silently ignored
Labels new issue
Assignees
Reporter meithecatte
    Consider the following translation unit:

```c
struct nya {
        int a;
};

void g(void *);

void f() {
        _Alignas(64)
        struct nya x = {3};
 g(&x);
}
```

When compiled with clang as follows, the generated code doesn't make any effort to actually satisfy the alignment constaint:

```
clang --target=avr-none -mmcu=atmega2560 -c test.c -o test-clang.o
```

<details>
<summary>Assembly generated by clang/LLVM</summary>

```
f:                                      ; @f
; %bb.0:
	push	r28
	push	r29
	in	r28, 61
	in	r29, 62
	sbiw	r28, 60
	in	r0, 63
	cli
	out	62, r29
	out	63, r0
	out	61, r28
	lds	r24, __const.f.x
	lds	r25, __const.f.x+1
	std	Y+2, r25
	std	Y+1, r24
	movw	r24, r28
	adiw	r24, 1
	call	g
	adiw	r28, 60
	in	r0, 63
	cli
	out	62, r29
	out	63, r0
	out	61, r28
	pop	r29
	pop	r28
	ret
```

</details>

By comparison, GCC properly aligns the stack allocation (by allocating `alignment - 1 + size` bytes and positioning the actual object at a variable stack frame offset):

```
avr-gcc -mmcu=atmega2560 -c test.c -o test-gcc.o
```

<details>
<summary>Assembly generated by GCC</summary>

```
f:
	push r28
	push r29
	in r28,__SP_L__
	in r29,__SP_H__
	subi r28,65
	sbc r29,__zero_reg__
	in __tmp_reg__,__SREG__
	cli
	out __SP_H__,r29
	out __SREG__,__tmp_reg__
	out __SP_L__,r28
/* prologue: function */
/* frame size = 65 */
/* stack size = 67 */
.L__stack_usage = 67
	movw r24,r28
	adiw r24,1
	adiw r24,63
	clr __tmp_reg__
	lsl r24
	rol r25
	rol __tmp_reg__
	lsl r24
	rol r25
	rol __tmp_reg__
	mov r24,r25
	mov r25,__tmp_reg__
	clr __tmp_reg__
	lsr r25
	ror r24
	ror __tmp_reg__
	lsr r25
	ror r24
	ror __tmp_reg__
	mov r25,r24
	mov r24,__tmp_reg__
	ldi r18,lo8(3)
	ldi r19,0
	movw r30,r24
	std Z+1,r19
	st Z,r18
	call g
	nop
/* epilogue start */
	subi r28,-65
	sbci r29,-1
	in __tmp_reg__,__SREG__
	cli
	out __SP_H__,r29
	out __SREG__,__tmp_reg__
	out __SP_L__,r28
	pop r29
	pop r28
	ret
```

</details>

---

Now, you might ask: "hold on, there's nothing on the AVR that'd be affected by memory alignment, why do you care about this". Well, recently the Rust formatting machinery has started making use of pointer tagging, which breaks spectacularly when LLVM ignores the alignment that rustc requests for some stack-allocated structs: https://github.com/rust-lang/rust/issues/149223
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to