[BPF] Handle offset reloc endpoint ending in the middle of chain properly
authorYonghong Song <yhs@fb.com>
Thu, 3 Oct 2019 16:30:29 +0000 (16:30 +0000)
committerYonghong Song <yhs@fb.com>
Thu, 3 Oct 2019 16:30:29 +0000 (16:30 +0000)
commit02ac75092dbbd9efb250bfb4f4e79dd042abf3c4
tree1f8755dee9ea1d5fd4de0f9a7a9cee18880c70bc
parentba643691ddbd7751f9c74859c6b3b3e281d36c5e
[BPF] Handle offset reloc endpoint ending in the middle of chain properly

During studying support for bitfield, I found an issue for
an example like the one in test offset-reloc-middle-chain.ll.
  struct t1 { int c; };
  struct s1 { struct t1 b; };
  struct r1 { struct s1 a; };
  #define _(x) __builtin_preserve_access_index(x)
  void test1(void *p1, void *p2, void *p3);
  void test(struct r1 *arg) {
    struct s1 *ps = _(&arg->a);
    struct t1 *pt = _(&arg->a.b);
    int *pi = _(&arg->a.b.c);
    test1(ps, pt, pi);
  }

The IR looks like:
  %0 = llvm.preserve.struct.access(base, ...)
  %1 = llvm.preserve.struct.access(%0, ...)
  %2 = llvm.preserve.struct.access(%1, ...)
  using %0, %1 and %2

In this case, we need to generate three relocatiions
corresponding to chains: (%0), (%0, %1) and (%0, %1, %2).
After collecting all the chains, the current implementation
process each chain (in a map) with code generation sequentially.
For example, after (%0) is processed, the code may look like:
  %0 = base + special_global_variable
  // llvm.preserve.struct.access(base, ...) is delisted
  // from the instruction stream.
  %1 = llvm.preserve.struct.access(%0, ...)
  %2 = llvm.preserve.struct.access(%1, ...)
  using %0, %1 and %2

When processing chain (%0, %1), the current implementation
tries to visit intrinsic llvm.preserve.struct.access(base, ...)
to get some of its properties and this caused segfault.

This patch fixed the issue by remembering all necessary
information (kind, metadata, access_index, base) during
analysis phase, so in code generation phase there is
no need to examine the intrinsic call instructions.
This also simplifies the code.

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

llvm-svn: 373621
llvm/lib/Target/BPF/BPFAbstractMemberAccess.cpp
llvm/test/CodeGen/BPF/CORE/offset-reloc-middle-chain.ll [new file with mode: 0644]