<div dir="ltr"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">(4) Move target registration out of Support so that it can include the<br>required definitions from MC.</blockquote><div><br></div><div>Yep -- left that one out. Chalk that up to the fever. This seems like an ideal solution if we can do it.</div><div><br></div><div>-- Lang.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Oct 11, 2017 at 11:25 AM, Rafael Avila de Espindola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">Lang Hames via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> writes:<br>
<br>
> Hi All,<br>
><br>
> While trying to put together an MC-based assembler the other day, I again<br>
> encountered MC's non-obvious memory ownership rules. The most egregious<br>
> example of these is MCObjectStreamer's destructor:<br>
><br>
> MCObjectStreamer::~<wbr>MCObjectStreamer() {<br>
>   delete &Assembler->getBackend();<br>
>   delete &Assembler->getEmitter();<br>
>   delete &Assembler->getWriter();<br>
>   delete Assembler;<br>
> }<br>
><br>
> In the depths of a fever from a head-cold, I snapped. I've been hacking MC<br>
> to convert these raw pointers (and worse: references!) to unique_ptrs<br>
> (apologies to people whose backbends I've broken), but I hit a big blocker<br>
> when I get to Target in "llvm/Support/TargetRegistry.<wbr>h".<br>
><br>
> Target vends MC objects by calling registered ctor functions. E.g.:<br>
><br>
> MCAsmBackend *createMCAsmBackend(const MCRegisterInfo &MRI,<br>
>                                  StringRef TheTriple, StringRef CPU,<br>
>                                  const MCTargetOptions &Options) const {<br>
>   if (!MCAsmBackendCtorFn)<br>
>     return nullptr;<br>
>   return MCAsmBackendCtorFn(*this, MRI, Triple(TheTriple), CPU, Options);<br>
> }<br>
><br>
> The callee owns the resulting object so ideally we would return a<br>
> unique_ptr<MCAsmBackend>, but to do this we'd need access to the definition<br>
> of MCAsmBackend which we can't get without violating layering. (We need the<br>
> definition because unique_ptr<MCAsmBackend> can notionally call<br>
> MCAsmBackend's destructor, though we know it won't here as the whole point<br>
> is to hand ownership back to the caller).<br>
><br>
> There are three approaches I can think of to improve things:<br>
><br>
> (1) Keep the raw pointers, but document *very* clearly, recommending the<br>
> caller stash the result in a unique_ptr immediately.<br>
><br>
> (2) Have the ctor functions take a unique_ptr<T>* as their first argument<br>
> and store the result there (thanks to Dave Blaikie for this suggestion).<br>
> Passing the unique_ptrs by pointer means we don't need a definition for T.<br>
> Usage looks like:<br>
><br>
> std::unique_ptr<MCAsmBackend> MAB;<br>
> Target.createMCAsmBackend(&<wbr>MAB, TheTriple, CPU, Options);<br>
><br>
><br>
> (3) Introduce a 'transient_unique_ptr<T>' type that never calls T's<br>
> destructor, and whose purpose is to pass off ownership to a real unique_ptr:<br>
><br>
> template <typename T><br>
> class transient_unique_ptr {<br>
> public:<br>
>   transient_unique_ptr(std::<wbr>unique_ptr<T> InVal) : Val(InVal.release()) {}<br>
>   transient_unique_ptr(<wbr>transient_unique_ptr<T> &&Other) :<br>
> Val(Other.Val) { Other.Val<br>
> = nullptr; }<br>
>   transient_unique_ptr&& operator=(transient_unique_<wbr>ptr<T> &&Other) {<br>
> std::swap(Val,<br>
> Other.Val); return *this; }<br>
>   ~transient_unique_ptr() { assert(!Val && "value should have been handed<br>
> off"); }<br>
>   std::unique_ptr<T> take() { auto Tmp = Val; Val = nullptr; return<br>
> unique_ptr<T>(Val); }<br>
> private:<br>
>   T *Val = nullptr;<br>
> };<br>
<br>
</div></div>(4) Move target registration out of Support so that it can include the<br>
required definitions from MC.<br>
<br>
Cheers,<br>
Rafael<br>
</blockquote></div><br></div>