[clang] [Clang] Fix -fpreferred-function-alignment for values of 32 or more (PR #246240)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 25 21:45:41 PST 2026
https://github.com/Glitch-On created https://github.com/llvm/llvm-project/pull/246240
PreferredFunctionAlignment is a 5-bit LangOpt but held the alignment in bytes, so every power of two of 32 or more truncated to 0 and no prefalign attribute was emitted.
Store the alignment with llvm::encode instead (log2 + 1, with 0 meaning not set), which covers the full range the driver accepts. Plain log2 would make an alignment of 1 look like "not set", and -fpreferred-function-alignment=1 matters because it overrides the target's default preferred alignment.
The cc1 option still takes a byte value. It now diagnoses values that are not powers of two or are above 65536. Before, a non-power-of-two value reached llvm::Align, which asserts on it.
Fixes #233837
>From 9571d4f9269b7c6d0597db1cc05aaf9e8465db7d Mon Sep 17 00:00:00 2001
From: Vansh <gaurvansh133 at gmail.com>
Date: Thu, 26 Nov 2026 11:10:14 +0530
Subject: [PATCH] [Clang] Fix -fpreferred-function-alignment for values of 32
or more
PreferredFunctionAlignment is a 5-bit LangOpt but held the alignment in
bytes, so every power of two of 32 or more truncated to 0 and no
prefalign attribute was emitted.
Store the alignment with llvm::encode instead (log2 + 1, with 0 meaning
not set), which covers the full range the driver accepts. Plain log2
would make an alignment of 1 look like "not set", and
-fpreferred-function-alignment=1 matters because it overrides the
target's default preferred alignment.
The cc1 option still takes a byte value. It now diagnoses values that
are not powers of two or are above 65536. Before, a non-power-of-two
value reached llvm::Align, which asserts on it.
Fixes #233837
---
clang/include/clang/Basic/LangOptions.def | 3 ++-
clang/include/clang/Options/Options.td | 4 ++-
clang/lib/CodeGen/CodeGenModule.cpp | 5 ++--
clang/lib/Frontend/CompilerInvocation.cpp | 31 +++++++++++++++++++++++
clang/test/CodeGen/prefalign.c | 18 +++++++++++--
5 files changed, 55 insertions(+), 6 deletions(-)
diff --git a/clang/include/clang/Basic/LangOptions.def b/clang/include/clang/Basic/LangOptions.def
index 1fef2c5d881c77..fccd02cff476ec 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -434,7 +434,8 @@ ENUM_LANGOPT(ClangABICompat, ClangABI, 5, ClangABI::Latest, NotCompatible,
"with")
VALUE_LANGOPT(FunctionAlignment, 5, 0, Compatible, "Default alignment for functions")
-VALUE_LANGOPT(PreferredFunctionAlignment, 5, 0, Compatible, "Preferred alignment for functions")
+VALUE_LANGOPT(PreferredFunctionAlignment, 5, 0, Compatible,
+ "Preferred alignment for functions, encoded with llvm::encode")
VALUE_LANGOPT(LoopAlignment, 32, 0, Compatible, "Default alignment for loops")
LANGOPT(FixedPoint, 1, 0, NotCompatible, "fixed point types")
diff --git a/clang/include/clang/Options/Options.td b/clang/include/clang/Options/Options.td
index db5ec793b4c4c9..ad7a0486ad382f 100644
--- a/clang/include/clang/Options/Options.td
+++ b/clang/include/clang/Options/Options.td
@@ -1719,7 +1719,9 @@ def fpreferred_function_alignment_EQ :
Joined<["-"], "fpreferred-function-alignment=">, Group<f_Group>,
HelpText<"preferred alignment for functions">,
Visibility<[ClangOption, CC1Option]>,
- MarshallingInfoInt<LangOpts<"PreferredFunctionAlignment">>;
+ MarshallingInfoInt<LangOpts<"PreferredFunctionAlignment">>,
+ Normalizer<"normalizeEncodedAlignment">,
+ Denormalizer<"denormalizeEncodedAlignment">;
def falign_loops_EQ : Joined<["-"], "falign-loops=">, Group<f_Group>,
Visibility<[ClangOption, CC1Option]>, MetaVarName<"<N>">,
HelpText<"N must be a power of two. Align loops to the boundary">,
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 390ca6dbba4616..1a54d70e32a2a1 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3412,8 +3412,9 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
if (ExplicitAlignment) {
F->setAlignment(ExplicitAlignment);
F->setPreferredAlignment(ExplicitAlignment);
- } else if (LangOpts.PreferredFunctionAlignment) {
- F->setPreferredAlignment(llvm::Align(LangOpts.PreferredFunctionAlignment));
+ } else if (llvm::MaybeAlign PrefAlign =
+ llvm::decodeMaybeAlign(LangOpts.PreferredFunctionAlignment)) {
+ F->setPreferredAlignment(PrefAlign);
}
// Some C++ ABIs require 2-byte alignment for member functions, in order to
diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp
index 9af6f6c41eb756..a78708d836370a 100644
--- a/clang/lib/Frontend/CompilerInvocation.cpp
+++ b/clang/lib/Frontend/CompilerInvocation.cpp
@@ -62,6 +62,7 @@
#include "llvm/Option/Option.h"
#include "llvm/ProfileData/InstrProfReader.h"
#include "llvm/Remarks/HotnessThresholdParser.h"
+#include "llvm/Support/Alignment.h"
#include "llvm/Support/CodeGen.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
@@ -506,6 +507,36 @@ static void denormalizeStringVector(ArgumentConsumer Consumer,
}
}
+// Parse an alignment in bytes and store it with llvm::encode, which keeps 0
+// free to mean "not specified" while still allowing an alignment of 1.
+static std::optional<unsigned>
+normalizeEncodedAlignment(OptSpecifier Opt, int, const ArgList &Args,
+ DiagnosticsEngine &Diags) {
+ auto *Arg = Args.getLastArg(Opt);
+ if (!Arg)
+ return std::nullopt;
+ unsigned Value;
+ if (StringRef(Arg->getValue()).getAsInteger(10, Value) || Value > 65536) {
+ Diags.Report(diag::err_drv_invalid_int_value)
+ << Arg->getAsString(Args) << Arg->getValue();
+ return std::nullopt;
+ }
+ if (!llvm::isPowerOf2_32(Value)) {
+ Diags.Report(diag::err_drv_alignment_not_power_of_two)
+ << Arg->getAsString(Args);
+ return std::nullopt;
+ }
+ return llvm::encode(llvm::Align(Value));
+}
+
+static void denormalizeEncodedAlignment(ArgumentConsumer Consumer,
+ unsigned SpellingOffset,
+ Option::OptionClass OptClass,
+ unsigned TableIndex, unsigned Value) {
+ denormalizeString(Consumer, SpellingOffset, OptClass, TableIndex,
+ llvm::decodeMaybeAlign(Value)->value());
+}
+
static std::optional<std::string> normalizeTriple(OptSpecifier Opt,
int TableIndex,
const ArgList &Args,
diff --git a/clang/test/CodeGen/prefalign.c b/clang/test/CodeGen/prefalign.c
index 4e3f2cbda970de..3b2ea64bb406c1 100644
--- a/clang/test/CodeGen/prefalign.c
+++ b/clang/test/CodeGen/prefalign.c
@@ -1,4 +1,18 @@
-// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=16 %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux %s -o - | FileCheck %s -check-prefix CHECK-NONE
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=1 %s -o - | FileCheck %s -check-prefix CHECK-1
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=16 %s -o - | FileCheck %s -check-prefix CHECK-16
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=32 %s -o - | FileCheck %s -check-prefix CHECK-32
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=64 %s -o - | FileCheck %s -check-prefix CHECK-64
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=65536 %s -o - | FileCheck %s -check-prefix CHECK-65536
+// RUN: not %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=3 %s -o - 2>&1 | FileCheck %s -check-prefix CHECK-ERR-3
+// RUN: not %clang_cc1 -emit-llvm -triple x86_64-unknown-linux -fpreferred-function-alignment=131072 %s -o - 2>&1 | FileCheck %s -check-prefix CHECK-ERR-131072
-// CHECK: define {{.*}} void @f() {{.*}} prefalign(16)
+// CHECK-NONE-NOT: prefalign(
+// CHECK-1: define {{.*}} void @f() {{.*}} prefalign(1)
+// CHECK-16: define {{.*}} void @f() {{.*}} prefalign(16)
+// CHECK-32: define {{.*}} void @f() {{.*}} prefalign(32)
+// CHECK-64: define {{.*}} void @f() {{.*}} prefalign(64)
+// CHECK-65536: define {{.*}} void @f() {{.*}} prefalign(65536)
+// CHECK-ERR-3: alignment is not a power of 2 in '-fpreferred-function-alignment=3'
+// CHECK-ERR-131072: invalid integral value '131072' in '-fpreferred-function-alignment=131072'
void f() {}
More information about the cfe-commits
mailing list