enki-anu 0.2.6

BorrowEngine, task recording, and pipeline synthesis for the Enki runtime
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use anyhow::Result;
use ash::vk;
use std::env;
use std::fs::{self, OpenOptions};
use std::io::{self, IsTerminal, Write};
use std::path::PathBuf;
use std::process::{Command, Stdio};

use crate::context::EnkiEngine;
use crate::nam_args_api::{AccessIntent, AccessMode, ArgDescriptor, MemoryDomain};
use crate::pipeline_synthesis::cache::{
    DiskCacheManager, calculate_compute_key, calculate_source_hash,
};
use crate::pipeline_synthesis::compiler::ComputePipelineFactory;
use crate::pipeline_synthesis::types::{ComputeSynthesisInput, TaskCompilationArtifact};
use parsu::compiler::{ParsuArgDescriptorC, ParsuCompiler};

fn find_project_root() -> Option<PathBuf> {
    let mut curr = std::env::current_dir().ok()?;
    loop {
        if curr.join("Cargo.toml").exists() {
            return Some(curr);
        }
        if !curr.pop() {
            break;
        }
    }
    None
}

fn prompt_interactive_config() -> bool {
    if !io::stdin().is_terminal() {
        return false;
    }

    eprintln!(
        "\n\x1b[1;33mwarning\x1b[0m\x1b[1m: missing required compilation profile and LLVM bitcode for GPU JIT synthesis\x1b[0m\n\
         \x1b[1;34m  =\x1b[0m \x1b[1mnote:\x1b[0m GPU synthesis requires optimization (`opt-level = 2`) to lower host abstractions into silicon primitives\n\
         \x1b[1;34m  =\x1b[0m \x1b[1mnote:\x1b[0m execution requires `target.'cfg(all())'.rustflags = [\"--emit=llvm-bc\"]`\n\
         \x1b[1;34m  =\x1b[0m \x1b[1mhelp:\x1b[0m configuration can be appended to `.cargo/config.toml` safely without modifying existing blocks"
    );

    eprint!(
        "\x1b[1;34m  -->\x1b[0m \x1b[1mappend configuration to `.cargo/config.toml`? [Y/n]\x1b[0m "
    );
    let _ = io::stderr().flush();

    let mut answer = String::new();
    if io::stdin().read_line(&mut answer).is_ok() {
        let trimmed = answer.trim();
        trimmed.is_empty() || trimmed.eq_ignore_ascii_case("y")
    } else {
        false
    }
}

fn apply_config_and_restart() {
    let root = match find_project_root() {
        Some(r) => r,
        None => return,
    };

    let cargo_dir = root.join(".cargo");
    let config_path = cargo_dir.join("config.toml");

    let _ = fs::create_dir_all(&cargo_dir);

    let already_configured = fs::read_to_string(&config_path)
        .map(|c| c.contains("--emit=llvm-bc"))
        .unwrap_or(false);

    if !already_configured {
        let snippet = r#"
# --- Added by Enki for GPU JIT compilation ---
[profile.dev]
opt-level = 2
codegen-units = 1

[profile.dev.package."*"]
opt-level = 2

[profile.release]
opt-level = 2
codegen-units = 1

[profile.release.package."*"]
opt-level = 2

[target.'cfg(all())']
rustflags = ["--emit=llvm-bc"]
# ---------------------------------------------
"#;
        let append_res = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&config_path)
            .and_then(|mut f| f.write_all(snippet.as_bytes()));

        if append_res.is_err() {
            return;
        }
    }

    eprintln!("\x1b[1;32m     Updated\x1b[0m `.cargo/config.toml` successfully");
    eprintln!("\x1b[1;32m   Re-running\x1b[0m `cargo run` with LLVM bitcode generation...\n");

    let mut cmd = Command::new("cargo");
    cmd.arg("run");

    if !cfg!(debug_assertions) {
        cmd.arg("--release");
    }

    let user_args: Vec<String> = std::env::args().skip(1).collect();
    if !user_args.is_empty() {
        cmd.arg("--");
        cmd.args(&user_args);
    }

    cmd.stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit());

    match cmd.status() {
        Ok(status) => std::process::exit(status.code().unwrap_or(1)),
        Err(err) => {
            eprintln!("\x1b[1;31merror\x1b[0m: failed to re-execute `cargo run`: {err}");
            std::process::exit(1);
        }
    }
}

