oxicuda-ptx 0.1.0

OxiCUDA PTX - PTX code generation DSL and IR for GPU kernel development
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Disk-based PTX kernel cache.
//!
//! [`PtxCache`] provides persistent caching of generated PTX text on disk,
//! keyed by kernel name, parameter hash, and target architecture. This avoids
//! redundant PTX generation for kernels that have already been compiled.
//!
//! The cache stores files at `~/.cache/oxicuda/ptx/` (or a fallback location
//! under `std::env::temp_dir()` if the home directory is unavailable).
//!
//! # Example
//!
//! ```
//! use oxicuda_ptx::cache::{PtxCache, PtxCacheKey};
//! use oxicuda_ptx::arch::SmVersion;
//!
//! let cache = PtxCache::new().expect("cache init failed");
//! let key = PtxCacheKey {
//!     kernel_name: "vector_add".to_string(),
//!     params_hash: 0x12345678,
//!     sm_version: SmVersion::Sm80,
//! };
//!
//! let ptx = cache.get_or_generate(&key, || {
//!     Ok("// generated PTX".to_string())
//! }).expect("generation failed");
//! assert!(ptx.contains("generated PTX"));
//! # cache.clear().ok();
//! ```

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;

use crate::arch::SmVersion;
use crate::error::PtxGenError;

/// Disk-based PTX kernel cache.
///
/// Caches generated PTX text files on disk to avoid redundant code generation.
/// Files are stored as `{kernel_name}_{sm}_{hash:016x}.ptx` in the cache
/// directory.
pub struct PtxCache {
    /// The root cache directory.
    cache_dir: PathBuf,
}

/// Cache lookup key for PTX kernels.
///
/// The key combines the kernel name, a hash of the generation parameters,
/// and the target architecture to produce a unique filename.
#[derive(Debug, Clone, Hash)]
pub struct PtxCacheKey {
    /// The kernel function name.
    pub kernel_name: String,
    /// A hash of the kernel generation parameters (tile sizes, precisions, etc.).
    pub params_hash: u64,
    /// The target GPU architecture.
    pub sm_version: SmVersion,
}

impl PtxCacheKey {
    /// Converts this key to a filename suitable for disk storage.
    ///
    /// Format: `{kernel_name}_{sm}_{combined_hash:016x}.ptx`
    ///
    /// The combined hash includes both the `params_hash` and the full key hash
    /// to minimize collision risk.
    #[must_use]
    pub fn to_filename(&self) -> String {
        let mut hasher = DefaultHasher::new();
        self.hash(&mut hasher);
        let full_hash = hasher.finish();
        format!(
            "{}_{}_{:016x}.ptx",
            sanitize_filename(&self.kernel_name),
            self.sm_version.as_ptx_str(),
            full_hash
        )
    }
}

impl PtxCache {
    /// Creates a new PTX cache, initializing the cache directory.
    ///
    /// The cache directory is `~/.cache/oxicuda/ptx/`. If the home directory
    /// cannot be determined, falls back to `{temp_dir}/oxicuda_ptx_cache/`.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if the cache directory cannot be created.
    pub fn new() -> Result<Self, std::io::Error> {
        let cache_dir = resolve_cache_dir();
        std::fs::create_dir_all(&cache_dir)?;
        Ok(Self { cache_dir })
    }

    /// Creates a new PTX cache at a specific directory.
    ///
    /// Useful for testing or when a custom cache location is desired.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if the directory cannot be created.
    pub fn with_dir(dir: PathBuf) -> Result<Self, std::io::Error> {
        std::fs::create_dir_all(&dir)?;
        Ok(Self { cache_dir: dir })
    }

    /// Returns the cache directory path.
    #[must_use]
    pub const fn cache_dir(&self) -> &PathBuf {
        &self.cache_dir
    }

