This is an archive of the discontinued LLVM Phabricator instance.

BPF: avoid same condition evaluated in multiple branch instructions
Needs ReviewPublic

Authored by yonghong-song on Sep 9 2020, 7:16 PM.

Details

Reviewers
ast
Summary

This patch specifically tries to address an issue reported at

https://lore.kernel.org/bpf/20200812074826.GB754656@krava/

If one icmp condition is used in more than one places, prevent
this condition from inlining into multiple places.

For example,

cond = a < b 
... 
select cond, val1, val2
... 
br cond, block1, block2

In actually lowering, cond will be put into select_cc and br_cc
so we will have proper cond in the jmp inst.
It is reported this caused the issue if cond happens to be

pkt_ptr < pkt_end

due to particular kernel implementation.
The transformation here is to put a barrier after "cond = a < b"
so cond cannot be inlined into select/br. The pseudo generated code
looks like:

if a < b then
  tmp = 1 
then
  tmp = 0 
... 
if (tmp == 1) val1; else val2
... 
if (tmp == 1) goto block1; else goto block2;

The below llc flag is added to turn off this optimization if needed.

llc -march=bpf -bpf-disable-eval-cond-once t.ll

Diff Detail

Event Timeline

yonghong-song created this revision.Sep 9 2020, 7:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptSep 9 2020, 7:16 PM
yonghong-song requested review of this revision.Sep 9 2020, 7:16 PM
yonghong-song retitled this revision from [RFC] BPF: avoid same condition evaluated in multiple branch instructions to [RFC PATCH 3] BPF: avoid same condition evaluated in multiple branch instructions.Sep 14 2020, 6:13 PM
yonghong-song retitled this revision from [RFC PATCH 3] BPF: avoid same condition evaluated in multiple branch instructions to BPF: avoid same condition evaluated in multiple branch instructions.
yonghong-song edited the summary of this revision. (Show Details)
  • added tests, added llc option to disable optimization if needed.
  • removed RFC tag.