/home/arjun/llvm-project/llvm/include/llvm/Support/ManagedStatic.h
Line | Count | Source (jump to first uncovered line) |
1 | | //===-- llvm/Support/ManagedStatic.h - Static Global wrapper ----*- C++ -*-===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file defines the ManagedStatic class and the llvm_shutdown() function. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #ifndef LLVM_SUPPORT_MANAGEDSTATIC_H |
14 | | #define LLVM_SUPPORT_MANAGEDSTATIC_H |
15 | | |
16 | | #include <atomic> |
17 | | #include <cstddef> |
18 | | |
19 | | namespace llvm { |
20 | | |
21 | | /// object_creator - Helper method for ManagedStatic. |
22 | | template <class C> struct object_creator { |
23 | 8 | static void *call() { return new C(); } Unexecuted instantiation: _ZN4llvm14object_creatorISt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EEE4callEv _ZN4llvm14object_creatorINS_2cl10SubCommandEE4callEv Line | Count | Source | 23 | 4 | static void *call() { return new C(); } |
CommandLine.cpp:_ZN4llvm14object_creatorIN12_GLOBAL__N_117CommandLineParserEE4callEv Line | Count | Source | 23 | 2 | static void *call() { return new C(); } |
Unexecuted instantiation: Error.cpp:_ZN4llvm14object_creatorIN12_GLOBAL__N_118ErrorErrorCategoryEE4callEv Unexecuted instantiation: _ZN4llvm14object_creatorISt5mutexE4callEv Unexecuted instantiation: CrashRecoveryContext.cpp:_ZN4llvm14object_creatorINS_3sys11ThreadLocalIKN12_GLOBAL__N_124CrashRecoveryContextImplEEEE4callEv Unexecuted instantiation: _ZN4llvm14object_creatorINS_3sys11ThreadLocalIKNS_20CrashRecoveryContextEEEE4callEv _ZN4llvm14object_creatorINS_3sys10SmartMutexILb1EEEE4callEv Line | Count | Source | 23 | 2 | static void *call() { return new C(); } |
Unexecuted instantiation: Signals.cpp:_ZN4llvm14object_creatorIN12_GLOBAL__N_120FilesToRemoveCleanupEE4callEv Unexecuted instantiation: AsmPrinter.cpp:_ZN4llvm14object_creatorIN12_GLOBAL__N_117AsmPrinterOptionsEE4callEv Unexecuted instantiation: _ZN4llvm14object_creatorINS_9MapVectorIN4mlir6TypeIDESt8functionIFvPNS2_11MLIRContextEEENS_8DenseMapIS3_jNS_12DenseMapInfoIS3_EENS_6detail12DenseMapPairIS3_jEEEESt6vectorISt4pairIS3_S8_ESaISI_EEEEE4callEv Unexecuted instantiation: MLIRContext.cpp:_ZN4llvm14object_creatorIN12_GLOBAL__N_118MLIRContextOptionsEE4callEv |
24 | | }; |
25 | | |
26 | | /// object_deleter - Helper method for ManagedStatic. |
27 | | /// |
28 | | template <typename T> struct object_deleter { |
29 | 0 | static void call(void *Ptr) { delete (T *)Ptr; } Unexecuted instantiation: _ZN4llvm14object_deleterISt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EEE4callEPv Unexecuted instantiation: _ZN4llvm14object_deleterINS_2cl10SubCommandEE4callEPv Unexecuted instantiation: CommandLine.cpp:_ZN4llvm14object_deleterIN12_GLOBAL__N_117CommandLineParserEE4callEPv Unexecuted instantiation: Error.cpp:_ZN4llvm14object_deleterIN12_GLOBAL__N_118ErrorErrorCategoryEE4callEPv Unexecuted instantiation: _ZN4llvm14object_deleterISt5mutexE4callEPv Unexecuted instantiation: CrashRecoveryContext.cpp:_ZN4llvm14object_deleterINS_3sys11ThreadLocalIKN12_GLOBAL__N_124CrashRecoveryContextImplEEEE4callEPv Unexecuted instantiation: _ZN4llvm14object_deleterINS_3sys11ThreadLocalIKNS_20CrashRecoveryContextEEEE4callEPv Unexecuted instantiation: _ZN4llvm14object_deleterINS_3sys10SmartMutexILb1EEEE4callEPv Unexecuted instantiation: Signals.cpp:_ZN4llvm14object_deleterIN12_GLOBAL__N_120FilesToRemoveCleanupEE4callEPv Unexecuted instantiation: AsmPrinter.cpp:_ZN4llvm14object_deleterIN12_GLOBAL__N_117AsmPrinterOptionsEE4callEPv Unexecuted instantiation: _ZN4llvm14object_deleterINS_9MapVectorIN4mlir6TypeIDESt8functionIFvPNS2_11MLIRContextEEENS_8DenseMapIS3_jNS_12DenseMapInfoIS3_EENS_6detail12DenseMapPairIS3_jEEEESt6vectorISt4pairIS3_S8_ESaISI_EEEEE4callEPv Unexecuted instantiation: MLIRContext.cpp:_ZN4llvm14object_deleterIN12_GLOBAL__N_118MLIRContextOptionsEE4callEPv |
30 | | }; |
31 | | template <typename T, size_t N> struct object_deleter<T[N]> { |
32 | | static void call(void *Ptr) { delete[](T *)Ptr; } |
33 | | }; |
34 | | |
35 | | // ManagedStatic must be initialized to zero, and it must *not* have a dynamic |
36 | | // initializer because managed statics are often created while running other |
37 | | // dynamic initializers. In standard C++11, the best way to accomplish this is |
38 | | // with a constexpr default constructor. However, different versions of the |
39 | | // Visual C++ compiler have had bugs where, even though the constructor may be |
40 | | // constexpr, a dynamic initializer may be emitted depending on optimization |
41 | | // settings. For the affected versions of MSVC, use the old linker |
42 | | // initialization pattern of not providing a constructor and leaving the fields |
43 | | // uninitialized. See http://llvm.org/PR41367 for details. |
44 | | #if !defined(_MSC_VER) || (_MSC_VER >= 1925) || defined(__clang__) |
45 | | #define LLVM_USE_CONSTEXPR_CTOR |
46 | | #endif |
47 | | |
48 | | /// ManagedStaticBase - Common base class for ManagedStatic instances. |
49 | | class ManagedStaticBase { |
50 | | protected: |
51 | | #ifdef LLVM_USE_CONSTEXPR_CTOR |
52 | | mutable std::atomic<void *> Ptr{}; |
53 | | mutable void (*DeleterFn)(void *) = nullptr; |
54 | | mutable const ManagedStaticBase *Next = nullptr; |
55 | | #else |
56 | | // This should only be used as a static variable, which guarantees that this |
57 | | // will be zero initialized. |
58 | | mutable std::atomic<void *> Ptr; |
59 | | mutable void (*DeleterFn)(void *); |
60 | | mutable const ManagedStaticBase *Next; |
61 | | #endif |
62 | | |
63 | | void RegisterManagedStatic(void *(*creator)(), void (*deleter)(void*)) const; |
64 | | |
65 | | public: |
66 | | #ifdef LLVM_USE_CONSTEXPR_CTOR |
67 | | constexpr ManagedStaticBase() = default; |
68 | | #endif |
69 | | |
70 | | /// isConstructed - Return true if this object has not been created yet. |
71 | 0 | bool isConstructed() const { return Ptr != nullptr; } |
72 | | |
73 | | void destroy() const; |
74 | | }; |
75 | | |
76 | | /// ManagedStatic - This transparently changes the behavior of global statics to |
77 | | /// be lazily constructed on demand (good for reducing startup times of dynamic |
78 | | /// libraries that link in LLVM components) and for making destruction be |
79 | | /// explicit through the llvm_shutdown() function call. |
80 | | /// |
81 | | template <class C, class Creator = object_creator<C>, |
82 | | class Deleter = object_deleter<C>> |
83 | | class ManagedStatic : public ManagedStaticBase { |
84 | | public: |
85 | | // Accessors. |
86 | 114 | C &operator*() { |
87 | 114 | void *Tmp = Ptr.load(std::memory_order_acquire); |
88 | 114 | if (!Tmp) |
89 | 8 | RegisterManagedStatic(Creator::call, Deleter::call); |
90 | 114 | |
91 | 114 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); |
92 | 114 | } Unexecuted instantiation: _ZN4llvm13ManagedStaticISt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EENS_14object_creatorIS9_EENS_14object_deleterIS9_EEEdeEv _ZN4llvm13ManagedStaticINS_2cl10SubCommandENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEdeEv Line | Count | Source | 86 | 76 | C &operator*() { | 87 | 76 | void *Tmp = Ptr.load(std::memory_order_acquire); | 88 | 76 | if (!Tmp) | 89 | 4 | RegisterManagedStatic(Creator::call, Deleter::call); | 90 | 76 | | 91 | 76 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 92 | 76 | } |
CommandLine.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_117CommandLineParserENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEdeEv Line | Count | Source | 86 | 36 | C &operator*() { | 87 | 36 | void *Tmp = Ptr.load(std::memory_order_acquire); | 88 | 36 | if (!Tmp) | 89 | 2 | RegisterManagedStatic(Creator::call, Deleter::call); | 90 | 36 | | 91 | 36 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 92 | 36 | } |
Unexecuted instantiation: Error.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_118ErrorErrorCategoryENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEdeEv Unexecuted instantiation: _ZN4llvm13ManagedStaticISt5mutexNS_14object_creatorIS1_EENS_14object_deleterIS1_EEEdeEv Unexecuted instantiation: CrashRecoveryContext.cpp:_ZN4llvm13ManagedStaticINS_3sys11ThreadLocalIKN12_GLOBAL__N_124CrashRecoveryContextImplEEENS_14object_creatorIS6_EENS_14object_deleterIS6_EEEdeEv Unexecuted instantiation: _ZN4llvm13ManagedStaticINS_3sys11ThreadLocalIKNS_20CrashRecoveryContextEEENS_14object_creatorIS5_EENS_14object_deleterIS5_EEEdeEv _ZN4llvm13ManagedStaticINS_3sys10SmartMutexILb1EEENS_14object_creatorIS3_EENS_14object_deleterIS3_EEEdeEv Line | Count | Source | 86 | 2 | C &operator*() { | 87 | 2 | void *Tmp = Ptr.load(std::memory_order_acquire); | 88 | 2 | if (!Tmp) | 89 | 2 | RegisterManagedStatic(Creator::call, Deleter::call); | 90 | 2 | | 91 | 2 | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); | 92 | 2 | } |
Unexecuted instantiation: Signals.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_120FilesToRemoveCleanupENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEdeEv Unexecuted instantiation: AsmPrinter.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_117AsmPrinterOptionsENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEdeEv Unexecuted instantiation: _ZN4llvm13ManagedStaticINS_9MapVectorIN4mlir6TypeIDESt8functionIFvPNS2_11MLIRContextEEENS_8DenseMapIS3_jNS_12DenseMapInfoIS3_EENS_6detail12DenseMapPairIS3_jEEEESt6vectorISt4pairIS3_S8_ESaISI_EEEENS_14object_creatorISL_EENS_14object_deleterISL_EEEdeEv Unexecuted instantiation: MLIRContext.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_118MLIRContextOptionsENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEdeEv |
93 | | |
94 | 38 | C *operator->() { return &**this; } Unexecuted instantiation: _ZN4llvm13ManagedStaticISt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS7_EENS_14object_creatorIS9_EENS_14object_deleterIS9_EEEptEv _ZN4llvm13ManagedStaticINS_2cl10SubCommandENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEptEv Line | Count | Source | 94 | 2 | C *operator->() { return &**this; } |
CommandLine.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_117CommandLineParserENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEptEv Line | Count | Source | 94 | 36 | C *operator->() { return &**this; } |
Unexecuted instantiation: CrashRecoveryContext.cpp:_ZN4llvm13ManagedStaticINS_3sys11ThreadLocalIKN12_GLOBAL__N_124CrashRecoveryContextImplEEENS_14object_creatorIS6_EENS_14object_deleterIS6_EEEptEv Unexecuted instantiation: _ZN4llvm13ManagedStaticINS_3sys11ThreadLocalIKNS_20CrashRecoveryContextEEENS_14object_creatorIS5_EENS_14object_deleterIS5_EEEptEv Unexecuted instantiation: AsmPrinter.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_117AsmPrinterOptionsENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEptEv Unexecuted instantiation: _ZN4llvm13ManagedStaticINS_9MapVectorIN4mlir6TypeIDESt8functionIFvPNS2_11MLIRContextEEENS_8DenseMapIS3_jNS_12DenseMapInfoIS3_EENS_6detail12DenseMapPairIS3_jEEEESt6vectorISt4pairIS3_S8_ESaISI_EEEENS_14object_creatorISL_EENS_14object_deleterISL_EEEptEv Unexecuted instantiation: MLIRContext.cpp:_ZN4llvm13ManagedStaticIN12_GLOBAL__N_118MLIRContextOptionsENS_14object_creatorIS2_EENS_14object_deleterIS2_EEEptEv |
95 | | |
96 | | const C &operator*() const { |
97 | | void *Tmp = Ptr.load(std::memory_order_acquire); |
98 | | if (!Tmp) |
99 | | RegisterManagedStatic(Creator::call, Deleter::call); |
100 | | |
101 | | return *static_cast<C *>(Ptr.load(std::memory_order_relaxed)); |
102 | | } |
103 | | |
104 | | const C *operator->() const { return &**this; } |
105 | | |
106 | | // Extract the instance, leaving the ManagedStatic uninitialized. The |
107 | | // user is then responsible for the lifetime of the returned instance. |
108 | | C *claim() { |
109 | | return static_cast<C *>(Ptr.exchange(nullptr)); |
110 | | } |
111 | | }; |
112 | | |
113 | | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
114 | | void llvm_shutdown(); |
115 | | |
116 | | /// llvm_shutdown_obj - This is a simple helper class that calls |
117 | | /// llvm_shutdown() when it is destroyed. |
118 | | struct llvm_shutdown_obj { |
119 | | llvm_shutdown_obj() = default; |
120 | 0 | ~llvm_shutdown_obj() { llvm_shutdown(); } |
121 | | }; |
122 | | |
123 | | } // end namespace llvm |
124 | | |
125 | | #endif // LLVM_SUPPORT_MANAGEDSTATIC_H |