[llvm-branch-commits] [clang] CodeGen, Driver: Introduce -fpreferred-function-alignment option. (PR #155528)

Peter Collingbourne via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Jan 13 18:12:42 PST 2026


================
@@ -2855,13 +2855,19 @@ void CodeGenModule::SetLLVMFunctionAttributesForDefinition(const Decl *D,
 
   F->addFnAttrs(B);
 
-  unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
-  if (alignment)
-    F->setAlignment(llvm::Align(alignment));
-
-  if (!D->hasAttr<AlignedAttr>())
-    if (LangOpts.FunctionAlignment)
-      F->setAlignment(llvm::Align(1ull << LangOpts.FunctionAlignment));
+  llvm::MaybeAlign ExplicitAlignment;
+  if (unsigned alignment = D->getMaxAlignment() / Context.getCharWidth())
+    ExplicitAlignment = llvm::Align(alignment);
+  else if (LangOpts.FunctionAlignment)
+    ExplicitAlignment = llvm::Align(1ull << LangOpts.FunctionAlignment);
+
+  if (ExplicitAlignment) {
+    F->setAlignment(ExplicitAlignment);
+    F->setPreferredAlignment(ExplicitAlignment);
----------------
pcc wrote:

I don't have a strong opinion, but GCC's aligned attribute can override the default or command line alignment with a smaller one, so it seems reasonable for the same to apply to the preferred alignment.

https://github.com/llvm/llvm-project/pull/155528


More information about the llvm-branch-commits mailing list