[cfe-dev] Disable certain llvm optimizations at clang frontend

Hal Finkel via cfe-dev cfe-dev at lists.llvm.org
Fri Jun 19 08:52:59 PDT 2020


On 6/19/20 10:19 AM, Y Song via cfe-dev wrote:
> Hi,
>
> [resend with proper cfe-dev tag]
>
> This is to seek some advice on how to disable certain llvm
> optimizations when compiled with clang for BPF backend. For example,
> we want to disable two optimizations, instcombine and simplifyCFG. I
> would like to get some advice about what is the best way to do it.
>
> Some Background on Why We want to do this
> ===================================
>
> Any BPF code needs to go through a kernel verifier
>     https://github.com/torvalds/linux/blob/master/kernel/bpf/verifier.c
> before it can be executed in the kernel. verifier tries to ensure the safety
> of the program, no illegal memory access, etc. In certain cases, clang
> generated optimized codes make verification very hard, see pending
> commits with another approach focused on BPF backend and some core
> optimizations.
>    https://reviews.llvm.org/D72787
>    https://reviews.llvm.org/D82112
>    https://reviews.llvm.org/D81859
> To workaround the issue, people have to write inline asm or tweak
> their program in a very user-unfriendly way to disable certain
> transformations, in particular, instcombine and simplifyCFG.
>
> Alternative Method in clang Frontend
> ============================
>
> The above approach tried to disable/undo/influence specific
> optimizations. Another approach is to disable instcombine and
> simplifyCFG during compilation process. This is a little bit heavy
> weight. But it may still be fine as this will ease the pain for
> development. For most customers, a little performance loss vs.
> development gain probably will be fine.
>
> How to disable instcombine/simplifyCFG in clang frontends? It looks
> like function attributes and insn metadatas are used to influence
> optimizations.
> The following approach might work, I think:
>     BPF backend may add the following two attributes to all functions:
>     "disable-instcombine" = "true"
>     "disable-simplifyCFG" = "true"
> And during instcombine and simplifyCFG, the above function attribute will
> be checked, it is true, the pass will be skipped.
>
> Do you think the above method could work? Any alternative suggestions
> are also appreciated.


This is certainly an interesting problem. A few thoughts:

  * A generalized mechanism to disable parts of the pass pipeline, or 
maybe specify a pass pipeline, using function attributes, may be of 
broad utility. That having been said, it's not clearly the right answer 
to this problem.

  * If we're going to disable things, I would much prefer attributes 
with a semantic meaning. It's not like those passes are the only passes 
that might perform transformations that would be problematic for your 
backend.

  * How difficult would it be for your backend to try harder to coerce 
the IR into the form accepted by the BPF verifier? The current 
compilation problem seems unsound, unfortunately, and I'm wondering how 
we might move the overall system to be better grounded.

  -Hal


>
> Thanks!
>
> Yonghong
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory



More information about the cfe-dev mailing list