[llvm] [BPF] Support wrapping BPF map structs into nested, single field structs (PR #144097)
Michal Rostecki via llvm-commits
llvm-commits at lists.llvm.org
Sat Jun 14 00:16:40 PDT 2025
================
@@ -0,0 +1,609 @@
+; RUN: llc -mtriple=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK-SHORT %s
+; RUN: llc -mtriple=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK %s
+
+; Source code:
+; #![no_std]
+; #![no_main]
+; #![allow(dead_code)]
+;
+; pub const BPF_MAP_TYPE_HASH: usize = 1;
+;
+; // The real map definition.
+; pub struct HashMapDef<K, V, const M: usize, const F: usize> {
+; r#type: *const [i32; BPF_MAP_TYPE_HASH],
+; key: *const K,
+; value: *const V,
+; max_entries: *const [i32; M],
+; map_flags: *const [i32; F],
+; }
+; impl<K, V, const M: usize, const F: usize> HashMapDef<K, V, M, F> {
+; pub const fn new() -> Self {
+; Self {
+; r#type: &[0i32; BPF_MAP_TYPE_HASH],
+; key: ::core::ptr::null(),
+; value: ::core::ptr::null(),
+; max_entries: &[0i32; M],
+; map_flags: &[0i32; F],
+; }
+; }
+; }
+; // Use `UnsafeCell` to allow mutability by multiple threads.
+; pub struct HashMap<K, V, const M: usize, const F: usize = 0>(
+; core::cell::UnsafeCell<HashMapDef<K, V, M, F>>,
+; );
+; impl<K, V, const M: usize, const F: usize> HashMap<K, V, M, F> {
+; pub const fn new() -> Self {
+; Self(core::cell::UnsafeCell::new(HashMapDef::new()))
+; }
+; }
+; // Mark `HashMap` as thread-safe.
+; unsafe impl<K: Sync, V: Sync, const M: usize, const F: usize> Sync for HashMap<K, V, M, F> {}
+;
+; // Define custom structs for key and values.
+; pub struct MyKey(u32);
+; pub struct MyValue(u32);
+;
+; #[link_section = ".maps"]
+; #[export_name = "HASH_MAP"]
+; pub static HASH_MAP: HashMap<MyKey, MyValue, 10> = HashMap::new();
----------------
vadorovsky wrote:
Not really, it's just an arbitrary max size of the map.
https://github.com/llvm/llvm-project/pull/144097
More information about the llvm-commits
mailing list