<div dir="ltr">+dvyukov<div><br></div><div>These annotations were needed when Nick was testing llvm-as-a-library in multi-threaded environment under the old valgrind-based tsan.</div><div>Now we have our new and shiny LLVM-based tsan, which understands atomics and c++11. </div>
<div>The next time we touch this code I suggest to rewrite the whole thing in c++11 (e.g. using std::call_once?) and get rid of the annotations. </div><div>BTW, do we have any multi-threaded tests for this code? </div><div>
<br></div><div>--kcc </div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Mar 31, 2014 at 12:22 AM, Nick Lewycky <span dir="ltr"><<a href="mailto:nicholas@mxc.ca" target="_blank">nicholas@mxc.ca</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="">Chandler Carruth wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Author: chandlerc<br>
Date: Sun Mar 30 06:20:25 2014<br>
New Revision: 205137<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=205137&view=rev" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project?rev=205137&view=rev</a><br>
Log:<br>
Don't mark the declarations of the TSan annotation functions as weak.<br>
That causes references to them to be weak references which can collapse<br>
to null if no definition is provided.<br>
</blockquote>
<br></div>
Whoops! I didn't realize that attribute weak on a declaration meant something different from attribute weak on a definition. Thanks for the fix!<br>
<br>
Nick<br>
<br>
 We call these functions<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
unconditionally, so a definition *must* be provided. Make the<br>
definitions provided in the .cpp file weak by re-declaring them as weak<br>
just prior to defining them. This should keep compilers which cannot<br>
attach the weak attribute to the definition happy while actually<br>
resolving the symbols correctly during the link.<br>
<br>
You might ask yourself upon reading this commit log: how did *any* of<br>
this work before? Well, fun story. It turns out we have some code in<br>
Support (BumpPtrAllocator) which both uses virtual dispatch and has<br>
out-of-line vtables used by that virtual dispatch. If you move the<br>
virtual dispatch into its header in *just* the right way, the optimizer<br>
gets to devirtualize, and remove all references to the vtable. Then the<br>
sad part: the references to this one vtable were the only strong symbol<br>
uses in the support library for llvm-tblgen AFAICT. At least, after<br>
doing something just like this, these symbols stopped getting their weak<br>
definition and random calls to them would segfault instead.<br>
<br>
Yay software.<br>
<br>
Modified:<br>
     llvm/trunk/include/llvm/<u></u>Support/Valgrind.h<br>
     llvm/trunk/lib/Support/<u></u>Valgrind.cpp<br>
<br>
Modified: llvm/trunk/include/llvm/<u></u>Support/Valgrind.h<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/Valgrind.h?rev=205137&r1=205136&r2=205137&view=diff" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/llvm/trunk/include/<u></u>llvm/Support/Valgrind.h?rev=<u></u>205137&r1=205136&r2=205137&<u></u>view=diff</a><br>

==============================<u></u>==============================<u></u>==================<br>
--- llvm/trunk/include/llvm/<u></u>Support/Valgrind.h (original)<br>
+++ llvm/trunk/include/llvm/<u></u>Support/Valgrind.h Sun Mar 30 06:20:25 2014<br>
@@ -24,12 +24,10 @@<br>
  // tsan (Thread Sanitizer) is a valgrind-based tool that detects these exact<br>
  // functions by name.<br>
  extern "C" {<br>
-LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,<br>
-                                              const volatile void *cv);<br>
-LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,<br>
-                                               const volatile void *cv);<br>
-LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(<u></u>const char *file, int line);<br>
-LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);<br>
+void AnnotateHappensAfter(const char *file, int line, const volatile void *cv);<br>
+void AnnotateHappensBefore(const char *file, int line, const volatile void *cv);<br>
+void AnnotateIgnoreWritesBegin(<u></u>const char *file, int line);<br>
+void AnnotateIgnoreWritesEnd(const char *file, int line);<br>
  }<br>
  #endif<br>
<br>
<br>
Modified: llvm/trunk/lib/Support/<u></u>Valgrind.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Valgrind.cpp?rev=205137&r1=205136&r2=205137&view=diff" target="_blank">http://llvm.org/viewvc/llvm-<u></u>project/llvm/trunk/lib/<u></u>Support/Valgrind.cpp?rev=<u></u>205137&r1=205136&r2=205137&<u></u>view=diff</a><br>

==============================<u></u>==============================<u></u>==================<br>
--- llvm/trunk/lib/Support/<u></u>Valgrind.cpp (original)<br>
+++ llvm/trunk/lib/Support/<u></u>Valgrind.cpp Sun Mar 30 06:20:25 2014<br>
@@ -55,13 +55,21 @@ void llvm::sys::<u></u>ValgrindDiscardTranslati<br>
<br></div></div>
  #if LLVM_ENABLE_THREADS != 0&&  !defined(NDEBUG)<div><div class="h5"><br>
  // These functions require no implementation, tsan just looks at the arguments<br>
-// they're called with.<br>
+// they're called with. However, they are required to be weak as some other<br>
+// application or library may already be providing these definitions for the<br>
+// same reason we are.<br>
  extern "C" {<br>
+LLVM_ATTRIBUTE_WEAK void AnnotateHappensAfter(const char *file, int line,<br>
+                                              const volatile void *cv);<br>
+void AnnotateHappensAfter(const char *file, int line, const volatile void *cv) {<br>
+}<br>
+LLVM_ATTRIBUTE_WEAK void AnnotateHappensBefore(const char *file, int line,<br>
+                                               const volatile void *cv);<br>
  void AnnotateHappensBefore(const char *file, int line,<br>
                             const volatile void *cv) {}<br>
-void AnnotateHappensAfter(const char *file, int line,<br>
-                          const volatile void *cv) {}<br>
+LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesBegin(<u></u>const char *file, int line);<br>
  void AnnotateIgnoreWritesBegin(<u></u>const char *file, int line) {}<br>
+LLVM_ATTRIBUTE_WEAK void AnnotateIgnoreWritesEnd(const char *file, int line);<br>
  void AnnotateIgnoreWritesEnd(const char *file, int line) {}<br>
  }<br>
  #endif<br>
<br>
<br>
______________________________<u></u>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvm-commits</a><br>
<br>
</div></div></blockquote><div class="HOEnZb"><div class="h5">
<br>
______________________________<u></u>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@cs.uiuc.edu" target="_blank">llvm-commits@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits" target="_blank">http://lists.cs.uiuc.edu/<u></u>mailman/listinfo/llvm-commits</a><br>
</div></div></blockquote></div><br></div>