    /// Looks up a cached PTX string, or generates and caches it if not found.
    ///
    /// If the cache contains a file matching the key, its contents are returned
    /// directly. Otherwise, the `generate` closure is called to produce the PTX
    /// text, which is then written to the cache before being returned.
    ///
    /// # Errors
    ///
    /// Returns [`PtxGenError`] if:
    /// - The generate closure fails
    /// - Disk I/O fails during read or write
    pub fn get_or_generate<F>(&self, key: &PtxCacheKey, generate: F) -> Result<String, PtxGenError>
    where
        F: FnOnce() -> Result<String, PtxGenError>,
    {
        let path = self.cache_dir.join(key.to_filename());

        // Try to read from cache
        match std::fs::read_to_string(&path) {
            Ok(contents) if !contents.is_empty() => return Ok(contents),
            _ => {}
        }

        // Generate fresh PTX
        let ptx = generate()?;

        // Write to cache (best-effort; cache write failure is non-fatal)
        if let Err(e) = std::fs::write(&path, &ptx) {
            // Log the error but don't fail the generation
            eprintln!(
                "oxicuda-ptx: cache write failed for {}: {e}",
                path.display()
            );
        }

        Ok(ptx)
    }

    /// Retrieves cached PTX for the given key, if it exists.
    ///
    /// Returns `None` if no cached entry is found or the file is empty.
    #[must_use]
    pub fn get(&self, key: &PtxCacheKey) -> Option<String> {
        let path = self.cache_dir.join(key.to_filename());
        match std::fs::read_to_string(&path) {
            Ok(contents) if !contents.is_empty() => Some(contents),
            _ => None,
        }
    }

    /// Stores PTX text in the cache under the given key.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if the write fails.
    pub fn put(&self, key: &PtxCacheKey, ptx: &str) -> Result<(), std::io::Error> {
        let path = self.cache_dir.join(key.to_filename());
        std::fs::write(&path, ptx)
    }

    /// Removes all cached PTX files from the cache directory.
    ///
    /// Only removes `.ptx` files; other files and subdirectories are left intact.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if directory listing or file removal fails.
    pub fn clear(&self) -> Result<(), std::io::Error> {
        let entries = std::fs::read_dir(&self.cache_dir)?;
        for entry in entries {
            let entry = entry?;
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("ptx") {
                std::fs::remove_file(&path)?;
            }
        }
        Ok(())
    }

    /// Returns the number of cached PTX files.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if the directory cannot be read.
    pub fn len(&self) -> Result<usize, std::io::Error> {
        let entries = std::fs::read_dir(&self.cache_dir)?;
        let count = entries
            .filter_map(Result::ok)
            .filter(|e| e.path().extension().and_then(|ext| ext.to_str()) == Some("ptx"))
            .count();
        Ok(count)
    }

    /// Returns `true` if the cache contains no PTX files.
    ///
    /// # Errors
    ///
    /// Returns `std::io::Error` if the directory cannot be read.
    pub fn is_empty(&self) -> Result<bool, std::io::Error> {
        self.len().map(|n| n == 0)
    }
}

/// Resolves the cache directory path, with fallback.
fn resolve_cache_dir() -> PathBuf {
    // Try ~/.cache/oxicuda/ptx/
    if let Some(home) = home_dir() {
        let cache = home.join(".cache").join("oxicuda").join("ptx");
        return cache;
    }

    // Fallback to temp dir
    std::env::temp_dir().join("oxicuda_ptx_cache")
}

/// Attempts to determine the user's home directory.
///
/// Checks `HOME` (Unix) and `USERPROFILE` (Windows) environment variables.
fn home_dir() -> Option<PathBuf> {
    std::env::var_os("HOME")
        .or_else(|| std::env::var_os("USERPROFILE"))
        .map(PathBuf::from)
}

