[PATCH] Add iterator for PHINode value/BB pair

Pete Cooper peter_cooper at apple.com
Thu Jul 23 12:29:36 PDT 2015


Sorry this took so long.  Had to learn a whole bunch about varargs templates.

This is a first attempt at an LLVM zip iterator.  I needed to add methods to STLExtras.h which do ++tuple<> and *tuple<>.  Originally I put those under different names and didn’t override the operators, but then I didn’t see any harm in overriding them.  I can rename them if anyone has a preference for that.

The zip iterator itself is almost identical to the edge_iterator I had before, just that it is now templated on its iterator type.  I’ve provided a subclass in zip_input_iterator for users who don’t need all the parameters and just want to provide a varargs of iterator types.  All of this is of course up for discussion, this is just one way I could see to implement it.

I also allowed for zip_iterator_traits so that users could provide their own type to return from operator*.  This is so that users don’t have to remember which iterator is first, second, get<0>, etc.  Personally I think its much more readable than to have .Value, .Block, etc.  This is optional though, as there’s a default implementation to provide a dereferenced tuple type from operator*.

BTW, no idea how much of this is MSVC safe.  I did find a user of 'auto fn()->decltype()’ in PDBTypes.h, so i’m not the first user of that, but this will need someone with MSVC to ok the final implementation.

I also need to write a unit test for all the functionality this enables.  Duncan mentioned std::copy requiring operator* to return a reference.  I’m still working through whether thats working or whether we only really have foreach support right now.

Duncan also pointed out in person, perhaps this is overkill as the edge_iterator I had before was easier to understand than what we have here.  Perhaps we’d prefer to hold off on a zip_iterator until we are sure we have more users for it.  If thats the case, then i’m happy for this to just be a demonstration of how it could be done, but not necessarily something to commit now.

Cheers,
Pete



> On Jul 22, 2015, at 5:10 PM, David Blaikie <dblaikie at gmail.com> wrote:
> 
> (oops, dropped the list by accident)
> ---------- Forwarded message ----------
> From: David Blaikie <dblaikie at gmail.com <mailto:dblaikie at gmail.com>>
> Date: Wed, Jul 22, 2015 at 5:10 PM
> Subject: Re: [PATCH] Add iterator for PHINode value/BB pair
> To: "Duncan P. N. Exon Smith" <dexonsmith at apple.com <mailto:dexonsmith at apple.com>>
> 
> 
> 
> 
> On Wed, Jul 22, 2015 at 5:05 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com <mailto:dexonsmith at apple.com>> wrote:
> 
> > On 2015-Jul-22, at 16:57, David Blaikie <dblaikie at gmail.com <mailto:dblaikie at gmail.com>> wrote:
> >
> >
> >
> > On Wed, Jul 22, 2015 at 4:50 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com <mailto:dexonsmith at apple.com>> wrote:
> >
> > > On 2015-Jul-22, at 15:07, David Blaikie <dblaikie at gmail.com <mailto:dblaikie at gmail.com>> wrote:
> > >
> > >
> > >
> > > On Wed, Jul 22, 2015 at 12:11 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com <mailto:dexonsmith at apple.com>> wrote:
> > >
> > > > On 2015-Jul-21, at 21:20, Pete Cooper <peter_cooper at apple.com <mailto:peter_cooper at apple.com>> wrote:
> > > >
> > > > Thanks for all the feedback.  This is a patch which addresses all of it.
> > > >
> > > > <phinode.diff>
> > > >
> > >
> > > > +
> > > > +    const PHINodeEdge operator*() const {
> > >
> > > No reason for the const in `const PHINodeEdge` here.
> > >
> > > to support operator-> you have to return a pointer, which means you need the PHINodeEdge storage inside the iterator to point to (& then you can just return a const ref from op*)
> >
> > Do we need operator->()?
> >
> > Seems poor form not to provide it (someone'll trip over it pretty quickly, I'd imagine)
> >
> > If so, we can return a proxy object:
> >
> >     struct PHINodeEdgeArrowProxy {
> >       const PHINodeEdge RefProxy;
> >     public:
> >       PHINodeEdgeArrowProxy(PHINodeEdge Edge) : RefProxy(Edge) {}
> >       const PHINodeEdge *operator->() const { return &RefProxy; }
> >     };
> >
> >     PHINodeEdgeArrowProxy operator->() const { return operator*(); }
> >
> > Then we avoid bloating the iterator, and only make the copy when we
> > actually need it.
> >
> > Non-conforming in terms of the iterator traits, I would imagine
> 
> AFAICT, iterators only require that `i->m` has the same semantics as
> `(*i).m`.  The return type isn't specified.
> 
> Fair enough - can't quite find the wording on what constitutes the iterators value type (as is mentioned in the iterator traits) or how it might relate, but the basic definition is as you've mentioned.
>  
> > - but I take it that's the N1550 stuff you're talking about below? It makes these sort of proxy solutions valid?
> >
> 
> IIRC, proxy solutions are always valid for InputIterator and for
> OutputIterator; it's ForwardIterator that prevents `operator*()`
> from returning a proxy.
> 
> *nod* I don't mind violating that too much, if that's the preference.
>  
> (This makes `std::vector<bool>::iterator` invalid.)
> 
> Yep. Fails in so many ways.
>  
> 
> >
> > >
> > >
> > > > +      return { *Values, *Blocks };
> > > > +    }
> > > > +  };
> > > > +
> > >
> > > Otherwise LGTM.  Might want to pass it through clang-format; I noticed
> > > some minor whitespace oddities.
> > >
> > > > *snip*
> > > >> On Jul 21, 2015, at 8:21 PM, Duncan P. N. Exon Smith <dexonsmith at apple.com <mailto:dexonsmith at apple.com>> wrote:
> > > >>
> > > >>
> > > >>
> > > >> If you drop the requirements from `forward_iterator` to
> > > >> `input_iterator`, then you're allowed to return a `PHINodeEdge` by-value
> > > >> here instead of by-reference (unfortunately this makes it illegal to use
> > > >> a bunch of STL algorithms; STL iterator traits are completely broken
> > > >> IMO).
> > > > I’m fine with this.  David, Chandler, please let me know how you feel about this.
> > > >
> > > > Also, i forgot to say that I considered doing a zip iterator and inheriting this from it.  This is something I think Chandler or David mentioned a few months ago.  If there’s been any progress in the C++ committee on that then i’m happy to try implement something better.  If not, then i don’t think what I have here should be difficult to change in future.
> > >
> > > I guess a generalized version would return a
> > > `std::tuple<Value *const &, BasicBlock *const &>` or some such.  Not
> > > sure how to actually make zip iterators work well without something
> > > like N1550 though.
> > >
> > > What's N1550 offer to make zip work?
> >
> > s/work/& well/
> >
> > I think bloated iterators are bad, but without bloating them (so you
> > can return a T&), you can't call a zip_iterator a ForwardIterator,
> > which means you can't use it in a bunch of algorithms that you might
> > want to (such as the destination for `std::copy()`).
> >
> > N1550 let's you correctly identify the type of traversal the
> > zip_iterator can do, without requiring a T& return from operator*().
> >
> > > I wouldn't mind a slightly half-hearted version that works for basic/common cases...
> 
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu <mailto:llvm-commits at cs.uiuc.edu>
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits <http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150723/bcbc2988/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: zip_iterator.diff
Type: application/octet-stream
Size: 10290 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150723/bcbc2988/attachment.obj>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150723/bcbc2988/attachment-0001.html>


More information about the llvm-commits mailing list