shape-jit 0.3.2

Tiered JIT compiler (Cranelift) for the Shape virtual machine
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
//! Content-addressed JIT code cache with dependency-based invalidation.
//!
//! Caches compiled native function pointers keyed by `FunctionHash`.
//! When the same function blob appears in a subsequent compilation
//! (same content hash), we skip recompilation and reuse the existing
//! native code pointer.
//!
//! Supports dependency tracking: when a function is inlined into another,
//! the caller records the callee hash as a dependency. If the callee
//! changes, all dependents can be invalidated via `invalidate_by_dependency()`.

use shape_vm::bytecode::FunctionHash;
use std::collections::HashMap;

use crate::optimizer::Tier2CacheKey;

/// Extended cache entry with dependency tracking.
#[derive(Debug, Clone)]
pub struct CacheEntry {
    /// Native code pointer.
    pub code_ptr: *const u8,
    /// Content hash of the function blob.
    pub function_hash: FunctionHash,
    /// Schema version at compilation time (for shape guard invalidation).
    pub schema_version: u32,
    /// Feedback epoch at compilation time (for speculation invalidation).
    pub feedback_epoch: u32,
    /// Hashes of functions this compiled code depends on (e.g., inlined callees).
    pub dependencies: Vec<FunctionHash>,
    /// Tier 2 cache key, present when this entry was produced by the
    /// optimizing compiler with cross-function inlining.
    pub tier2_key: Option<Tier2CacheKey>,
}

// SAFETY: CacheEntry contains a raw pointer produced by Cranelift.
// The same safety argument as JitCodeCache applies (see below).
unsafe impl Send for CacheEntry {}
unsafe impl Sync for CacheEntry {}

/// Cache of JIT-compiled function pointers, keyed by content hash.
///
/// Same blob hash = skip recompilation, reuse function pointer.
///
/// Tracks dependency edges so that when an inlined callee changes,
/// all callers that embedded it can be invalidated.
///
/// # Safety
///
/// The raw `*const u8` pointers stored here point into Cranelift
/// `JITModule` memory regions. Callers must ensure that the
/// `JITModule` that produced a pointer outlives any use of that
/// pointer through this cache.
pub struct JitCodeCache {
    entries: HashMap<FunctionHash, CacheEntry>,
    /// Reverse index: dependency_hash -> set of dependent function hashes.
    /// Used by `invalidate_by_dependency()` to find affected entries.
    dependents: HashMap<FunctionHash, Vec<FunctionHash>>,
}

// SAFETY: The function pointers are produced by Cranelift and are
// valid for the lifetime of the owning JITModule. The cache itself
// does not execute code, it only stores and returns pointers.
unsafe impl Send for JitCodeCache {}
unsafe impl Sync for JitCodeCache {}

impl JitCodeCache {
    /// Create an empty cache.
    pub fn new() -> Self {
        Self {
            entries: HashMap::new(),
            dependents: HashMap::new(),
        }
    }

