This is an archive of the discontinued LLVM Phabricator instance.

BPF: add AdjustOpt IR pass to generate verifier friendly codes
ClosedPublic

Authored by yonghong-song on Aug 7 2020, 4:43 PM.

Details

Summary

Add an IR phase right before main module optimization.
This is to modify IR to restrict certain downward optimizations
in order to generate verifier friendly code.

> prevent certain instcombine optimizations, handling both
  in-block/cross-block instcombines.
> avoid speculative code motion if the variable used in
  condition is also used in the later blocks.

Internally, a bpf IR builtin

result = __builtin_bpf_passthrough(seq_num, result)

is used to enforce ordering. This builtin is only used
during target independent IR optimizations and it will
be removed at the beginning of target dependent IR
optimizations.

For example, removing the following workaround,

    • a/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c +++ b/tools/testing/selftests/bpf/progs/test_sysctl_loop1.c @@ -47,7 +47,7 @@ int sysctl_tcp_mem(struct bpf_sysctl *ctx) /* a workaround to prevent compiler from generating
      • codes verifier cannot handle yet. */
  • volatile int ret; + int ret;

this patch is able to generate code which passed the verifier.

To disable optimization, users need to use "opt" command like below:

clang -target bpf -O2 -S -emit-llvm -Xclang -disable-llvm-passes test.c
// disable icmp serialization
opt -O2 -bpf-disable-serialize-icmp test.ll | llvm-dis > t.ll
// disable avoid-speculation
opt -O2 -bpf-disable-avoid-speculation test.ll | llvm-dis > t.ll
llc t.ll

Diff Detail

Event Timeline

yonghong-song created this revision.Aug 7 2020, 4:43 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2020, 4:43 PM
yonghong-song requested review of this revision.Aug 7 2020, 4:43 PM
yonghong-song edited the summary of this revision. (Show Details)

now all bpf selftests can pass.

yonghong-song edited the summary of this revision. (Show Details)

add another adjustopt phase right after clang codegen to prevent certain early CSE optimization for __builtin_preserve_* intrinsics.

yonghong-song edited the summary of this revision. (Show Details)

A few changes:

  • AdjustOpt IR pass only called at EP_ModuleOptimizerEarly now
  • remove the pass which deals with a condition is used in more than one branch insns. This will be added to BPFCheckAndAdjustIR later.
ast added a comment.Sep 10 2020, 1:18 PM

is this based on D87153 ?
the passthrough will be removed later, right?

In D85570#2266620, @ast wrote:

is this based on D87153 ?
the passthrough will be removed later, right?

Yes, this is on top of D87153 and all these passthrough builtin will be removed.

yonghong-song retitled this revision from [RFC] BPF: add AdjustOpt IR pass to generate verifier friendly codes to [RFC PATCH 2] BPF: add AdjustOpt IR pass to generate verifier friendly codes.Sep 14 2020, 6:13 PM

minor change: add some more conditions to further restrict transformation.

yonghong-song edited the summary of this revision. (Show Details)

add seq_num arg to passthrough builtin

yonghong-song retitled this revision from [RFC PATCH 2] BPF: add AdjustOpt IR pass to generate verifier friendly codes to BPF: add AdjustOpt IR pass to generate verifier friendly codes.
yonghong-song edited the summary of this revision. (Show Details)
  • remove RFC tag as proper tests are added
  • added "opt" options so the added optimizations can be disabled.
ast accepted this revision.Oct 5 2020, 4:35 PM
This revision is now accepted and ready to land.Oct 5 2020, 4:35 PM
This revision was landed with ongoing or failed builds.Oct 7 2020, 8:50 AM
This revision was automatically updated to reflect the committed changes.