-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[LLVM][Support] Add getTrailingObjects() for single trailing type #138970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
9036cd2
to
600296a
Compare
@llvm/pr-subscribers-llvm-support Author: Rahul Joshi (jurahul) ChangesAdd a specialization of getTrailingObjects() for a single trailing type. This is a common case and with the specialization you don't need to specify the single trailing type redundantly. Also add an overload for getTrailingObjects which takes size and returns an ArryaRef/MutableArrayRef as that's a common use case as well. Full diff: https://github.com/llvm/llvm-project/pull/138970.diff 2 Files Affected:
diff --git a/llvm/include/llvm/Support/TrailingObjects.h b/llvm/include/llvm/Support/TrailingObjects.h
index 07cf08df45a6a..02c7ccf911aa8 100644
--- a/llvm/include/llvm/Support/TrailingObjects.h
+++ b/llvm/include/llvm/Support/TrailingObjects.h
@@ -46,6 +46,7 @@
#ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
#define LLVM_SUPPORT_TRAILINGOBJECTS_H
+#include "llvm/ADT/ArrayRef.h"
#include "llvm/Support/Alignment.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/MathExtras.h"
@@ -301,6 +302,41 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl<
static_cast<BaseTy *>(this), TrailingObjectsBase::OverloadToken<T>());
}
+ // getTrailingObjects() specialization for a single trailing type.
+ using FirstTrailingType =
+ typename std::tuple_element_t<0, std::tuple<TrailingTys...>>;
+
+ const FirstTrailingType *getTrailingObjects() const {
+ static_assert(sizeof...(TrailingTys) == 1,
+ "Can use non-templated getTrailingObjects() only when there "
+ "is a single trailing type");
+ return getTrailingObjects<FirstTrailingType>();
+ }
+
+ FirstTrailingType *getTrailingObjects() {
+ static_assert(sizeof...(TrailingTys) == 1,
+ "Can use non-templated getTrailingObjects() only when there "
+ "is a single trailing type");
+ return getTrailingObjects<FirstTrailingType>();
+ }
+
+ // Functions that return the trailing objects as ArrayRefs.
+ template <typename T> MutableArrayRef<T> getTrailingObjects(size_t N) {
+ return {getTrailingObjects<T>(), N};
+ }
+
+ template <typename T> ArrayRef<T> getTrailingObjects(size_t N) const {
+ return {getTrailingObjects<T>(), N};
+ }
+
+ MutableArrayRef<FirstTrailingType> getTrailingObjects(size_t N) {
+ return {getTrailingObjects(), N};
+ }
+
+ ArrayRef<FirstTrailingType> getTrailingObjects(size_t N) const {
+ return {getTrailingObjects(), N};
+ }
+
/// Returns the size of the trailing data, if an object were
/// allocated with the given counts (The counts are in the same order
/// as the template arguments). This does not include the size of the
diff --git a/llvm/unittests/Support/TrailingObjectsTest.cpp b/llvm/unittests/Support/TrailingObjectsTest.cpp
index e36979e75d7f7..c461301207db0 100644
--- a/llvm/unittests/Support/TrailingObjectsTest.cpp
+++ b/llvm/unittests/Support/TrailingObjectsTest.cpp
@@ -14,19 +14,19 @@
using namespace llvm;
namespace {
-// This class, beyond being used by the test case, a nice
-// demonstration of the intended usage of TrailingObjects, with a
-// single trailing array.
-class Class1 final : protected TrailingObjects<Class1, short> {
+// This class, beyond being used by the test case, a nice demonstration of the
+// intended usage of TrailingObjects, with a single trailing array.
+class Class1 final : private TrailingObjects<Class1, short> {
friend TrailingObjects;
unsigned NumShorts;
protected:
- size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
-
Class1(ArrayRef<int> ShortArray) : NumShorts(ShortArray.size()) {
- llvm::copy(ShortArray, getTrailingObjects<short>());
+ // This tests the non-templated getTrailingObjects() when using a single
+ // trailing type.
+ llvm::copy(ShortArray, getTrailingObjects());
+ EXPECT_EQ(getTrailingObjects(), getTrailingObjects<short>());
}
public:
@@ -36,7 +36,8 @@ class Class1 final : protected TrailingObjects<Class1, short> {
}
void operator delete(void *Ptr) { ::operator delete(Ptr); }
- short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }
+ // This indexes into the ArrayRef<> returned by `getTrailingObjects`.
+ short get(unsigned Num) const { return getTrailingObjects(NumShorts)[Num]; }
unsigned numShorts() const { return NumShorts; }
@@ -49,9 +50,9 @@ class Class1 final : protected TrailingObjects<Class1, short> {
using TrailingObjects::getTrailingObjects;
};
-// Here, there are two singular optional object types appended. Note
-// that the alignment of Class2 is automatically increased to account
-// for the alignment requirements of the trailing objects.
+// Here, there are two singular optional object types appended. Note that the
+// alignment of Class2 is automatically increased to account for the alignment
+// requirements of the trailing objects.
class Class2 final : protected TrailingObjects<Class2, double, short> {
friend TrailingObjects;
@@ -172,10 +173,9 @@ TEST(TrailingObjects, TwoArg) {
delete C2;
}
-// This test class is not trying to be a usage demo, just asserting
-// that three args does actually work too (it's the same code as
-// handles the second arg, so it's basically covered by the above, but
-// just in case..)
+// This test class is not trying to be a usage demo, just asserting that three
+// args does actually work too (it's the same code as handles the second arg, so
+// it's basically covered by the above, but just in case..)
class Class3 final : public TrailingObjects<Class3, double, short, bool> {
friend TrailingObjects;
@@ -237,9 +237,9 @@ TEST(TrailingObjects, Realignment) {
}
}
-// Test the use of TrailingObjects with a template class. This
-// previously failed to compile due to a bug in MSVC's member access
-// control/lookup handling for OverloadToken.
+// Test the use of TrailingObjects with a template class. This previously failed
+// to compile due to a bug in MSVC's member access control/lookup handling for
+// OverloadToken.
template <typename Derived>
class Class5Tmpl : private llvm::TrailingObjects<Derived, float, int> {
using TrailingObjects = typename llvm::TrailingObjects<Derived, float>;
|
600296a
to
51a82f8
Compare
Thanks |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unrequest changes
51a82f8
to
dfe58ed
Compare
Add a specialization of getTrailingObjects() for a single trailing type. This is a common case and with the specialization you don't need to specify the single trailing type redundantly. Also add an overload for getTrailingObjects which takes size and returns an ArryaRef/MutableArrayRef as that's a common use case as well.
dfe58ed
to
05938c9
Compare
Adopt `TrailingObjects` convienence API that was added in llvm#138970 in LLVM and MLIR code.
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/190/builds/19745 Here is the relevant piece of the build log for the reference
|
Adopt `TrailingObjects` convienence API that was added in llvm#138970 in LLVM and MLIR code.
Adopt `TrailingObjects` convienence API that was added in llvm#138970 in LLVM and MLIR code.
Adopt `TrailingObjects` convenience API that was added in #138970 in LLVM and MLIR code.
…(#138554) Adopt `TrailingObjects` convenience API that was added in llvm/llvm-project#138970 in LLVM and MLIR code.
Adopt `getTrailingObjects()` overload that returns an ArrayRef that was added in #138970.
…croArgs (#139635) Adopt convenience API for single trailing type added in llvm/llvm-project#138970.
… (#139639) Adopt `getTrailingObjects()` overload that returns an ArrayRef that was added in llvm/llvm-project#138970.
Add a specialization of getTrailingObjects() for a single trailing type. This is a common case and with the specialization you don't need to specify the single trailing type redundantly. Also add an overload for getTrailingObjects which takes size and returns an ArryaRef/MutableArrayRef as that's a common use case as well.