From a907d36cfe8069ce191ffada3b84e37ca50e05b6 Mon Sep 17 00:00:00 2001 From: Augie Fackler Date: Thu, 24 Feb 2022 10:40:20 -0500 Subject: [PATCH] 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 --- llvm/docs/LangRef.rst | 7 +++++++ llvm/include/llvm/AsmParser/LLToken.h | 1 + llvm/include/llvm/Bitcode/LLVMBitCodes.h | 1 + llvm/include/llvm/IR/Attributes.td | 5 ++++- llvm/lib/AsmParser/LLLexer.cpp | 1 + llvm/lib/Bitcode/Reader/BitcodeReader.cpp | 2 ++ llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 2 ++ llvm/lib/IR/Attributes.cpp | 3 ++- llvm/lib/Transforms/Utils/CodeExtractor.cpp | 1 + llvm/test/Bitcode/compatibility.ll | 2 ++ 10 files changed, 23 insertions(+), 2 deletions(-) diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst index d880fbd321b6..041b51418eb8 100644 --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -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 diff --git a/llvm/include/llvm/AsmParser/LLToken.h b/llvm/include/llvm/AsmParser/LLToken.h index c4ccd358f938..5a1cf3432114 100644 --- a/llvm/include/llvm/AsmParser/LLToken.h +++ b/llvm/include/llvm/AsmParser/LLToken.h @@ -199,6 +199,7 @@ enum Kind { kw_inreg, kw_jumptable, kw_minsize, + kw_allocptr, kw_naked, kw_nest, kw_noalias, diff --git a/llvm/include/llvm/Bitcode/LLVMBitCodes.h b/llvm/include/llvm/Bitcode/LLVMBitCodes.h index 1d6f237572c6..0ca294a33279 100644 --- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h +++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h @@ -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 { diff --git a/llvm/include/llvm/IR/Attributes.td b/llvm/include/llvm/IR/Attributes.td index 66212a7da8b3..6bb093b3715f 100644 --- a/llvm/include/llvm/IR/Attributes.td +++ b/llvm/include/llvm/IR/Attributes.td @@ -47,10 +47,13 @@ class StrBoolAttr : Attr; /// 0 means unaligned (different from align(1)). def Alignment : IntAttr<"align", [ParamAttr, RetAttr]>; -/// Parameter of a function that tells us the alignment of an allocation, as in +/// Parameter of a function that tells us the alignment of an allocation, as in /// 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]>; diff --git a/llvm/lib/AsmParser/LLLexer.cpp b/llvm/lib/AsmParser/LLLexer.cpp index 2a091a9c94f8..f86504f16f29 100644 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -652,6 +652,7 @@ lltok::Kind LLLexer::LexIdentifier() { KEYWORD(inreg); KEYWORD(jumptable); KEYWORD(minsize); + KEYWORD(allocptr); KEYWORD(naked); KEYWORD(nest); KEYWORD(noalias); diff --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp index 62d9904e6ca6..901c1173fe4f 100644 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -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: diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index e773ba8607fa..dcfe12a26903 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -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: diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp index c4759f858a2e..3cade807ece7 100644 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -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. diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp index 37689225b646..2895b242ca47 100644 --- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp +++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp @@ -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: diff --git a/llvm/test/Bitcode/compatibility.ll b/llvm/test/Bitcode/compatibility.ll index 68b554980282..ab9c3b1c61ab 100644 --- a/llvm/test/Bitcode/compatibility.ll +++ b/llvm/test/Bitcode/compatibility.ll @@ -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