Skip to content

Add BTF relocation builtins - #161107

Open
vadorovsky wants to merge 5 commits into
rust-lang:mainfrom
vadorovsky:vad/btf-relocations
Open

Add BTF relocation builtins#161107
vadorovsky wants to merge 5 commits into
rust-lang:mainfrom
vadorovsky:vad/btf-relocations

Conversation

@vadorovsky

@vadorovsky vadorovsky commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

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_exists
  • btf_field_byte_offset
  • btf_field_byte_size

This PR does not yet expose these builtins to the users, the core_arch API 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

@rustbot

rustbot commented Aug 14, 2026

Copy link
Copy Markdown
Collaborator

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

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @folkertdev, @sayantn

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a, @makai410

Some changes occurred to the CTFE machinery

cc @RalfJung, @oli-obk, @lcnr

Some changes occurred in compiler/rustc_passes/src/check_attr.rs

cc @jdonszelmann, @JonathanBrouwer

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

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 rustc_ty_utils::consts.rs

cc @BoxyUwU

The parser was modified, potentially altering the grammar of (stable) Rust
which would be a breaking change.

cc @fmease

Some changes occurred to the CTFE / Miri interpreter

cc @rust-lang/miri

@rustbot rustbot added the A-attributes Area: Attributes (`#[…]`, `#![…]`) label Aug 14, 2026
@rustbot rustbot added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Aug 14, 2026
@rust-log-analyzer

This comment has been minimized.

@vadorovsky

Copy link
Copy Markdown
Contributor Author

stdarch is developed in its own repository. If possible, consider making this change to rust-lang/stdarch instead.

cc @Amanieu, @folkertdev, @sayantn

[...]

rustc_codegen_cranelift is developed in its own repository. If possible, consider making this change to rust-lang/rustc_codegen_cranelift instead.

cc @bjorn3

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.

@vadorovsky
vadorovsky force-pushed the vad/btf-relocations branch from 7de4ec2 to 869a54b Compare August 15, 2026 07:01
@rust-log-analyzer

This comment has been minimized.

@vadorovsky

Copy link
Copy Markdown
Contributor Author

Now CI fails on rustc_codegen_cranelift. https://github.com/rust-lang/rust/actions/runs/31870909600/job/94979319914 How do we usually fix such chicken-and-egg problems?

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.
@vadorovsky
vadorovsky force-pushed the vad/btf-relocations branch from 869a54b to b42b869 Compare August 15, 2026 07:23
@vadorovsky vadorovsky changed the title BTF relocations Add BTF relocation builtins Aug 15, 2026
@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job pr-check-2 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
    Checking cranelift-object v0.134.0
[RUSTC-TIMING] cranelift_frontend test:false 0.209
[RUSTC-TIMING] cranelift_object test:false 0.097
    Checking rustc_codegen_cranelift v0.1.0 (/checkout/compiler/rustc_codegen_cranelift)
error[E0004]: non-exhaustive patterns: `rustc_middle::mir::Rvalue::BtfFieldInfo { .. }` not covered
    --> compiler/rustc_codegen_cranelift/src/base.rs:632:19
     |
 632 |             match to_place_and_rval.1 {
     |                   ^^^^^^^^^^^^^^^^^^^ pattern `rustc_middle::mir::Rvalue::BtfFieldInfo { .. }` not covered
     |
note: `rustc_middle::mir::Rvalue<'_>` defined here
    --> compiler/rustc_middle/src/mir/syntax.rs:1342:1
     |
1342 | pub enum Rvalue<'tcx> {
     | ^^^^^^^^^^^^^^^^^^^^^
...
1469 |     BtfFieldInfo { base_ty: Ty<'tcx>, path: Box<[BtfFieldStep<'tcx>]>, kind: BtfFieldInfoKind },
     |     ------------ not covered
     = note: the matched value is of type `rustc_middle::mir::Rvalue<'_>`
help: ensure that all possible cases are being handled by adding a match arm with a wildcard pattern or an explicit pattern as shown
     |
 924 ~                 Rvalue::CopyForDeref(_) => bug!("`CopyForDeref` in codegen"),
 925 ~                 rustc_middle::mir::Rvalue::BtfFieldInfo { .. } => todo!(),
     |

For more information about this error, try `rustc --explain E0004`.
[RUSTC-TIMING] rustc_codegen_cranelift test:false 0.944
error: could not compile `rustc_codegen_cranelift` (lib) due to 1 previous error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-attributes Area: Attributes (`#[…]`, `#![…]`) A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. A-test-infra-minicore Area: `minicore` test auxiliary and `//@ add-core-stubs` S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants