Skip to content

Commit b229281

Browse files
committed
[LLVM][Support] Add getTrailingObjects() for single trailing type.
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.
1 parent 492ad84 commit b229281

File tree

15 files changed

+108
-102
lines changed

15 files changed

+108
-102
lines changed

llvm/include/llvm/DebugInfo/BTF/BTF.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,14 +304,12 @@ enum PatchableRelocKind : uint32_t {
304304
// For CommonType sub-types that are followed by a single entry of
305305
// some type in the binary format.
306306
#define BTF_DEFINE_TAIL(Type, Accessor) \
307-
const Type &Accessor() const { return *getTrailingObjects<Type>(); }
307+
const Type &Accessor() const { return *getTrailingObjects(); }
308308

309309
// For CommonType sub-types that are followed by CommonType::getVlen()
310310
// number of entries of some type in the binary format.
311311
#define BTF_DEFINE_TAIL_ARR(Type, Accessor) \
312-
ArrayRef<Type> Accessor() const { \
313-
return ArrayRef<Type>(getTrailingObjects<Type>(), getVlen()); \
314-
}
312+
ArrayRef<Type> Accessor() const { return getTrailingObjects(getVlen()); }
315313

316314
struct ArrayType final : CommonType,
317315
private TrailingObjects<ArrayType, BTFArray> {

llvm/include/llvm/IR/DataLayout.h

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,9 @@ inline LLVMTargetDataRef wrap(const DataLayout *P) {
564564

565565
/// Used to lazily calculate structure layout information for a target machine,
566566
/// based on the DataLayout structure.
567-
class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
567+
class StructLayout final : private TrailingObjects<StructLayout, TypeSize> {
568+
friend TrailingObjects;
569+
568570
TypeSize StructSize;
569571
Align StructAlignment;
570572
unsigned IsPadded : 1;
@@ -586,11 +588,11 @@ class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
586588
unsigned getElementContainingOffset(uint64_t FixedOffset) const;
587589

588590
MutableArrayRef<TypeSize> getMemberOffsets() {
589-
return llvm::MutableArrayRef(getTrailingObjects<TypeSize>(), NumElements);
591+
return getTrailingObjects(NumElements);
590592
}
591593

592594
ArrayRef<TypeSize> getMemberOffsets() const {
593-
return llvm::ArrayRef(getTrailingObjects<TypeSize>(), NumElements);
595+
return getTrailingObjects(NumElements);
594596
}
595597

596598
TypeSize getElementOffset(unsigned Idx) const {
@@ -606,10 +608,6 @@ class StructLayout final : public TrailingObjects<StructLayout, TypeSize> {
606608
friend class DataLayout; // Only DataLayout can create this class
607609

608610
StructLayout(StructType *ST, const DataLayout &DL);
609-
610-
size_t numTrailingObjects(OverloadToken<TypeSize>) const {
611-
return NumElements;
612-
}
613611
};
614612

615613
// The implementation of this method is provided inline as it is particularly

llvm/include/llvm/Support/TrailingObjects.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#ifndef LLVM_SUPPORT_TRAILINGOBJECTS_H
4747
#define LLVM_SUPPORT_TRAILINGOBJECTS_H
4848

49+
#include "llvm/ADT/ArrayRef.h"
4950
#include "llvm/Support/Alignment.h"
5051
#include "llvm/Support/Compiler.h"
5152
#include "llvm/Support/MathExtras.h"
@@ -301,6 +302,38 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl<
301302
static_cast<BaseTy *>(this), TrailingObjectsBase::OverloadToken<T>());
302303
}
303304

305+
// getTrailingObjects() specialization for a single trailing type.
306+
using FirstTrailingType =
307+
typename std::tuple_element_t<0, std::tuple<TrailingTys...>>;
308+
309+
const FirstTrailingType *getTrailingObjects() const {
310+
static_assert(sizeof...(TrailingTys) == 1,
311+
"Can use non-templated getTrailingObjects() only when there "
312+
"is a single trailing type");
313+
return getTrailingObjects<FirstTrailingType>();
314+
}
315+
316+
FirstTrailingType *getTrailingObjects() {
317+
static_assert(sizeof...(TrailingTys) == 1,
318+
"Can use non-templated getTrailingObjects() only when there "
319+
"is a single trailing type");
320+
return getTrailingObjects<FirstTrailingType>();
321+
}
322+
323+
// Functions that return the trailing objects as ArrayRefs.
324+
template <typename T> MutableArrayRef<T> getTrailingObjects(size_t N) {
325+
return {getTrailingObjects<T>(), N};
326+
}
327+
template <typename T> ArrayRef<T> getTrailingObjects(size_t N) const {
328+
return {getTrailingObjects<T>(), N};
329+
}
330+
MutableArrayRef<FirstTrailingType> getTrailingObjects(size_t N) {
331+
return {getTrailingObjects(), N};
332+
}
333+
ArrayRef<FirstTrailingType> getTrailingObjects(size_t N) const {
334+
return {getTrailingObjects(), N};
335+
}
336+
304337
/// Returns the size of the trailing data, if an object were
305338
/// allocated with the given counts (The counts are in the same order
306339
/// as the template arguments). This does not include the size of the

llvm/include/llvm/TableGen/Record.h

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class RecordRecTy final : public RecTy,
258258
void Profile(FoldingSetNodeID &ID) const;
259259

260260
ArrayRef<const Record *> getClasses() const {
261-
return ArrayRef(getTrailingObjects<const Record *>(), NumClasses);
261+
return getTrailingObjects(NumClasses);
262262
}
263263

264264
using const_record_iterator = const Record *const *;
@@ -632,9 +632,7 @@ class BitsInit final : public TypedInit,
632632

633633
const Init *resolveReferences(Resolver &R) const override;
634634

635-
ArrayRef<const Init *> getBits() const {
636-
return ArrayRef(getTrailingObjects<const Init *>(), NumBits);
637-
}
635+
ArrayRef<const Init *> getBits() const { return getTrailingObjects(NumBits); }
638636

639637
const Init *getBit(unsigned Bit) const override { return getBits()[Bit]; }
640638
};
@@ -1026,10 +1024,6 @@ class CondOpInit final : public TypedInit,
10261024
CondOpInit(ArrayRef<const Init *> Conds, ArrayRef<const Init *> Values,
10271025
const RecTy *Type);
10281026

1029-
size_t numTrailingObjects(OverloadToken<Init *>) const {
1030-
return 2*NumConds;
1031-
}
1032-
10331027
public:
10341028
CondOpInit(const CondOpInit &) = delete;
10351029
CondOpInit &operator=(const CondOpInit &) = delete;
@@ -1053,11 +1047,11 @@ class CondOpInit final : public TypedInit,
10531047
const Init *getVal(unsigned Num) const { return getVals()[Num]; }
10541048

10551049
ArrayRef<const Init *> getConds() const {
1056-
return ArrayRef(getTrailingObjects<const Init *>(), NumConds);
1050+
return getTrailingObjects(NumConds);
10571051
}
10581052

10591053
ArrayRef<const Init *> getVals() const {
1060-
return ArrayRef(getTrailingObjects<const Init *>() + NumConds, NumConds);
1054+
return ArrayRef(getTrailingObjects() + NumConds, NumConds);
10611055
}
10621056

10631057
const Init *Fold(const Record *CurRec) const;
@@ -1375,7 +1369,7 @@ class VarDefInit final
13751369
bool args_empty() const { return NumArgs == 0; }
13761370

13771371
ArrayRef<const ArgumentInit *> args() const {
1378-
return ArrayRef(getTrailingObjects<const ArgumentInit *>(), NumArgs);
1372+
return getTrailingObjects(NumArgs);
13791373
}
13801374

13811375
const Init *getBit(unsigned Bit) const override {
@@ -1488,11 +1482,11 @@ class DagInit final
14881482
}
14891483

14901484
ArrayRef<const Init *> getArgs() const {
1491-
return ArrayRef(getTrailingObjects<const Init *>(), NumArgs);
1485+
return getTrailingObjects<const Init *>(NumArgs);
14921486
}
14931487

14941488
ArrayRef<const StringInit *> getArgNames() const {
1495-
return ArrayRef(getTrailingObjects<const StringInit *>(), NumArgs);
1489+
return getTrailingObjects<const StringInit *>(NumArgs);
14961490
}
14971491

14981492
const Init *resolveReferences(Resolver &R) const override;

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ class BitcodeConstant final : public Value,
560560
static bool classof(const Value *V) { return V->getValueID() == SubclassID; }
561561

562562
ArrayRef<unsigned> getOperandIDs() const {
563-
return ArrayRef(getTrailingObjects<unsigned>(), NumOperands);
563+
return ArrayRef(getTrailingObjects(), NumOperands);
564564
}
565565

566566
std::optional<ConstantRange> getInRange() const {

llvm/lib/IR/AttributeImpl.h

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -195,15 +195,12 @@ class StringAttributeImpl final
195195

196196
unsigned KindSize;
197197
unsigned ValSize;
198-
size_t numTrailingObjects(OverloadToken<char>) const {
199-
return KindSize + 1 + ValSize + 1;
200-
}
201198

202199
public:
203200
StringAttributeImpl(StringRef Kind, StringRef Val = StringRef())
204201
: AttributeImpl(StringAttrEntry), KindSize(Kind.size()),
205202
ValSize(Val.size()) {
206-
char *TrailingString = getTrailingObjects<char>();
203+
char *TrailingString = getTrailingObjects();
207204
// Some users rely on zero-termination.
208205
llvm::copy(Kind, TrailingString);
209206
TrailingString[KindSize] = '\0';
@@ -212,10 +209,10 @@ class StringAttributeImpl final
212209
}
213210

214211
StringRef getStringKind() const {
215-
return StringRef(getTrailingObjects<char>(), KindSize);
212+
return StringRef(getTrailingObjects(), KindSize);
216213
}
217214
StringRef getStringValue() const {
218-
return StringRef(getTrailingObjects<char>() + KindSize + 1, ValSize);
215+
return StringRef(getTrailingObjects() + KindSize + 1, ValSize);
219216
}
220217

221218
static size_t totalSizeToAlloc(StringRef Kind, StringRef Val) {
@@ -250,25 +247,23 @@ class ConstantRangeListAttributeImpl final
250247
friend TrailingObjects;
251248

252249
unsigned Size;
253-
size_t numTrailingObjects(OverloadToken<ConstantRange>) const { return Size; }
254250

255251
public:
256252
ConstantRangeListAttributeImpl(Attribute::AttrKind Kind,
257253
ArrayRef<ConstantRange> Val)
258254
: EnumAttributeImpl(ConstantRangeListAttrEntry, Kind), Size(Val.size()) {
259255
assert(Size > 0);
260-
ConstantRange *TrailingCR = getTrailingObjects<ConstantRange>();
256+
ConstantRange *TrailingCR = getTrailingObjects();
261257
std::uninitialized_copy(Val.begin(), Val.end(), TrailingCR);
262258
}
263259

264260
~ConstantRangeListAttributeImpl() {
265-
ConstantRange *TrailingCR = getTrailingObjects<ConstantRange>();
266-
for (unsigned I = 0; I != Size; ++I)
267-
TrailingCR[I].~ConstantRange();
261+
for (ConstantRange &CR : getTrailingObjects(Size))
262+
CR.~ConstantRange();
268263
}
269264

270265
ArrayRef<ConstantRange> getConstantRangeListValue() const {
271-
return ArrayRef(getTrailingObjects<ConstantRange>(), Size);
266+
return getTrailingObjects(Size);
272267
}
273268

274269
static size_t totalSizeToAlloc(ArrayRef<ConstantRange> Val) {
@@ -353,7 +348,7 @@ class AttributeSetNode final
353348

354349
using iterator = const Attribute *;
355350

356-
iterator begin() const { return getTrailingObjects<Attribute>(); }
351+
iterator begin() const { return getTrailingObjects(); }
357352
iterator end() const { return begin() + NumAttrs; }
358353

359354
void Profile(FoldingSetNodeID &ID) const {
@@ -383,9 +378,6 @@ class AttributeListImpl final
383378
/// Union of enum attributes available at any index.
384379
AttributeBitSet AvailableSomewhereAttrs;
385380

386-
// Helper fn for TrailingObjects class.
387-
size_t numTrailingObjects(OverloadToken<AttributeSet>) { return NumAttrSets; }
388-
389381
public:
390382
AttributeListImpl(ArrayRef<AttributeSet> Sets);
391383

@@ -407,7 +399,7 @@ class AttributeListImpl final
407399

408400
using iterator = const AttributeSet *;
409401

410-
iterator begin() const { return getTrailingObjects<AttributeSet>(); }
402+
iterator begin() const { return getTrailingObjects(); }
411403
iterator end() const { return begin() + NumAttrSets; }
412404

413405
void Profile(FoldingSetNodeID &ID) const;

llvm/lib/IR/Attributes.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,7 +1237,7 @@ LLVM_DUMP_METHOD void AttributeSet::dump() const {
12371237
AttributeSetNode::AttributeSetNode(ArrayRef<Attribute> Attrs)
12381238
: NumAttrs(Attrs.size()) {
12391239
// There's memory after the node where we can store the entries in.
1240-
llvm::copy(Attrs, getTrailingObjects<Attribute>());
1240+
llvm::copy(Attrs, getTrailingObjects());
12411241

12421242
for (const auto &I : *this) {
12431243
if (I.isStringAttribute())
@@ -1423,7 +1423,7 @@ AttributeListImpl::AttributeListImpl(ArrayRef<AttributeSet> Sets)
14231423
assert(!Sets.empty() && "pointless AttributeListImpl");
14241424

14251425
// There's memory after the node where we can store the entries in.
1426-
llvm::copy(Sets, getTrailingObjects<AttributeSet>());
1426+
llvm::copy(Sets, getTrailingObjects());
14271427

14281428
// Initialize AvailableFunctionAttrs and AvailableSomewhereAttrs
14291429
// summary bitsets.

llvm/lib/Support/TrieRawHashMap.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TrieSubtrie final
6262
public:
6363
using Slot = LazyAtomicPointer<TrieNode>;
6464

65-
Slot &get(size_t I) { return getTrailingObjects<Slot>()[I]; }
65+
Slot &get(size_t I) { return getTrailingObjects()[I]; }
6666
TrieNode *load(size_t I) { return get(I).load(); }
6767

6868
unsigned size() const { return Size; }
@@ -190,7 +190,7 @@ class ThreadSafeTrieRawHashMapBase::ImplType final
190190
}
191191

192192
// Get the root which is the trailing object.
193-
TrieSubtrie *getRoot() { return getTrailingObjects<TrieSubtrie>(); }
193+
TrieSubtrie *getRoot() { return getTrailingObjects(); }
194194

195195
static void *operator new(size_t Size) { return ::operator new(Size); }
196196
void operator delete(void *Ptr) { ::operator delete(Ptr); }

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,6 @@ class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
285285
// module and its jumptable entry needs to be exported to thinlto backends.
286286
bool IsExported;
287287

288-
size_t numTrailingObjects(OverloadToken<MDNode *>) const { return NTypes; }
289-
290288
public:
291289
static GlobalTypeMember *create(BumpPtrAllocator &Alloc, GlobalObject *GO,
292290
bool IsJumpTableCanonical, bool IsExported,
@@ -298,7 +296,7 @@ class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
298296
GTM->IsJumpTableCanonical = IsJumpTableCanonical;
299297
GTM->IsExported = IsExported;
300298
std::uninitialized_copy(Types.begin(), Types.end(),
301-
GTM->getTrailingObjects<MDNode *>());
299+
GTM->getTrailingObjects());
302300
return GTM;
303301
}
304302

@@ -314,9 +312,7 @@ class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
314312
return IsExported;
315313
}
316314

317-
ArrayRef<MDNode *> types() const {
318-
return ArrayRef(getTrailingObjects<MDNode *>(), NTypes);
319-
}
315+
ArrayRef<MDNode *> types() const { return getTrailingObjects(NTypes); }
320316
};
321317

322318
struct ICallBranchFunnel final
@@ -331,13 +327,13 @@ struct ICallBranchFunnel final
331327
Call->UniqueId = UniqueId;
332328
Call->NTargets = Targets.size();
333329
std::uninitialized_copy(Targets.begin(), Targets.end(),
334-
Call->getTrailingObjects<GlobalTypeMember *>());
330+
Call->getTrailingObjects());
335331
return Call;
336332
}
337333

338334
CallInst *CI;
339335
ArrayRef<GlobalTypeMember *> targets() const {
340-
return ArrayRef(getTrailingObjects<GlobalTypeMember *>(), NTargets);
336+
return getTrailingObjects(NTargets);
341337
}
342338

343339
unsigned UniqueId;

llvm/unittests/Support/TrailingObjectsTest.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ class Class1 final : protected TrailingObjects<Class1, short> {
2121
unsigned NumShorts;
2222

2323
protected:
24-
size_t numTrailingObjects(OverloadToken<short>) const { return NumShorts; }
25-
2624
Class1(int *ShortArray, unsigned NumShorts) : NumShorts(NumShorts) {
2725
std::uninitialized_copy(ShortArray, ShortArray + NumShorts,
28-
getTrailingObjects<short>());
26+
getTrailingObjects());
2927
}
3028

3129
public:
@@ -35,7 +33,7 @@ class Class1 final : protected TrailingObjects<Class1, short> {
3533
}
3634
void operator delete(void *p) { ::operator delete(p); }
3735

38-
short get(unsigned Num) const { return getTrailingObjects<short>()[Num]; }
36+
short get(unsigned Num) const { return getTrailingObjects()[Num]; }
3937

4038
unsigned numShorts() const { return NumShorts; }
4139

0 commit comments

Comments
 (0)