-
Notifications
You must be signed in to change notification settings - Fork 92
Description
Context
I've been looking at why -g -O2 is necessary when compiling from C to eBPF with clang. It's not documented as far as I know, and I haven't been able to find the specific reasons again. I'm sure I saw them on mailing lists in the past, but I can't find it now.
Disclaimer
I realise this may be outside of the scope of this documentation, I note that you don't really mention how to compile with clang, or much of the ecosystem in general. I thought I'd open the issue as a suggestion, anyway - feel free to close if it's not relevant.
Description
So it would be nice to have some documentation about why one needs to pass -g and -O2 to clang when compiling.
For -g it's easy, it's necessary to get 1) debug information about the program, in particular the C source code that we dump with bpftool or llvm-objdump, for inspection, and 2) more importantly for many use case, BTF info.
For -O2 it's more tricky, everybody agrees it's necessary, but it's hard to find out why, precisely. From what I remember - and I may be totally wrong here - it may be related to:
- more dead code elimination, although the verifier can now suppress dead code for programs loaded by root, but removing it beforehand still reduces the size of the program to validate
- simplification of the flow graph, meaning again a smaller program to validate
- loop unrolling, although this is less relevant nowadays
- more inlining, meaning less stack usage
- improved register usage, fewer spills to stack
- something to do with variables relocation, but I can't remember the details
- ...?