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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Mon Sep 15 09:03:19 PDT 2025


================
@@ -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
----------------
ldionne wrote:

Let's say we had this bug fixed and the code went back to looking like:

```c++
vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
    pointer __end = this->__end_;
    if (__end < this->__cap_) [[likely]] {
        __emplace_back_assume_capacity(std::forward<_Args>(__args)...);
        ++__end;
    } else {
        __end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
    }
    this->__end_ = __end;

    ...
}
```

Then we could evolve this into something like:

```c++
vector<_Tp, _Allocator>::emplace_back(_Args&&... __args) {
    pointer __end = this->__end_;
    if (__end < this->__cap_) _LIBCPP_LIKELY_UNLESS_COMPILER_KNOWS_BETTER {
        __emplace_back_assume_capacity(std::forward<_Args>(__args)...);
        ++__end;
    } else {
        __end = __emplace_back_slow_path(std::forward<_Args>(__args)...);
    }
    this->__end_ = __end;

    ...
}
```

`_LIBCPP_LIKELY_UNLESS_COMPILER_KNOWS_BETTER` would basically be a likelihood annotation that is a no-op when the compiler knows better, such as when we're being compiled with PGO. That would probably resolve @nico 's comments from https://github.com/llvm/llvm-project/pull/94379#issuecomment-3292065900

Actually, this comment applies even if we keep our ugly workaround for the bug. The `likely` annotation can still be conditionalized on whether e.g. PGO is enabled. All that we'd need is a way to tell whether PGO is enabled.

https://github.com/llvm/llvm-project/pull/158606


More information about the libcxx-commits mailing list