    /// Create a cache pre-sized for `capacity` entries.
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            entries: HashMap::with_capacity(capacity),
            dependents: HashMap::new(),
        }
    }

    /// Look up a cached native code pointer by content hash.
    pub fn get(&self, hash: &FunctionHash) -> Option<*const u8> {
        self.entries.get(hash).map(|e| e.code_ptr)
    }

    /// Insert a compiled function pointer for the given content hash.
    ///
    /// Creates a minimal `CacheEntry` with no dependencies and zero
    /// version/epoch. If an entry with the same hash already exists
    /// it is overwritten.
    pub fn insert(&mut self, hash: FunctionHash, ptr: *const u8) {
        // Remove old dependency edges if overwriting.
        self.remove_dependency_edges(&hash);
        self.entries.insert(
            hash,
            CacheEntry {
                code_ptr: ptr,
                function_hash: hash,
                schema_version: 0,
                feedback_epoch: 0,
                dependencies: Vec::new(),
                tier2_key: None,
            },
        );
    }

    /// Insert a cache entry with full dependency information.
    ///
    /// Builds reverse-index edges so that `invalidate_by_dependency()`
    /// can find this entry when any of its dependencies change.
    pub fn insert_entry(&mut self, entry: CacheEntry) {
        let hash = entry.function_hash;
        // Remove stale dependency edges if overwriting.
        self.remove_dependency_edges(&hash);
        // Build reverse edges for the new entry.
        for dep in &entry.dependencies {
            self.dependents.entry(*dep).or_default().push(hash);
        }
        self.entries.insert(hash, entry);
    }

    /// Invalidate all entries that depend on the given function hash.
    ///
    /// Performs a transitive walk: if A depends on B and B depends on C,
    /// invalidating C will remove both B and A.
    ///
    /// Returns the list of invalidated function hashes.
    pub fn invalidate_by_dependency(&mut self, changed_hash: &FunctionHash) -> Vec<FunctionHash> {
        let mut invalidated = Vec::new();
        let mut worklist = vec![*changed_hash];

        while let Some(current) = worklist.pop() {
            if let Some(deps) = self.dependents.remove(&current) {
                for dep_hash in deps {
                    if self.entries.remove(&dep_hash).is_some() {
                        invalidated.push(dep_hash);
                        // Cascade: anything that depended on the now-removed
                        // entry must also be invalidated.
                        worklist.push(dep_hash);
                    }
                }
            }
        }

        // Clean up reverse edges for invalidated entries.
        for inv in &invalidated {
            self.remove_dependency_edges(inv);
        }

        invalidated
    }

    /// Get a cache entry with full metadata.
    pub fn get_entry(&self, hash: &FunctionHash) -> Option<&CacheEntry> {
        self.entries.get(hash)
    }

    /// Check whether a function with the given hash has been compiled.
    pub fn contains(&self, hash: &FunctionHash) -> bool {
        self.entries.contains_key(hash)
    }

    /// Number of cached entries.
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns `true` if the cache is empty.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Remove all entries from the cache.
    ///
    /// This does **not** free the underlying native code memory (that
    /// is owned by the Cranelift `JITModule`).
    pub fn clear(&mut self) {
        self.entries.clear();
        self.dependents.clear();
    }

    /// Remove reverse-index edges for a given function hash.
    fn remove_dependency_edges(&mut self, hash: &FunctionHash) {
        if let Some(entry) = self.entries.get(hash) {
            let deps: Vec<FunctionHash> = entry.dependencies.clone();
            for dep in &deps {
                if let Some(rev) = self.dependents.get_mut(dep) {
                    rev.retain(|h| h != hash);
                    if rev.is_empty() {
                        self.dependents.remove(dep);
                    }
                }
            }
        }
    }
}

