Skip to content

[MC] Optimize loops in MC. #98114

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
merged 2 commits into from
Jul 12, 2024
Merged

[MC] Optimize loops in MC. #98114

merged 2 commits into from
Jul 12, 2024

Conversation

chestnykh
Copy link
Contributor

https://llvm.org/docs/CodingStandards.html tells us that we should avoid evaluating .end() each time if possible.

@llvmbot llvmbot added the mc Machine (object) code label Jul 9, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 9, 2024

@llvm/pr-subscribers-mc

Author: Dmitriy Chestnykh (chestnykh)

Changes

https://llvm.org/docs/CodingStandards.html tells us that we should avoid evaluating .end() each time if possible.


Full diff: https://github.com/llvm/llvm-project/pull/98114.diff

3 Files Affected:

  • (modified) llvm/lib/MC/MCParser/AsmParser.cpp (+3-3)
  • (modified) llvm/lib/MC/MCParser/MasmParser.cpp (+3-3)
  • (modified) llvm/lib/MC/XCOFFObjectWriter.cpp (+14-11)
diff --git a/llvm/lib/MC/MCParser/AsmParser.cpp b/llvm/lib/MC/MCParser/AsmParser.cpp
index f3caa90eedfb1..dba56b15424d5 100644
--- a/llvm/lib/MC/MCParser/AsmParser.cpp
+++ b/llvm/lib/MC/MCParser/AsmParser.cpp
@@ -6116,8 +6116,8 @@ bool AsmParser::parseMSInlineAsm(
   const char *AsmStart = ASMString.begin();
   const char *AsmEnd = ASMString.end();
   array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
-  for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
-    const AsmRewrite &AR = *it;
+  for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
+    const AsmRewrite &AR = *I;
     // Check if this has already been covered by another rewrite...
     if (AR.Done)
       continue;
@@ -6160,7 +6160,7 @@ bool AsmParser::parseMSInlineAsm(
         SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
         size_t OffsetLen = OffsetName.size();
         auto rewrite_it = std::find_if(
-            it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
+            I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
               return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
                      (FusingAR.Kind == AOK_Input ||
                       FusingAR.Kind == AOK_CallInput);
diff --git a/llvm/lib/MC/MCParser/MasmParser.cpp b/llvm/lib/MC/MCParser/MasmParser.cpp
index 653cc64b4c36a..f64b7f62d61d0 100644
--- a/llvm/lib/MC/MCParser/MasmParser.cpp
+++ b/llvm/lib/MC/MCParser/MasmParser.cpp
@@ -7474,8 +7474,8 @@ bool MasmParser::parseMSInlineAsm(
   const char *AsmStart = ASMString.begin();
   const char *AsmEnd = ASMString.end();
   array_pod_sort(AsmStrRewrites.begin(), AsmStrRewrites.end(), rewritesSort);
-  for (auto it = AsmStrRewrites.begin(); it != AsmStrRewrites.end(); ++it) {
-    const AsmRewrite &AR = *it;
+  for (auto I = AsmStrRewrites.begin(), E = AsmStrRewrites.end(); I != E; ++I) {
+    const AsmRewrite &AR = *I;
     // Check if this has already been covered by another rewrite...
     if (AR.Done)
       continue;
@@ -7518,7 +7518,7 @@ bool MasmParser::parseMSInlineAsm(
         SMLoc OffsetLoc = SMLoc::getFromPointer(AR.IntelExp.OffsetName.data());
         size_t OffsetLen = OffsetName.size();
         auto rewrite_it = std::find_if(
-            it, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
+            I, AsmStrRewrites.end(), [&](const AsmRewrite &FusingAR) {
               return FusingAR.Loc == OffsetLoc && FusingAR.Len == OffsetLen &&
                      (FusingAR.Kind == AOK_Input ||
                       FusingAR.Kind == AOK_CallInput);
diff --git a/llvm/lib/MC/XCOFFObjectWriter.cpp b/llvm/lib/MC/XCOFFObjectWriter.cpp
index 03a4ee831d895..122f19367ae7b 100644
--- a/llvm/lib/MC/XCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/XCOFFObjectWriter.cpp
@@ -1385,11 +1385,12 @@ void XCOFFObjectWriter::addExceptionEntry(
 unsigned XCOFFObjectWriter::getExceptionSectionSize() {
   unsigned EntryNum = 0;
 
-  for (auto it = ExceptionSection.ExceptionTable.begin();
-       it != ExceptionSection.ExceptionTable.end(); ++it)
+  for (auto I = ExceptionSection.ExceptionTable.begin(),
+            E = ExceptionSection.ExceptionTable.end();
+      I != E; ++I)
     // The size() gets +1 to account for the initial entry containing the
     // symbol table index.
-    EntryNum += it->second.Entries.size() + 1;
+    EntryNum += I->second.Entries.size() + 1;
 
   return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize64
                                : XCOFF::ExceptionSectionEntrySize32);
@@ -1397,11 +1398,12 @@ unsigned XCOFFObjectWriter::getExceptionSectionSize() {
 
 unsigned XCOFFObjectWriter::getExceptionOffset(const MCSymbol *Symbol) {
   unsigned EntryNum = 0;
-  for (auto it = ExceptionSection.ExceptionTable.begin();
-       it != ExceptionSection.ExceptionTable.end(); ++it) {
-    if (Symbol == it->second.FunctionSymbol)
+  for (auto I = ExceptionSection.ExceptionTable.begin(),
+            E = ExceptionSection.ExceptionTable.end();
+      I != E; ++I) {
+    if (Symbol == I->second.FunctionSymbol)
       break;
-    EntryNum += it->second.Entries.size() + 1;
+    EntryNum += I->second.Entries.size() + 1;
   }
   return EntryNum * (is64Bit() ? XCOFF::ExceptionSectionEntrySize64
                                : XCOFF::ExceptionSectionEntrySize32);
@@ -1667,17 +1669,18 @@ void XCOFFObjectWriter::writeSectionForDwarfSectionEntry(
 void XCOFFObjectWriter::writeSectionForExceptionSectionEntry(
     const MCAssembler &Asm, ExceptionSectionEntry &ExceptionEntry,
     uint64_t &CurrentAddressLocation) {
-  for (auto it = ExceptionEntry.ExceptionTable.begin();
-       it != ExceptionEntry.ExceptionTable.end(); it++) {
+  for (auto I = ExceptionEntry.ExceptionTable.begin(),
+          E = ExceptionEntry.ExceptionTable.end();
+     I != E; ++I) {
     // For every symbol that has exception entries, you must start the entries
     // with an initial symbol table index entry
-    W.write<uint32_t>(SymbolIndexMap[it->second.FunctionSymbol]);
+    W.write<uint32_t>(SymbolIndexMap[I->second.FunctionSymbol]);
     if (is64Bit()) {
       // 4-byte padding on 64-bit.
       W.OS.write_zeros(4);
     }
     W.OS.write_zeros(2);
-    for (auto &TrapEntry : it->second.Entries) {
+    for (auto &TrapEntry : I->second.Entries) {
       writeWord(TrapEntry.TrapAddress);
       W.write<uint8_t>(TrapEntry.Lang);
       W.write<uint8_t>(TrapEntry.Reason);

Copy link

github-actions bot commented Jul 9, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@chestnykh chestnykh force-pushed the optimize-loops-in-MC branch from d2d3977 to 13bafeb Compare July 9, 2024 05:56
@chestnykh chestnykh changed the title Optimize loops in MC. [MC] Optimize loops in MC. Jul 9, 2024
Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

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

(nit) We don't usually end the subject line of a commit message with a period.

@chestnykh chestnykh force-pushed the optimize-loops-in-MC branch from 13bafeb to 20032d3 Compare July 9, 2024 06:34
@@ -1385,23 +1385,25 @@ void XCOFFObjectWriter::addExceptionEntry(
unsigned XCOFFObjectWriter::getExceptionSectionSize() {
unsigned EntryNum = 0;

for (auto it = ExceptionSection.ExceptionTable.begin();
it != ExceptionSection.ExceptionTable.end(); ++it)
for (auto I = ExceptionSection.ExceptionTable.begin(),
Copy link
Member

Choose a reason for hiding this comment

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

this can use for each

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Rewritten to for_each

@chestnykh
Copy link
Contributor Author

(nit) We don't usually end the subject line of a commit message with a period.

Got it

@chestnykh chestnykh force-pushed the optimize-loops-in-MC branch 2 times, most recently from b7b980e to fdf9be2 Compare July 9, 2024 07:04
Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

Can't some/all of these be range based for loops instead?

@chestnykh
Copy link
Contributor Author

Can't some/all of these be range based for loops instead?

Is range based loop preferable?

Can't some/all of these be range based for loops instead?

I left comments where the loop can be transformed into range-based. Other loops use iterator in their body so i think we can leave them without changes.

@chestnykh chestnykh force-pushed the optimize-loops-in-MC branch from fdf9be2 to 6d4ae81 Compare July 9, 2024 07:35
@nikic
Copy link
Contributor

nikic commented Jul 9, 2024

Can't some/all of these be range based for loops instead?

Is range based loop preferable?

Yes :) From the same guide: https://llvm.org/docs/CodingStandards.html#use-range-based-for-loops-wherever-possible

@s-barannikov s-barannikov self-requested a review July 9, 2024 07:42
@chestnykh chestnykh force-pushed the optimize-loops-in-MC branch from 6d4ae81 to 659626a Compare July 9, 2024 07:47
@chestnykh chestnykh force-pushed the optimize-loops-in-MC branch from 659626a to 4b9c1ef Compare July 9, 2024 07:57
chestnykh added 2 commits July 9, 2024 10:59
In accordance with https://llvm.org/docs/CodingStandards.html
we shouldn't do this. Use range-based loops where possible
Copy link
Contributor

@s-barannikov s-barannikov left a comment

Choose a reason for hiding this comment

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

LGTM
A note for the future: amending commits and force pushing are discouraged, see
https://llvm.org/docs/GitHub.html#updating-pull-requests

@chestnykh
Copy link
Contributor Author

LGTM A note for the future: amending commits and force pushing are discouraged, see https://llvm.org/docs/GitHub.html#updating-pull-requests

FYI i don't have commit access so please commit my PR when it is ready

@nikic nikic merged commit 345861b into llvm:main Jul 12, 2024
5 of 7 checks passed
aaryanshukla pushed a commit to aaryanshukla/llvm-project that referenced this pull request Jul 14, 2024
https://llvm.org/docs/CodingStandards.html tells us that we should avoid
evaluating `.end()` each time if possible.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mc Machine (object) code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants