diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst --- a/llvm/docs/LangRef.rst +++ b/llvm/docs/LangRef.rst @@ -1392,6 +1392,13 @@ 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 --- a/llvm/include/llvm/AsmParser/LLToken.h +++ b/llvm/include/llvm/AsmParser/LLToken.h @@ -199,6 +199,7 @@ 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 --- a/llvm/include/llvm/Bitcode/LLVMBitCodes.h +++ b/llvm/include/llvm/Bitcode/LLVMBitCodes.h @@ -679,6 +679,7 @@ 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 --- a/llvm/include/llvm/IR/Attributes.td +++ b/llvm/include/llvm/IR/Attributes.td @@ -47,10 +47,13 @@ /// 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 maniuplated 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 --- a/llvm/lib/AsmParser/LLLexer.cpp +++ b/llvm/lib/AsmParser/LLLexer.cpp @@ -652,6 +652,7 @@ 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 --- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1526,6 +1526,8 @@ 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 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -646,6 +646,8 @@ 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 --- a/llvm/lib/IR/Attributes.cpp +++ b/llvm/lib/IR/Attributes.cpp @@ -1803,7 +1803,9 @@ .addAttribute(Attribute::ByVal) .addAttribute(Attribute::StructRet) .addAttribute(Attribute::ByRef) - .addAttribute(Attribute::ElementType); + .addAttribute(Attribute::ElementType) + // TODO(durin42): this might be safe to drop? + .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 --- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp +++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp @@ -963,6 +963,7 @@ 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 --- a/llvm/test/Bitcode/compatibility.ll +++ b/llvm/test/Bitcode/compatibility.ll @@ -562,6 +562,8 @@ ; 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