-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OpenMP] Use simple VLA implementation to replace uses of actual VLA
Use of VLA can cause compile warning that was introduced in D156565. This patch implements a simple stack/heap-based VLA that can miminc the behavior of an actual VLA and prevent the warning. By default the stack accomodates the elements. If the number of emelements is greater than N, which by default is 8, a heap buffer will be allocated and used to acccomodate the elements.
- Loading branch information
Showing
3 changed files
with
61 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* 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> | ||
|
||
#include "kmp.h" | ||
|
||
/// A simple pure header implementation of VLA that aims to replace uses of | ||
/// actual VLA, which can cause compile warning. This class by default creates a | ||
/// stack buffer that can accomodate \p N elements. If the number of elements is | ||
/// greater than \p N, then a heap buffer will be allocated and used to | ||
/// accomodate the elements. Similar to the actual VLA, we don't check boundary | ||
/// (for now), so we will not store the number of elements. We can always revise | ||
/// it later. | ||
template <typename T, unsigned N = 8> class SimpleVLA final { | ||
T StackBuffer[N]; | ||
T *HeapBuffer = nullptr; | ||
T *Ptr = StackBuffer; | ||
|
||
public: | ||
SimpleVLA() = delete; | ||
SimpleVLA(const SimpleVLA &) = delete; | ||
SimpleVLA(SimpleVLA &&) = delete; | ||
SimpleVLA &operator=(const SimpleVLA &) = delete; | ||
SimpleVLA &operator=(SimpleVLA &&) = delete; | ||
|
||
explicit SimpleVLA(unsigned NumOfElements) noexcept { | ||
if (NumOfElements > N) { | ||
HeapBuffer = | ||
reinterpret_cast<T *>(__kmp_allocate(NumOfElements * sizeof(T))); | ||
Ptr = HeapBuffer; | ||
} | ||
} | ||
|
||
~SimpleVLA() { | ||
if (HeapBuffer) | ||
__kmp_free(HeapBuffer); | ||
} | ||
|
||
operator T *() noexcept { return Ptr; } | ||
operator const T *() const noexcept { return Ptr; } | ||
}; | ||
|
||
#endif |