This is an archive of the discontinued LLVM Phabricator instance.

[BOLT] Avoid pointless loop rotation
ClosedPublic

Authored by spupyrev on Mar 17 2022, 9:53 AM.

Details

Summary

It seems the earlier implementation does not follow the description
in LoopRotationPass.h: It rotates loops even if they are already laid out
correctly. The diff adjusts the behaviour.

Given that the impact of LoopInversionPass is minor, this change won't
yield significant perf differences. Tested on clang-10: there seems to be a
0.1%-0.3% cpu win and a small reduction of branch misses.

Before:
BOLT-INFO: 120 Functions were reordered by LoopInversionPass

After:
BOLT-INFO: 79 Functions were reordered by LoopInversionPass

Diff Detail

Event Timeline

spupyrev created this revision.Mar 17 2022, 9:53 AM
Herald added a reviewer: Amir. · View Herald Transcript
Herald added a reviewer: maksfb. · View Herald Transcript
Herald added a project: Restricted Project. · View Herald Transcript
Herald added a subscriber: ayermolo. · View Herald Transcript
spupyrev requested review of this revision.Mar 17 2022, 9:53 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2022, 9:53 AM
spupyrev edited the summary of this revision. (Show Details)Mar 17 2022, 9:56 AM
yota9 added a comment.Mar 17 2022, 9:57 AM

Hello! Thank you for your patch, I will take a closer look a bit later, that time may you please add the test to repro the issue?

lebedev.ri retitled this revision from adjusting LoopRotationPass to [LoopInversionPass] Avoid pointless loop rotation.Mar 17 2022, 10:01 AM

Actually, this is not in llvm/, so ignore me.

spupyrev updated this revision to Diff 416258.Mar 17 2022, 11:06 AM

adding a test

yota9 added inline comments.Mar 17 2022, 11:18 AM
bolt/test/X86/loop-inversion-pass.s
9

Just wondering what is the reason to change reorder-blocks alg here?

spupyrev added inline comments.Mar 17 2022, 11:22 AM
bolt/test/X86/loop-inversion-pass.s
9

'cache+' and 'ext-tsp' are synonyms; this is different names of the same alg.

I'll let someone else to get rid of 'cache+' altogether at some point )

yota9 added inline comments.Mar 18 2022, 5:48 AM
bolt/lib/Passes/LoopInversionPass.cpp
61

Could you please re-format this "if" condition back how it was and use "else if" for new condition?

yota9 added inline comments.Mar 18 2022, 6:14 AM
bolt/lib/Passes/LoopInversionPass.cpp
61

Don't pay attention to the first part of the comment above, just reformat "else if" please

Amir added a comment.Mar 21 2022, 6:35 AM

Can you please retitle to "[BOLT] Avoid pointless loop rotation"?

bolt/test/X86/loop-inversion-pass.s
9

Can you please keep cache+ here? We'll remove cache+ in a follow-up diff.

spupyrev retitled this revision from [LoopInversionPass] Avoid pointless loop rotation to [BOLT] Avoid pointless loop rotation.Mar 21 2022, 7:38 AM
spupyrev updated this revision to Diff 416939.Mar 21 2022, 7:46 AM

review

@yota9 please let me know if I got your suggestion right.
Also would be great to understand the motivation for the suggested change; I actually
like the earlier version more, as it shows the two conditions symmetrically (but that
might be just a personal preference)

yota9 added a comment.Mar 21 2022, 8:00 AM

@spupyrev I don't see strict rules about the else/eslse if statement indeed. This is clearly personal preference, I hust don't like the idea of extra nesting where you can go without it. As for my personal experience LLVM tries to eliminate extra nesting where it is possible. But speaking of the new variant - I think you need to add braces for "else if " statement, since in coding style https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements there are some words about the if/else statement to be uniform e.g.

// Use braces for the `if` block to keep it uniform with the else block.
if (isa<FunctionDecl>(D)) {
  handleFunctionDecl(D);
} else {
  // In this else case, it is necessary that we explain the situation with this
  // surprisingly long comment, so it would be unclear without the braces whether
  // the following statement is in the scope of the `if`.
  handleOtherDecl(D);
}

@spupyrev I don't see strict rules about the else/eslse if statement indeed. This is clearly personal preference, I hust don't like the idea of extra nesting where you can go without it. As for my personal experience LLVM tries to eliminate extra nesting where it is possible. But speaking of the new variant - I think you need to add braces for "else if " statement, since in coding style https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements there are some words about the if/else statement to be uniform e.g.

// Use braces for the `if` block to keep it uniform with the else block.
if (isa<FunctionDecl>(D)) {
  handleFunctionDecl(D);
} else {
  // In this else case, it is necessary that we explain the situation with this
  // surprisingly long comment, so it would be unclear without the braces whether
  // the following statement is in the scope of the `if`.
  handleOtherDecl(D);
}

Could you please suggest the code change?

yota9 added a comment.Mar 21 2022, 8:21 AM

@spupyrev I don't see strict rules about the else/eslse if statement indeed. This is clearly personal preference, I hust don't like the idea of extra nesting where you can go without it. As for my personal experience LLVM tries to eliminate extra nesting where it is possible. But speaking of the new variant - I think you need to add braces for "else if " statement, since in coding style https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements there are some words about the if/else statement to be uniform e.g.

// Use braces for the `if` block to keep it uniform with the else block.
if (isa<FunctionDecl>(D)) {
  handleFunctionDecl(D);
} else {
  // In this else case, it is necessary that we explain the situation with this
  // surprisingly long comment, so it would be unclear without the braces whether
  // the following statement is in the scope of the `if`.
  handleOtherDecl(D);
}

Could you please suggest the code change?

Sure:

 if (LoopCount < ExitCount) {
   if (BBIndex > SuccBBIndex)
     continue;
 } else if (BBIndex < SuccBBIndex) {
   continue;
}

Like this if you don't mind :)

yota9 accepted this revision.Mar 21 2022, 8:25 AM

LGTM, thank you for the fix!

This revision is now accepted and ready to land.Mar 21 2022, 8:25 AM
This revision was automatically updated to reflect the committed changes.