Skip to content

Commit 86c5112

Browse files
authored
[NFCI][LLVM/MLIR] Adopt TrailingObjects convenience API (#138554)
Adopt `TrailingObjects` convenience API that was added in #138970 in LLVM and MLIR code.
1 parent 2b934cb commit 86c5112

File tree

14 files changed

+81
-107
lines changed

14 files changed

+81
-107
lines changed

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

+2-4
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

+5-7
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/TableGen/Record.h

+8-14
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
};
@@ -781,7 +779,7 @@ class ListInit final : public TypedInit,
781779
void Profile(FoldingSetNodeID &ID) const;
782780

783781
ArrayRef<const Init *> getValues() const {
784-
return ArrayRef(getTrailingObjects<const Init *>(), NumValues);
782+
return ArrayRef(getTrailingObjects(), NumValues);
785783
}
786784

787785
const Init *getElement(unsigned Index) const { return getValues()[Index]; }
@@ -1024,10 +1022,6 @@ class CondOpInit final : public TypedInit,
10241022
CondOpInit(ArrayRef<const Init *> Conds, ArrayRef<const Init *> Values,
10251023
const RecTy *Type);
10261024

1027-
size_t numTrailingObjects(OverloadToken<Init *>) const {
1028-
return 2*NumConds;
1029-
}
1030-
10311025
public:
10321026
CondOpInit(const CondOpInit &) = delete;
10331027
CondOpInit &operator=(const CondOpInit &) = delete;
@@ -1051,11 +1045,11 @@ class CondOpInit final : public TypedInit,
10511045
const Init *getVal(unsigned Num) const { return getVals()[Num]; }
10521046

10531047
ArrayRef<const Init *> getConds() const {
1054-
return ArrayRef(getTrailingObjects<const Init *>(), NumConds);
1048+
return getTrailingObjects(NumConds);
10551049
}
10561050

10571051
ArrayRef<const Init *> getVals() const {
1058-
return ArrayRef(getTrailingObjects<const Init *>() + NumConds, NumConds);
1052+
return ArrayRef(getTrailingObjects() + NumConds, NumConds);
10591053
}
10601054

10611055
const Init *Fold(const Record *CurRec) const;
@@ -1373,7 +1367,7 @@ class VarDefInit final
13731367
bool args_empty() const { return NumArgs == 0; }
13741368

13751369
ArrayRef<const ArgumentInit *> args() const {
1376-
return ArrayRef(getTrailingObjects<const ArgumentInit *>(), NumArgs);
1370+
return getTrailingObjects(NumArgs);
13771371
}
13781372

13791373
const Init *getBit(unsigned Bit) const override {
@@ -1486,11 +1480,11 @@ class DagInit final
14861480
}
14871481

14881482
ArrayRef<const Init *> getArgs() const {
1489-
return ArrayRef(getTrailingObjects<const Init *>(), NumArgs);
1483+
return getTrailingObjects<const Init *>(NumArgs);
14901484
}
14911485

14921486
ArrayRef<const StringInit *> getArgNames() const {
1493-
return ArrayRef(getTrailingObjects<const StringInit *>(), NumArgs);
1487+
return getTrailingObjects<const StringInit *>(NumArgs);
14941488
}
14951489

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

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ class BitcodeConstant final : public Value,
542542
: Value(Ty, SubclassID), Opcode(Info.Opcode), Flags(Info.Flags),
543543
NumOperands(OpIDs.size()), BlockAddressBB(Info.BlockAddressBB),
544544
SrcElemTy(Info.SrcElemTy), InRange(Info.InRange) {
545-
llvm::uninitialized_copy(OpIDs, getTrailingObjects<unsigned>());
545+
llvm::uninitialized_copy(OpIDs, getTrailingObjects());
546546
}
547547

548548
BitcodeConstant &operator=(const BitcodeConstant &) = delete;
@@ -559,7 +559,7 @@ class BitcodeConstant final : public Value,
559559
static bool classof(const Value *V) { return V->getValueID() == SubclassID; }
560560

