Skip to content

Conversation

kazutakahirata
Copy link
Contributor

In C++17, we can initialize a static member variable with
"inline static" as part of the class definition. With this, we can
eliminate the out-of-line static initializers that are a bit hard to
decipher.

In C++17, we can initialize a static member variable with
"inline static" as part of the class definition.  With this, we can
eliminate the out-of-line static initializers that are a bit hard to
decipher.
@llvmbot
Copy link
Member

llvmbot commented Sep 21, 2025

@llvm/pr-subscribers-llvm-adt

Author: Kazu Hirata (kazutakahirata)

Changes

In C++17, we can initialize a static member variable with
"inline static" as part of the class definition. With this, we can
eliminate the out-of-line static initializers that are a bit hard to
decipher.


Full diff: https://github.com/llvm/llvm-project/pull/160003.diff

1 Files Affected:

  • (modified) llvm/include/llvm/ADT/FunctionExtras.h (+3-16)
diff --git a/llvm/include/llvm/ADT/FunctionExtras.h b/llvm/include/llvm/ADT/FunctionExtras.h
index e45d507bfd4bf..1311452a17bb3 100644
--- a/llvm/include/llvm/ADT/FunctionExtras.h
+++ b/llvm/include/llvm/ADT/FunctionExtras.h
@@ -231,7 +231,6 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
 
   // The pointers to call/move/destroy functions are determined for each
   // callable type (and called-as type, which determines the overload chosen).
-  // (definitions are out-of-line).
 
   // By default, we need an object that contains all the different
   // type erased behaviors needed. Create a static instance of the struct type
@@ -239,14 +238,15 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
   // Wrap in a struct to avoid https://gcc.gnu.org/PR71954
   template <typename CallableT, typename CalledAs, typename Enable = void>
   struct CallbacksHolder {
-    static NonTrivialCallbacks Callbacks;
+    inline static NonTrivialCallbacks Callbacks = {
+        &CallImpl<CalledAs>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
   };
   // See if we can create a trivial callback. We need the callable to be
   // trivially moved and trivially destroyed so that we don't have to store
   // type erased callbacks for those operations.
   template <typename CallableT, typename CalledAs>
   struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> {
-    static TrivialCallback Callbacks;
+    inline static TrivialCallback Callbacks = {&CallImpl<CalledAs>};
   };
 
   // A simple tag type so the call-as type to be passed to the constructor.
@@ -344,19 +344,6 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
   }
 };
 
-template <typename R, typename... P>
-template <typename CallableT, typename CalledAsT, typename Enable>
-typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase<
-    R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
-    &CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
-
-template <typename R, typename... P>
-template <typename CallableT, typename CalledAsT>
-typename UniqueFunctionBase<R, P...>::TrivialCallback
-    UniqueFunctionBase<R, P...>::CallbacksHolder<
-        CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
-        &CallImpl<CalledAsT>};
-
 } // namespace detail
 
 template <typename R, typename... P>

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this affect the number of relocations?

@kazutakahirata
Copy link
Contributor Author

Does this affect the number of relocations?

No, I checked on lib/ExecutionEngine/Orc/CMakeFiles/LLVMOrcJIT.dir/Core.cpp.o.

The text, data, and bss sizes stay the same although the actual executable magically shrinks by a few hundred bytes.

@kazutakahirata kazutakahirata merged commit 591e60b into llvm:main Sep 22, 2025
11 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20250921_10TMP_ADT_FunctionExtras_CallbacksHolder branch September 22, 2025 02:17
SeongjaeP pushed a commit to SeongjaeP/llvm-project that referenced this pull request Sep 23, 2025
…60003)

In C++17, we can initialize a static member variable with
"inline static" as part of the class definition.  With this, we can
eliminate the out-of-line static initializers that are a bit hard to
decipher.
YixingZhang007 pushed a commit to YixingZhang007/llvm-project that referenced this pull request Sep 27, 2025
…60003)

In C++17, we can initialize a static member variable with
"inline static" as part of the class definition.  With this, we can
eliminate the out-of-line static initializers that are a bit hard to
decipher.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants