libstdc++: Handling user-defined interlike object in submdspan.
Luc Grosheintz
luc.grosheintz@gmail.com
Tue Sep 30 12:39:27 GMT 2025
On 9/30/25 1:01 PM, Tomasz Kaminski wrote:
> On Tue, Sep 30, 2025 at 12:50 PM Luc Grosheintz <luc.grosheintz@gmail.com>
> wrote:
>
>>
>>
>> On 9/30/25 11:44 AM, Tomasz Kaminski wrote:
>>> On Tue, Sep 30, 2025 at 11:05 AM Luc Grosheintz <
>> luc.grosheintz@gmail.com>
>>> wrote:
>>>
>>>>
>>>> On 9/30/25 10:01 AM, Tomasz Kaminski wrote:
>>>>>>
>>>>>> I canonicalize tuple-like to `std::tuple`s of possibly wrapped
>>>>>> index_type; and not to stride<*, *, std::cw<1>>.
>>>>>>
>>>>>> The advantage is that the preconditions remain the same, because
>>>>>> tuples still exist, it's just that we don't encounter as much
>>>>>> variability but the kinds of objects remain the same.
>>>>>>
>>>>>> Generally, and as written in the paper, it does make sense to maybe
>>>>>> not canonicalize things like:
>>>>>>
>>>>>> - std::array<index_type, 2>
>>>>>> - std::tuple<std::constant_wrapper<index_type, *>,
>>>>>> std::constant_wrapper<index_type, *>>
>>>>>>
>>>>> Note that pair-like for the submdspan is representing [begin, end)
>> range,
>>>>> while the strided_slice has [begin, offset, stride)?
>>>>>
>>>>
>>>> Yes, the thought is that
>>>> strided_slice{begin, end - begin, std::cw<1>}
>>>> is a less direct way of expressing the idea. So users
>>>> might opt for one of the other two instead. If we allow
>>>> those to be passed through unchanged, they'll not have to
>>>> worry about optimizing their code by making sure they
>>>> use stride_slice{..., std::cw<1>}, it would just work.
>>>>
>>> Note, that strided_slice{..., std::cw<1>} may still be passed, so I do
>> not
>>> see how that removes the number of cases that may be optimized.
>>> Canonicalization is strictly reducing the number of cases that user
>> defined
>>> mappings needs to handle.
>>>
>>
>> True, and not the point I'm trying to make. To me it seems like we've
>> put a lot of effort into optimizing small inefficiencies, e.g. removing
>> __glibcxx_asserts if they're known to be true at compile time, etc.
>>
>> Therefore, I was expecting push-back if I simply copy the slices into a
>> canonical form. Causing every use of std::array<*, 2> and std::tuple to
>> require a copy.
>
> For index_type, passing them by value (copy) should be more efficient than
> passing by reference, and the submdspan and standard layouts take them by
> value,
> so we copy std::array or std::tuple anway (see
> https://eel.is/c++draft/mdspan.sub.map).
> I do not see much difference between copying two ints by passing
> std::array<int, 2>
> by value or constructing prvalue strided_slice<int, int,
> constant_wrapper<1>>.
>
> std::tuple has a user-provided assignment operator, so it is not trivially
> copyable, thus
> will not be passed by registers.
>
> Note, that for the implementation, I would not use
> submdspan_canonicalize_indicies,
> that creates a tuple. And instead do something like:
> submdspan(SliceSpecifiers... slices)
> {
> if constexpr (!is_cannonical_slice<SliceSpecifiers> || ....)
> return submdspan(canonicalize(slices)....);
> else
> {
> // Now all slices are cannonica.
> }
> }
>
>
>> I've recently seen the GCC optimizer perform small
>> miracles, so canonicalize first and then add exceptions as needed makes
>> sense to me.
>>
>> To make the above more meaningful: I don't believe that users would
>> express std::array{being, end} as:
>>
>> std::strided_slice{begin, end - begin, std::cw<1>}.
>>
>> Hence, the assumption that exceptions for std::array and std::tuple
>> make sense (for the sake of saving copies).
>>
> Copies are still done, as we define the friend function as passing by value.
> Or do you mean something else here?
We (or maybe just I) definitely need code to disassemble :) I was
distinguishing two cases where:
- one just passes a value from one function to the next
by value,
- a conversion happens as part of the copy, e.g.
std::array -> std::strided_slice (because extent = end - begin),
or std::array<int, 2> -> std::array<uint8_t, 2>.
The former I disregarded because inlining gets rid of them (hopefully).
The latter where the ones I was more worried *might* cost something
at runtime.
I think I know how to proceed: I'll try and apply as much of P3663
that isn't public API and then send that.
>
>>>
>>>>
>>>>>
>>>>>>
>>>>>> because those are very natural ways of specifying that kind of slice.
>>>>>> Therefore, even if there's a cost to canonicalization, every kind of
>>>>>> slice has a convenient canonical form (that users can provide to skip
>>>>>> canonicalization).
>>>>>>
>>>>> I do not think there should be differences between canonical forms of
>>>>> strided_slice and pair_like, they represent the same concept. And
>>>>> This should reduce the number of various concepts that we need to
>> handle.
>>>>> Note that the __first, __last functions are now completely gone from
>> the
>>>>> wording.
>>>>
>>>> I agree that multiple canonical forms is odd. On the other hand
>>>> it's mentioned/suggested in the paper that standardized mapping
>>>> may do less canonicalization:
>>>>
>>>>
>>>>
>> https://isocpp.org/files/papers/D3663R3.html#submdspan-can-skip-canonicalization-for-standard-layout-mappings
>>>
>>> Standard mapping are required to strided_slice{..., std::cw<1>} anyway,
>>> and
>>> I would prefer to avoid having two code paths, one for standard mapping
>> and
>>> other
>>> for user-defined mappings, if that is not proven strictly necessary.
>>>
>>>
>>>>
>>>>>
>>>>>
>>>>>>
>>>>>> Is it okay to send this variation as a v1? Naturally, you can then
>>>>>> say: nope, do the full P3663 please.
>>>>>>
>>>>> You can send that of course, but after rewriting the whole wording
>>>> section,
>>>>> I think the previous
>>>>> specification was a bit of a dead end, so I think I wil answer that way
>>>>> anyway.
>>>>>
>>>>>
>>>>>>>
>>>>>>>
>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> The idea I'm pursuing is to follow the pattern of casting slices
>> to
>>>>>>>>>> "standard slices" immediately at the user-facing API layer, e.g.
>>>>>>>>>>
>>>>>>>>>> template<typename _Slices...>
>>>>>>>>>> constexpr auto
>>>>>>>>>> submdspan_extents(_Slices... __slices)
>>>>>>>>>> {
>>>>>>>>>> return
>>>>>> __submdspan_extents(__slice_cast(std::move(__slices))...);
>>>>>>>>>> }
>>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>
>>>>
>>>
>>
>>
>
More information about the Libstdc++
mailing list