On Thu, Dec 14, 2023 at 7:10 PM Alexei Starovoitov <alexei.starovoitov@xxxxxxxxx> wrote: > > Just realized that we can go the other way instead. > > We can get rid of bpf_assert_eq/ne/... and replace with: > > diff --git a/tools/testing/selftests/bpf/bpf_experimental.h > b/tools/testing/selftests/bpf/bpf_experimental.h > index 1386baf9ae4a..1c500287766d 100644 > --- a/tools/testing/selftests/bpf/bpf_experimental.h > +++ b/tools/testing/selftests/bpf/bpf_experimental.h > @@ -254,6 +254,15 @@ extern void bpf_throw(u64 cookie) __ksym; > } > \ > }) > > +#define _EQ(LHS, RHS) \ > + ({ int var = 1;\ > + asm volatile goto("if %[lhs] == %[rhs] goto %l[l_yes]" \ > + :: [lhs] "r"(LHS), [rhs] "i"(RHS) :: l_yes);\ > + var = 0;\ > +l_yes:\ > + var;\ > + }) Realized we can do much better. We can take advantage that bpf assembly syntax resembles C and do: bpf_assert(CMP(cookie, "!=", 0); and use it as generic "volatile compare" that compiler cannot optimize out: Replacing: if (foo < bar) ... with if (CMP(foo, "<", bar)) ... when the compare operator should be preserved. I'll try to prototype it soon. Might go further and use C++ for bpf programs :) Override operator<, opreator==, ... then if (foo < bar) will be in asm code as written in C++ bpf prog.