Skip to content

[Support] Do not use llvm::size in getLoopPreheader #94540

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 1 commit into from
Jun 8, 2024

Conversation

bnbarham
Copy link
Contributor

@bnbarham bnbarham commented Jun 5, 2024

BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() was changed in
7243607 to use llvm::size rather than
the checking that child_begin() + 1 == child_end(). llvm::size
requires that std::distance be O(1) and hence that clients support
random access. Use llvm::hasSingleElement instead.

@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2024

@llvm/pr-subscribers-llvm-support

Author: Ben Barham (bnbarham)

Changes

BlockT *LoopBase&lt;BlockT, LoopT&gt;::getLoopPreheader() was changed in 7243607 to use llvm::size rather than the checking that child_begin() + 1 == child_end(). llvm::size requires that std::distance be O(1) and hence that clients support random access. Switch back to the original check.


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

1 Files Affected:

  • (modified) llvm/include/llvm/Support/GenericLoopInfoImpl.h (+4-1)
diff --git a/llvm/include/llvm/Support/GenericLoopInfoImpl.h b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
index 1e0d0ee446fc4..c9c7354937545 100644
--- a/llvm/include/llvm/Support/GenericLoopInfoImpl.h
+++ b/llvm/include/llvm/Support/GenericLoopInfoImpl.h
@@ -208,7 +208,10 @@ BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader() const {
     return nullptr;
 
   // Make sure there is only one exit out of the preheader.
-  if (llvm::size(llvm::children<BlockT *>(Out)) != 1)
+  typedef GraphTraits<BlockT *> BlockTraits;
+  typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
+  ++SI;
+  if (SI != BlockTraits::child_end(Out))
     return nullptr; // Multiple exits from the block, must not be a preheader.
 
   // The predecessor has exactly one successor, so it is a preheader.

@bnbarham
Copy link
Contributor Author

bnbarham commented Jun 6, 2024

@kazutakahirata this was a NFC commit, was there an underlying reason for the change or are you happy with reverting this part of it?

Comment on lines 211 to 214
typedef GraphTraits<BlockT *> BlockTraits;
typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
++SI;
if (SI != BlockTraits::child_end(Out))
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we do something like this?

Suggested change
typedef GraphTraits<BlockT *> BlockTraits;
typename BlockTraits::ChildIteratorType SI = BlockTraits::child_begin(Out);
++SI;
if (SI != BlockTraits::child_end(Out))
if (!llvm::hasSingleElement(GraphTraits<BlockT *>::children(Out)))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah nice, much better. Switched to llvm::hasSingleElement, thanks!

`BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader()` was changed in
7243607 to use `llvm::size` rather than
the checking that `child_begin() + 1 == child_end()`. `llvm::size`
requires that `std::distance` be O(1) and hence that clients support
random access. Use `llvm::hasSingleElement` instead.
@bnbarham bnbarham force-pushed the do-not-require-random branch from 527f574 to 3d0d614 Compare June 8, 2024 04:09
Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@bnbarham bnbarham merged commit 6885281 into llvm:main Jun 8, 2024
7 checks passed
@bnbarham bnbarham deleted the do-not-require-random branch June 8, 2024 07:32
nekoshirro pushed a commit to nekoshirro/Alchemist-LLVM that referenced this pull request Jun 9, 2024
`BlockT *LoopBase<BlockT, LoopT>::getLoopPreheader()` was changed in
7243607 to use `llvm::size` rather than
the checking that `child_begin() + 1 == child_end()`. `llvm::size`
requires that `std::distance` be O(1) and hence that clients support
random access. Use `llvm::hasSingleElement` instead.

Signed-off-by: Hafidz Muzakky <[email protected]>
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants