| Issue |
233842
|
| Summary |
[RISC-V] prefalign breaks explicit minimum function alignment with linker relaxation
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
kdupontbe
|
## Summary
On RISC-V, a function defined with `align 4 prefalign(64)` can end up at an address that is not 4-byte aligned after linking with LLD and relaxation enabled.
The issue is not merely that the preferred alignment is lost: the explicit **minimum alignment** is violated.
Removing `prefalign(64)` from the affected function, or disabling relaxation, restores the required alignment in this reproducer.
## Reproducer
[Compiler Explorer reproducer using LLVM IR](https://godbolt.org/z/nzE9WzrPf)
Compiler: **RISC-V rv32gc clang (trunk)**, as selected in Compiler Explorer.
Options for the linked output:
```text
-x ir -O2 -mrelax -fuse-ld=lld
```
The IR uses:
```llvm
target triple = "riscv32-unknown-unknown-elf"
```
The affected function is defined with:
```llvm
define i32 @big(ptr %p) noinline align 4 prefalign(64) {
; See the linked reproducer for the complete body.
}
```
A preceding function contains several calls that are shortened during linker relaxation.
## Actual behavior
With relaxation enabled, `big` starts at:
```text
0x112d6
```
This address is **2 modulo 4**, violating the explicit `align 4` requirement.
## Control experiments
The following results were observed in Compiler Explorer:
| Attributes on `big` | Compiler options | Linked address of `big` | Meets `align 4`? |
|---|---|---:|---|
| `align 4 prefalign(64)` | `-x ir -O2 -mrelax -fuse-ld=lld` | `0x112d6` | No |
| `align 4 prefalign(64)` | `-x ir -O2 -mno-relax -fuse-ld=lld` | `0x11300` | Yes |
| `align 4` | `-x ir -O2 -mrelax -fuse-ld=lld` | `0x11298` | Yes |
For the third case, only `prefalign(64)` on `big` was removed; the other functions retain their preferred-alignment attributes.
These controls isolate the interaction between preferred alignment on `big` and linker relaxation in this reproducer.
## Expected behavior
`big` must remain at least 4-byte aligned after linking, regardless of whether its preferred 64-byte alignment is achieved.
The preferred alignment is a hint, but it must not weaken the explicit minimum alignment.
See the [LLVM function alignment documentation](https://llvm.org/docs/LangRef.html#functions).
## Suspected mechanism
The object output appears to contain an `R_RISCV_ALIGN` relocation for the `.p2align 2` padding, followed by additional fixed NOP padding from `.prefalign`.
If that additional padding is not accounted for by the relaxation-aware alignment machinery, relaxation can preserve alignment at the end of the `.p2align` padding without preserving alignment at the actual function entry.
This is a suspected explanation, not yet a confirmed attribution to MC emission or LLD.
## Related C reproducer
[Compiler Explorer reproducer using C](https://godbolt.org/z/YMKExz3Pd)
With `-fpreferred-function-alignment=16`, the linked `big` address is `0x11246`. Using the reproducer’s `-DHARD` variant with `aligned(16)` instead gives `0x11240`.
The IR reproducer above demonstrates the stronger correctness issue: loss of an explicitly requested minimum alignment.
Assisted by AI when drafting the issue text
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs