Skip to content

Commit f233275

Browse files
committed
[𝘀𝗽𝗿] changes to main this commit is based on
Created using spr 1.3.6-beta.1 [skip ci]
1 parent a1eeb59 commit f233275

File tree

10 files changed

+73
-21
lines changed

10 files changed

+73
-21
lines changed

llvm/docs/LangRef.rst

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,9 @@ an optional ``unnamed_addr`` attribute, a return type, an optional
877877
name, a (possibly empty) argument list (each with optional :ref:`parameter
878878
attributes <paramattrs>`), optional :ref:`function attributes <fnattrs>`,
879879
an optional address space, an optional section, an optional partition,
880-
an optional alignment, an optional :ref:`comdat <langref_comdats>`,
880+
an optional minimum alignment,
881+
an optional preferred alignment,
882+
an optional :ref:`comdat <langref_comdats>`,
881883
an optional :ref:`garbage collector name <gc>`, an optional :ref:`prefix <prefixdata>`,
882884
an optional :ref:`prologue <prologuedata>`,
883885
an optional :ref:`personality <personalityfn>`,
@@ -891,8 +893,8 @@ Syntax::
891893
<ResultType> @<FunctionName> ([argument list])
892894
[(unnamed_addr|local_unnamed_addr)] [AddrSpace] [fn Attrs]
893895
[section "name"] [partition "name"] [comdat [($name)]] [align N]
894-
[gc] [prefix Constant] [prologue Constant] [personality Constant]
895-
(!name !N)* { ... }
896+
[prefalign N] [gc] [prefix Constant] [prologue Constant]
897+
[personality Constant] (!name !N)* { ... }
896898

897899
The argument list is a comma-separated sequence of arguments where each
898900
argument is of the following form:
@@ -942,11 +944,24 @@ LLVM allows an explicit section to be specified for functions. If the
942944
target supports it, it will emit functions to the section specified.
943945
Additionally, the function can be placed in a COMDAT.
944946

945-
An explicit alignment may be specified for a function. If not present,
946-
or if the alignment is set to zero, the alignment of the function is set
947-
by the target to whatever it feels convenient. If an explicit alignment
948-
is specified, the function is forced to have at least that much
949-
alignment. All alignments must be a power of 2.
947+
An explicit minimum alignment (``align``) may be specified for a
948+
function. If not present, or if the alignment is set to zero, the
949+
alignment of the function is set according to the preferred alignment
950+
rules described below. If an explicit minimum alignment is specified, the
951+
function is forced to have at least that much alignment. All alignments
952+
must be a power of 2.
953+
954+
An explicit preferred alignment (``prefalign``) may also be specified
955+
for a function (definitions only, and must be a power of 2). If a
956+
function does not have a preferred alignment attribute, the preferred
957+
alignment is determined in a target-specific way. The final alignment
958+
of the function is determined in the following way: if the function
959+
size is less than the minimum alignment, the function's alignment will
960+
be at least the minimum alignment. Otherwise, if the function size is
961+
between the minimum alignment and the preferred alignment, the function's
962+
alignment will be at least the power of 2 greater than or equal to the
963+
function size. Otherwise, the function's alignment will be at least the
964+
preferred alignment.
950965

