Skip to content

[llvm] Use llvm::append_range (NFC) #136066

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions llvm/include/llvm/Analysis/BlockFrequencyInfoImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1159,8 +1159,7 @@ void BlockFrequencyInfoImpl<BT>::setBlockFreq(const BlockT *BB,

template <class BT> void BlockFrequencyInfoImpl<BT>::initializeRPOT() {
const BlockT *Entry = &F->front();
RPOT.reserve(F->size());
std::copy(po_begin(Entry), po_end(Entry), std::back_inserter(RPOT));
llvm::append_range(RPOT, post_order(Entry));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reserve here got lost. No idea if that's the cause or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikic Ack. Let me restore reserve. It's possible that llvm::append_range may not be (eventually) calling std::vector::reserve.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could detect if the container type has .reserve and call it when available in append_range

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or actually, I wonder if we use it with any container types that do not have reserve...

Copy link
Member

@kuhar kuhar Apr 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, append_values does call reserve:

void append_values(Container &C, Args &&...Values) {
C.reserve(range_size(C) + sizeof...(Args));
// Append all values one by one.
((void)C.insert(C.end(), std::forward<Args>(Values)), ...);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That wouldn't help here anyway, because the post order iterator does not have a known length.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point -- the inserted range may not support fast size calculation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've posted #136215 to store the call to std::vector::reserve.

std::reverse(RPOT.begin(), RPOT.end());

assert(RPOT.size() - 1 <= BlockNode::getMaxIndex() &&
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/Bitcode/BitcodeConvenience.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ template <typename ElementTy> class BCRecordCoding<BCArray<ElementTy>> {
ElementTy::assertValid(element);
#endif
buffer.reserve(buffer.size() + std::distance(array.begin(), array.end()));
std::copy(array.begin(), array.end(), std::back_inserter(buffer));
llvm::append_range(buffer, array);
Stream.EmitRecordWithAbbrev(code, buffer);
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/DiagnosticInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ class DiagnosticInfoIROptimization : public DiagnosticInfoOptimizationBase {
Orig.RemarkName, Orig.getFunction(), Orig.getLocation()),
CodeRegion(Orig.getCodeRegion()) {
*this << Prepend;
std::copy(Orig.Args.begin(), Orig.Args.end(), std::back_inserter(Args));
llvm::append_range(Args, Orig.Args);
}

/// Legacy interface.
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ void RuntimePointerChecking::groupChecks(

// We've computed the grouped checks for this partition.
// Save the results and continue with the next one.
llvm::copy(Groups, std::back_inserter(CheckingGroups));
llvm::append_range(CheckingGroups, Groups);
}
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ ArrayRef<Register> IRTranslator::getOrCreateVRegs(const Value &Val) {
unsigned Idx = 0;
while (auto Elt = C.getAggregateElement(Idx++)) {
auto EltRegs = getOrCreateVRegs(*Elt);
llvm::copy(EltRegs, std::back_inserter(*VRegs));
llvm::append_range(*VRegs, EltRegs);
}
} else {
assert(SplitTys.size() == 1 && "unexpectedly split LLT");
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ llvm::createLibcall(MachineIRBuilder &MIRBuilder, const char *Name,
isLibCallInTailPosition(Result, *MI, MIRBuilder.getTII(),
*MIRBuilder.getMRI());

std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
llvm::append_range(Info.OrigArgs, Args);
if (!CLI.lowerCall(MIRBuilder, Info))
return LegalizerHelper::UnableToLegalize;

Expand Down Expand Up @@ -708,7 +708,7 @@ llvm::createMemLibcall(MachineIRBuilder &MIRBuilder, MachineRegisterInfo &MRI,
MI.getOperand(MI.getNumOperands() - 1).getImm() &&
isLibCallInTailPosition(Info.OrigRet, MI, MIRBuilder.getTII(), MRI);

std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
llvm::append_range(Info.OrigArgs, Args);
if (!CLI.lowerCall(MIRBuilder, Info))
return LegalizerHelper::UnableToLegalize;

Expand Down Expand Up @@ -855,7 +855,7 @@ createAtomicLibcall(MachineIRBuilder &MIRBuilder, MachineInstr &MI) {
Info.Callee = MachineOperand::CreateES(Name);
Info.OrigRet = CallLowering::ArgInfo(RetRegs, RetTy, 0);

std::copy(Args.begin(), Args.end(), std::back_inserter(Info.OrigArgs));
llvm::append_range(Info.OrigArgs, Args);
if (!CLI.lowerCall(MIRBuilder, Info))
return LegalizerHelper::UnableToLegalize;

Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/CodeGen/ModuloSchedule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1855,9 +1855,9 @@ void PeelingModuloScheduleExpander::peelPrologAndEpilogs() {

// Create a list of all blocks in order.
SmallVector<MachineBasicBlock *, 8> Blocks;
llvm::copy(PeeledFront, std::back_inserter(Blocks));
llvm::append_range(Blocks, PeeledFront);
Blocks.push_back(BB);
llvm::copy(PeeledBack, std::back_inserter(Blocks));
llvm::append_range(Blocks, PeeledBack);

// Iterate in reverse order over all instructions, remapping as we go.
for (MachineBasicBlock *B : reverse(Blocks)) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/ExecutionEngine/JITLink/JITLink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ void LinkGraph::dump(raw_ostream &OS) {
OS << "section " << Sec->getName() << ":\n\n";

std::vector<Block *> SortedBlocks;
llvm::copy(Sec->blocks(), std::back_inserter(SortedBlocks));
llvm::append_range(SortedBlocks, Sec->blocks());
llvm::sort(SortedBlocks, [](const Block *LHS, const Block *RHS) {
return LHS->getAddress() < RHS->getAddress();
});
Expand All @@ -339,7 +339,7 @@ void LinkGraph::dump(raw_ostream &OS) {
if (!B->edges_empty()) {
OS << " edges:\n";
std::vector<Edge> SortedEdges;
llvm::copy(B->edges(), std::back_inserter(SortedEdges));
llvm::append_range(SortedEdges, B->edges());
llvm::sort(SortedEdges, [](const Edge &LHS, const Edge &RHS) {
return LHS.getOffset() < RHS.getOffset();
});
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Transforms/Scalar/GVNSink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ class ModelledPHI {
SmallSetVector<BasicBlock *, 4> &B,
const DenseMap<const BasicBlock *, unsigned> &BlockOrder) {
// The order of Values and Blocks are already ordered by the caller.
llvm::copy(V, std::back_inserter(Values));
llvm::copy(B, std::back_inserter(Blocks));
llvm::append_range(Values, V);
llvm::append_range(Blocks, B);
verifyModelledPHI(BlockOrder);
}

/// Create a PHI from [I[OpNum] for I in Insts].
/// TODO: Figure out a way to verifyModelledPHI in this constructor.
ModelledPHI(ArrayRef<Instruction *> Insts, unsigned OpNum,
SmallSetVector<BasicBlock *, 4> &B) {
llvm::copy(B, std::back_inserter(Blocks));
llvm::append_range(Blocks, B);
for (auto *I : Insts)
Values.push_back(I->getOperand(OpNum));
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-jitlink/llvm-jitlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,7 @@ static Error addLibraries(Session &S,
SmallVector<char, 256> LibPath;
LibPath.reserve(SearchPath.size() + strlen("lib") + LL.LibName.size() +
LibExt.size() + 2); // +2 for pathsep, null term.
llvm::copy(SearchPath, std::back_inserter(LibPath));
llvm::append_range(LibPath, SearchPath);
if (LibExt != ".lib" && LibExt != ".dll")
sys::path::append(LibPath, "lib" + LL.LibName + LibExt);
else
Expand Down
3 changes: 1 addition & 2 deletions llvm/utils/TableGen/SearchableTableEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,8 +701,7 @@ void SearchableTableEmitter::collectTableEntries(
}

SearchIndex Idx;
std::copy(Table.Fields.begin(), Table.Fields.end(),
std::back_inserter(Idx.Fields));
llvm::append_range(Idx.Fields, Table.Fields);
llvm::sort(Table.Entries, [&](const Record *LHS, const Record *RHS) {
return compareBy(LHS, RHS, Idx);
});
Expand Down
Loading