Rollup merge of #153069 - blueshift-gg:BPF_unaligned, r=chenyukang

[BPF] add target feature allows-misaligned-mem-access

This PR adds the allows-misaligned-mem-access target feature to the BPF target. The feature can enable misaligned memory access support in the LLVM backend, aligning Rust’s BPF target behavior with the corresponding LLVM update introduced in [llvm/llvm-project#167013](https://github.com/llvm/llvm-project/pull/167013) (included in LLVM 22).
This commit is contained in:
Jonathan Brouwer
2026-03-23 12:00:58 +01:00
committed by GitHub
7 changed files with 52 additions and 3 deletions

View File

@@ -252,7 +252,10 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
"fp16" => Some(LLVMFeature::new("fullfp16")),
s => Some(LLVMFeature::new(s)),
},
Arch::Bpf => match s {
"allows-misaligned-mem-access" if major < 22 => None,
s => Some(LLVMFeature::new(s)),
},
// Filter out features that are not supported by the current LLVM version
Arch::PowerPC | Arch::PowerPC64 => match s {
"power8-crypto" => Some(LLVMFeature::new("crypto")),

View File

@@ -756,8 +756,10 @@ static WASM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
// tidy-alphabetical-end
];
const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
&[("alu32", Unstable(sym::bpf_target_feature), &[])];
const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
("alu32", Unstable(sym::bpf_target_feature), &[]),
("allows-misaligned-mem-access", Unstable(sym::bpf_target_feature), &[]),
];
static CSKY_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
// tidy-alphabetical-start

View File

@@ -217,6 +217,7 @@ const TARGET_FEATURE_IMPLICATIONS_RAW: &[(&str, &[&str])] = &[
("relaxed-simd", &["simd128"]),
// BPF
("alu32", &[]),
("allows-misaligned-mem-access", &[]),
// CSKY
("10e60", &["7e10"]),
("2e3", &["e2"]),

View File

@@ -0,0 +1,27 @@
//@ add-minicore
//@ assembly-output: emit-asm
//@ compile-flags: --target bpfel-unknown-none -C target_feature=+allows-misaligned-mem-access
//@ min-llvm-version: 22
//@ needs-llvm-components: bpf
#![feature(no_core)]
#![crate_type = "rlib"]
#![no_core]
extern crate minicore;
use minicore::*;
// CHECK-LABEL: test_load_i64:
// CHECK: r0 = *(u64 *)(r1 + 0)
#[no_mangle]
pub unsafe fn test_load_i64(p: *const u64) -> u64 {
let mut tmp: u64 = 0;
copy_nonoverlapping(p as *const u8, &mut tmp as *mut u64 as *mut u8, 8);
tmp
}
// CHECK-LABEL: test_store_i64:
// CHECK: *(u64 *)(r1 + 0) = r2
#[no_mangle]
pub unsafe fn test_store_i64(p: *mut u64, v: u64) {
copy_nonoverlapping(&v as *const u64 as *const u8, p as *mut u8, 8);
}

View File

@@ -277,6 +277,10 @@ trait Drop {
fn drop(&mut self);
}
#[rustc_nounwind]
#[rustc_intrinsic]
pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
pub mod mem {
#[rustc_nounwind]
#[rustc_intrinsic]

View File

@@ -0,0 +1,11 @@
//@ only-bpf
#![crate_type = "lib"]
#![feature(bpf_target_feature)]
#![no_std]
#[no_mangle]
#[target_feature(enable = "allows-misaligned-mem-access")]
// CHECK: define noundef zeroext i8 @foo(i8 noundef returned %arg) unnamed_addr #0 {
pub unsafe fn foo(arg: u8) -> u8 {
arg
}

View File

@@ -17,6 +17,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
`addsubiw`
`adx`
`aes`
`allows-misaligned-mem-access`
`altivec`
`alu32`
`amx-avx512`