951966
If the ``unnamed_addr`` attribute is given, the address is known to not
952967
be significant and two identical functions can be merged.

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ namespace llvm {
311311
GlobalValueSummary::ImportKind &Res);
312312
void parseOptionalDLLStorageClass(unsigned &Res);
313313
bool parseOptionalCallingConv(unsigned &CC);
314-
bool parseOptionalAlignment(MaybeAlign &Alignment,
314+
bool parseOptionalAlignment(lltok::Kind KW, MaybeAlign &Alignment,
315315
bool AllowParens = false);
316316
bool parseOptionalCodeModel(CodeModel::Model &model);
317317
bool parseOptionalDerefAttrBytes(lltok::Kind AttrKind, uint64_t &Bytes);

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ enum Kind {
130130
kw_prefix,
131131
kw_prologue,
132132
kw_c,
133+
kw_prefalign,
133134

134135
kw_cc,
135136
kw_ccc,

llvm/include/llvm/IR/Function.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
8585
unsigned BlockNumEpoch = 0;
8686

8787
mutable Argument *Arguments = nullptr; ///< The formal arguments
88-
size_t NumArgs;
88+
uint32_t NumArgs;
89+
MaybeAlign PreferredAlign;
8990
std::unique_ptr<ValueSymbolTable>
9091
SymTab; ///< Symbol table of args/instructions
9192
AttributeList AttributeSets; ///< Parameter attributes
@@ -1043,6 +1044,12 @@ class LLVM_ABI Function : public GlobalObject, public ilist_node<Function> {
10431044
/// defined.
10441045
void setAlignment(MaybeAlign Align) { GlobalObject::setAlignment(Align); }
10451046

1047+
/// Returns the prefalign of the given function.
1048+
MaybeAlign getPreferredAlignment() const { return PreferredAlign; }
1049+
1050+
/// Sets the prefalign attribute of the Function.
1051+
void setPreferredAlignment(MaybeAlign Align) { PreferredAlign = Align; }
1052+
10461053
/// Return the value for vscale based on the vscale_range attribute or 0 when
10471054
/// unknown.
10481055
unsigned getVScaleValue() const;

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ lltok::Kind LLLexer::LexIdentifier() {
625625
KEYWORD(gc);
626626
KEYWORD(prefix);
627627
KEYWORD(prologue);
628+
KEYWORD(prefalign);
628629

629630
KEYWORD(no_sanitize_address);
630631
KEYWORD(no_sanitize_hwaddress);

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,7 +1449,7 @@ bool LLParser::parseGlobal(const std::string &Name, unsigned NameID,
14491449
return true;
14501450
} else if (Lex.getKind() == lltok::kw_align) {
14511451
MaybeAlign Alignment;
1452-
if (parseOptionalAlignment(Alignment))
1452+
if (parseOptionalAlignment(lltok::kw_align, Alignment))
14531453
return true;
14541454
if (Alignment)
14551455
GV->setAlignment(*Alignment);
@@ -1548,7 +1548,7 @@ bool LLParser::parseEnumAttribute(Attribute::AttrKind Attr, AttrBuilder &B,
15481548
return true;
15491549
Alignment = Align(Value);
15501550
} else {
1551-
if (parseOptionalAlignment(Alignment, true))
1551+
if (parseOptionalAlignment(lltok::kw_align, Alignment, true))
15521552
return true;
15531553
}
15541554
B.addAlignmentAttr(Alignment);
@@ -2382,10 +2382,11 @@ bool LLParser::parseOptionalFunctionMetadata(Function &F) {
23822382

23832383
/// parseOptionalAlignment
23842384
/// ::= /* empty */
2385-
/// ::= 'align' 4
2386-
bool LLParser::parseOptionalAlignment(MaybeAlign &Alignment, bool AllowParens) {
2385+
/// ::= KW 4
2386+
bool LLParser::parseOptionalAlignment(lltok::Kind KW, MaybeAlign &Alignment,
2387+
bool AllowParens) {
23872388
Alignment = std::nullopt;
2388-
if (!EatIfPresent(lltok::kw_align))
2389+
if (!EatIfPresent(KW))
23892390
return false;
23902391
LocTy AlignLoc = Lex.getLoc();
23912392
uint64_t Value = 0;
@@ -2695,7 +2696,7 @@ bool LLParser::parseOptionalCommaAlign(MaybeAlign &Alignment,
26952696
if (Lex.getKind() != lltok::kw_align)
26962697
return error(Lex.getLoc(), "expected metadata or 'align'");
26972698

2698-
if (parseOptionalAlignment(Alignment))
2699+
if (parseOptionalAlignment(lltok::kw_align, Alignment))
26992700
return true;
27002701
}
27012702

@@ -6705,7 +6706,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
67056706
LocTy BuiltinLoc;
67066707
std::string Section;
67076708
std::string Partition;
6708-
MaybeAlign Alignment;
6709+
MaybeAlign Alignment, PrefAlignment;
67096710
std::string GC;
67106711
GlobalValue::UnnamedAddr UnnamedAddr = GlobalValue::UnnamedAddr::None;
67116712
unsigned AddrSpace = 0;
@@ -6722,7 +6723,8 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
67226723
(EatIfPresent(lltok::kw_section) && parseStringConstant(Section)) ||
67236724
(EatIfPresent(lltok::kw_partition) && parseStringConstant(Partition)) ||
67246725
parseOptionalComdat(FunctionName, C) ||
6725-
parseOptionalAlignment(Alignment) ||
6726+
parseOptionalAlignment(lltok::kw_align, Alignment) ||
6727+
parseOptionalAlignment(lltok::kw_prefalign, PrefAlignment) ||
67266728
(EatIfPresent(lltok::kw_gc) && parseStringConstant(GC)) ||
67276729
(EatIfPresent(lltok::kw_prefix) && parseGlobalTypeAndValue(Prefix)) ||
67286730
(EatIfPresent(lltok::kw_prologue) && parseGlobalTypeAndValue(Prologue)) ||
@@ -6824,6 +6826,7 @@ bool LLParser::parseFunctionHeader(Function *&Fn, bool IsDefine,
68246826
Fn->setUnnamedAddr(UnnamedAddr);
68256827
if (Alignment)
68266828
Fn->setAlignment(*Alignment);
6829+
Fn->setPreferredAlignment(PrefAlignment);
68276830
Fn->setSection(Section);
68286831
Fn->setPartition(Partition);
68296832
Fn->setComdat(C);
@@ -8446,7 +8449,7 @@ int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
84468449
bool AteExtraComma = false;
84478450
if (EatIfPresent(lltok::comma)) {
84488451
if (Lex.getKind() == lltok::kw_align) {
8449-
if (parseOptionalAlignment(Alignment))
8452+
if (parseOptionalAlignment(lltok::kw_align, Alignment))
84508453
return true;
84518454
if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
84528455
return true;
@@ -8461,7 +8464,7 @@ int LLParser::parseAlloc(Instruction *&Inst, PerFunctionState &PFS) {
84618464
return true;
84628465
if (EatIfPresent(lltok::comma)) {
84638466
if (Lex.getKind() == lltok::kw_align) {
8464-
if (parseOptionalAlignment(Alignment))
8467+
if (parseOptionalAlignment(lltok::kw_align, Alignment))
84658468
return true;
84668469
if (parseOptionalCommaAddrSpace(AddrSpace, ASLoc, AteExtraComma))
84678470
return true;

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4385,6 +4385,13 @@ Error BitcodeReader::parseFunctionRecord(ArrayRef<uint64_t> Record) {
43854385
Func->setPartition(StringRef(Strtab.data() + Record[17], Record[18]));
43864386
}
43874387

4388+
if (Record.size() > 19) {
4389+
MaybeAlign PrefAlignment;
4390+
if (Error Err = parseAlignmentValue(Record[19], PrefAlignment))
4391+
return Err;
4392+
Func->setPreferredAlignment(PrefAlignment);
4393+
}
4394+
43884395
ValueList.push_back(Func, getVirtualTypeID(Func->getType(), FTyID));
43894396

43904397
if (OperandInfo.PersonalityFn || OperandInfo.Prefix || OperandInfo.Prologue)

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,8 @@ void ModuleBitcodeWriter::writeModuleInfo() {
16441644
// FUNCTION: [strtab offset, strtab size, type, callingconv, isproto,
16451645
// linkage, paramattrs, alignment, section, visibility, gc,
16461646
// unnamed_addr, prologuedata, dllstorageclass, comdat,
1647-
// prefixdata, personalityfn, DSO_Local, addrspace]
1647+
// prefixdata, personalityfn, DSO_Local, addrspace,
1648+
// partition_strtab, partition_size, prefalign]
16481649
Vals.push_back(addToStrtab(F.getName()));
16491650
Vals.push_back(F.getName().size());
16501651
Vals.push_back(VE.getTypeID(F.getFunctionType()));
@@ -1671,6 +1672,7 @@ void ModuleBitcodeWriter::writeModuleInfo() {
16711672
Vals.push_back(F.getAddressSpace());
16721673
Vals.push_back(addToStrtab(F.getPartition()));
16731674
Vals.push_back(F.getPartition().size());
1675+
Vals.push_back(getEncodedAlign(F.getPreferredAlignment()));
16741676

16751677
unsigned AbbrevToUse = 0;
16761678
Stream.EmitRecord(bitc::MODULE_CODE_FUNCTION, Vals, AbbrevToUse);

llvm/lib/IR/AsmWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4229,6 +4229,8 @@ void AssemblyWriter::printFunction(const Function *F) {
42294229
maybePrintComdat(Out, *F);
42304230
if (MaybeAlign A = F->getAlign())
42314231
Out << " align " << A->value();
4232+
if (MaybeAlign A = F->getPreferredAlignment())
4233+
Out << " prefalign " << A->value();
42324234
if (F->hasGC())
42334235
Out << " gc \"" << F->getGC() << '"';
42344236
if (F->hasPrefixData()) {

llvm/test/Bitcode/compatibility.ll

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,20 @@ declare void @f.align4() align 4
759759
declare void @f.align8() align 8
760760
; CHECK: declare void @f.align8() align 8
761761

762+
; Functions -- prefalign
763+
define void @f.prefalign2() prefalign 2 {
764+
ret void
765+
}
766+
; CHECK: define void @f.prefalign2() prefalign 2
767+
define void @f.prefalign4() prefalign 4 {
768+
ret void
769+
}
770+
; CHECK: define void @f.prefalign4() prefalign 4
771+
define void @f.prefalign8() prefalign 8 {
772+
ret void
773+
}
774+
; CHECK: define void @f.prefalign8() prefalign 8
775+
762776
; Functions -- GC
763777
declare void @f.gcshadow() gc "shadow-stack"
764778
; CHECK: declare void @f.gcshadow() gc "shadow-stack"

0 commit comments

Comments
 (0)