[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: for_each_set_bit() clean-up (API RFC)


  • To: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
  • From: Jan Beulich <jbeulich@xxxxxxxx>
  • Date: Mon, 17 Jun 2024 11:54:56 +0200
  • Autocrypt: addr=jbeulich@xxxxxxxx; keydata= xsDiBFk3nEQRBADAEaSw6zC/EJkiwGPXbWtPxl2xCdSoeepS07jW8UgcHNurfHvUzogEq5xk hu507c3BarVjyWCJOylMNR98Yd8VqD9UfmX0Hb8/BrA+Hl6/DB/eqGptrf4BSRwcZQM32aZK 7Pj2XbGWIUrZrd70x1eAP9QE3P79Y2oLrsCgbZJfEwCgvz9JjGmQqQkRiTVzlZVCJYcyGGsD /0tbFCzD2h20ahe8rC1gbb3K3qk+LpBtvjBu1RY9drYk0NymiGbJWZgab6t1jM7sk2vuf0Py O9Hf9XBmK0uE9IgMaiCpc32XV9oASz6UJebwkX+zF2jG5I1BfnO9g7KlotcA/v5ClMjgo6Gl MDY4HxoSRu3i1cqqSDtVlt+AOVBJBACrZcnHAUSuCXBPy0jOlBhxPqRWv6ND4c9PH1xjQ3NP nxJuMBS8rnNg22uyfAgmBKNLpLgAGVRMZGaGoJObGf72s6TeIqKJo/LtggAS9qAUiuKVnygo 3wjfkS9A3DRO+SpU7JqWdsveeIQyeyEJ/8PTowmSQLakF+3fote9ybzd880fSmFuIEJldWxp Y2ggPGpiZXVsaWNoQHN1c2UuY29tPsJgBBMRAgAgBQJZN5xEAhsDBgsJCAcDAgQVAggDBBYC AwECHgECF4AACgkQoDSui/t3IH4J+wCfQ5jHdEjCRHj23O/5ttg9r9OIruwAn3103WUITZee e7Sbg12UgcQ5lv7SzsFNBFk3nEQQCACCuTjCjFOUdi5Nm244F+78kLghRcin/awv+IrTcIWF hUpSs1Y91iQQ7KItirz5uwCPlwejSJDQJLIS+QtJHaXDXeV6NI0Uef1hP20+y8qydDiVkv6l IreXjTb7DvksRgJNvCkWtYnlS3mYvQ9NzS9PhyALWbXnH6sIJd2O9lKS1Mrfq+y0IXCP10eS FFGg+Av3IQeFatkJAyju0PPthyTqxSI4lZYuJVPknzgaeuJv/2NccrPvmeDg6Coe7ZIeQ8Yj t0ARxu2xytAkkLCel1Lz1WLmwLstV30g80nkgZf/wr+/BXJW/oIvRlonUkxv+IbBM3dX2OV8 AmRv1ySWPTP7AAMFB/9PQK/VtlNUJvg8GXj9ootzrteGfVZVVT4XBJkfwBcpC/XcPzldjv+3 HYudvpdNK3lLujXeA5fLOH+Z/G9WBc5pFVSMocI71I8bT8lIAzreg0WvkWg5V2WZsUMlnDL9 mpwIGFhlbM3gfDMs7MPMu8YQRFVdUvtSpaAs8OFfGQ0ia3LGZcjA6Ik2+xcqscEJzNH+qh8V m5jjp28yZgaqTaRbg3M/+MTbMpicpZuqF4rnB0AQD12/3BNWDR6bmh+EkYSMcEIpQmBM51qM EKYTQGybRCjpnKHGOxG0rfFY1085mBDZCH5Kx0cl0HVJuQKC+dV2ZY5AqjcKwAxpE75MLFkr wkkEGBECAAkFAlk3nEQCGwwACgkQoDSui/t3IH7nnwCfcJWUDUFKdCsBH/E5d+0ZnMQi+G0A nAuWpQkjM1ASeQwSHEeAWPgskBQL
  • Cc: Roger Pau Monné <roger.pau@xxxxxxxxxx>, Stefano Stabellini <sstabellini@xxxxxxxxxx>, Julien Grall <julien@xxxxxxx>, Michal Orzel <michal.orzel@xxxxxxx>, Bertrand Marquis <Bertrand.Marquis@xxxxxxx>, Oleksii Kurochko <oleksii.kurochko@xxxxxxxxx>, Shawn Anastasio <sanastasio@xxxxxxxxxxxxxxxxxxxxx>, xen-devel <xen-devel@xxxxxxxxxxxxxxxxxxxx>
  • Delivery-date: Mon, 17 Jun 2024 09:55:01 +0000
  • List-id: Xen developer discussion <xen-devel.lists.xenproject.org>

On 14.06.2024 19:07, Andrew Cooper wrote:
> More fallout from looking at the code generation...
> 
> for_each_set_bit() forces it's bitmap parameter out into memory.  For an
> arbitrary sized bitmap, this is fine - and likely preferable as it's an
> in-memory to begin with.
> 
> However, more than half the current users of for_each_set_bit() are
> operating over an single int/long, and this too is spilled to the
> stack.  Worse, x86 seems to be the only architecture which (tries, but
> not very well) to optimise find_{first,next}_bit() for GPR-sized
> quantities, meaning that for_each_set_bit() hides 2 backing function calls.
> 
> The ARM (v)GIC code in particular suffers horribly because of this.
> 
> We also have several interesting opencoded forms:
> * evtchn_check_pollers() is a (preprocessor identical) opencoding.
> * hvm_emulate_writeback() is equivalent.
> * for_each_vp() exists just to hardcode a constant and swap the other
> two parameters.
> 
> and several others forms which I think could be expressed more cleanly
> as for_each_set_bit().

I agree.

> We also have the while()/ffs() forms which are "just" for_each_set_bit()
> and some even manage to not spill their main variable to memory.
> 
> 
> I want to get to a position where there is one clear API to use, and
> that the compiler will handle nicely.  Xen's code generation will
> definitely improve as a consequence.
> 
> 
> Sadly, transforming the ideal while()/ffs() form into a for() loop is a
> bit tricky.  This works:
> 
> for ( unsigned int v = (val), (bit);
>       v;
>       v &= v - 1 )
> if ( 1 )
> {
>     (bit) = ffs(v) - 1;
>     goto body;
> }
> else
>     body:
> 
> which is a C metaprogramming trick borrowed from PuTTY to make:
> 
> for_each_BLAH ( bit, val )
> {
>     // nice loop body
> }
> 
> work, while having the ffs() calculated logically within the loop body.

What's wrong with

#define for_each_set_bit(iter, val) \
    for ( unsigned int v_ = (val), iter; \
          v_ && ((iter) = ffs(v_) - 1, true); \
          v_ &= v_ - 1 )

? I'll admit though that it's likely a matter of taste which one is
"uglier". Yet I'd be in favor of avoiding the scope trickery.

> The first issue I expect people to have with the above is the raw 'body'
> label, although with a macro that can be fixed using body_ ## __COUNTER__.
> 
> A full example is https://godbolt.org/z/oMGfah696 although a real
> example in Xen is going to have to be variadic for at least ffs() and
> ffsl().

How would variadic-ness help with this? Unless we play some type
trickery (like typeof((val) + 0U), thus yielding at least an unsigned,
but an unsigned long if the incoming value is such, followed by a
compile-time conditional operator to select between ffs() and ffsl()),
I don't think we'd get away with just a single construct for both the
int and long (for Arm32: long long) cases.

> Now, from an API point of view, it would be lovely if we could make a
> single for_each_set_bit() which covers both cases, and while I can
> distinguish the two forms by whether there are 2 or 3 args,

With the 3-argument form specifying the number of bits in the 3rd arg?
I'd fear such mixed uses may end up confusing.

> I expect
> MISRA is going to have a fit at that.  Also there's a difference based
> on the scope of 'bit' and also whether modifications to 'val' in the
> loop body take effect on the loop condition (they don't because a copy
> is taken).
> 
> So I expect everyone is going to want a new API to use here.  But what
> to call it?
> 
> More than half of the callers in Xen really want the GPR form, so we
> could introduce a new bitmap_for_each_set_bit(), move all the callers
> over, then introduce a "new" for_each_set_bit() which is only of the GPR
> form.
> 
> Or does anyone want to suggest an alternative name?

I'd be okay-ish with those, maybe with slight shortening to bitmap_for_each()
or bitmap_for_each_set().

Jan



 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.