[llvm-branch-commits] [openmp] [libomp] Add kmp_vector (ADT 2/2) (PR #176163)

Michael Kruse via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jun 10 04:46:19 PDT 2026


================
@@ -136,4 +137,185 @@ class kmp_str_ref final {
   const char *end() const { return sv.data() + length(); }
 };
 
+/// kmp_vector is a vector class for managing small vectors.
+/// inline_threshold: Number of elements in the inline array. If exceeded, the
+/// vector will grow dynamically.
+template <typename T, size_t inline_threshold = 8> class kmp_vector final {
+  static_assert(std::is_copy_constructible_v<T>,
+                "T must be copy constructible");
+  static_assert(std::is_destructible_v<T>, "T must be destructible");
+
+  T inline_data[inline_threshold];
+  T *data = inline_data;
+  size_t count = 0;
+  size_t capacity = inline_threshold;
+
+  void copy_data(T *dst, const T *src, size_t num_elements) {
+    if constexpr (std::is_trivially_copyable_v<T>) {
+      memcpy(dst, src, num_elements * sizeof(T));
+    } else {
+      for (size_t i = 0; i < num_elements; i++)
+        new (&dst[i]) T(src[i]); // copy-construct to memory
+    }
+  }
+
+  void grow() {
----------------
Meinersbur wrote:

Seems that currently it is only used by `push_back`, but `append` might need a minimum number of reserved elements. SmallVector's is
```
  /// Guarantees space for at least one more
  /// element, or MinSize more elements if specified.
```
Could you add the guarantee of growing my at least one element to the comment?

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


More information about the llvm-branch-commits mailing list