Skip to content
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

[ADT][NFCI]: Fix iterator category for graph iterators with external … #116403

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Maetveis
Copy link
Contributor

…storage

Set the iterator category for graph iterators to input_iterator_tag when the visited set is stored externally. In that case we can't provide multi-pass guarantee, so we should not claim to be a forward iterator.

Fixes: #116400

…storage

Set the iterator category for graph iterators to input_iterator_tag when
the visited set is stored externally. In that case we can't provide
multi-pass guarantee, so we should not claim to be a forward iterator.

Fixes: llvm#116400
@llvmbot
Copy link

llvmbot commented Nov 16, 2024

@llvm/pr-subscribers-llvm-adt

Author: Mészáros Gergely (Maetveis)

Changes

…storage

Set the iterator category for graph iterators to input_iterator_tag when the visited set is stored externally. In that case we can't provide multi-pass guarantee, so we should not claim to be a forward iterator.

Fixes: #116400


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

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/DepthFirstIterator.h (+5-1)
  • (modified) llvm/include/llvm/ADT/PostOrderIterator.h (+5-1)
diff --git a/llvm/include/llvm/ADT/DepthFirstIterator.h b/llvm/include/llvm/ADT/DepthFirstIterator.h
index 71053c2d0d8a8e..4ced758343806e 100644
--- a/llvm/include/llvm/ADT/DepthFirstIterator.h
+++ b/llvm/include/llvm/ADT/DepthFirstIterator.h
@@ -39,6 +39,7 @@
 #include "llvm/ADT/iterator_range.h"
 #include <iterator>
 #include <optional>
+#include <type_traits>
 #include <utility>
 #include <vector>
 
@@ -84,7 +85,10 @@ template <class GraphT,
           bool ExtStorage = false, class GT = GraphTraits<GraphT>>
 class df_iterator : public df_iterator_storage<SetType, ExtStorage> {
 public:
-  using iterator_category = std::forward_iterator_tag;
+  // When External storage is used we are not multi-pass safe.
+  using iterator_category =
+      std::conditional_t<ExtStorage, std::input_iterator_tag,
+                         std::forward_iterator_tag>;
   using value_type = typename GT::NodeRef;
   using difference_type = std::ptrdiff_t;
   using pointer = value_type *;
diff --git a/llvm/include/llvm/ADT/PostOrderIterator.h b/llvm/include/llvm/ADT/PostOrderIterator.h
index edf0401a751708..1cbd3c170052c7 100644
--- a/llvm/include/llvm/ADT/PostOrderIterator.h
+++ b/llvm/include/llvm/ADT/PostOrderIterator.h
@@ -23,6 +23,7 @@
 #include <iterator>
 #include <optional>
 #include <set>
+#include <type_traits>
 #include <utility>
 
 namespace llvm {
@@ -95,7 +96,10 @@ template <class GraphT,
           bool ExtStorage = false, class GT = GraphTraits<GraphT>>
 class po_iterator : public po_iterator_storage<SetType, ExtStorage> {
 public:
-  using iterator_category = std::forward_iterator_tag;
+  // When External storage is used we are not multi-pass safe.
+  using iterator_category =
+      std::conditional_t<ExtStorage, std::input_iterator_tag,
+                         std::forward_iterator_tag>;
   using value_type = typename GT::NodeRef;
   using difference_type = std::ptrdiff_t;
   using pointer = value_type *;

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

Can you add a test for this?

@Maetveis
Copy link
Contributor Author

Can you add a test for this?

Sure, see latest commit.

Copy link

github-actions bot commented Nov 16, 2024

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

Check that using the same iterators for multiple passes produces the
same result, as required by the C++ standard for forward iterators.

Add a static_asserts for the iterator_category of Graph iterators
(BreadthFirstIterator, DepthFirstIterator, PostOrderIterator).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants