Api traits test code (#487)

Add initial templated API test support. This needs to be improved to use an attribute macro, but that will require some major surgery :(
This commit is contained in:
aiwhskruht 2025-09-02 08:57:54 -07:00 committed by GitHub
commit 4752fcdcf2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 115 additions and 4 deletions

16
Cargo.lock generated
View file

@ -447,6 +447,21 @@ dependencies = [
"syn 2.0.89",
]
[[package]]
name = "cuda_tests"
version = "0.0.0"
dependencies = [
"cuda_macros",
"cuda_types",
"dtor 0.0.6",
"lazy_static",
"lz4-sys",
"num_enum",
"paste",
"rustc-hash 1.1.0",
"tempfile",
]
[[package]]
name = "cuda_types"
version = "0.0.0"
@ -3714,6 +3729,7 @@ dependencies = [
"blake3",
"comgr",
"cuda_macros",
"cuda_tests",
"cuda_types",
"dark_api",
"dtor 0.0.7",

View file

@ -6,6 +6,7 @@ members = [
"comgr",
"cuda_macros",
"cuda_types",
"cuda_tests",
"dark_api",
"detours-sys",
"ext/amd_comgr-sys",

View file

@ -10,7 +10,7 @@ use syn::punctuated::Punctuated;
use syn::visit_mut::VisitMut;
use syn::{
bracketed, parse_macro_input, File, ForeignItem, ForeignItemFn, Ident, Item, Path, Signature,
Token,
Token, token
};
const CUDA_RS: &'static str = include_str! {"cuda.rs"};
@ -308,3 +308,40 @@ fn join(
}
}
#[proc_macro]
pub fn generate_api_macro(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as ApiMacroInput);
let ApiMacroInput(_, trait_name, _, type_name, _, macro_name) = input;
let expanded = quote! {
struct #type_name;
macro_rules! #macro_name {
($($abi:literal fn $fn_name:ident( $( $arg_id:ident : $arg_type:ty ),* ) -> $ret_type:ty;)*) => {
impl #trait_name for #type_name {
fn new() -> Self { Self }
$(
#[inline(always)]
fn $fn_name(&self, $( $arg_id : $arg_type ),* ) -> $ret_type {
unsafe { super::$fn_name( $( $arg_id ),* ) }
}
)*
}
};
}
};
TokenStream::from(expanded)
}
#[allow(dead_code)]
struct ApiMacroInput(token::Impl, Path, token::For, Path, token::Use, Ident);
impl syn::parse::Parse for ApiMacroInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
Ok(ApiMacroInput(
input.parse()?, // impl
input.parse()?, // trait
input.parse()?, // for
input.parse()?, // type
input.parse()?, // using
input.parse()? // macro
))
}
}

16
cuda_tests/Cargo.toml Normal file
View file

@ -0,0 +1,16 @@
[package]
name = "cuda_tests"
version = "0.0.0"
authors = ["Andrzej Janik <vosen@vosen.pl>"]
edition = "2021"
[dependencies]
cuda_types = { path = "../cuda_types" }
cuda_macros = { path = "../cuda_macros" }
lazy_static = "1.4"
num_enum = "0.4"
lz4-sys = "1.9"
tempfile = "3"
paste = "1.0"
rustc-hash = "1.1"
dtor = "0.0.6"

View file

@ -0,0 +1,8 @@
use crate::api_trait;
use cuda_types::cuda::*;
cuda_macros::cuda_function_declarations!(api_trait);
pub unsafe fn init_check<T: Api>(api: T) {
assert_eq!(api.cuInit(0), CUresult::SUCCESS);
}

24
cuda_tests/src/lib.rs Normal file
View file

@ -0,0 +1,24 @@
#[macro_export]
macro_rules! api_trait {
($($abi:literal fn $fn_name:ident( $($arg_id:ident : $arg_type:ty),* ) -> $ret_type:ty;)*) => {
pub trait Api {
fn new() -> Self;
$(fn $fn_name(&self, $( $arg_id : $arg_type ),* ) -> $ret_type;)*
}
};
}
#[macro_export]
macro_rules! api_test {
($func:ident, $type:ty) => {
paste::paste! {
#[test]
#[allow(non_snake_case)]
fn [<$func _test>]() {
unsafe { $func::<$type>(<$type>::new()) }
}
}
};
}
pub mod cuda;

View file

@ -27,6 +27,7 @@ zluda_common = { path = "../zluda_common" }
blake3 = "1.8.2"
serde = "1.0.219"
serde_json = "1.0.142"
cuda_tests = { path = "../cuda_tests" }
[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["heapapi", "std"] }

View file

@ -1 +0,0 @@
pub mod rt;

View file

@ -1,2 +0,0 @@
pub enum ContextState {}
pub enum ContextStateManager {}

View file

@ -160,3 +160,6 @@ cuda_macros::cuda_function_declarations!(
],
implemented_in_function <= [cuLaunchKernel,]
);
#[cfg(test)]
mod tests;

8
zluda/src/tests/mod.rs Normal file
View file

@ -0,0 +1,8 @@
use cuda_macros::generate_api_macro;
use cuda_tests::api_test;
use cuda_tests::cuda::*;
generate_api_macro!(impl cuda_tests::cuda::Api for TestApi use implemented_test);
cuda_macros::cuda_function_declarations!(implemented_test);
api_test!(init_check, TestApi);