<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Oct 2, 2017 at 11:20 AM, Vedant Kumar <span dir="ltr"><<a href="mailto:vsk@apple.com" target="_blank">vsk@apple.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On Oct 2, 2017, at 10:58 AM, Rong Xu <<a href="mailto:xur@google.com" target="_blank">xur@google.com</a>> wrote:</div><br class="m_-2794571362470032571Apple-interchange-newline"><div><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 29, 2017 at 4:32 PM, Vedant Kumar via Phabricator <span dir="ltr"><<a href="mailto:reviews@reviews.llvm.org" target="_blank">reviews@reviews.llvm.org</a>></span> wrote:<br><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>
<br>
<br>
================<br>
Comment at: lib/ProfileData/InstrProfReade<wbr>r.cpp:736<br>
 Error IndexedInstrProfReader::readNe<wbr>xtRecord(NamedInstrProfRecord &Record) {<br>
-  static unsigned RecordIndex = 0;<br>
+  thread_local static unsigned RecordIndex = 0;<br>
<br>
----------------<br>
Why isn't this just a member variable?<br>
<br>
If we used "thread_local", wouldn't there still be a bug if you instantiated and used two IndexedInstrProfReaders?<br></blockquote><div><br></div><div>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"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>But why you think "thread_local" won't work? IndexedInstrProfReader::<wbr>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></div></span><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><span class=""><br></span></div></div></blockquote><div>Yes. If two Readers are created by the same thread, then 'thread_local" will break. But at the same time, if two threads are access the one Reader, member variable solution will break too. API does not mention any of the permitted usage as of now. Maybe we should document it. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><span class=""><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<br>
================<br>
Comment at: tools/llvm-profdata/llvm-profd<wbr>ata.cpp:230<br>
     // Load the inputs in parallel (N/NumThreads serial steps).<br>
-    unsigned Ctx = 0;<br>
-    for (const auto &Input : Inputs) {<br>
-      Pool.async(loadInput, Input, Contexts[Ctx].get());<br>
-      Ctx = (Ctx + 1) % NumThreads;<br>
+    unsigned ChunkSize = Inputs.size() / NumThreads;<br>
+    unsigned ChunkRemainder = Inputs.size() % NumThreads;<br>
----------------<br>
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></blockquote><div><br></div><div>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><span><div><span><br></span></div>Sorry, I see now that locking the WriterContext isn't relevant to this problem.</span></div><div><br></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></blockquote><div><br></div><div>Sorry I don't follow here. I was talking the race to WriterContext. It was not clear to me that there is lock to WriterContext. Can you explain if there is one?</div><div><br></div><div>Thanks,</div><div><br></div><div>-Rong</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>vedant</div><div><br><blockquote type="cite"><div><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<br>
<a href="https://reviews.llvm.org/D38431" rel="noreferrer" target="_blank">https://reviews.llvm.org/D3843<wbr>1</a><br>
<br>
<br>
<br>
</blockquote></div><br></div></div>
</div></blockquote></div><br></div></blockquote></div><br></div></div>