<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="">Hi,<div class=""><br class=""></div><div class="">Thanks for the answers.</div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Apr 18, 2016, at 10:38 AM, Kostya Serebryany <<a href="mailto:kcc@google.com" class="">kcc@google.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">+dvyukov<div class=""><br class=""></div><div class=""><br class=""></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Fri, Apr 15, 2016 at 10:24 PM, Mehdi Amini via llvm-dev<span class="Apple-converted-space"> </span><span dir="ltr" class=""><<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>></span><span class="Apple-converted-space"> </span>wrote:<br class=""><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;"><div style="word-wrap: break-word;" class="">Hello,<div class=""><br class=""></div><div class="">I trying TSAN on Darwin on LLVM itself (sanitizing multi-threaded ThinLTO link).</div><div class="">However I see two main issues on my debug build: </div><div class=""><br class=""></div><div class="">1) Statistics: the pre/post increment is not safe, it seems to be acknowledge in the code itself:</div><div class=""><br class=""></div><div class=""><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 143, 0);" class=""><span style="" class="">    </span><span class="">// FIXME: This function and all those that follow carefully use an</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 143, 0);" class=""><span style="" class="">   <span class="Apple-converted-space"> </span></span><span class="">// atomic operation to update the value safely in the presence of</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 143, 0);" class=""><span style="" class="">   <span class="Apple-converted-space"> </span></span><span class="">// concurrent accesses, but not to read the return value, so the</span></div><div style="margin: 0px; font-size: 11px; line-height: normal; font-family: menlo; color: rgb(0, 143, 0);" class=""><span style="" class="">   <span class="Apple-converted-space"> </span></span><span class="">// return value is not thread safe.</span></div></div><div class=""><span class=""><br class=""></span></div><div class=""><span class="">Can we tell TSAN to ignore the statistics somehow? Alternatively, is there a fix someone can suggest?</span></div></div></blockquote><div class=""><br class=""></div><div class="">It's better to add proper synchronization, most likely an atomic read with memory_order_relaxed is enough. </div></div></div></div></div></blockquote><div><br class=""></div><div>Ok, will look into this!</div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" 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-width: 1px; border-left-style: solid; border-left-color: rgb(204, 204, 204); padding-left: 1ex;"><div style="word-wrap: break-word;" class=""><div class=""><span class=""><br class=""></span></div><div class="">2) Pass initialization. This macro does not please TSAN (even though it seems written with TSAN in mind):</div><div class=""><br class=""></div><div class="">#define CALL_ONCE_INITIALIZATION(function) \<br class=""> <span class="Apple-converted-space"> </span>static volatile sys::cas_flag initialized = 0; \<br class=""> <span class="Apple-converted-space"> </span>sys::cas_flag old_val = sys::CompareAndSwap(&initialized, 1, 0); \<br class=""> <span class="Apple-converted-space"> </span>if (old_val == 0) { \<br class="">   <span class="Apple-converted-space"> </span>function(Registry); \<br class="">   <span class="Apple-converted-space"> </span>sys::MemoryFence(); \<br class="">   <span class="Apple-converted-space"> </span>TsanIgnoreWritesBegin(); \<br class="">   <span class="Apple-converted-space"> </span>TsanHappensBefore(&initialized); \<br class="">   <span class="Apple-converted-space"> </span>initialized = 2; \<br class="">   <span class="Apple-converted-space"> </span>TsanIgnoreWritesEnd(); \<br class=""> <span class="Apple-converted-space"> </span>} else { \<br class="">   <span class="Apple-converted-space"> </span>sys::cas_flag tmp = initialized; \<br class="">   <span class="Apple-converted-space"> </span>sys::MemoryFence(); \<br class="">   <span class="Apple-converted-space"> </span>while (tmp != 2) { \<br class="">     <span class="Apple-converted-space"> </span>tmp = initialized; \<br class="">     <span class="Apple-converted-space"> </span>sys::MemoryFence(); \<br class="">   <span class="Apple-converted-space"> </span>} \<br class=""> <span class="Apple-converted-space"> </span>} \<br class=""> <span class="Apple-converted-space"> </span>TsanHappensAfter(&initialized);</div></div></blockquote><div class=""><br class=""></div><div class=""><br class=""></div><div class="">This code might have been added to please the old valgrind-based tsan (which did not understand atomics). <br class=""></div><div class="">For the current tsan we should just implement proper synchronization and remove the annotations. </div><div class="">Why can't we use std::call_once here? </div></div></div></div></div></blockquote><div><br class=""></div><div>Maybe some pre c++11 thing? </div><div>I'll try to upgrade std::call_once.</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">Looks like you are serious about running llvm under tsan. Very cool! </div><div class="">Does it make sense to add a tsan build to our<span class="Apple-converted-space"> </span><a href="http://lab.llvm.org:8011/builders/sanitizer-x86_64-linux-fast" class="">sanitizer bot</a><span class="Apple-converted-space"> </span>(which already has asan, ubsan, lsan, and msan)? </div><div class="">What tests and what build mode would you suggest for the bot? </div></div></div></div></div></blockquote><div><br class=""></div><div>I plan on adding a TSan bot on Green Dragon in the near future.</div><div>My idea is to perform a ThinLTO bootstrap of clang (i.e. stage 1 being a Release+Assert+TSAN and stage2 Release-ThinLTO). This involves running the optimizer and the codegen in multiple threads.</div><div><br class=""></div><div><br class=""></div><div>-- </div><div>Mehdi</div><div><br class=""></div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div dir="ltr" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=""><div class="gmail_extra"><div class="gmail_quote"><div class=""><br class=""></div><div class="">--kcc </div><div class=""> </div><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;"><div style="word-wrap: break-word;" class=""><div class=""><br class=""></div><div class="">The failure looks like:</div><div class=""><br class=""></div><div class="">==================<br class="">WARNING: ThreadSanitizer: data race (pid=17192)<br class=""> <span class="Apple-converted-space"> </span>Atomic write of size 4 at 0x0001113619e8 by thread T13:<br class="">   <span class="Apple-converted-space"> </span>#0 __tsan_atomic32_compare_exchange_val <null>:568340296 (libclang_rt.tsan_osx_dynamic.dylib+0x00000003e7aa)<br class="">   <span class="Apple-converted-space"> </span>#1 llvm::sys::CompareAndSwap(unsigned int volatile*, unsigned int, unsigned int) Atomic.cpp:52 (libLTO.dylib+0x000000511914)<br class="">   <span class="Apple-converted-space"> </span>#2 llvm::initializeSimpleInlinerPass(llvm::PassRegistry&) InlineSimple.cpp:82 (libLTO.dylib+0x000000ab2b55)<br class="">   <span class="Apple-converted-space"> </span>#3 (anonymous namespace)::SimpleInliner::SimpleInliner() InlineSimple.cpp:50 (libLTO.dylib+0x000000ab2e8e)<br class="">   <span class="Apple-converted-space"> </span>#4 (anonymous namespace)::SimpleInliner::SimpleInliner() InlineSimple.cpp:49 (libLTO.dylib+0x000000ab2d19)<br class="">   <span class="Apple-converted-space"> </span>#5 llvm::createFunctionInliningPass() InlineSimple.cpp:85 (libLTO.dylib+0x000000ab2ce3)</div><div class="">...</div><div class=""><br class=""> <span class="Apple-converted-space"> </span>Previous read of size 4 at 0x0001113619e8 by thread T14:<br class="">   <span class="Apple-converted-space"> </span>#0 llvm::initializeSimpleInlinerPass(llvm::PassRegistry&) InlineSimple.cpp:82 (libLTO.dylib+0x000000ab2b65)<br class="">   <span class="Apple-converted-space"> </span>#1 (anonymous namespace)::SimpleInliner::SimpleInliner() InlineSimple.cpp:50 (libLTO.dylib+0x000000ab2e8e)<br class="">   <span class="Apple-converted-space"> </span>#2 (anonymous namespace)::SimpleInliner::SimpleInliner() InlineSimple.cpp:49 (libLTO.dylib+0x000000ab2d19)<br class="">   <span class="Apple-converted-space"> </span>#3 llvm::createFunctionInliningPass() InlineSimple.cpp:85 (libLTO.dylib+0x000000ab2ce3)<br class="">...</div><div class="">  Location is global 'llvm::initializeSimpleInlinerPass(llvm::PassRegistry&)::initialized' at 0x0001113619e8 (libLTO.dylib+0x0000016389e8)<br class=""><br class=""></div><div class=""><br class=""></div><div class="">Thanks!</div><span class="gmail-HOEnZb"><font color="#888888" class=""><div class=""><br class=""></div><div class="">Mehdi</div><div class=""><br class=""></div><div class=""><br class=""></div></font></span></div><br class="">_______________________________________________<br class="">LLVM Developers mailing list<br class=""><a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a><br class=""><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" class="">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></blockquote></div></div></div></div></blockquote></div><br class=""></div></body></html>