This is an archive of the discontinued LLVM Phabricator instance.

[BPF] support 128bit int explicitly in layout spec
ClosedPublic

Authored by yonghong-song on Mar 22 2020, 11:29 PM.

Details

Summary

Currently, bpf does not specify 128bit alignment in its
layout spec. So for a structure like

struct ipv6_key_t {
  unsigned pid;
  unsigned __int128 saddr;
  unsigned short lport;
};

clang will generate IR type

%struct.ipv6_key_t = type { i32, [12 x i8], i128, i16, [14 x i8] }

Additional padding is to ensure later IR->MIR can generate correct
stack layout with target layout spec.

But it is common practice for a tracing program to be
first compiled with target flag (e.g., x86_64 or aarch64) through
clang to generate IR and then go through llc to generate bpf
byte code. Tracing program often refers to kernel internal
data structures which needs to be compiled with non-bpf target.

But such a compilation model may cause a problem on aarch64.
The bcc issue https://github.com/iovisor/bcc/issues/2827
reported such a problem.

For the above structure, since aarch64 has "i128:128" in its
layout string, the generated IR will have

%struct.ipv6_key_t = type { i32, i128, i16 }

Since bpf does not have "i128:128" in its spec string,
the selectionDAG assumes alignment 8 for i128 and
computes the stack storage size for the above is 32 bytes,
which leads incorrect code later.

The x86_64 does not have this issue as it does not have
"i128:128" in its layout spec as it does permits i128 to
be alignmented at 8 bytes at stack. Its IR type looks like

%struct.ipv6_key_t = type { i32, [12 x i8], i128, i16, [14 x i8] }

The fix here is add i128 support in layout spec, the same as
aarch64. The only downside is we may have less optimal stack
allocation in certain cases since we require 16byte alignment
for i128 instead of 8. But this is probably fine as i128 is
not used widely and in most cases users should already
have proper alignment.

Diff Detail

Event Timeline

yonghong-song created this revision.Mar 22 2020, 11:29 PM
ast accepted this revision.Mar 27 2020, 7:35 PM
This revision is now accepted and ready to land.Mar 27 2020, 7:35 PM
This revision was automatically updated to reflect the committed changes.