Add BTF relocation builtins - #161107
Conversation
|
Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @vakaras Some changes occurred to constck cc @fee1-dead
cc @Amanieu, @folkertdev, @sayantn This PR changes rustc_public cc @oli-obk, @celinval, @ouz-a, @makai410 Some changes occurred to the CTFE machinery Some changes occurred in compiler/rustc_passes/src/check_attr.rs cc @jdonszelmann, @JonathanBrouwer
cc @bjorn3 Some changes occurred in match checking cc @Nadrieril Some changes occurred in compiler/rustc_attr_parsing cc @jdonszelmann, @JonathanBrouwer Some changes occurred in compiler/rustc_attr_ir cc @jdonszelmann, @JonathanBrouwer Some changes occurred in cc @BoxyUwU The parser was modified, potentially altering the grammar of (stable) Rust cc @fmease Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri |
This comment has been minimized.
This comment has been minimized.
Sorry, I will trim this PR only to the builtins. They can be still tested with minicore and a macro embedded in test. And I'm going to leave the core_arch change for later. |
7de4ec2 to
869a54b
Compare
This comment has been minimized.
This comment has been minimized.
|
Now CI fails on |
Register the unstable `btf_relocations` feature and add the `#[btf_relocatable]` built-in attribute for structs and unions, that can be used only on BPF architecture. Preserve the attribute in crate metadata, add UI coverage for feature gating and attribute target validation.
Reject field projection and `offset_off!` usage on `#[btf_relocatable]` types.
Add builtin macros for requesting BTF field information from the Rust compiler's frontend perspective: * `btf_field_byte_offset` * `btf_field_byte_size` * `btf_field_exists` Parse them as `BtfFieldInfo` expressions, that carry the kind of requested information (offset, size, exists), base type and the field path. This mechanism supports nested field accesses in one query. Compile these expressions to `BtfFieldInfo` RValue. Add `btf_field_info` method to the `BuilderMethods` trait in codegen SSA, and use it for lowering the `BtfFieldInfo` RValue. Backends without BTF relocation support report an error. Support in backends will be added in follow-up changes. This change does not expose the functionality to the users. A user-facing API will also be added in a follow-up change.
Expose wrappers for the `llvm.preserve.struct.access.index` and
`llvm.preserve.union.access.index` intrinsics.
Lower backend-neutral BTF field paths by mapping Rust field indices to
LLVM aggregate indices and emitting the corresponding intrinsic calls
(`llvm.preserve.{struct,union}.access.index`). Pass the resulting field
pointer to `llvm.bpf.preserve.field.info`.
Test the `btf_field_exists`, `btf_field_byte_offset` and `btf_field_byte_size` builtins and make sure they emit correct LLVM intrinsic calls.
869a54b to
b42b869
Compare
|
The job Click to see the possible cause of the failure (guessed by this bot) |
|
☔ The latest upstream changes (presumably #160889) made this pull request unmergeable. Please resolve the merge conflicts by rebasing. |
| Reborrow(Ty<'tcx>, Mutability, Place<'tcx>), | ||
|
|
||
| /// Queries BTF metadata for a statically resolved field path. | ||
| BtfFieldInfo { base_ty: Ty<'tcx>, path: Box<[BtfFieldStep<'tcx>]>, kind: BtfFieldInfoKind }, |
There was a problem hiding this comment.
This is a very unusual MIR construct, to put it quite mildly. Please add a bit more detail. What is this "metadata" that is being queried and how is it returned? What are the validity conditions? When is this operation UB (if ever)?
We usually work very hard to keep target-specific things away from this core part of the compiler, so seeing BTF-specific things here is somewhat alarming.
There was a problem hiding this comment.
Also, please write comments in this part in the compiler assuming that the reader never heard of BTF or BPF before. So, always include a link for such acronyms.
| Self::ByteOffset => 0, | ||
| Self::ByteSize => 1, | ||
| Self::Exists => 2, |
There was a problem hiding this comment.
Where do these constants come from? Can rustc just do whatever it wants here or are these magic constants defined by some outside protocol?
| } | ||
|
|
||
| BtfFieldInfo { .. } => { | ||
| throw_unsup_format!("BTF field relocation queries cannot be interpreted"); |
There was a problem hiding this comment.
FWIW Miri will probably eventually want to support these, so for stabilization we need to consider what to do. But for now this is fine.
| (unstable, bpf_target_feature, "1.54.0", Some(150247)), | ||
| // no-tracking-issue-start | ||
| /// Allows BTF CO-RE field relocation queries. | ||
| (unstable, btf_relocations, "CURRENT_RUSTC_VERSION", None), |
There was a problem hiding this comment.
You do have a tracking issue though...?
Add experimental Rust support for Compile Once, Run Everywhere (CO-RE) relocations based on the BPF Type Format (BTF). The entire feature is specific to the BPF architecture and cannot be used elsewhere. The feature introduces a
#[btf_relocatable]attribute for structs and unions whose field layout must be queried through BTF-aware operations, and adds BTF-aware bultins:btf_field_existsbtf_field_byte_offsetbtf_field_byte_sizeThis PR does not yet expose these builtins to the users, the
core_archAPI will be added as a follow-up change.The user-facing feature is gated by
#![feature(btf_relocations)].See individual commits for details.
Created with the help of an LLM, initially used to analyze the existing MIR, codegen SSA and LLVM backend code, to help identifying which parts of code need modification. Afterwards, the code and commit messages were written by hand, then reviewed by an LLM.
Tracking issue: #160616
RFC: rust-lang/rfcs#3966
r? @traviscross @nagisa