Skip to content

Commit f62b2f3

Browse files
[BPF] add target feature allows-misaligned-mem-access
1 parent 11ad63a commit f62b2f3

7 files changed

Lines changed: 52 additions & 3 deletions

File tree

compiler/rustc_codegen_llvm/src/llvm_util.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ pub(crate) fn to_llvm_features<'a>(sess: &Session, s: &'a str) -> Option<LLVMFea
252252
"fp16" => Some(LLVMFeature::new("fullfp16")),
253253
s => Some(LLVMFeature::new(s)),
254254
},
255-
255+
Arch::Bpf => match s {
256+
"allows-misaligned-mem-access" if major < 22 => None,
257+
s => Some(LLVMFeature::new(s)),
258+
},
256259
// Filter out features that are not supported by the current LLVM version
257260
Arch::LoongArch32 | Arch::LoongArch64 => match s {
258261
"32s" if major < 21 => None,

compiler/rustc_target/src/target_features.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -756,8 +756,10 @@ static WASM_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
756756
// tidy-alphabetical-end
757757
];
758758

759-
const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] =
760-
&[("alu32", Unstable(sym::bpf_target_feature), &[])];
759+
const BPF_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
760+
("alu32", Unstable(sym::bpf_target_feature), &[]),
761+
("allows-misaligned-mem-access", Unstable(sym::bpf_target_feature), &[]),
762+
];
761763

762764
static CSKY_FEATURES: &[(&str, Stability, ImpliedFeatures)] = &[
763765
// tidy-alphabetical-start

src/tools/rust-analyzer/crates/hir-ty/src/target_feature.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ const TARGET_FEATURE_IMPLICATIONS_RAW: &[(&str, &[&str])] = &[
217217
("relaxed-simd", &["simd128"]),
218218
// BPF
219219
("alu32", &[]),
220+
("allows-misaligned-mem-access", &[]),
220221
// CSKY
221222
("10e60", &["7e10"]),
222223
("2e3", &["e2"]),
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ add-minicore
2+
//@ assembly-output: emit-asm
3+
//@ compile-flags: --target bpfel-unknown-none -C target_feature=+allows-misaligned-mem-access
4+
//@ min-llvm-version: 22
5+
//@ needs-llvm-components: bpf
6+
#![feature(no_core)]
7+
#![crate_type = "rlib"]
8+
#![no_core]
9+
10+
extern crate minicore;
11+
use minicore::*;
12+
13+
// CHECK-LABEL: test_load_i64:
14+
// CHECK: r0 = *(u64 *)(r1 + 0)
15+
#[no_mangle]
16+
pub unsafe fn test_load_i64(p: *const u64) -> u64 {
17+
let mut tmp: u64 = 0;
18+
copy_nonoverlapping(p as *const u8, &mut tmp as *mut u64 as *mut u8, 8);
19+
tmp
20+
}
21+
22+
// CHECK-LABEL: test_store_i64:
23+
// CHECK: *(u64 *)(r1 + 0) = r2
24+
#[no_mangle]
25+
pub unsafe fn test_store_i64(p: *mut u64, v: u64) {
26+
copy_nonoverlapping(&v as *const u64 as *const u8, p as *mut u8, 8);
27+
}

tests/auxiliary/minicore.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,10 @@ trait Drop {
275275
fn drop(&mut self);
276276
}
277277

278+
#[rustc_nounwind]
279+
#[rustc_intrinsic]
280+
pub const unsafe fn copy_nonoverlapping<T>(src: *const T, dst: *mut T, count: usize);
281+
278282
pub mod mem {
279283
#[rustc_nounwind]
280284
#[rustc_intrinsic]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ only-bpf
2+
#![crate_type = "lib"]
3+
#![feature(bpf_target_feature)]
4+
#![no_std]
5+
6+
#[no_mangle]
7+
#[target_feature(enable = "allows-misaligned-mem-access")]
8+
// CHECK: define noundef zeroext i8 @foo(i8 noundef returned %arg) unnamed_addr #0 {
9+
pub unsafe fn foo(arg: u8) -> u8 {
10+
arg
11+
}

tests/ui/check-cfg/target_feature.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ LL | cfg!(target_feature = "_UNEXPECTED_VALUE");
1717
`addsubiw`
1818
`adx`
1919
`aes`
20+
`allows-misaligned-mem-access`
2021
`altivec`
2122
`alu32`
2223
`amx-avx512`

0 commit comments

Comments
 (0)