<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <meta content="text/html; charset=ISO-8859-1"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#ffffff" text="#000000">
    Sorry to resurrect an old thread, but I finally got around to
    testing this approach (round tripping through bitcode in memory) and
    it works beautifully - and isn't that much slower than cloning.<br>
    <br>
    I have noticed however that the copy process isn't thread-safe. The
    problem is that in Function, there is lazy initialization code for
    arguments:<br>
    <br>
      void CheckLazyArguments() const {<br>
        if (hasLazyArguments())<br>
          BuildLazyArguments();<br>
      }<br>
      void BuildLazyArguments() const;<br>
    <br>
    I've worked around this by calling Function::getArgumentList()
    outside the threaded code to harden it before the threaded copies. 
    Are there other lazy data structures that need to be solidified
    before threading?  Should I be playing it safe and put a thread lock
    around the entire copy procedure?<br>
    <br>
    In case you are interested, here is the algorithm I'm using to copy
    a Module to a same (or different) LLVMContext:<br>
    <br>
            if (&context == &other.myContext)<br>
            {<br>
                // If the context is shared, we can use cloning<br>
                ValueToValueMapTy   map;<br>
                myModule = CloneModule(other.myModule, map);<br>
            }<br>
            else<br>
            {<br>
                // Otherwise, round trip the module to a stream and then
    back<br>
                // into the new context.  This approach allows for
    duplication<br>
                // and optimization to proceed in parallel for different<br>
                // modules.<br>
                std::string         str;<br>
                raw_string_ostream  stream(str);<br>
                WriteBitcodeToFile(other.myModule, stream);<br>
    <br>
                StringRef                   ref(stream.str());<br>
                UT_ScopedPtr<MemoryBuffer> 
    buf(MemoryBuffer::getMemBuffer(ref));<br>
                myModule = ParseBitcodeFile(buf.get(), myContext);<br>
    <br>
                UT_ASSERT(myModule);<br>
            }<br>
    <br>
    <br>
    On 06/18/2013 01:29 PM, Reid Kleckner wrote:
    <blockquote
cite="mid:CACs=ty+ot6actnh944Z0Ft1w0CMWWRNzS+N4X+Q7B9b=GQX-PA@mail.gmail.com"
      type="cite">
      <div dir="ltr">You could probably round trip it through bitcode in
        memory.  I think all of the IR cloning functionality assumes
        that only one context is being used.  Even if the serialization
        isn't efficient as a clone could be, it should give you very
        high confidence that everything Just Works.  :)</div>
      <div class="gmail_extra"><br>
        <br>
        <div class="gmail_quote">On Tue, Jun 18, 2013 at 1:16 PM, Andrew
          Clinton <span dir="ltr"><<a moz-do-not-send="true"
              href="mailto:andrew@sidefx.com" target="_blank">andrew@sidefx.com</a>></span>
          wrote:<br>
          <blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
            0.8ex; border-left: 1px solid rgb(204, 204, 204);
            padding-left: 1ex;">
            I have a Module/LLVMContext that I'd like to clone and
            manipulate in different threads (each thread may perform
            different translation / optimization, so they need unique
            copies).  Currently this process has to be locked, since
            each clone of the Module still refers to the same
            LLVMContext.  Is there a way to clone both the Module and
            LLVMContext so that the copies can be manipulated
            independently in different threads?<br>
            <br>
            Andrew<br>
            _______________________________________________<br>
            LLVM Developers mailing list<br>
            <a moz-do-not-send="true" href="mailto:LLVMdev@cs.uiuc.edu"
              target="_blank">LLVMdev@cs.uiuc.edu</a>         <a
              moz-do-not-send="true" href="http://llvm.cs.uiuc.edu"
              target="_blank">http://llvm.cs.uiuc.edu</a><br>
            <a moz-do-not-send="true"
              href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev"
              target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
          </blockquote>
        </div>
        <br>
      </div>
    </blockquote>
    <br>
  </body>
</html>