[llvm] 943504e - IR: Add prefalign attribute for function definitions.
via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 20 10:54:05 PST 2026
Author: Peter Collingbourne
Date: 2026-02-20T10:54:01-08:00
New Revision: 943504eb08adca2106768715285626dd92e166a2
URL: https://github.com/llvm/llvm-project/commit/943504eb08adca2106768715285626dd92e166a2
DIFF: https://github.com/llvm/llvm-project/commit/943504eb08adca2106768715285626dd92e166a2.diff
LOG: IR: Add prefalign attribute for function definitions.
The prefalign attribute determines the function's preferred alignment.
By default, the function's preferred alignment is set in a target-specific
way, but it may be overridden with this attribute.
The backend logic will be added in followup patches.
Part of this RFC:
https://discourse.llvm.org/t/rfc-enhancing-function-alignment-attributes/88019
Reviewers: efriedma-quic, nikic, arsenm
Pull Request: https://github.com/llvm/llvm-project/pull/155527
Added:
llvm/test/Assembler/prefalign-error1.ll
llvm/test/Assembler/prefalign-error2.ll
llvm/test/Assembler/prefalign-error3.ll
llvm/test/Assembler/prefalign-error4.ll
Modified:
llvm/docs/LangRef.rst
llvm/include/llvm/AsmParser/LLParser.h
llvm/include/llvm/AsmParser/LLToken.h
llvm/include/llvm/IR/Function.h
llvm/lib/AsmParser/LLLexer.cpp
llvm/lib/AsmParser/LLParser.cpp
llvm/lib/Bitcode/Reader/BitcodeReader.cpp
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
llvm/lib/IR/AsmWriter.cpp
llvm/test/Bitcode/compatibility.ll
Removed:
################################################################################
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 50a2515f69189..cfae4393f2fb2 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -972,7 +972,9 @@ an optional ``unnamed_addr`` attribute, a return type, an optional
name, a (possibly empty) argument list (each with optional :ref:`parameter
attributes <paramattrs>`), optional :ref:`function attributes <fnattrs>`,
an optional address space, an optional section, an optional partition,
-an optional alignment, an optional :ref:`comdat <langref_comdats>`,
+an optional minimum alignment,
+an optional preferred alignment,
+an optional :ref:`comdat <langref_comdats>`,
an optional :ref:`garbage collector name <gc>`, an optional :ref:`prefix <prefixdata>`,
an optional :ref:`prologue <prologuedata>`,
an optional :ref:`personality <personalityfn>`,
@@ -986,8 +988,8 @@ Syntax::
<ResultType> @<FunctionName> ([argument list])
[(unnamed_addr|local_unnamed_addr)] [AddrSpace] [fn Attrs]
[section "name"] [partition "name"] [comdat [($name)]] [align N]
- [gc] [prefix Constant] [prologue Constant] [personality Constant]
- (!name !N)* { ... }
+ [prefalign(N)] [gc] [prefix Constant] [prologue Constant]
+ [personality Constant] (!name !N)* { ... }
The argument list is a comma-separated sequence of arguments where each
argument is of the following form:
@@ -1037,11 +1039,20 @@ LLVM allows an explicit section to be specified for functions. If the
target supports it, it will emit functions to the section specified.
Additionally, the function can be placed in a COMDAT.
-An explicit alignment may be specified for a function. If not present,
-or if the alignment is set to zero, the alignment of the function is set
-by the target to whatever it feels convenient. If an explicit alignment
-is specified, the function is forced to have at least that much
-alignment. All alignments must be a power of 2.
+An explicit minimum alignment (``align``) may be specified for a
+function. If not present, or if the alignment is set to zero, the
+alignment of the function is set according to the preferred alignment
+rules described below. If an explicit minimum alignment is specified, the
+function is forced to have at least that much alignment. All alignments
+must be a power of 2.
+
+An explicit preferred alignment (``prefalign``) may also be specified for
+a function (definitions only, and must be a power of 2). If a function
+does not have a preferred alignment attribute, the preferred alignment
+is determined in a target-specific way. The preferred alignment, if
+provided, is treated as a hint; the final alignment of the function will
+generally be set to a value somewhere between the minimum alignment and
+the preferred alignment.
If the ``unnamed_addr`` attribute is given, the address is known to not
be significant and two identical functions can be merged.
diff --git a/llvm/include/llvm/AsmParser/LLParser.h b/llvm/include/llvm/AsmParser/LLParser.h
index 2db7f3423fce2..938a45951e074 100644
--- a/llvm/include/llvm/AsmParser/LLParser.h
+++ b/llvm/include/llvm/AsmParser/LLParser.h
@@ -334,6 +334,7 @@ namespace llvm {
bool parseOptionalCallingConv(unsigned &CC);
bool parseOptionalAlignment(MaybeAlign &Alignment,
bool AllowParens = false);
+ bool parseOptionalPrefAlignment(MaybeAlign &Alignment);
bool parseOptionalCodeModel(CodeModel::Model &model);
bool parseOptionalAttrBytes(lltok::Kind AttrKind,
std::optional<uint64_t> &Bytes,
diff --git a/llvm/include/llvm/AsmParser/LLToken.h b/llvm/include/llvm/AsmParser/LLToken.h
index dfe51466c6152..5d5d70f89bde5 100644
--- a/llvm/include/llvm/AsmParser/LLToken.h
+++ b/llvm/include/llvm/AsmParser/LLToken.h
@@ -130,6 +130,7 @@ enum Kind {
kw_prefix,
kw_prologue,
kw_c,
+ kw_prefalign,
kw_cc,
kw_ccc,
diff --git a/llvm/include/llvm/IR/Function.h b/llvm/include/llvm/IR/Function.h
index 8760e99d38169..f39fe509a49a4 100644
--- a/llvm/include/llvm/IR/Function.h
+++ b/llvm/include/llvm/IR/Function.h
@@ -86,7 +86,8 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
unsigned BlockNumEpoch = 0;
mutable Argument *Arguments = nullptr; ///< The formal arguments
- size_t NumArgs;
+ uint32_t NumArgs;
+ MaybeAlign PreferredAlign;
std::unique_ptr<ValueSymbolTable>
SymTab; ///< Symbol table of args/instructions
AttributeList AttributeSets; ///< Parameter attributes
@@ -1044,6 +1045,12 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
/// defined.
void setAlignment(MaybeAlign Align) { GlobalObject::setAlignment(Align); }
+ /// Returns the prefalign of the given function.
+ MaybeAlign getPreferredAlignment() const { return PreferredAlign; }
+
+ /// Sets the prefalign attribute of the Function.
+ void setPreferredAlignment(MaybeAlign Align) { PreferredAlign = Align; }
+
/// Return the value for vscale based on the vscale_range attribute or 0 when
/// unknown.
unsigned getVScaleValue() const;
diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp
index f47579ca8fa9b..8d20ff72a8505 100644
--- a/llvm/lib/AsmParser/LLLexer.cpp
+++ b/llvm/lib/AsmParser/LLLexer.cpp
@@ -627,6 +627,7 @@ lltok::Kind LLLexer::LexIdentifier() {
KEYWORD(gc);
KEYWORD(prefix);
KEYWORD(prologue);
+ KEYWORD(prefalign);
KEYWORD(no_sanitize_address);
KEYWORD(no_sanitize_hwaddress);
diff --git a/llvm/lib/AsmParser/LLParser.cpp b/llvm/lib/AsmParser/LLParser.cpp
index 2dfd070e068d0..09b030b28dce8 100644
--- a/llvm/lib/AsmParser/LLParser.cpp
+++ b/llvm/lib/AsmParser/LLParser.cpp
@@ -2471,6 +2471,35 @@ bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
return false;
}
+/// parseOptionalPrefAlignment
+/// ::= /* empty */
+/// ::= 'prefalign' '(' 4 ')'
+bool LLParser::parseOptionalPrefAlignment(MaybeAlign &Alignment) {
+ Alignment = std::nullopt;
+ if (!EatIfPresent(lltok::kw_prefalign))
+ return false;
+ LocTy AlignLoc = Lex.getLoc();
+ uint64_t Value = 0;
+
+ LocTy ParenLoc = Lex.getLoc();
+ if (!EatIfPresent(lltok::lparen))
+ return error(ParenLoc, "expected '('");
+
+ if (parseUInt64(Value))
+ return true;
+
+ ParenLoc = Lex.getLoc();
+ if (!EatIfPresent(lltok::rparen))
+ return error(ParenLoc, "expected ')'");
+
+ if (!isPowerOf2_64(Value))
+ return error(AlignLoc, "alignment is not a power of two");
+ if (Value > Value::MaximumAlignment)
+ return error(AlignLoc, "huge alignments are not supported yet");
+ Alignment = Align(Value);
+ return false;
+}
+
/// parseOptionalCodeModel
/// ::= /* empty */
/// ::= 'code_model' "large"
@@ -6953,7 +6982,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
LocTy BuiltinLoc;
std::string Section;
std::string Partition;
- MaybeAlign Alignment;
+ MaybeAlign Alignment, PrefAlignment;
std::string GC;
GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
unsigned AddrSpace = 0;
@@ -6971,6 +7000,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
(EatIfPresent(lltok::kw_partition) && parseStringConstant(Partition)) ||
parseOptionalComdat(FunctionName, C) ||
parseOptionalAlignment(Alignment) ||
+ parseOptionalPrefAlignment(PrefAlignment) ||
(EatIfPresent(lltok::kw_gc) && parseStringConstant(GC)) ||
(EatIfPresent(lltok::kw_prefix) && parseGlobalTypeAndValue(Prefix)) ||
(EatIfPresent(lltok::kw_prologue) && parseGlobalTypeAndValue(Prologue)) ||
@@ -7072,6 +7102,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
Fn->setUnnamedAddr(UnnamedAddr);
if (Alignment)
Fn->setAlignment(*Alignment);
+ Fn->setPreferredAlignment(PrefAlignment);
Fn->setSection(Section);
Fn->setPartition(Partition);
Fn->setComdat(C);
diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 2c8612a9d7822..c0807c7be3e4f 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -4427,6 +4427,13 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
}
+ if (Record.size() > 19) {
+ MaybeAlign PrefAlignment;
+ if (Error Err = parseAlignmentValue(Record[19], PrefAlignment))
+ return Err;
+ Func->setPreferredAlignment(PrefAlignment);
+ }
+
ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID));
if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 6b332554bddd4..cc0491bd8577f 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -1646,7 +1646,8 @@ void ModuleBitcodeWriter::writeModuleInfo() {
// FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
// linkage, paramattrs, alignment, section, visibility, gc,
// unnamed_addr, prologuedata, dllstorageclass, comdat,
- // prefixdata, personalityfn, DSO_Local, addrspace]
+ // prefixdata, personalityfn, DSO_Local, addrspace,
+ // partition_strtab, partition_size, prefalign]
Vals.push_back(addToStrtab(F.getName()));
Vals.push_back(F.getName().size());
Vals.push_back(VE.getTypeID(F.getFunctionType()));
@@ -1673,6 +1674,7 @@ void ModuleBitcodeWriter::writeModuleInfo() {
Vals.push_back(F.getAddressSpace());
Vals.push_back(addToStrtab(F.getPartition()));
Vals.push_back(F.getPartition().size());
+ Vals.push_back(getEncodedAlign(F.getPreferredAlignment()));
unsigned AbbrevToUse = 0;
Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index b3e0323fd8633..35a19091aa097 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -4211,6 +4211,8 @@ void AssemblyWriter::printFunction(const Function *F) {
maybePrintComdat(Out, *F);
if (MaybeAlign A = F->getAlign())
Out << " align " << A->value();
+ if (MaybeAlign A = F->getPreferredAlignment())
+ Out << " prefalign(" << A->value() << ')';
if (F->hasGC())
Out << " gc \"" << F->getGC() << '"';
if (F->hasPrefixData()) {
diff --git a/llvm/test/Assembler/prefalign-error1.ll b/llvm/test/Assembler/prefalign-error1.ll
new file mode 100644
index 0000000000000..1e9d142f18541
--- /dev/null
+++ b/llvm/test/Assembler/prefalign-error1.ll
@@ -0,0 +1,6 @@
+; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
+
+; CHECK: expected '('
+define void @error() prefalign {
+ ret void
+}
diff --git a/llvm/test/Assembler/prefalign-error2.ll b/llvm/test/Assembler/prefalign-error2.ll
new file mode 100644
index 0000000000000..782b730a28d95
--- /dev/null
+++ b/llvm/test/Assembler/prefalign-error2.ll
@@ -0,0 +1,6 @@
+; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
+
+; CHECK: expected integer
+define void @error() prefalign() {
+ ret void
+}
diff --git a/llvm/test/Assembler/prefalign-error3.ll b/llvm/test/Assembler/prefalign-error3.ll
new file mode 100644
index 0000000000000..5af88d9bd13a5
--- /dev/null
+++ b/llvm/test/Assembler/prefalign-error3.ll
@@ -0,0 +1,6 @@
+; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
+
+; CHECK: alignment is not a power of two
+define void @error() prefalign(0) {
+ ret void
+}
diff --git a/llvm/test/Assembler/prefalign-error4.ll b/llvm/test/Assembler/prefalign-error4.ll
new file mode 100644
index 0000000000000..3cf8efeb10e35
--- /dev/null
+++ b/llvm/test/Assembler/prefalign-error4.ll
@@ -0,0 +1,6 @@
+; RUN: not llvm-as -disable-output %s 2>&1 | FileCheck %s
+
+; CHECK: huge alignments are not supported yet
+define void @error() prefalign(8589934592) {
+ ret void
+}
diff --git a/llvm/test/Bitcode/compatibility.ll b/llvm/test/Bitcode/compatibility.ll
index 7ecc92ae16e97..6cce477e2af04 100644
--- a/llvm/test/Bitcode/compatibility.ll
+++ b/llvm/test/Bitcode/compatibility.ll
@@ -763,6 +763,24 @@ declare void @f.align4() align 4
declare void @f.align8() align 8
; CHECK: declare void @f.align8() align 8
+; Functions -- prefalign
+define void @f.prefalign2() prefalign(2) {
+ ret void
+}
+; CHECK: define void @f.prefalign2() prefalign(2)
+define void @f.prefalign4() prefalign(4) {
+ ret void
+}
+; CHECK: define void @f.prefalign4() prefalign(4)
+define void @f.prefalign8() prefalign(8) {
+ ret void
+}
+; CHECK: define void @f.prefalign8() prefalign(8)
+define void @f.prefalign4294967296() prefalign(4294967296) {
+ ret void
+}
+; CHECK: define void @f.prefalign4294967296() prefalign(4294967296)
+
; Functions -- GC
declare void @f.gcshadow() gc "shadow-stack"
; CHECK: declare void @f.gcshadow() gc "shadow-stack"
More information about the llvm-commits
mailing list