Skip to main content

softgpu_core/
executable.rs

1//! SoftGPU HSA executable / code-object reader state (v0.8).
2
3use softgpu_amd_code_object::{load_agent_image, LaunchAbi, LoadableKernel};
4use std::collections::HashMap;
5
6/// SoftGPU code-object reader (owned bytes).
7#[derive(Debug, Clone)]
8pub struct CodeObjectReader {
9    pub bytes: Vec<u8>,
10}
11
12/// SoftGPU executable symbol (kernel).
13#[derive(Debug, Clone)]
14pub struct ExecutableSymbol {
15    pub name: String,
16    pub symbol: String,
17    pub kernel_object: u64,
18    pub kernarg_segment_size: u32,
19    pub kernarg_segment_align: u32,
20    pub group_segment_size: u32,
21    pub private_segment_size: u32,
22    pub launch_abi: LaunchAbi,
23}
24
25/// SoftGPU executable (unfrozen → load → freeze).
26#[derive(Debug, Clone)]
27pub struct SoftGpuExecutable {
28    pub frozen: bool,
29    pub symbols: Vec<ExecutableSymbol>,
30}
31
32/// Parse SoftGPU-supported agent bytes into loadable kernels.
33pub fn parse_agent_kernels(bytes: &[u8]) -> Result<Vec<LoadableKernel>, String> {
34    let img = load_agent_image(bytes).map_err(|e| e.to_string())?;
35    Ok(img.kernels)
36}
37
38/// Index symbols by name and by `.kd` symbol for SoftGPU lookup.
39pub fn index_symbols(symbols: &[ExecutableSymbol]) -> HashMap<String, usize> {
40    let mut map = HashMap::new();
41    for (i, s) in symbols.iter().enumerate() {
42        map.insert(s.name.clone(), i);
43        map.insert(s.symbol.clone(), i);
44        // HIP often strips `.kd`
45        if let Some(stripped) = s.symbol.strip_suffix(".kd") {
46            map.insert(stripped.to_string(), i);
47        }
48    }
49    map
50}