Issue 233837
Summary [clang] -fpreferred-function-alignment=N silently ignored for N >= 32
Labels clang
Assignees
Reporter kdupontbe
    ## Summary

`-fpreferred-function-alignment` drops every value of 32 or more without a
diagnostic. No `prefalign` attribute is emitted, so the option has no effect.
Values up to 16 work.

## Reproducer

Compiler Explorer (clang trunk): https://godbolt.org/z/rfK9v9q7z

```c
int f(int x) { return x + 1; }
```

```
$ clang -O2 -S -emit-llvm -fpreferred-function-alignment=16 t.c -o - | grep define
define dso_local ... i32 @f(i32 noundef %0) ... prefalign(16) {

$ clang -O2 -S -emit-llvm -fpreferred-function-alignment=64 t.c -o - | grep define
define dso_local ... i32 @f(i32 noundef %0) ... {
```

The same happens with `-cc1` directly and on every target I tried (x86-64, RISC-V).

**Expected:** `prefalign(64)`.
**Actual:** no attribute and no warning.

## Cause

`clang/include/clang/Basic/LangOptions.def`:

```
VALUE_LANGOPT(FunctionAlignment, 5, 0, Compatible, "Default alignment for functions")
VALUE_LANGOPT(PreferredFunctionAlignment, 5, 0, Compatible, "Preferred alignment for functions")
```

`FunctionAlignment` holds a log2 value, so 5 bits are enough.
`PreferredFunctionAlignment` holds the value in bytes, but the driver accepts
anything up to 65536 (`Clang.cpp`). Every power of two of 32 or more truncates to 0, so
`CodeGenModule::SetLLVMFunctionAttributesForDefinition` skips it:

```cpp
} else if (LangOpts.PreferredFunctionAlignment) {   // 64 -> 0
 F->setPreferredAlignment(llvm::Align(LangOpts.PreferredFunctionAlignment));
}
```

Introduced in 4a7bffc2c41a ("CodeGen, Driver: Introduce
-fpreferred-function-alignment option").

## Possible fix

Either:

- store log2 in the LangOpt, the same way as `FunctionAlignment` (the driver
  already validates the value as a power of two), and convert to bytes in
  CodeGen; or
- widen the field to hold larger values.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to