impl Default for JitCodeCache {
    fn default() -> Self {
        Self::new()
    }
}

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

    #[test]
    fn empty_cache() {
        let cache = JitCodeCache::new();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
        assert!(cache.get(&FunctionHash::ZERO).is_none());
    }

    #[test]
    fn insert_and_get() {
        let mut cache = JitCodeCache::new();
        let hash = FunctionHash([0xAB; 32]);
        let fake_ptr = 0xDEAD_BEEF_usize as *const u8;

        cache.insert(hash, fake_ptr);
        assert_eq!(cache.len(), 1);
        assert!(!cache.is_empty());
        assert!(cache.contains(&hash));
        assert_eq!(cache.get(&hash), Some(fake_ptr));
    }

    #[test]
    fn missing_hash_returns_none() {
        let mut cache = JitCodeCache::new();
        let hash_a = FunctionHash([1u8; 32]);
        let hash_b = FunctionHash([2u8; 32]);
        cache.insert(hash_a, 0x1 as *const u8);

        assert!(cache.get(&hash_b).is_none());
        assert!(!cache.contains(&hash_b));
    }

    #[test]
    fn overwrite_entry() {
        let mut cache = JitCodeCache::new();
        let hash = FunctionHash([0xCC; 32]);
        let ptr1 = 0x1000_usize as *const u8;
        let ptr2 = 0x2000_usize as *const u8;

        cache.insert(hash, ptr1);
        assert_eq!(cache.get(&hash), Some(ptr1));

        cache.insert(hash, ptr2);
        assert_eq!(cache.get(&hash), Some(ptr2));
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn clear_removes_all() {
        let mut cache = JitCodeCache::new();
        cache.insert(FunctionHash([1; 32]), 0x1 as *const u8);
        cache.insert(FunctionHash([2; 32]), 0x2 as *const u8);
        assert_eq!(cache.len(), 2);

        cache.clear();
        assert!(cache.is_empty());
        assert_eq!(cache.len(), 0);
    }

    #[test]
    fn with_capacity() {
        let cache = JitCodeCache::with_capacity(64);
        assert!(cache.is_empty());
    }

    // --- Dependency-tracking tests ---

    fn make_entry(hash: FunctionHash, ptr: usize, deps: Vec<FunctionHash>) -> CacheEntry {
        CacheEntry {
            code_ptr: ptr as *const u8,
            function_hash: hash,
            schema_version: 1,
            feedback_epoch: 1,
            dependencies: deps,
            tier2_key: None,
        }
    }

    #[test]
    fn test_insert_entry_with_dependencies() {
        let mut cache = JitCodeCache::new();
        let callee = FunctionHash([0x01; 32]);
        let caller = FunctionHash([0x02; 32]);

        // Insert the callee (no deps).
        cache.insert_entry(make_entry(callee, 0x1000, vec![]));

        // Insert the caller which depends on the callee.
        cache.insert_entry(make_entry(caller, 0x2000, vec![callee]));

        assert_eq!(cache.len(), 2);
        assert!(cache.contains(&callee));
        assert!(cache.contains(&caller));

        // Verify metadata is accessible.
        let entry = cache.get_entry(&caller).unwrap();
        assert_eq!(entry.schema_version, 1);
        assert_eq!(entry.feedback_epoch, 1);
        assert_eq!(entry.dependencies, vec![callee]);
    }

    #[test]
    fn test_invalidate_by_dependency() {
        let mut cache = JitCodeCache::new();
        let callee = FunctionHash([0x01; 32]);
        let caller_a = FunctionHash([0x02; 32]);
        let caller_b = FunctionHash([0x03; 32]);
        let unrelated = FunctionHash([0x04; 32]);

        cache.insert_entry(make_entry(callee, 0x1000, vec![]));
        cache.insert_entry(make_entry(caller_a, 0x2000, vec![callee]));
        cache.insert_entry(make_entry(caller_b, 0x3000, vec![callee]));
        cache.insert_entry(make_entry(unrelated, 0x4000, vec![]));
        assert_eq!(cache.len(), 4);

        // Invalidate everything that depends on callee.
        let mut invalidated = cache.invalidate_by_dependency(&callee);
        invalidated.sort_by_key(|h| h.0);

        assert_eq!(invalidated.len(), 2);
        assert!(invalidated.contains(&caller_a));
        assert!(invalidated.contains(&caller_b));

        // callee itself is NOT removed (only its dependents are).
        assert!(cache.contains(&callee));
        assert!(cache.contains(&unrelated));
        assert!(!cache.contains(&caller_a));
        assert!(!cache.contains(&caller_b));
        assert_eq!(cache.len(), 2);
    }

    #[test]
    fn test_invalidate_cascading() {
        // A depends on B, B depends on C. Invalidate C -> both B and A removed.
        let mut cache = JitCodeCache::new();
        let c = FunctionHash([0x01; 32]);
        let b = FunctionHash([0x02; 32]);
        let a = FunctionHash([0x03; 32]);

        cache.insert_entry(make_entry(c, 0x1000, vec![]));
        cache.insert_entry(make_entry(b, 0x2000, vec![c]));
        cache.insert_entry(make_entry(a, 0x3000, vec![b]));
        assert_eq!(cache.len(), 3);

        let mut invalidated = cache.invalidate_by_dependency(&c);
        invalidated.sort_by_key(|h| h.0);

        // Both B and A should be invalidated (B directly, A transitively).
        assert_eq!(invalidated.len(), 2);
        assert!(invalidated.contains(&b));
        assert!(invalidated.contains(&a));

        // Only C remains.
        assert!(cache.contains(&c));
        assert!(!cache.contains(&b));
        assert!(!cache.contains(&a));
        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_get_entry_returns_metadata() {
        let mut cache = JitCodeCache::new();
        let hash = FunctionHash([0xAA; 32]);
        let dep = FunctionHash([0xBB; 32]);

        cache.insert_entry(CacheEntry {
            code_ptr: 0x5000 as *const u8,
            function_hash: hash,
            schema_version: 42,
            feedback_epoch: 7,
            dependencies: vec![dep],
            tier2_key: None,
        });

        let entry = cache.get_entry(&hash).unwrap();
        assert_eq!(entry.code_ptr, 0x5000 as *const u8);
        assert_eq!(entry.function_hash, hash);
        assert_eq!(entry.schema_version, 42);
        assert_eq!(entry.feedback_epoch, 7);
        assert_eq!(entry.dependencies, vec![dep]);

        // get() still returns just the pointer.
        assert_eq!(cache.get(&hash), Some(0x5000 as *const u8));

        // Missing entry returns None.
        assert!(cache.get_entry(&FunctionHash([0xFF; 32])).is_none());
    }

    #[test]
    fn test_tier2_cache_key_stored_in_entry() {
        let mut cache = JitCodeCache::new();
        let root = FunctionHash([0x10; 32]);
        let inlined_callee = FunctionHash([0x20; 32]);

        let key = Tier2CacheKey::with_versions(
            root.0,
            vec![inlined_callee.0],
            1, // compiler_version
            5, // schema_version
            3, // feedback_epoch
        );

        cache.insert_entry(CacheEntry {
            code_ptr: 0x8000 as *const u8,
            function_hash: root,
            schema_version: 5,
            feedback_epoch: 3,
            dependencies: vec![inlined_callee],
            tier2_key: Some(key.clone()),
        });

        let entry = cache.get_entry(&root).unwrap();
        let stored_key = entry.tier2_key.as_ref().unwrap();
        assert_eq!(stored_key.root_hash, root.0);
        assert_eq!(stored_key.inlined_hashes, vec![inlined_callee.0]);
        assert_eq!(stored_key.schema_version, 5);
        assert_eq!(stored_key.feedback_epoch, 3);
        assert_eq!(stored_key.compiler_version, 1);

        // Verify combined_hash includes version metadata.
        let key_no_versions = Tier2CacheKey::new(root.0, vec![inlined_callee.0], 1);
        assert_ne!(stored_key.combined_hash(), key_no_versions.combined_hash());
    }

    #[test]
    fn test_invalidate_with_tier2_entries() {
        // Tier 2 entry with inlined callee: invalidating the callee
        // removes the tier 2 entry.
        let mut cache = JitCodeCache::new();
        let callee = FunctionHash([0x01; 32]);
        let optimized = FunctionHash([0x02; 32]);

        let key = Tier2CacheKey::with_versions(optimized.0, vec![callee.0], 1, 0, 0);

        cache.insert_entry(CacheEntry {
            code_ptr: 0x1000 as *const u8,
            function_hash: callee,
            schema_version: 0,
            feedback_epoch: 0,
            dependencies: vec![],
            tier2_key: None,
        });
        cache.insert_entry(CacheEntry {
            code_ptr: 0x2000 as *const u8,
            function_hash: optimized,
            schema_version: 0,
            feedback_epoch: 0,
            dependencies: vec![callee],
            tier2_key: Some(key),
        });

        let invalidated = cache.invalidate_by_dependency(&callee);
        assert_eq!(invalidated.len(), 1);
        assert_eq!(invalidated[0], optimized);
        assert!(!cache.contains(&optimized));
        assert!(cache.contains(&callee));
    }
}