Skip to content

Commit d664c4b

Browse files
committed
Attributes: add a new allocalign attribute
This will let us start moving away from hard-coded attributes in MemoryBuiltins.cpp and put the knowledge about various attribute functions in the compilers that emit those calls where it probably belongs. Differential Revision: https://reviews.llvm.org/D117921
1 parent e5eb365 commit d664c4b

File tree

10 files changed

+24
-1
lines changed

10 files changed

+24
-1
lines changed

llvm/docs/LangRef.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1379,6 +1379,14 @@ Currently, only the following parameter attributes are defined:
13791379
information that is not mapped to base types in the backend (for example,
13801380
over-alignment specification through language attributes).
13811381

1382+
``allocalign``
1383+
The function parameter marked with this attribute is is the alignment in bytes of the
1384+
newly allocated block returned by this function. The returned value must either have
1385+
the specified alignment or be the null pointer. The return value MAY be more aligned
1386+
than the requested alignment, but not less aligned. Invalid (e.g. non-power-of-2)
1387+
alignments are permitted for the allocalign parameter, so long as the returned pointer
1388+
is null. This attribute may only be applied to integer parameters.
1389+
13821390
.. _gc:
13831391

13841392
Garbage Collector Strategy Names

llvm/include/llvm/AsmParser/LLToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ enum Kind {
177177

178178
// Attributes:
179179
kw_attributes,
180+
kw_allocalign,
180181
kw_allocsize,
181182
kw_alwaysinline,
182183
kw_argmemonly,

llvm/include/llvm/Bitcode/LLVMBitCodes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ enum AttributeKindCodes {
678678
ATTR_KIND_ELEMENTTYPE = 77,
679679
ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION = 78,
680680
ATTR_KIND_NO_SANITIZE_BOUNDS = 79,
681+
ATTR_KIND_ALLOC_ALIGN = 80,
681682
};
682683

683684
enum ComdatSelectionKindCodes {

llvm/include/llvm/IR/Attributes.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ class StrBoolAttr<string S> : Attr<S, []>;
4747
/// 0 means unaligned (different from align(1)).
4848
def Alignment : IntAttr<"align", [ParamAttr, RetAttr]>;
4949

50+
/// Parameter of a function that tells us the alignment of an allocation, as in
51+
/// aligned_alloc and aligned ::operator::new.
52+
def AllocAlign: EnumAttr<"allocalign", [ParamAttr]>;
53+
5054
/// The result of the function is guaranteed to point to a number of bytes that
5155
/// we can determine if we know the value of the function's arguments.
5256
def AllocSize : IntAttr<"allocsize", [FnAttr]>;

llvm/lib/AsmParser/LLLexer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ lltok::Kind LLLexer::LexIdentifier() {
634634
KEYWORD(attributes);
635635

636636
KEYWORD(alwaysinline);
637+
KEYWORD(allocalign);
637638
KEYWORD(allocsize);
638639
KEYWORD(argmemonly);
639640
KEYWORD(builtin);

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
15201520
return Attribute::Dereferenceable;
15211521
case bitc::ATTR_KIND_DEREFERENCEABLE_OR_NULL:
15221522
return Attribute::DereferenceableOrNull;
1523+
case bitc::ATTR_KIND_ALLOC_ALIGN:
1524+
return Attribute::AllocAlign;
15231525
case bitc::ATTR_KIND_ALLOC_SIZE:
15241526
return Attribute::AllocSize;
15251527
case bitc::ATTR_KIND_NO_RED_ZONE:

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
610610
switch (Kind) {
611611
case Attribute::Alignment:
612612
return bitc::ATTR_KIND_ALIGNMENT;
613+
case Attribute::AllocAlign:
614+
return bitc::ATTR_KIND_ALLOC_ALIGN;
613615
case Attribute::AllocSize:
614616
return bitc::ATTR_KIND_ALLOC_SIZE;
615617
case Attribute::AlwaysInline:

llvm/lib/IR/Attributes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,8 @@ AttributeMask AttributeFuncs::typeIncompatible(Type *Ty) {
17801780
if (!Ty->isIntegerTy())
17811781
// Attributes that only apply to integers.
17821782
Incompatible.addAttribute(Attribute::SExt)
1783-
.addAttribute(Attribute::ZExt);
1783+
.addAttribute(Attribute::ZExt)
1784+
.addAttribute(Attribute::AllocAlign);
17841785

17851786
if (!Ty->isPointerTy())
17861787
// Attributes that only apply to pointers.

llvm/lib/Transforms/Utils/CodeExtractor.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
962962
break;
963963
// These attributes cannot be applied to functions.
964964
case Attribute::Alignment:
965+
case Attribute::AllocAlign:
965966
case Attribute::ByVal:
966967
case Attribute::Dereferenceable:
967968
case Attribute::DereferenceableOrNull:

llvm/test/Bitcode/compatibility.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,8 @@ declare void @f.param.swiftasync(i8* swiftasync)
560560
; CHECK: declare void @f.param.swiftasync(i8* swiftasync)
561561
declare void @f.param.swifterror(i8** swifterror)
562562
; CHECK: declare void @f.param.swifterror(i8** swifterror)
563+
declare void @f.param.allocalign(i32 allocalign)
564+
; CHECK: declare void @f.param.allocalign(i32 allocalign)
563565

564566
; Functions -- unnamed_addr and local_unnamed_addr
565567
declare void @f.unnamed_addr() unnamed_addr

0 commit comments

Comments
 (0)