<div dir="ltr">On 5 November 2013 08:40, Andrew Clinton <span dir="ltr"><<a href="mailto:andrew@sidefx.com" target="_blank">andrew@sidefx.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<u></u>
<div 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></div></blockquote><div><br></div><div>If you're interested, I think we shouldn't make this lazy at all, I don't think it buys us anything. I once filed <a href="http://llvm.org/PR16536">llvm.org/PR16536</a> with what I think we should do, though it's a fundamental change that is may be more work than you want to take on.</div>
<div><br></div><div>Nick</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div bgcolor="#ffffff" text="#000000">
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);<div><div class="h5"><br>
}<br>
<br>
<br>
On 06/18/2013 01:29 PM, Reid Kleckner wrote:
<blockquote 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 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-width:1px;border-left-style:solid;border-left-color: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 href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a 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>
</div></div></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div></div>