[libcxx-commits] [libcxx] 76a11c7 - Reapply "[libc++] Mark __{emplace, push}_back_slow_path as noinline (#94379)" (#158606)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Sep 17 02:10:11 PDT 2025


Author: Nikolas Klauser
Date: 2025-09-17T11:10:06+02:00
New Revision: 76a11c769d3cc2c1d986e86f58d9fb25fe9830c6

URL: https://github.com/llvm/llvm-project/commit/76a11c769d3cc2c1d986e86f58d9fb25fe9830c6
DIFF: https://github.com/llvm/llvm-project/commit/76a11c769d3cc2c1d986e86f58d9fb25fe9830c6.diff

LOG: Reapply "[libc++] Mark __{emplace,push}_back_slow_path as noinline (#94379)" (#158606)

This reverts commit 7f2e9b17098f42c85ef469b029bb84ef4eea189c.

The LLDB failures have been resolved in trunk.

Added: 
    

Modified: 
    libcxx/include/__vector/vector.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__vector/vector.h b/libcxx/include/__vector/vector.h
index 5e6572b1a82c4..27e681aeef22a 100644
--- a/libcxx/include/__vector/vector.h
+++ b/libcxx/include/__vector/vector.h
@@ -1161,6 +1161,24 @@ vector<_Tp, _Allocator>::__emplace_back_slow_path(_Args&&... __args) {
   return this->__end_;
 }
 
+// This makes the compiler inline `__else()` if `__cond` is known to be false. Currently LLVM doesn't do that without
+// the `__builtin_constant_p`, since it considers `__else` unlikely even through it's known to be run.
+// See https://llvm.org/PR154292
+template <class _If, class _Else>
+_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __if_likely_else(bool __cond, _If __if, _Else __else) {
+  if (__builtin_constant_p(__cond)) {
+    if (__cond)
+      __if();
+    else
+      __else();
+  } else {
+    if (__cond) [[__likely__]]
+      __if();
+    else
+      __else();
+  }
+}
+
 template <class _Tp, class _Allocator>
 template <class... _Args>
 _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
@@ -1171,12 +1189,14 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 inline
 #endif
     vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
   pointer __end = this->__end_;
-  if (__end < this->__cap_) {
-    __emplace_back_assume_capacity(std::forward<_Args>(__args)...);
-    ++__end;
-  } else {
-    __end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
-  }
+  std::__if_likely_else(
+      __end < this->__cap_,
+      [&] {
+        __emplace_back_assume_capacity(std::forward<_Args>(__args)...);
+        ++__end;
+      },
+      [&] { __end = __emplace_back_slow_path(std::forward<_Args>(__args)...); });
+
   this->__end_ = __end;
 #if _LIBCPP_STD_VER >= 17
   return *(__end - 1);


        


More information about the libcxx-commits mailing list