[PATCH] D21624: Linker: Copy metadata when linking declarations.

Peter Collingbourne via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 23 12:14:28 PDT 2016


On Thu, Jun 23, 2016 at 3:05 AM, Duncan Exon Smith <dexonsmith at apple.com>
wrote:

>
>
> > On Jun 23, 2016, at 01:13, Peter Collingbourne <peter at pcc.me.uk> wrote:
> >
> > pcc created this revision.
> > pcc added a reviewer: dexonsmith.
> > pcc added subscribers: llvm-commits, eugenis.
> >
> > Split out from D21053.
> >
> > http://reviews.llvm.org/D21624
> >
> > Files:
> >  include/llvm/IR/GlobalObject.h
> >  lib/IR/Metadata.cpp
> >  lib/Linker/IRMover.cpp
> >  test/Linker/metadata-attach.ll
> >
> > Index: test/Linker/metadata-attach.ll
> > ===================================================================
> > --- /dev/null
> > +++ test/Linker/metadata-attach.ll
> > @@ -0,0 +1,19 @@
> > +; RUN: llvm-link %s -S -o - | FileCheck %s
> > +
> > +; CHECK: @g1 = global i32 0, !attach !0
> > + at g1 = global i32 0, !attach !0
> > +
> > +; CHECK: @g2 = external global i32, !attach !0
> > + at g2 = external global i32, !attach !0
> > +
> > +; CHECK: define void @f1() !attach !0
> > +define void @f1() !attach !0 {
> > +  call void @f2()
> > +  store i32 0, i32* @g2
> > +  ret void
> > +}
> > +
> > +; CHECK: declare !attach !0 void @f2()
> > +declare !attach !0 void @f2()
> > +
> > +!0 = !{}
> > Index: lib/Linker/IRMover.cpp
> > ===================================================================
> > --- lib/Linker/IRMover.cpp
> > +++ lib/Linker/IRMover.cpp
> > @@ -637,6 +637,12 @@
> >
> >   NewGV->copyAttributesFrom(SGV);
> >
> > +  if (auto *NewGO = dyn_cast<GlobalObject>(NewGV)) {
> > +    // Metadata for global variables and function declarations is
> copied eagerly.
> > +    if (isa<GlobalVariable>(SGV) || SGV->isDeclaration())
> > +      NewGO->copyMetadata(cast<GlobalObject>(SGV));
> > +  }
> > +
> >   // Remove these copied constants in case this stays a declaration,
> since
> >   // they point to the source module. If the def is linked the values
> will
> >   // be mapped in during linkFunctionBody.
> > @@ -952,10 +958,7 @@
> >     Dst.setPersonalityFn(Src.getPersonalityFn());
> >
> >   // Copy over the metadata attachments without remapping.
> > -  SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
> > -  Src.getAllMetadata(MDs);
> > -  for (const auto &I : MDs)
> > -    Dst.setMetadata(I.first, I.second);
> > +  Dst.copyMetadata(&Src);
> >
> >   // Steal arguments and splice the body of Src into Dst.
> >   Dst.stealArgumentListFrom(Src);
> > Index: lib/IR/Metadata.cpp
> > ===================================================================
> > --- lib/IR/Metadata.cpp
> > +++ lib/IR/Metadata.cpp
> > @@ -1393,6 +1393,13 @@
> >   return getMetadata(getContext().getMDKindID(Kind));
> > }
> >
> > +void GlobalObject::copyMetadata(const GlobalObject *Other) {
> > +  SmallVector<std::pair<unsigned, MDNode *>, 1> MDs;
>
> Why so small?
>

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.

Peter

>
> > +  Other->getAllMetadata(MDs);
> > +  for (auto &MD : MDs)
> > +    addMetadata(MD.first, *MD.second);
> > +}
> > +
> > void Function::setSubprogram(DISubprogram *SP) {
> >   setMetadata(LLVMContext::MD_dbg, SP);
> > }
> > Index: include/llvm/IR/GlobalObject.h
> > ===================================================================
> > --- include/llvm/IR/GlobalObject.h
> > +++ include/llvm/IR/GlobalObject.h
> > @@ -114,6 +114,9 @@
> >   /// Erase all metadata attachments with the given kind.
> >   void eraseMetadata(unsigned KindID);
> >
> > +  /// Copy metadata from Src.
> > +  void copyMetadata(const GlobalObject *Src);
> > +
> >   void copyAttributesFrom(const GlobalValue *Src) override;
> >
> >   // Methods for support type inquiry through isa, cast, and dyn_cast:
> >
> >
> > <D21624.61617.patch>
>



-- 
-- 
Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160623/cb051b95/attachment.html>


More information about the llvm-commits mailing list