On Wed, Aug 21, 2024 at 10:52 AM Qing Zhao <qing.z...@oracle.com> wrote: > > On Aug 21, 2024, at 11:43, Martin Uecker <uec...@tugraz.at> wrote: > > Am Mittwoch, dem 21.08.2024 um 15:24 +0000 schrieb Qing Zhao: > >>> > >>> But if we changed it to return a void pointer, we could make this > >>> a compile-time check: > >>> > >>> auto ret = __builtin_get_counted_by(__p->FAM); > >>> > >>> _Generic(ret, void*: (void)0, default: *ret = COUNT); > >> > >> Is there any benefit to return a void pointer than a SIZE_T pointer for > >> the NULL pointer? > > > > Yes! You can test with _Generic (or __builtin_types_compatible_p) > > at compile-time based on the type whether you can set *ret to COUNT > > or not as in the example above. > > > > So it is not a weird run-time test which needs to be optimized > > away. > Okay, I see. > Yes, this makes good sense to me. > > > > > >> > >>> > >>> > >>>> > >>>> Yes, I do feel that the approach __builtin_get_counted_by is not very > >>>> good. > >>>> Maybe it’s better to provide > >>>> A. __builtin_set_counted_by > >>>> or > >>>> B. The unary operator __counted_by(PTR) to return a Lvalue, in this case, > >>>> we need a __builtin_has_attribute first to check whether PTR has the > >>>> counted_by attribute first. > >>> > >>> You could potentially do the same __counted_by and test for type void. > >>> > >>> _Generic(typeof(__counted_by(PTR)), void: (void)0, __counted_by(PTR) = > >>> COUNT); > >> > >> Oh, so, is there any benefit for the unary operator __counted_by(PTR) than > >> the current __builtin_get_counted_by? > > > > I don't know. You suggested it ;-) > > > > It probably makes it harder to test the type because you need the > > typeof / C2Y Generic combination, but maybe there are other ways > > to test. > > For the unary operator __counted_by(PTR), “PTR” must have a counted_by > attribute, if not, there will be a compilation time error. > > Then the user could write the following code: > > If __builtin_has_attriubtes (PTR,counted_by) > __counted_by(PTR) = COUNT; > > > From the design point of view, I think this might be the cleanest solution. > > However, currently, CLANG doesn’t have __builtin_has_attributes. In order to > provide a consistent interface, __builtin_get_counted_by(PTR) is fine. > This was the confusion I had during our meeting today. For the above to be a compilation time error, we would have to diagnose it after DCE, which is okay, but seems like we're opening ourselves up to future issues when DCE misses. Maybe not the biggest concern, but...
-bw