1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-25 20:23:11 +01:00
llvm-mirror/test/ThinLTO/X86
Yonghong Song 61809186ed [BPF] add new intrinsics preserve_{array,union,struct}_access_index
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.

The test case ThinLTO/X86/lazyload_metadata.ll is adjusted to reflect the
new addition of the metadata.

Signed-off-by: Yonghong Song <yhs@fb.com>

Differential Revision: https://reviews.llvm.org/D61810

llvm-svn: 365423
2019-07-09 01:51:36 +00:00
..
Inputs [ThinLTO] Attempt to recommit r365188 after alignment fix 2019-07-05 15:25:05 +00:00
alias_import.ll [ThinLTO] Use original alias visibility when importing 2019-05-29 16:50:46 +00:00
alias_internal.ll
alias_resolution.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
autoupgrade.ll
builtin-nostrip.ll Commit accidentally omitted test case. 2019-04-01 16:29:40 +00:00
cache-config.ll
cache-icall.ll [llvm-nm][llvm-readelf] Avoid single-dash -long-option in tests 2019-04-27 16:12:14 +00:00
cache-import-lists.ll
cache-typeid-resolutions.ll [ThinLTO] Look through aliases when computing hash keys 2018-12-04 00:02:33 +00:00
cache.ll [test] Skip ThinLTO cache tests requiring atime setting on NetBSD 2018-12-05 11:15:50 +00:00
callees-metadata.ll
cfi-devirt.ll [CodeGen] Make branch funnels pass the machine verifier 2019-07-03 17:16:45 +00:00
cfi-distributed.ll [LTO] Record whether LTOUnit splitting is enabled in index 2019-01-11 18:31:57 +00:00
cfi-icall.ll [LTO] Record whether LTOUnit splitting is enabled in index 2019-01-11 18:31:57 +00:00
cfi.ll [LTO] Record whether LTOUnit splitting is enabled in index 2019-01-11 18:31:57 +00:00
crash_debuginfo.ll
deadstrip.ll Fix 2-field llvm.global_ctors REQUIRES: asserts tests after rL360742 2019-05-15 03:08:21 +00:00
debuginfo-compositetype-import.ll
debuginfo-cu-import.ll
devirt-after-icp.ll [CodeGen] Make branch funnels pass the machine verifier 2019-07-03 17:16:45 +00:00
devirt.ll [ThinLTO] Reenable test with workaround for known failure 2019-07-03 02:14:47 +00:00
diagnostic-handler-remarks-with-hotness.ll
diagnostic-handler-remarks.ll [Remarks] Extend -fsave-optimization-record to specify the format 2019-06-17 16:06:00 +00:00
dicompositetype-unique2.ll
dicompositetype-unique-alias.ll
dicompositetype-unique.ll
distributed_import.ll
distributed_indexes.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
dot-dumper2.ll [ThinLTO] Attempt to recommit r365188 after alignment fix 2019-07-05 15:25:05 +00:00
dot-dumper-full-lto.ll
dot-dumper.ll [ThinLTO] Restructure AliasSummary to contain ValueInfo of Aliasee 2019-03-15 15:11:38 +00:00
drop-debug-info.ll
dsolocal_dllimport.ll
emit_imports.ll
empty_module_with_cache.ll
empty-module.ll
export.ll
funcimport2.ll
funcimport-debug.ll
funcimport-tbaa.ll
funcimport.ll
function_entry_count.ll [ThinLTO] Fix test added in rL349076 2018-12-14 08:21:08 +00:00
globals-import-blockaddr.ll
globals-import-const-fold.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
globals-import.ll
import_opaque_type.ll
index-const-prop2.ll [ThinLTO] Attempt to recommit r365188 after alignment fix 2019-07-05 15:25:05 +00:00
index-const-prop-alias.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-cache.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-comdat.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-dead.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-full-lto.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-gvref.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-ldst.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-linkage.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop-O0.ll [ThinLTO] Internalize readonly globals 2018-11-16 07:08:00 +00:00
index-const-prop.ll ThinLTO: Do not import debug info for imported global constants 2018-12-05 21:42:17 +00:00
internalize.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
lazyload_metadata.ll [BPF] add new intrinsics preserve_{array,union,struct}_access_index 2019-07-09 01:51:36 +00:00
linkonce_aliasee_ref_import.ll
linkonce_odr_unnamed_addr.ll [ThinLTO] Auto-hide prevailing linkonce_odr only when all copies eligible 2019-05-10 20:08:24 +00:00
linkonce_resolution_comdat.ll [ThinLTO] Auto-hide prevailing linkonce_odr only when all copies eligible 2019-05-10 20:08:24 +00:00
lit.local.cfg [lit] Delete empty lines at the end of lit.local.cfg NFC 2019-06-17 09:51:07 +00:00
llvm.used.ll
load-store-caching.ll [ThinLTO] Attempt to recommit r365188 after alignment fix 2019-07-05 15:25:05 +00:00
local_name_conflict_var.ll [ThinLTO] Import local variables from the same module as caller 2018-11-29 17:02:42 +00:00
local_name_conflict.ll [ThinLTO] Import local variables from the same module as caller 2018-11-29 17:02:42 +00:00
merge-triple.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
module_asm2.ll
module_asm_glob.ll
module_summary_graph_traits.ll
newpm-basic.ll
noinline.ll
personality-local.ll [llvm-readobj] Change -t to --symbols in tests. NFC 2019-05-01 09:28:24 +00:00
personality.ll [llvm-readobj] Change -t to --symbols in tests. NFC 2019-05-01 09:28:24 +00:00
pr35472.ll
prefix_replace.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
reference_non_importable.ll
referenced_by_constant.ll
save_objects.ll [ThinLTO] Adding architecture name into saved object filename 2019-04-29 21:39:54 +00:00
section.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
strong_non_prevailing.ll [llvm-nm] Fix handling of symbol types 't' 'd' 'r' 2019-05-09 12:43:37 +00:00
thinlto-internalize-used.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
tli-nobuiltin.ll
weak_externals.ll [ThinLTO] Don't internalize weak writeable variables 2019-05-13 11:53:05 +00:00
weak_resolution_single.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
weak_resolution.ll [ThinLTO] Fix ThinLTOCodegenerator to export llvm.used symbols 2019-04-17 17:38:09 +00:00
writeonly2.ll [ThinLTO] Attempt to recommit r365188 after alignment fix 2019-07-05 15:25:05 +00:00
writeonly.ll [ThinLTO] Attempt to recommit r365188 after alignment fix 2019-07-05 15:25:05 +00:00