<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 2, 2017, at 10:58 AM, Rong Xu <<a href="mailto:xur@google.com" class="">xur@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><br class=""><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Sep 29, 2017 at 4:32 PM, Vedant Kumar via Phabricator <span dir="ltr" class=""><<a href="mailto:reviews@reviews.llvm.org" target="_blank" class="">reviews@reviews.llvm.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">vsk added inline comments.<br class="">
<br class="">
<br class="">
================<br class="">
Comment at: lib/ProfileData/<wbr class="">InstrProfReader.cpp:736<br class="">
 Error IndexedInstrProfReader::<wbr class="">readNextRecord(<wbr class="">NamedInstrProfRecord &Record) {<br class="">
-  static unsigned RecordIndex = 0;<br class="">
+  thread_local static unsigned RecordIndex = 0;<br class="">
<br class="">
----------------<br class="">
Why isn't this just a member variable?<br class="">
<br class="">
If we used "thread_local", wouldn't there still be a bug if you instantiated and used two IndexedInstrProfReaders?<br class=""></blockquote><div class=""><br class=""></div><div class="">I think making this member variable would also work -- and this might be a better way.</div></div></div></div></div></blockquote><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">But why you think "thread_local" won't work? IndexedInstrProfReader::readNextRecord can not be multithreaded in either way. If we instantiate two IndexedInstrProfReaders, each should be in it own thread and accesses its own RecordIndex. There is not racing.</div></div></div></div></div></blockquote><div><br class=""></div><div>I was concerned about the case where two IndexedInstrProfReaders are used by the same thread. Or two IndexedInstrProfReaders are created, but are used intermittently by two different threads. Admittedly we're not doing these things now, but because the API permits this type of usage, it needs to work properly.</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br class="">
<br class="">
================<br class="">
Comment at: tools/llvm-profdata/llvm-<wbr class="">profdata.cpp:230<br class="">
     // Load the inputs in parallel (N/NumThreads serial steps).<br class="">
-    unsigned Ctx = 0;<br class="">
-    for (const auto &Input : Inputs) {<br class="">
-      Pool.async(loadInput, Input, Contexts[Ctx].get());<br class="">
-      Ctx = (Ctx + 1) % NumThreads;<br class="">
+    unsigned ChunkSize = Inputs.size() / NumThreads;<br class="">
+    unsigned ChunkRemainder = Inputs.size() % NumThreads;<br class="">
----------------<br class="">
I don't understand the problem here. Each time a WriterContext is accessed, the accessing thread immediately acquires a unique lock on the context. Where does the racing access occur?<br class=""></blockquote><div class=""><br class=""></div><div class="">Where the lock to WriterContext comes from? I only see the lock for the queue in ThreadPool. I thought it was the user's responsibility to make sure the share data is race free.</div></div></div></div></div></blockquote><span class=""><div><span class=""><br class=""></span></div>Sorry, I see now that locking the WriterContext isn't relevant to this problem.</span></div><div><br class=""></div><div>I think I understand where you're coming from now. Your solution would get rid of the race, but I don't think users of IndexedInstrProfReader should have to take care to only use one instance per thread. If we can make "RecordIndex" a member variable, we wouldn't have to change how the API is used, so I'd prefer that solution.</div><div><br class=""></div><div>vedant</div><div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br class="">
<br class="">
<a href="https://reviews.llvm.org/D38431" rel="noreferrer" target="_blank" class="">https://reviews.llvm.org/<wbr class="">D38431</a><br class="">
<br class="">
<br class="">
<br class="">
</blockquote></div><br class=""></div></div>
</div></blockquote></div><br class=""></body></html>