Attributes: add a new `allocptr` attribute

This continues the push away from hard-coded knowledge about functions
towards attributes. We'll use this to annotate free(), realloc() and
cousins and obviate the hard-coded list of free functions.

Differential Revision: https://reviews.llvm.org/D123083
This commit is contained in:
Augie Fackler 2022-02-24 10:40:20 -05:00
parent 3a9ae9cf7c
commit a907d36cfe
10 changed files with 23 additions and 2 deletions

View File

@ -1392,6 +1392,13 @@ Currently, only the following parameter attributes are defined:
alignments are permitted for the allocalign parameter, so long as the returned pointer
is null. This attribute may only be applied to integer parameters.
``allocptr``
The function parameter marked with this attribute is the pointer
that will be manipulated by the allocator. For a realloc-like
function the pointer will be invalidated upon success (but the
same address may be returned), for a free-like function the
pointer will always be invalidated.
.. _gc:
Garbage Collector Strategy Names

View File

@ -199,6 +199,7 @@ enum Kind {
kw_inreg,
kw_jumptable,
kw_minsize,
kw_allocptr,
kw_naked,
kw_nest,
kw_noalias,

View File

@ -683,6 +683,7 @@ enum AttributeKindCodes {
ATTR_KIND_DISABLE_SANITIZER_INSTRUMENTATION = 78,
ATTR_KIND_NO_SANITIZE_BOUNDS = 79,
ATTR_KIND_ALLOC_ALIGN = 80,
ATTR_KIND_ALLOCATED_POINTER = 81,
};
enum ComdatSelectionKindCodes {

View File

@ -51,6 +51,9 @@ def Alignment : IntAttr<"align", [ParamAttr, RetAttr]>;
/// aligned_alloc and aligned ::operator::new.
def AllocAlign: EnumAttr<"allocalign", [ParamAttr]>;
/// Parameter is the pointer to be manipulated by the allocator function.
def AllocatedPointer : EnumAttr<"allocptr", [ParamAttr]>;
/// The result of the function is guaranteed to point to a number of bytes that
/// we can determine if we know the value of the function's arguments.
def AllocSize : IntAttr<"allocsize", [FnAttr]>;

View File

@ -652,6 +652,7 @@ lltok::Kind LLLexer::LexIdentifier() {
KEYWORD(inreg);
KEYWORD(jumptable);
KEYWORD(minsize);
KEYWORD(allocptr);
KEYWORD(naked);
KEYWORD(nest);
KEYWORD(noalias);

View File

@ -1538,6 +1538,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
return Attribute::AllocAlign;
case bitc::ATTR_KIND_ALLOC_SIZE:
return Attribute::AllocSize;
case bitc::ATTR_KIND_ALLOCATED_POINTER:
return Attribute::AllocatedPointer;
case bitc::ATTR_KIND_NO_RED_ZONE:
return Attribute::NoRedZone;
case bitc::ATTR_KIND_NO_RETURN:

View File

@ -647,6 +647,8 @@ static uint64_t getAttrKindEncoding(Attribute::AttrKind Kind) {
return bitc::ATTR_KIND_JUMP_TABLE;
case Attribute::MinSize:
return bitc::ATTR_KIND_MIN_SIZE;
case Attribute::AllocatedPointer:
return bitc::ATTR_KIND_ALLOCATED_POINTER;
case Attribute::Naked:
return bitc::ATTR_KIND_NAKED;
case Attribute::Nest:

View File

@ -1803,7 +1803,8 @@ AttributeMask AttributeFuncs::typeIncompatible(Type *Ty,
.addAttribute(Attribute::ByVal)
.addAttribute(Attribute::StructRet)
.addAttribute(Attribute::ByRef)
.addAttribute(Attribute::ElementType);
.addAttribute(Attribute::ElementType)
.addAttribute(Attribute::AllocatedPointer);
}
// Attributes that only apply to pointers or vectors of pointers.

View File

@ -963,6 +963,7 @@ Function *CodeExtractor::constructFunction(const ValueSet &inputs,
break;
// These attributes cannot be applied to functions.
case Attribute::Alignment:
case Attribute::AllocatedPointer:
case Attribute::AllocAlign:
case Attribute::ByVal:
case Attribute::Dereferenceable:

View File

@ -562,6 +562,8 @@ declare void @f.param.swifterror(i8** swifterror)
; CHECK: declare void @f.param.swifterror(i8** swifterror)
declare void @f.param.allocalign(i32 allocalign)
; CHECK: declare void @f.param.allocalign(i32 allocalign)
declare void @f.param.allocptr(i32* allocptr)
; CHECK: declare void @f.param.allocptr(i32* allocptr)
; Functions -- unnamed_addr and local_unnamed_addr
declare void @f.unnamed_addr() unnamed_addr