[Openmp-commits] [openmp] [OpenMP] Use simple VLA implementation to replace uses of actual VLA (PR #71412)
A. Jiang via Openmp-commits
openmp-commits at lists.llvm.org
Mon Nov 6 18:16:20 PST 2023
================
@@ -0,0 +1,42 @@
+/*
+ * kmp_utils.h -- Utilities that used internally
+ */
+
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+#ifndef __KMP_UTILS_H__
+#define __KMP_UTILS_H__
+
+#include <cstddef>
+
+/// A simple pure header implementation of VLA that aims to replace uses of
+/// actual VLA, which can cause compile warning.
+/// Similar to the actual VLA, we don't check boundary (for now), so we will not
+/// store the size. We can always revise it later.
+template <typename T> class SimpleVLA final {
+ T *Data = nullptr;
+
+public:
+ SimpleVLA() = delete;
+ SimpleVLA(const SimpleVLA &) = delete;
+ SimpleVLA(SimpleVLA &&) = default;
+ SimpleVLA &operator=(const SimpleVLA &) = delete;
+ SimpleVLA &operator=(SimpleVLA &&) = default;
+
+ explicit SimpleVLA(std::size_t Size) noexcept : Data(new T[Size]) {}
+
+ ~SimpleVLA() { delete[] Data; }
+
+ const T &operator[](std::size_t Idx) const noexcept { return Data[Idx]; }
+ T &operator[](std::size_t Idx) noexcept { return Data[Idx]; }
----------------
frederick-vs-ja wrote:
> I'm not sure if these ones should be marked `noexcept`. Every array-like C++ data structure (vector, string and array) does not mark them as `noexcept` because there can be possibly an UB (where UB = can happen anything). Here, it may be ok, may be not, I don't know.
The purpose of not marking `noexcept` is to allow implementations to diagnose UB by throwing exceptions. This is not the case for user codes.
https://github.com/llvm/llvm-project/pull/71412
More information about the Openmp-commits
mailing list