[PATCH] D84860: [LangRef] Add uniqueptr attribute.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 29 08:51:39 PDT 2020


fhahn created this revision.
fhahn added reviewers: jdoerfert, efriedma, t.p.northover, arsenm, rjmccall.
Herald added subscribers: dexonsmith, steven_wu, kosarev, hiraditya, kristof.beyls.
Herald added a project: LLVM.
fhahn requested review of this revision.
Herald added a subscriber: wdng.

This patch adds a new uniqueptr attribute.

The attribute can be used to indicate that an argument is the only
pointer referencing the underlying object based on the pointer.

The intent of this attribute is to provide a guarantee that the no
pointers to the object are escaped before calling the function.

The motivating case is passing indirectly passed arguments on platforms
that do not use byval, like AArch64. On AArch64, objects passed by value
are allocated in the caller and then passed as pointers, but there is no
information in the called function that the argument is the only pointer
to the underlying object.

This currently prohibits some optimizations. The example below is roughly the
IR generated by Clang for a function that takes a struct by value on AArch64.
We cannot remove the second load, because nothing guarantees that `%x`
did not escape before calling the function. The new attribute provides
exactly this guarantee, allowing us to remove the second load.

  %struct.Foo = type { i8*, i8*, i8*, i32 }
  
  define i32 @test(%struct.Foo* nocapture readonly %x) {
  entry:
    %x1 = getelementptr inbounds %struct.Foo, %struct.Foo* %x, i64 0, i32 3
    %0 = load i32, i32* %x1, align 8, !tbaa !2
    tail call void bitcast (void (...)* @sideeffect to void ()*)() #2
    %1 = load i32, i32* %x1, align 8, !tbaa !2
    %add = add nsw i32 %1, %0
    ret i32 %add
  }

In a way, the uniqueptr attribute should behave similar to byval, just
that the object is allocated in the caller, rather than on the callee
stack.

I am a bit reluctant to add yet another attribute, but unfortunately there
seems to be no combination of attributes that achieves the desired
optimizations.

NoAlias is relatively close, but it is not suitable, because references
to the object can escape in the called function.

I am also not sure about the name of the attribute. Any suggestions for
a better name are more than welcome!


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D84860

Files:
  llvm/docs/LangRef.rst
  llvm/include/llvm/Bitcode/LLVMBitCodes.h
  llvm/include/llvm/IR/Argument.h
  llvm/include/llvm/IR/Attributes.td
  llvm/lib/Analysis/BasicAliasAnalysis.cpp
  llvm/lib/AsmParser/LLLexer.cpp
  llvm/lib/AsmParser/LLParser.cpp
  llvm/lib/AsmParser/LLToken.h
  llvm/lib/Bitcode/Reader/BitcodeReader.cpp
  llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
  llvm/lib/IR/Attributes.cpp
  llvm/lib/IR/Function.cpp
  llvm/test/Transforms/EarlyCSE/uniqueptr.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84860.281623.patch
Type: text/x-patch
Size: 8587 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200729/da097681/attachment.bin>


More information about the llvm-commits mailing list