/// Sanitizes a string for use as part of a filename.
///
/// Replaces any character that is not alphanumeric, underscore, or hyphen
/// with an underscore.
fn sanitize_filename(name: &str) -> String {
    name.chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '_' || c == '-' {
                c
            } else {
                '_'
            }
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Returns a unique temp directory for a test, using a counter to avoid collisions.
    fn test_cache_dir_named(name: &str) -> PathBuf {
        std::env::temp_dir()
            .join("oxicuda_ptx_cache_test")
            .join(format!("{}_{}", name, std::process::id()))
    }

    fn cleanup(dir: &PathBuf) {
        let _ = std::fs::remove_dir_all(dir);
    }

    #[test]
    fn cache_key_to_filename() {
        let key = PtxCacheKey {
            kernel_name: "vector_add".to_string(),
            params_hash: 0xDEAD_BEEF,
            sm_version: SmVersion::Sm80,
        };
        let filename = key.to_filename();
        assert!(filename.starts_with("vector_add_sm_80_"));
        assert!(
            std::path::Path::new(&filename)
                .extension()
                .is_some_and(|ext| ext.eq_ignore_ascii_case("ptx"))
        );
    }

    #[test]
    fn cache_key_sanitization() {
        let key = PtxCacheKey {
            kernel_name: "my.kernel/v2".to_string(),
            params_hash: 42,
            sm_version: SmVersion::Sm90,
        };
        let filename = key.to_filename();
        assert!(
            !filename.contains('.')
                || std::path::Path::new(&filename)
                    .extension()
                    .is_some_and(|ext| ext.eq_ignore_ascii_case("ptx"))
        );
        // The kernel name part should not contain dots or slashes
        let prefix = filename.split("_sm_90_").next().unwrap_or("");
        assert!(!prefix.contains('/'));
    }

    #[test]
    fn cache_new_and_clear() {
        let dir = test_cache_dir_named("new_and_clear");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");
        assert!(cache.is_empty().expect("should check empty"));

        let key = PtxCacheKey {
            kernel_name: "test".to_string(),
            params_hash: 1,
            sm_version: SmVersion::Sm80,
        };
        cache.put(&key, "// test ptx").expect("put should succeed");
        assert!(!cache.is_empty().expect("should check non-empty"));
        assert_eq!(cache.len().expect("len"), 1);

        cache.clear().expect("clear should succeed");
        assert!(cache.is_empty().expect("should be empty after clear"));

        cleanup(&dir);
    }

    #[test]
    fn get_or_generate_caches_result() {
        let dir = test_cache_dir_named("get_or_generate");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");

        let key = PtxCacheKey {
            kernel_name: "cached_kernel".to_string(),
            params_hash: 42,
            sm_version: SmVersion::Sm80,
        };

        let mut call_count = 0u32;

        // First call should generate
        let ptx1 = cache
            .get_or_generate(&key, || {
                call_count += 1;
                Ok("// generated ptx v1".to_string())
            })
            .expect("should generate");
        assert_eq!(ptx1, "// generated ptx v1");
        assert_eq!(call_count, 1);

        // Second call should hit cache
        let ptx2 = cache
            .get_or_generate(&key, || {
                call_count += 1;
                Ok("// should not be called".to_string())
            })
            .expect("should cache hit");
        assert_eq!(ptx2, "// generated ptx v1");
        assert_eq!(call_count, 1);

        cleanup(&dir);
    }

    #[test]
    fn get_nonexistent_returns_none() {
        let dir = test_cache_dir_named("get_nonexistent");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");
        let key = PtxCacheKey {
            kernel_name: "nonexistent".to_string(),
            params_hash: 0,
            sm_version: SmVersion::Sm80,
        };
        assert!(cache.get(&key).is_none());

        cleanup(&dir);
    }

    #[test]
    fn sanitize_filename_fn() {
        assert_eq!(sanitize_filename("hello_world"), "hello_world");
        assert_eq!(sanitize_filename("foo.bar/baz"), "foo_bar_baz");
        assert_eq!(sanitize_filename("a b c"), "a_b_c");
    }

    // -------------------------------------------------------------------------
    // P7: PTX disk cache round-trip tests
    // -------------------------------------------------------------------------

    /// Store a PTX string, retrieve it, and verify it is byte-for-byte identical.
    #[test]
    fn test_cache_round_trip() {
        let dir = test_cache_dir_named("round_trip");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");
        let key = PtxCacheKey {
            kernel_name: "round_trip_kernel".to_string(),
            params_hash: 0xABCD_1234,
            sm_version: SmVersion::Sm80,
        };
        let original = "// round-trip PTX content\n.version 8.0\n.target sm_80\n";

        cache.put(&key, original).expect("put should succeed");
        let retrieved = cache.get(&key).expect("get should return cached value");
        assert_eq!(
            original, retrieved,
            "retrieved PTX must be identical to stored"
        );

        cleanup(&dir);
    }

    /// The same key always retrieves the same content.
    #[test]
    fn test_cache_same_key_same_content() {
        let dir = test_cache_dir_named("same_key");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");
        let key = PtxCacheKey {
            kernel_name: "stable_kernel".to_string(),
            params_hash: 0x1111_2222,
            sm_version: SmVersion::Sm90,
        };
        let ptx = "// stable content";

        cache.put(&key, ptx).expect("first put should succeed");
        let first = cache.get(&key).expect("first get should succeed");
        let second = cache.get(&key).expect("second get should succeed");
        assert_eq!(
            first, second,
            "same key must return identical content on repeated lookups"
        );

        cleanup(&dir);
    }

    /// Different keys store and retrieve independent content.
    #[test]
    fn test_cache_different_keys() {
        let dir = test_cache_dir_named("diff_keys");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");
        let key_a = PtxCacheKey {
            kernel_name: "kernel_a".to_string(),
            params_hash: 0x0000_0001,
            sm_version: SmVersion::Sm80,
        };
        let key_b = PtxCacheKey {
            kernel_name: "kernel_b".to_string(),
            params_hash: 0x0000_0002,
            sm_version: SmVersion::Sm80,
        };

        cache
            .put(&key_a, "// PTX for kernel A")
            .expect("put A should succeed");
        cache
            .put(&key_b, "// PTX for kernel B")
            .expect("put B should succeed");

        let content_a = cache.get(&key_a).expect("get A should succeed");
        let content_b = cache.get(&key_b).expect("get B should succeed");

        assert_eq!(content_a, "// PTX for kernel A");
        assert_eq!(content_b, "// PTX for kernel B");
        assert_ne!(
            content_a, content_b,
            "different keys must retrieve different content"
        );

        cleanup(&dir);
    }

    /// A cache hit must avoid calling the generation closure a second time.
    #[test]
    fn test_cache_hit_avoids_regeneration() {
        let dir = test_cache_dir_named("hit_avoids_regen");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");
        let key = PtxCacheKey {
            kernel_name: "hit_kernel".to_string(),
            params_hash: 0xCAFE_BABE,
            sm_version: SmVersion::Sm80,
        };

        let mut call_count: u32 = 0;

        // First call — cache miss, generation runs
        let ptx_first = cache
            .get_or_generate(&key, || {
                call_count += 1;
                Ok("// generated".to_string())
            })
            .expect("first generation should succeed");
        assert_eq!(
            call_count, 1,
            "generation closure must be called on cache miss"
        );

        // Second call — cache hit, generation must NOT run
        let ptx_second = cache
            .get_or_generate(&key, || {
                call_count += 1;
                Ok("// should not be called".to_string())
            })
            .expect("second call should hit cache");
        assert_eq!(
            call_count, 1,
            "generation closure must not be called on cache hit"
        );
        assert_eq!(
            ptx_first, ptx_second,
            "cache hit must return original content"
        );

        cleanup(&dir);
    }

    /// A cache miss for an unknown key always triggers the generation closure.
    #[test]
    fn test_cache_miss_for_new_key() {
        let dir = test_cache_dir_named("miss_new_key");
        cleanup(&dir);

        let cache = PtxCache::with_dir(dir.clone()).expect("cache creation should succeed");

        let mut call_count: u32 = 0;

        // Each distinct key must produce a cache miss
        for i in 0u64..3 {
            let key = PtxCacheKey {
                kernel_name: format!("miss_kernel_{i}"),
                params_hash: i,
                sm_version: SmVersion::Sm80,
            };
            cache
                .get_or_generate(&key, || {
                    call_count += 1;
                    Ok(format!("// ptx for key {i}"))
                })
                .expect("generation should succeed");
        }

        assert_eq!(
            call_count, 3,
            "each new key must trigger one generation call"
        );

        cleanup(&dir);
    }
}