561561
ArrayRef<unsigned> getOperandIDs() const {
562-
return ArrayRef(getTrailingObjects<unsigned>(), NumOperands);
562+
return ArrayRef(getTrailingObjects(), NumOperands);
563563
}
564564

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

llvm/lib/IR/AttributeImpl.h

+9-18
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,22 @@ 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>();
261-
llvm::uninitialized_copy(Val, TrailingCR);
256+
llvm::uninitialized_copy(Val, getTrailingObjects());
262257
}
263258

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

270264
ArrayRef<ConstantRange> getConstantRangeListValue() const {
271-
return ArrayRef(getTrailingObjects<ConstantRange>(), Size);
265+
return getTrailingObjects(Size);
272266
}
273267

274268
static size_t totalSizeToAlloc(ArrayRef<ConstantRange> Val) {
@@ -353,7 +347,7 @@ class AttributeSetNode final
353347

354348
using iterator = const Attribute *;
355349

356-
iterator begin() const { return getTrailingObjects<Attribute>(); }
350+
iterator begin() const { return getTrailingObjects(); }
357351
iterator end() const { return begin() + NumAttrs; }
358352

359353
void Profile(FoldingSetNodeID &ID) const {
@@ -383,9 +377,6 @@ class AttributeListImpl final
383377
/// Union of enum attributes available at any index.
384378
AttributeBitSet AvailableSomewhereAttrs;
385379

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

@@ -407,7 +398,7 @@ class AttributeListImpl final
407398

408399
using iterator = const AttributeSet *;
409400

410-
iterator begin() const { return getTrailingObjects<AttributeSet>(); }
401+
iterator begin() const { return getTrailingObjects(); }
411402
iterator end() const { return begin() + NumAttrSets; }
412403

413404
void Profile(FoldingSetNodeID &ID) const;

llvm/lib/IR/Attributes.cpp

+2-2
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

+2-2
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/TableGen/Record.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ static void ProfileRecordRecTy(FoldingSetNodeID &ID,
240240

241241
RecordRecTy::RecordRecTy(RecordKeeper &RK, ArrayRef<const Record *> Classes)
242242
: RecTy(RecordRecTyKind, RK), NumClasses(Classes.size()) {
243-
llvm::uninitialized_copy(Classes, getTrailingObjects<const Record *>());
243+
llvm::uninitialized_copy(Classes, getTrailingObjects());
244244
}
245245

246246
const RecordRecTy *RecordRecTy::get(RecordKeeper &RK,
@@ -473,7 +473,7 @@ static void ProfileBitsInit(FoldingSetNodeID &ID,
473473
BitsInit::BitsInit(RecordKeeper &RK, ArrayRef<const Init *> Bits)
474474
: TypedInit(IK_BitsInit, BitsRecTy::get(RK, Bits.size())),
475475
NumBits(Bits.size()) {
476-
llvm::uninitialized_copy(Bits, getTrailingObjects<const Init *>());
476+
llvm::uninitialized_copy(Bits, getTrailingObjects());
477477
}
478478

479479
BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<const Init *> Bits) {
@@ -493,7 +493,7 @@ BitsInit *BitsInit::get(RecordKeeper &RK, ArrayRef<const Init *> Bits) {
493493
}
494494

495495
void BitsInit::Profile(FoldingSetNodeID &ID) const {
496-
ProfileBitsInit(ID, ArrayRef(getTrailingObjects<const Init *>(), NumBits));
496+
ProfileBitsInit(ID, getBits());
497497
}
498498

499499
const Init *BitsInit::convertInitializerTo(const RecTy *Ty) const {
@@ -706,7 +706,7 @@ static void ProfileListInit(FoldingSetNodeID &ID, ArrayRef<const Init *> Range,
706706
ListInit::ListInit(ArrayRef<const Init *> Elements, const RecTy *EltTy)
707707
: TypedInit(IK_ListInit, ListRecTy::get(EltTy)),
708708
NumValues(Elements.size()) {
709-
llvm::uninitialized_copy(Elements, getTrailingObjects<const Init *>());
709+
llvm::uninitialized_copy(Elements, getTrailingObjects());
710710
}
711711

712712
const ListInit *ListInit::get(ArrayRef<const Init *> Elements,
@@ -2432,7 +2432,7 @@ VarDefInit::VarDefInit(SMLoc Loc, const Record *Class,
24322432
ArrayRef<const ArgumentInit *> Args)
24332433
: TypedInit(IK_VarDefInit, RecordRecTy::get(Class)), Loc(Loc), Class(Class),
24342434
NumArgs(Args.size()) {
2435-
llvm::uninitialized_copy(Args, getTrailingObjects<const ArgumentInit *>());
2435+
llvm::uninitialized_copy(Args, getTrailingObjects());
24362436
}
24372437

24382438
const VarDefInit *VarDefInit::get(SMLoc Loc, const Record *Class,
@@ -2616,7 +2616,7 @@ static void ProfileCondOpInit(FoldingSetNodeID &ID,
26162616
CondOpInit::CondOpInit(ArrayRef<const Init *> Conds,
26172617
ArrayRef<const Init *> Values, const RecTy *Type)
26182618
: TypedInit(IK_CondOpInit, Type), NumConds(Conds.size()), ValType(Type) {
2619-
auto *TrailingObjects = getTrailingObjects<const Init *>();
2619+
const Init **TrailingObjects = getTrailingObjects();
26202620
llvm::uninitialized_copy(Conds, TrailingObjects);
26212621
llvm::uninitialized_copy(Values, TrailingObjects + NumConds);
26222622
}

llvm/lib/Transforms/IPO/LowerTypeTests.cpp

+4-8
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,
@@ -297,7 +295,7 @@ class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
297295
GTM->NTypes = Types.size();
298296
GTM->IsJumpTableCanonical = IsJumpTableCanonical;
299297
GTM->IsExported = IsExported;
300-
llvm::copy(Types, GTM->getTrailingObjects<MDNode *>());
298+
llvm::copy(Types, GTM->getTrailingObjects());
301299
return GTM;
302300
}
303301

@@ -313,9 +311,7 @@ class GlobalTypeMember final : TrailingObjects<GlobalTypeMember, MDNode *> {
313311
return IsExported;
314312
}
315313

316-
ArrayRef<MDNode *> types() const {
317-
return ArrayRef(getTrailingObjects<MDNode *>(), NTypes);
318-
}
314+
ArrayRef<MDNode *> types() const { return getTrailingObjects(NTypes); }
319315
};
320316

321317
struct ICallBranchFunnel final
@@ -329,13 +325,13 @@ struct ICallBranchFunnel final
329325
Call->CI = CI;
330326
Call->UniqueId = UniqueId;
331327
Call->NTargets = Targets.size();
332-
llvm::copy(Targets, Call->getTrailingObjects<GlobalTypeMember *>());
328+
llvm::copy(Targets, Call->getTrailingObjects());
333329
return Call;
334330
}
335331

336332
CallInst *CI;
337333
ArrayRef<GlobalTypeMember *> targets() const {
338-
return ArrayRef(getTrailingObjects<GlobalTypeMember *>(), NTargets);
334+
return getTrailingObjects(NTargets);
339335
}
340336

341337
unsigned UniqueId;

mlir/include/mlir/IR/Operation.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -679,8 +679,7 @@ class alignas(8) Operation final
679679
if (numRegions == 0)
680680
return MutableArrayRef<Region>();
681681

682-
auto *regions = getTrailingObjects<Region>();
683-
return {regions, numRegions};
682+
return getTrailingObjects<Region>(numRegions);
684683
}
685684

686685
/// Returns the region held by this operation at position 'index'.
@@ -694,7 +693,7 @@ class alignas(8) Operation final
694693
//===--------------------------------------------------------------------===//
695694

696695
MutableArrayRef<BlockOperand> getBlockOperands() {
697-
return {getTrailingObjects<BlockOperand>(), numSuccs};
696+
return getTrailingObjects<BlockOperand>(numSuccs);
698697
}
699698

700699
// Successor iteration.

0 commit comments

Comments
 (0)