fn convert_to_parsu_c_descriptors(descriptors: &[ArgDescriptor]) -> Vec<ParsuArgDescriptorC> {
    descriptors
        .iter()
        .map(|d| {
            let mode_val = match d.mode {
                AccessMode::ByValue => 0,
                AccessMode::SpmdCellMut => 1,
                AccessMode::SpmdCellConst => 2,
                AccessMode::GlobalSliceRead => 3,
                AccessMode::GlobalSliceReadWrite => 4,
                AccessMode::AtomicCell => 5,
                AccessMode::AtomicSlice => 6,
                AccessMode::WorkgroupScratchpad => 7,
            };

            let domain_val = match d.domain {
                MemoryDomain::ArenaPayload => 0,
                MemoryDomain::PhysicalStorageBuffer => 1,
                MemoryDomain::WorkgroupShared => 2,
                MemoryDomain::ZeroSized => 3,
            };

            let intent_val = match d.intent {
                AccessIntent::Read => 0,
                AccessIntent::Write => 1,
                AccessIntent::ReadWrite => 2,
            };

            ParsuArgDescriptorC {
                mode: mode_val,
                domain: domain_val,
                intent: intent_val,
                is_optional: if d.is_optional { 1 } else { 0 },
                element_size: d.element_size,
                stride: d.stride,
                alignment: d.alignment,
                arena_size_bytes: d.arena_size_bytes,
            }
        })
        .collect()
}

fn find_native_bitcode() -> Result<PathBuf> {
    let exe_path = env::current_exe()?;
    let raw_name = exe_path
        .file_name()
        .and_then(|n| n.to_str())
        .ok_or_else(|| anyhow::anyhow!("Failed to parse executable name"))?;

    let exe_name = raw_name.strip_suffix(".exe").unwrap_or(raw_name);
    let exe_name_alt = exe_name.replace('-', "_");

    let exe_dir = exe_path
        .parent()
        .ok_or_else(|| anyhow::anyhow!("Failed to find executable directory"))?;

    let deps_dir = exe_dir.join("deps");
    if !deps_dir.exists() {
        return Err(anyhow::anyhow!(
            "Dependencies directory not found at {:?}",
            deps_dir
        ));
    }

    let mut newest_bc: Option<(PathBuf, std::time::SystemTime)> = None;

    if let Ok(entries) = fs::read_dir(deps_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_file() && path.extension().is_some_and(|ext| ext == "bc") {
                let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

                let matches_exe =
                    file_name.starts_with(exe_name) || file_name.starts_with(&exe_name_alt);

                if matches_exe && let Ok(mtime) = entry.metadata().and_then(|m| m.modified()) {
                    newest_bc = Some(newest_bc.map_or(
                        (path.clone(), mtime),
                        |(ref best_path, best_mtime)| {
                            if mtime > best_mtime {
                                (path, mtime)
                            } else {
                                (best_path.clone(), best_mtime)
                            }
                        },
                    ));
                }
            }
        }
    }

    newest_bc.map(|(path, _)| path).ok_or_else(|| {
        anyhow::anyhow!(
            "Bitcode file not found for '{}'. Please rebuild your project using 'cargo build'.",
            exe_name
        )
    })
}

pub struct JitCoordinator;

