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 alignments are permitted for the allocalign parameter, so long as the returned pointer
is null. This attribute may only be applied to integer parameters. 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: .. _gc:
Garbage Collector Strategy Names Garbage Collector Strategy Names

View File

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

View File

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

View File

@ -51,6 +51,9 @@ def Alignment : IntAttr<"align", [ParamAttr, RetAttr]>;
/// aligned_alloc and aligned ::operator::new. /// aligned_alloc and aligned ::operator::new.
def AllocAlign: EnumAttr<"allocalign", [ParamAttr]>; 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 /// 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. /// we can determine if we know the value of the function's arguments.
def AllocSize : IntAttr<"allocsize", [FnAttr]>; def AllocSize : IntAttr<"allocsize", [FnAttr]>;

View File

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

View File

@ -1538,6 +1538,8 @@ static Attribute::AttrKind getAttrFromCode(uint64_t Code) {
return Attribute::AllocAlign; return Attribute::AllocAlign;
case bitc::ATTR_KIND_ALLOC_SIZE: case bitc::ATTR_KIND_ALLOC_SIZE:
return Attribute::AllocSize; return Attribute::AllocSize;
case bitc::ATTR_KIND_ALLOCATED_POINTER:
return Attribute::AllocatedPointer;
case bitc::ATTR_KIND_NO_RED_ZONE: case bitc::ATTR_KIND_NO_RED_ZONE:
return Attribute::NoRedZone; return Attribute::NoRedZone;
case bitc::ATTR_KIND_NO_RETURN: 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; return bitc::ATTR_KIND_JUMP_TABLE;
case Attribute::MinSize: case Attribute::MinSize:
return bitc::ATTR_KIND_MIN_SIZE; return bitc::ATTR_KIND_MIN_SIZE;
case Attribute::AllocatedPointer:
return bitc::ATTR_KIND_ALLOCATED_POINTER;
case Attribute::Naked: case Attribute::Naked:
return bitc::ATTR_KIND_NAKED; return bitc::ATTR_KIND_NAKED;
case Attribute::Nest: case Attribute::Nest:

View File

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

View File

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

View File

@ -562,6 +562,8 @@ declare void @f.param.swifterror(i8** swifterror)
; CHECK: declare void @f.param.swifterror(i8** swifterror) ; CHECK: declare void @f.param.swifterror(i8** swifterror)
declare void @f.param.allocalign(i32 allocalign) declare void @f.param.allocalign(i32 allocalign)
; CHECK: 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 ; Functions -- unnamed_addr and local_unnamed_addr
declare void @f.unnamed_addr() unnamed_addr declare void @f.unnamed_addr() unnamed_addr