[PATCH] D131313: [Support] Use constexpr if

Jun Zhang via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 6 01:13:31 PDT 2022


junaire created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
junaire requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

After D130689 <https://reviews.llvm.org/D130689>, we're allowed to use C++17 features. This patches replaces
the old fashion macros to C++17 constexpr if, and turn some
runtime branches to compile time.

Signed-off-by: Jun Zhang <jun at junz.org>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D131313

Files:
  llvm/include/llvm/Support/Mutex.h
  llvm/include/llvm/Support/Threading.h
  llvm/lib/Support/Threading.cpp


Index: llvm/lib/Support/Threading.cpp
===================================================================
--- llvm/lib/Support/Threading.cpp
+++ llvm/lib/Support/Threading.cpp
@@ -28,12 +28,10 @@
 //===          independent code.
 //===----------------------------------------------------------------------===//
 
-bool llvm::llvm_is_multithreaded() {
-#if LLVM_ENABLE_THREADS != 0
-  return true;
-#else
+constexpr bool llvm::llvm_is_multithreaded() {
+  if constexpr (LLVM_ENABLE_THREADS)
+    return true;
   return false;
-#endif
 }
 
 #if LLVM_ENABLE_THREADS == 0 ||                                                \
Index: llvm/include/llvm/Support/Threading.h
===================================================================
--- llvm/include/llvm/Support/Threading.h
+++ llvm/include/llvm/Support/Threading.h
@@ -50,7 +50,7 @@
 
 /// Returns true if LLVM is compiled with support for multi-threading, and
 /// false otherwise.
-bool llvm_is_multithreaded();
+constexpr bool llvm_is_multithreaded();
 
 #if LLVM_THREADING_USE_STD_CALL_ONCE
 
Index: llvm/include/llvm/Support/Mutex.h
===================================================================
--- llvm/include/llvm/Support/Mutex.h
+++ llvm/include/llvm/Support/Mutex.h
@@ -31,36 +31,34 @@
 
     public:
       bool lock() {
-        if (!mt_only || llvm_is_multithreaded()) {
+        if constexpr (!mt_only || llvm_is_multithreaded()) {
           impl.lock();
           return true;
-        } else {
+        }
           // Single-threaded debugging code.  This would be racy in
           // multithreaded mode, but provides not basic checks in single
           // threaded mode.
           ++acquired;
           return true;
-        }
       }
 
       bool unlock() {
-        if (!mt_only || llvm_is_multithreaded()) {
+        if constexpr (!mt_only || llvm_is_multithreaded()) {
           impl.unlock();
           return true;
-        } else {
+        }
           // Single-threaded debugging code.  This would be racy in
           // multithreaded mode, but provides not basic checks in single
           // threaded mode.
           assert(acquired && "Lock not acquired before release!");
           --acquired;
           return true;
-        }
       }
 
       bool try_lock() {
-        if (!mt_only || llvm_is_multithreaded())
+        if constexpr (!mt_only || llvm_is_multithreaded())
           return impl.try_lock();
-        else return true;
+        return true;
       }
     };
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D131313.450490.patch
Type: text/x-patch
Size: 2486 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220806/3225ac87/attachment.bin>


More information about the llvm-commits mailing list