<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jun 23, 2016 at 3:05 AM, Duncan Exon Smith <span dir="ltr"><<a href="mailto:dexonsmith@apple.com" target="_blank">dexonsmith@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div><br>
<br>
> On Jun 23, 2016, at 01:13, Peter Collingbourne <<a href="mailto:peter@pcc.me.uk" target="_blank">peter@pcc.me.uk</a>> wrote:<br>
><br>
> pcc created this revision.<br>
> pcc added a reviewer: dexonsmith.<br>
> pcc added subscribers: llvm-commits, eugenis.<br>
><br>
> Split out from D21053.<br>
><br>
> <a href="http://reviews.llvm.org/D21624" rel="noreferrer" target="_blank">http://reviews.llvm.org/D21624</a><br>
><br>
> Files:<br>
>  include/llvm/IR/GlobalObject.h<br>
>  lib/IR/Metadata.cpp<br>
>  lib/Linker/IRMover.cpp<br>
>  test/Linker/metadata-attach.ll<br>
><br>
> Index: test/Linker/metadata-attach.ll<br>
> ===================================================================<br>
> --- /dev/null<br>
> +++ test/Linker/metadata-attach.ll<br>
> @@ -0,0 +1,19 @@<br>
> +; RUN: llvm-link %s -S -o - | FileCheck %s<br>
> +<br>
> +; CHECK: @g1 = global i32 0, !attach !0<br>
> +@g1 = global i32 0, !attach !0<br>
> +<br>
> +; CHECK: @g2 = external global i32, !attach !0<br>
> +@g2 = external global i32, !attach !0<br>
> +<br>
> +; CHECK: define void @f1() !attach !0<br>
> +define void @f1() !attach !0 {<br>
> +  call void @f2()<br>
> +  store i32 0, i32* @g2<br>
> +  ret void<br>
> +}<br>
> +<br>
> +; CHECK: declare !attach !0 void @f2()<br>
> +declare !attach !0 void @f2()<br>
> +<br>
> +!0 = !{}<br>
> Index: lib/Linker/IRMover.cpp<br>
> ===================================================================<br>
> --- lib/Linker/IRMover.cpp<br>
> +++ lib/Linker/IRMover.cpp<br>
> @@ -637,6 +637,12 @@<br>
><br>
>   NewGV->copyAttributesFrom(SGV);<br>
><br>
> +  if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {<br>
> +    // Metadata for global variables and function declarations is copied eagerly.<br>
> +    if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())<br>
> +      NewGO->copyMetadata(cast<GlobalObject>(SGV));<br>
> +  }<br>
> +<br>
>   // Remove these copied constants in case this stays a declaration, since<br>
>   // they point to the source module. If the def is linked the values will<br>
>   // be mapped in during linkFunctionBody.<br>
> @@ -952,10 +958,7 @@<br>
>     Dst.setPersonalityFn(Src.getPersonalityFn());<br>
><br>
>   // Copy over the metadata attachments without remapping.<br>
> -  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;<br>
> -  Src.getAllMetadata(MDs);<br>
> -  for (const auto &I : MDs)<br>
> -    Dst.setMetadata(I.first, I.second);<br>
> +  Dst.copyMetadata(&Src);<br>
><br>
>   // Steal arguments and splice the body of Src into Dst.<br>
>   Dst.stealArgumentListFrom(Src);<br>
> Index: lib/IR/Metadata.cpp<br>
> ===================================================================<br>
> --- lib/IR/Metadata.cpp<br>
> +++ lib/IR/Metadata.cpp<br>
> @@ -1393,6 +1393,13 @@<br>
>   return getMetadata(getContext().getMDKindID(Kind));<br>
> }<br>
><br>
> +void GlobalObject::copyMetadata(const GlobalObject *Other) {<br>
> +  SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;<br>
<br>
</div></div>Why so small?<br></blockquote><div><br></div><div>I wouldn't normally expect there to be more than one attachment here (for debug info; PGO and CFI metadata are rarer). But I suppose it wouldn't hurt to set this to an estimated upper bound rather than an expected value, since the stack allocation here costs ~nothing. Increased to 8.</div><div><br></div><div>Peter</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<span><br>
> +  Other->getAllMetadata(MDs);<br>
> +  for (auto &MD : MDs)<br>
> +    addMetadata(MD.first, *MD.second);<br>
> +}<br>
> +<br>
> void Function::setSubprogram(DISubprogram *SP) {<br>
>   setMetadata(LLVMContext::MD_dbg, SP);<br>
> }<br>
> Index: include/llvm/IR/GlobalObject.h<br>
> ===================================================================<br>
> --- include/llvm/IR/GlobalObject.h<br>
> +++ include/llvm/IR/GlobalObject.h<br>
> @@ -114,6 +114,9 @@<br>
>   /// Erase all metadata attachments with the given kind.<br>
>   void eraseMetadata(unsigned KindID);<br>
><br>
> +  /// Copy metadata from Src.<br>
> +  void copyMetadata(const GlobalObject *Src);<br>
> +<br>
>   void copyAttributesFrom(const GlobalValue *Src) override;<br>
><br>
>   // Methods for support type inquiry through isa, cast, and dyn_cast:<br>
><br>
><br>
</span>> <D21624.61617.patch><br>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div data-smartmail="gmail_signature"><div dir="ltr">-- <div>Peter</div></div></div>
</div></div>