mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2024-11-23 11:13:28 +01:00
18796da7b0
For background of BPF CO-RE project, please refer to http://vger.kernel.org/bpfconf2019.html In summary, BPF CO-RE intends to compile bpf programs adjustable on struct/union layout change so the same program can run on multiple kernels with adjustment before loading based on native kernel structures. In order to do this, we need keep track of GEP(getelementptr) instruction base and result debuginfo types, so we can adjust on the host based on kernel BTF info. Capturing such information as an IR optimization is hard as various optimization may have tweaked GEP and also union is replaced by structure it is impossible to track fieldindex for union member accesses. Three intrinsic functions, preserve_{array,union,struct}_access_index, are introducted. addr = preserve_array_access_index(base, index, dimension) addr = preserve_union_access_index(base, di_index) addr = preserve_struct_access_index(base, gep_index, di_index) here, base: the base pointer for the array/union/struct access. index: the last access index for array, the same for IR/DebugInfo layout. dimension: the array dimension. gep_index: the access index based on IR layout. di_index: the access index based on user/debuginfo types. For example, for the following example, $ cat test.c struct sk_buff { int i; int b1:1; int b2:2; union { struct { int o1; int o2; } o; struct { char flags; char dev_id; } dev; int netid; } u[10]; }; static int (*bpf_probe_read)(void *dst, int size, const void *unsafe_ptr) = (void *) 4; #define _(x) (__builtin_preserve_access_index(x)) int bpf_prog(struct sk_buff *ctx) { char dev_id; bpf_probe_read(&dev_id, sizeof(char), _(&ctx->u[5].dev.dev_id)); return dev_id; } $ clang -target bpf -O2 -g -emit-llvm -S -mllvm -print-before-all \ test.c >& log The generated IR looks like below: ... define dso_local i32 @bpf_prog(%struct.sk_buff*) #0 !dbg !15 { %2 = alloca %struct.sk_buff*, align 8 %3 = alloca i8, align 1 store %struct.sk_buff* %0, %struct.sk_buff** %2, align 8, !tbaa !45 call void @llvm.dbg.declare(metadata %struct.sk_buff** %2, metadata !43, metadata !DIExpression()), !dbg !49 call void @llvm.lifetime.start.p0i8(i64 1, i8* %3) #4, !dbg !50 call void @llvm.dbg.declare(metadata i8* %3, metadata !44, metadata !DIExpression()), !dbg !51 %4 = load i32 (i8*, i32, i8*)*, i32 (i8*, i32, i8*)** @bpf_probe_read, align 8, !dbg !52, !tbaa !45 %5 = load %struct.sk_buff*, %struct.sk_buff** %2, align 8, !dbg !53, !tbaa !45 %6 = call [10 x %union.anon]* @llvm.preserve.struct.access.index.p0a10s_union.anons.p0s_struct.sk_buffs( %struct.sk_buff* %5, i32 2, i32 3), !dbg !53, !llvm.preserve.access.index !19 %7 = call %union.anon* @llvm.preserve.array.access.index.p0s_union.anons.p0a10s_union.anons( [10 x %union.anon]* %6, i32 1, i32 5), !dbg !53 %8 = call %union.anon* @llvm.preserve.union.access.index.p0s_union.anons.p0s_union.anons( %union.anon* %7, i32 1), !dbg !53, !llvm.preserve.access.index !26 %9 = bitcast %union.anon* %8 to %struct.anon.0*, !dbg !53 %10 = call i8* @llvm.preserve.struct.access.index.p0i8.p0s_struct.anon.0s( %struct.anon.0* %9, i32 1, i32 1), !dbg !53, !llvm.preserve.access.index !34 %11 = call i32 %4(i8* %3, i32 1, i8* %10), !dbg !52 %12 = load i8, i8* %3, align 1, !dbg !54, !tbaa !55 %13 = sext i8 %12 to i32, !dbg !54 call void @llvm.lifetime.end.p0i8(i64 1, i8* %3) #4, !dbg !56 ret i32 %13, !dbg !57 } !19 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "sk_buff", file: !3, line: 1, size: 704, elements: !20) !26 = distinct !DICompositeType(tag: DW_TAG_union_type, scope: !19, file: !3, line: 5, size: 64, elements: !27) !34 = distinct !DICompositeType(tag: DW_TAG_structure_type, scope: !26, file: !3, line: 10, size: 16, elements: !35) Note that @llvm.preserve.{struct,union}.access.index calls have metadata llvm.preserve.access.index attached to instructions to provide struct/union debuginfo type information. For &ctx->u[5].dev.dev_id, . The "%6 = ..." represents struct member "u" with index 2 for IR layout and index 3 for DI layout. . The "%7 = ..." represents array subscript "5". . The "%8 = ..." represents union member "dev" with index 1 for DI layout. . The "%10 = ..." represents struct member "dev_id" with index 1 for both IR and DI layout. Basically, traversing the use-def chain recursively for the 3rd argument of bpf_probe_read() and examining all preserve_*_access_index calls, the debuginfo struct/union/array access index can be achieved. The intrinsics also contain enough information to regenerate codes for IR layout. For array and structure intrinsics, the proper GEP can be constructed. For union intrinsics, replacing all uses of "addr" with "base" should be enough. Signed-off-by: Yonghong Song <yhs@fb.com> Differential Revision: https://reviews.llvm.org/D61810 llvm-svn: 365352 |
||
---|---|---|
.. | ||
_ocamldoc | ||
_static | ||
_templates | ||
_themes/llvm-theme | ||
AMDGPU | ||
CommandGuide | ||
Frontend | ||
HistoricalNotes | ||
PDB | ||
Proposals | ||
TableGen | ||
tutorial | ||
AddingConstrainedIntrinsics.rst | ||
AdvancedBuilds.rst | ||
AliasAnalysis.rst | ||
AMDGPUInstructionNotation.rst | ||
AMDGPUInstructionSyntax.rst | ||
AMDGPUModifierSyntax.rst | ||
AMDGPUOperandSyntax.rst | ||
AMDGPUUsage.rst | ||
ARM-BE-bitcastfail.png | ||
ARM-BE-bitcastsuccess.png | ||
ARM-BE-ld1.png | ||
ARM-BE-ldr.png | ||
Atomics.rst | ||
Benchmarking.rst | ||
BigEndianNEON.rst | ||
BitCodeFormat.rst | ||
BlockFrequencyTerminology.rst | ||
BranchWeightMetadata.rst | ||
BugLifeCycle.rst | ||
Bugpoint.rst | ||
BuildingADistribution.rst | ||
CFIVerify.rst | ||
CMake.rst | ||
CMakeLists.txt | ||
CMakePrimer.rst | ||
CodeGenerator.rst | ||
CodeOfConduct.rst | ||
CodingStandards.rst | ||
CommandLine.rst | ||
CompileCudaWithLLVM.rst | ||
CompilerWriterInfo.rst | ||
conf.py | ||
Contributing.rst | ||
Coroutines.rst | ||
CoverageMappingFormat.rst | ||
DebuggingJITedCode.rst | ||
DeveloperPolicy.rst | ||
Docker.rst | ||
doxygen-mainpage.dox | ||
doxygen.cfg.in | ||
ExceptionHandling.rst | ||
ExtendedIntegerResults.txt | ||
ExtendingLLVM.rst | ||
Extensions.rst | ||
FAQ.rst | ||
FaultMaps.rst | ||
FuzzingLLVM.rst | ||
GarbageCollection.rst | ||
gcc-loops.png | ||
GetElementPtr.rst | ||
GettingStarted.rst | ||
GettingStartedVS.rst | ||
GlobalISel.rst | ||
GoldPlugin.rst | ||
HowToAddABuilder.rst | ||
HowToBuildOnARM.rst | ||
HowToBuildWithPGO.rst | ||
HowToCrossCompileBuiltinsOnArm.rst | ||
HowToCrossCompileLLVM.rst | ||
HowToReleaseLLVM.rst | ||
HowToSetUpLLVMStyleRTTI.rst | ||
HowToSubmitABug.rst | ||
HowToUseAttributes.rst | ||
HowToUseInstrMappings.rst | ||
InAlloca.rst | ||
index.rst | ||
LangRef.rst | ||
Lexicon.rst | ||
LibFuzzer.rst | ||
LinkTimeOptimization.rst | ||
linpack-pc.png | ||
llvm-objdump.1 | ||
LLVMBuild.rst | ||
LLVMBuild.txt | ||
make.bat | ||
Makefile.sphinx | ||
MarkdownQuickstartTemplate.md | ||
MarkedUpDisassembly.rst | ||
MCJIT-creation.png | ||
MCJIT-dyld-load.png | ||
MCJIT-engine-builder.png | ||
MCJIT-load-object.png | ||
MCJIT-load.png | ||
MCJIT-resolve-relocations.png | ||
MCJITDesignAndImplementation.rst | ||
MeetupGuidelines.rst | ||
MemorySSA.rst | ||
MergeFunctions.rst | ||
MIRLangRef.rst | ||
NVPTXUsage.rst | ||
OptBisect.rst | ||
ORCv2DesignAndImplementation.rst | ||
Packaging.rst | ||
Passes.rst | ||
Phabricator.rst | ||
ProgrammersManual.rst | ||
Projects.rst | ||
re_format.7 | ||
README.txt | ||
ReleaseNotes.rst | ||
ReleaseProcess.rst | ||
ReportingGuide.rst | ||
ScudoHardenedAllocator.rst | ||
SegmentedStacks.rst | ||
SourceLevelDebugging.rst | ||
speculative_load_hardening_microbenchmarks.png | ||
SpeculativeLoadHardening.md | ||
SphinxQuickstartTemplate.rst | ||
StackMaps.rst | ||
StackSafetyAnalysis.rst | ||
Statepoints.rst | ||
SupportLibrary.rst | ||
SystemLibrary.rst | ||
TableGenFundamentals.rst | ||
TestingGuide.rst | ||
TestSuiteGuide.md | ||
TestSuiteMakefileGuide.rst | ||
TransformMetadata.rst | ||
TypeMetadata.rst | ||
Vectorizers.rst | ||
WritingAnLLVMBackend.rst | ||
WritingAnLLVMPass.rst | ||
XRay.rst | ||
XRayExample.rst | ||
XRayFDRFormat.rst | ||
yaml2obj.rst | ||
YamlIO.rst |
LLVM Documentation ================== LLVM's documentation is written in reStructuredText, a lightweight plaintext markup language (file extension `.rst`). While the reStructuredText documentation should be quite readable in source form, it is mostly meant to be processed by the Sphinx documentation generation system to create HTML pages which are hosted on <http://llvm.org/docs/> and updated after every commit. Manpage output is also supported, see below. If you instead would like to generate and view the HTML locally, install Sphinx <http://sphinx-doc.org/> and then do: cd <build-dir> cmake -DLLVM_ENABLE_SPHINX=true -DSPHINX_OUTPUT_HTML=true <src-dir> make -j3 docs-llvm-html $BROWSER <build-dir>/docs//html/index.html The mapping between reStructuredText files and generated documentation is `docs/Foo.rst` <-> `<build-dir>/docs//html/Foo.html` <-> `http://llvm.org/docs/Foo.html`. If you are interested in writing new documentation, you will want to read `SphinxQuickstartTemplate.rst` which will get you writing documentation very fast and includes examples of the most important reStructuredText markup syntax. Manpage Output =============== Building the manpages is similar to building the HTML documentation. The primary difference is to use the `man` makefile target, instead of the default (which is `html`). Sphinx then produces the man pages in the directory `<build-dir>/docs/man/`. cd <build-dir> cmake -DLLVM_ENABLE_SPHINX=true -DSPHINX_OUTPUT_MAN=true <src-dir> make -j3 docs-llvm-man man -l >build-dir>/docs/man/FileCheck.1 The correspondence between .rst files and man pages is `docs/CommandGuide/Foo.rst` <-> `<build-dir>/docs//man/Foo.1`. These .rst files are also included during HTML generation so they are also viewable online (as noted above) at e.g. `http://llvm.org/docs/CommandGuide/Foo.html`. Checking links ============== The reachability of external links in the documentation can be checked by running: cd docs/ make -f Makefile.sphinx linkcheck Doxygen page Output ============== Install doxygen <http://www.stack.nl/~dimitri/doxygen/download.html> and dot2tex <https://dot2tex.readthedocs.io/en/latest>. cd <build-dir> cmake -DLLVM_ENABLE_DOXYGEN=On <llvm-top-src-dir> make doxygen-llvm # for LLVM docs make doxygen-clang # for clang docs It will generate html in <build-dir>/docs/doxygen/html # for LLVM docs <build-dir>/tools/clang/docs/doxygen/html # for clang docs