[PATCH] D106206: Lazy initialized the NotUnderValgrind flag

Mehdi AMINI via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 16 18:21:48 PDT 2021


mehdi_amini created this revision.
mehdi_amini added a reviewer: MaskRay.
Herald added subscribers: dexonsmith, hiraditya.
mehdi_amini requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

In an attempt to remove global constructor from libSupport,
this is moving the initialization of this flag to the first
use.

The overall pattern here is obscure to me though, I implemented
the minimal changes but the reason for this flag to exist in the
first place isn't clear to me.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D106206

Files:
  llvm/lib/Support/Valgrind.cpp


Index: llvm/lib/Support/Valgrind.cpp
===================================================================
--- llvm/lib/Support/Valgrind.cpp
+++ llvm/lib/Support/Valgrind.cpp
@@ -14,29 +14,31 @@
 
 #include "llvm/Support/Valgrind.h"
 #include "llvm/Config/config.h"
+#include "llvm/Support/ManagedStatic.h"
 #include <cstddef>
 
 #if HAVE_VALGRIND_VALGRIND_H
 #include <valgrind/valgrind.h>
 
-static bool InitNotUnderValgrind() {
-  return !RUNNING_ON_VALGRIND;
-}
-
 // This bool is negated from what we'd expect because code may run before it
 // gets initialized.  If that happens, it will appear to be 0 (false), and we
 // want that to cause the rest of the code in this file to run the
 // Valgrind-provided macros.
-static const bool NotUnderValgrind = InitNotUnderValgrind();
+namespace {
+struct CreateNotUnderValgrind {
+  static void *call() { return new bool{!RUNNING_ON_VALGRIND}; }
+};
+} // namespace
+static const llvm::ManagedStatic<bool, CreateNotUnderValgrind> NotUnderValgrind;
 
 bool llvm::sys::RunningOnValgrind() {
-  if (NotUnderValgrind)
+  if (*NotUnderValgrind)
     return false;
   return RUNNING_ON_VALGRIND;
 }
 
 void llvm::sys::ValgrindDiscardTranslations(const void *Addr, size_t Len) {
-  if (NotUnderValgrind)
+  if (*NotUnderValgrind)
     return;
 
   VALGRIND_DISCARD_TRANSLATIONS(Addr, Len);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106206.359510.patch
Type: text/x-patch
Size: 1332 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210717/bab049d8/attachment.bin>


More information about the llvm-commits mailing list