impl JitCoordinator {
    pub fn orchestrate(engine: &EnkiEngine, input: &ComputeSynthesisInput) -> Result<()> {
        let cache_manager = DiskCacheManager::new();
        let target_nam_name = &input.nam_name;
        let local_size = input.local_size;

        let bc_path = match find_native_bitcode() {
            Ok(path) => path,
            Err(_) => {
                if prompt_interactive_config() {
                    apply_config_and_restart();
                }

                let diag = crate::diagnostics::ingress::compiler::bitcode_not_found();
                let formatted = crate::diagnostics::emit_diagnostic(&diag);
                return Err(anyhow::anyhow!("{formatted}"));
            }
        };

        let bc_mtime = fs::metadata(&bc_path).and_then(|m| m.modified()).ok();

        let hash_key = calculate_compute_key(
            calculate_source_hash(target_nam_name),
            target_nam_name,
            local_size,
            &input.arg_descriptors,
        );

        let cache_path = cache_manager.get_cache_path(hash_key);
        let cache_mtime = fs::metadata(&cache_path).and_then(|m| m.modified()).ok();

        let is_dirty = match (bc_mtime, cache_mtime) {
            (Some(bc_time), Some(cache_time)) => bc_time > cache_time,
            _ => true,
        };

        let mut cache_lock = engine.pipeline_cache.lock().unwrap();

        if is_dirty {
            let bc_path_str = bc_path
                .to_str()
                .ok_or_else(|| anyhow::anyhow!("Invalid bitcode path string"))?;

            let project_root = input.host_manifest_dir.clone().unwrap_or_else(|| {
                std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| {
                    std::env::current_dir()
                        .unwrap()
                        .to_string_lossy()
                        .to_string()
                })
            });

            let local_size_i32 = (
                local_size.0 as i32,
                local_size.1 as i32,
                local_size.2 as i32,
            );

            let c_descriptors = convert_to_parsu_c_descriptors(&input.arg_descriptors);

            let compiled_nams = match ParsuCompiler::compile_bitcode(
                bc_path_str,
                target_nam_name,
                &project_root,
                local_size_i32,
                &c_descriptors,
            ) {
                Ok(nams) => nams,
                Err(parsu::compiler::ParsuError::ToolchainMissing) => {
                    let diag = crate::diagnostics::ingress::compiler::toolchain_not_found();
                    let formatted = crate::diagnostics::emit_diagnostic(&diag);
                    return Err(anyhow::anyhow!("{formatted}"));
                }

                Err(parsu::compiler::ParsuError::Diagnostics(diags)) => {
                    let unified_diags = crate::diagnostics::from_parsu_batch(diags);

                    let error_count = unified_diags.len();
                    let formatted = crate::diagnostics::emit_batch(&unified_diags);

                    let bin_name = std::env::current_exe()
                        .ok()
                        .and_then(|p| p.file_name().map(|n| n.to_string_lossy().to_string()))
                        .unwrap_or_else(|| "enki_app".to_string());

                    let count_str = if error_count == 1 {
                        "1 previous error".to_string()
                    } else {
                        format!("{error_count} previous errors")
                    };

                    let summary = format!(
                        "\x1b[1;91merror\x1b[0m: could not compile `{bin_name}` (nam `{target_nam_name}`) due to {count_str}\n"
                    );

                    return Err(anyhow::anyhow!("{formatted}{summary}"));
                }
                Err(parsu::compiler::ParsuError::Generic(msg)) => {
                    return Err(anyhow::anyhow!("Parsu compilation failed: {}", msg));
                }
            };

            let target_nam = compiled_nams
                .into_iter()
                .find(|k| target_nam_name.ends_with(&k.nam_name))
                .ok_or_else(|| {
                    anyhow::anyhow!(
                        "[JitCoordinator] Nam '{}' not found in compiled modules",
                        target_nam_name
                    )
                })?;

            let stack_size_per_thread = target_nam.stack_size_per_thread;

            let spirv_bytes = bytemuck::cast_slice::<u32, u8>(&target_nam.spirv_bytecode);

            let (pipeline, layout) =
                ComputePipelineFactory::compile(engine, spirv_bytes, *cache_lock)?;

            let artifact = TaskCompilationArtifact {
                pipeline,
                layout,
                local_size,
                stack_size_per_thread,
            };

            engine.synthesizer.cache.insert(hash_key, artifact);

            let pipeline_cache_data = unsafe {
                engine
                    .raw_device()
                    .get_pipeline_cache_data(*cache_lock)
                    .unwrap_or_default()
            };

            let _ = cache_manager.save_cache(
                hash_key,
                &engine.hardware_profile,
                &pipeline_cache_data,
                spirv_bytes,
                stack_size_per_thread,
            );
        } else {
            if let Some((pipeline_cache_data, spirv_bytes, stack_size_per_thread)) =
                cache_manager.load_cache(hash_key, &engine.hardware_profile)
            {
                device_update_pipeline_cache(
                    engine.raw_device(),
                    &mut cache_lock,
                    &pipeline_cache_data,
                );

                if let Ok((pipeline, layout)) =
                    ComputePipelineFactory::compile(engine, &spirv_bytes, *cache_lock)
                {
                    let artifact = TaskCompilationArtifact {
                        pipeline,
                        layout,
                        local_size,
                        stack_size_per_thread,
                    };
                    engine.synthesizer.cache.insert(hash_key, artifact);
                }
            }
        }

        Ok(())
    }
}

fn device_update_pipeline_cache(
    device: &ash::Device,
    cache_lock: &mut vk::PipelineCache,
    pipeline_cache_data: &[u8],
) {
    unsafe {
        device.destroy_pipeline_cache(*cache_lock, None);
        let create_info = vk::PipelineCacheCreateInfo::default().initial_data(pipeline_cache_data);
        if let Ok(new_cache) = device.create_pipeline_cache(&create_info, None) {
            *cache_lock = new_cache;
        }
    }
}