-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[llvm] Use range-based for loops (NFC) #105861
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
[llvm] Use range-based for loops (NFC) #105861
Conversation
@llvm/pr-subscribers-llvm-ir @llvm/pr-subscribers-debuginfo Author: Kazu Hirata (kazutakahirata) ChangesFull diff: https://github.com/llvm/llvm-project/pull/105861.diff 5 Files Affected:
diff --git a/llvm/include/llvm/IR/ModuleSummaryIndex.h b/llvm/include/llvm/IR/ModuleSummaryIndex.h
index 00934cc1ce6f2d..22a6cba7c89464 100644
--- a/llvm/include/llvm/IR/ModuleSummaryIndex.h
+++ b/llvm/include/llvm/IR/ModuleSummaryIndex.h
@@ -1808,9 +1808,9 @@ class ModuleSummaryIndex {
/// the ThinLTO backends.
TypeIdSummary &getOrInsertTypeIdSummary(StringRef TypeId) {
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
- for (auto It = TidIter.first; It != TidIter.second; ++It)
- if (It->second.first == TypeId)
- return It->second.second;
+ for (auto &KV : make_range(TidIter))
+ if (KV.second.first == TypeId)
+ return KV.second.second;
auto It = TypeIdMap.insert(
{GlobalValue::getGUID(TypeId), {std::string(TypeId), TypeIdSummary()}});
return It->second.second;
@@ -1820,9 +1820,9 @@ class ModuleSummaryIndex {
/// summary map) or null (if not present). This may be used when importing.
const TypeIdSummary *getTypeIdSummary(StringRef TypeId) const {
auto TidIter = TypeIdMap.equal_range(GlobalValue::getGUID(TypeId));
- for (auto It = TidIter.first; It != TidIter.second; ++It)
- if (It->second.first == TypeId)
- return &It->second.second;
+ for (const auto &KV : make_range(TidIter))
+ if (KV.second.first == TypeId)
+ return &KV.second.second;
return nullptr;
}
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 03d0537291dada..cd8f9033c62a02 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -4807,9 +4807,9 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
// corresponding type id records.
for (auto &T : ReferencedTypeIds) {
auto TidIter = Index.typeIds().equal_range(T);
- for (auto It = TidIter.first; It != TidIter.second; ++It) {
- writeTypeIdSummaryRecord(NameVals, StrtabBuilder, It->second.first,
- It->second.second);
+ for (const auto &KV : make_range(TidIter)) {
+ writeTypeIdSummaryRecord(NameVals, StrtabBuilder, KV.second.first,
+ KV.second.second);
Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
NameVals.clear();
}
diff --git a/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp b/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
index e146fb7e576819..2959d3261bea71 100644
--- a/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
+++ b/llvm/lib/CodeGen/LiveDebugValues/VarLocBasedImpl.cpp
@@ -1312,7 +1312,7 @@ void VarLocBasedLDV::cleanupEntryValueTransfers(
return;
auto TransRange = EntryValTransfers.equal_range(TRInst);
- for (auto &TDPair : llvm::make_range(TransRange.first, TransRange.second)) {
+ for (auto &TDPair : llvm::make_range(TransRange)) {
const VarLoc &EmittedEV = VarLocIDs[TDPair.second];
if (std::tie(EntryVL.Var, EntryVL.Locs[0].Value.RegNo, EntryVL.Expr) ==
std::tie(EmittedEV.Var, EmittedEV.Locs[0].Value.RegNo,
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index 01a16ccd688f43..1b569715345cca 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -3488,9 +3488,9 @@ void AssemblyWriter::printTypeIdInfo(
continue;
}
// Print all type id that correspond to this GUID.
- for (auto It = TidIter.first; It != TidIter.second; ++It) {
+ for (const auto &KV : make_range(TidIter)) {
Out << FS;
- auto Slot = Machine.getTypeIdSlot(It->second.first);
+ auto Slot = Machine.getTypeIdSlot(KV.second.first);
assert(Slot != -1);
Out << "^" << Slot;
}
@@ -3529,10 +3529,10 @@ void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
}
// Print all type id that correspond to this GUID.
FieldSeparator FS;
- for (auto It = TidIter.first; It != TidIter.second; ++It) {
+ for (const auto &KV : make_range(TidIter)) {
Out << FS;
Out << "vFuncId: (";
- auto Slot = Machine.getTypeIdSlot(It->second.first);
+ auto Slot = Machine.getTypeIdSlot(KV.second.first);
assert(Slot != -1);
Out << "^" << Slot;
Out << ", offset: " << VFId.Offset;
diff --git a/llvm/tools/llvm-profgen/ProfiledBinary.cpp b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
index 574a9c9f52bf18..07966bc92e3933 100644
--- a/llvm/tools/llvm-profgen/ProfiledBinary.cpp
+++ b/llvm/tools/llvm-profgen/ProfiledBinary.cpp
@@ -423,8 +423,8 @@ void ProfiledBinary::decodePseudoProbe(const ELFObjectFileBase *Obj) {
GuidFilter.insert(Function::getGUID(F->FuncName));
for (auto &Range : F->Ranges) {
auto GUIDs = StartAddrToSymMap.equal_range(Range.first);
- for (auto I = GUIDs.first; I != GUIDs.second; ++I)
- FuncStartAddresses[I->second] = I->first;
+ for (const auto &[K, V] : make_range(GUIDs))
+ FuncStartAddresses[V] = K;
}
}
}
|
What does |
for (auto It = TidIter.first; It != TidIter.second; ++It) | ||
if (It->second.first == TypeId) | ||
return It->second.second; | ||
for (auto &KV : make_range(TidIter)) |
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.
Structured binding allows you to name the pair elements directly, which I think help readability (you might know of better names than Key
/Value
). I'm not sure if this will work here or in the rest of this PR, but it's worth a try.
for (auto &KV : make_range(TidIter)) | |
for (auto &[Key, Value] : make_range(TidIter)) |
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.
yeah, given how the original code is already using auto anyway, I think this is a big win because it would also allow renaming each entry of the pair with a more meaningful name
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.
I avoided structured binding where one of the two variables is not used because I was afraid that the host compiler might issue an unused variable warning.
Now, I just tried Compiler Explorer. gcc-7.4 issues a warning, but gcc-8.1 doesn't. For clang, version 5 and later do not issue a warning. (I didn't go back any further).
"Key Value pair". |
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/72/builds/2581 Here is the relevant piece of the build log for the reference:
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/51/builds/2884 Here is the relevant piece of the build log for the reference:
|
…d-gfx:4d9f2b277a22 Local branch amd-gfx 4d9f2b2 Merged main:7b4b85b75d22a792b2ef80e6af4f0faf18da0a43 into amd-gfx:0d77823cc97e Remote branch main ca53611 [llvm] Use range-based for loops (NFC) (llvm#105861) Change-Id: I32ea54eb199ff599dc3c4775f15022248d20b576
No description provided.