RE: [PATCH] net: Remove branch in csum_shift()

From: David Laight
Date: Sun Feb 13 2022 - 12:48:34 EST


From: Segher Boessenkool
> Sent: 13 February 2022 09:16
....
>
> > What happens on x86-64?
> >
> > Trying to do the same in the x86 ipcsum code tended to make the code worse.
> > (Although that test is for an odd length fragment and can just be removed.)
>
> In an ideal world the compiler could choose the optimal code sequences
> everywhere. But that won't ever happen, the search space is way too
> big. So compilers just use heuristics, not exhaustive search like
> superopt does. There is a middle way of course, something with directed
> searches, and maybe in a few decades systems will be fast enough. Until
> then we will very often see code that is 10% slower and 30% bigger than
> necessary. A single insn more than needed isn't so bad :-)

But it can be a lot more than that.

> Making things branch-free is very much worth it here though!

I tried to find out where 'here' is.

I can't get godbolt to generate anything like that object code
for a call to csum_shift().

I can't actually get it to issue a rotate (x86 of ppc).

I think it is only a single instruction because the compiler
has saved 'offset & 1' much earlier instead of doing testing
'offset & 1' just prior to the conditional.
It certainly has a nasty habit of doing that pessimisation.

So while it helps a specific call site it may be much
worse in general.

I also suspect that the addc/addze pair could be removed
by passing the old checksum into csum_partial.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)