[PATCH] Add a builtin for indexing into parameter packs

Louis Dionne via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 30 06:13:34 PST 2015


Hi,

I wrote a patch that adds a `__nth_element` builtin that allows fetching the 
n-th type of a parameter pack with very little compile-time overhead. The patch 
was inspired by r252036 and r252115 by David Majnemer, which add a similar 
`__make_integer_seq` builtin for efficiently creating a `std::integer_sequence`. 
At its core, the builtin makes the following specialization

__nth_element<i, T1, ..., Tn>

equivalent to Ti. Further analysis of the problem this patch is trying to solve 
is available on a blog post I just wrote [1]. The post also present benchmarks 
showing the compile-time improvement over existing techniques.

The patch is currently in a very rough state. It is my first time contributing 
to Clang, and I encountered some issues that I hacked around just to get a 
working solution. If there is interest in this patch, I will gladly improve 
it until it is in an acceptable state, but I will most likely need some 
pointers to do so. Things that are hacky right now:

1. I wasn't able to create a non-type template parameter from a builtin type. 
   Hence, `__nth_element` must currently be used as 
     __nth_element<TypeOfTheIndex, Index, Ts...>
   instead of just `__nth_element<Index, Ts...>`, while forcing `Index` to be 
   something like `unsigned long`. This way, I was able to copy Majnemer's 
   code that creates non-type template parameters. This is probably trivial 
   to change, but I couldn't figure it out immediately.

2. I wasn't sure how to retrieve an actual integer from an `APSInt`, so right 
   now I use `getExtValue()`. This is probably wrong, as I didn't really 
   understand what I was doing.

There are certainly other issues that a code review would address, if we make 
it that far. Also, the name `__nth_element` is badly chosen, since it seems 
to refer to the `nth_element` algorithm of the standard library. We should 
settle on something else, but I have no preference.

I also wrote a corresponding patch for libc++ to use the builtin to implement
`std::tuple_element`. You can find both patches attached to this message. Also,
these patches were initially posted as bug reports [2] and [3], respectively.

Please let me know if there is any interest for going forward with these patches.

Regards,
Louis Dionne

[1]: http://ldionne.com/2015/11/29/efficient-parameter-pack-indexing
[2]: https://llvm.org/bugs/show_bug.cgi?id=25670
[3]: https://llvm.org/bugs/show_bug.cgi?id=25671

-------------- next part --------------
A non-text attachment was scrubbed...
Name: clang-sema.diff
Type: application/octet-stream
Size: 16618 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151130/ab8a2809/attachment.obj>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: libcxx-tuple_element.diff
Type: application/octet-stream
Size: 1073 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20151130/ab8a2809/attachment-0001.obj>


More information about the cfe-commits mailing list