warp-types 0.3.2

Type-safe GPU warp programming via linear typestate: compile-time prevention of shuffle-from-inactive-lane bugs
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
559
560
561
562
563
564
565
566
567
568
//! Borrowing Patterns for GPU Shared Memory
//!
//! **STATUS: Research** — Exploratory prototype, not promoted to main API.
//!
//! Research question: "How to handle temporary shared access (borrowing)?"
//!
//! # Background
//!
//! GPU shared memory is a scarce resource. We want to:
//! 1. Prevent race conditions (multiple writers)
//! 2. Allow temporary access (borrowing)
//! 3. Track ownership precisely (linear types)
//!
//! Rust's borrow checker works for CPU memory. Can we adapt it to GPU?
//!
//! # Key Insight
//!
//! GPU borrowing differs from CPU borrowing:
//! - Multiple LANES may access simultaneously (within a warp)
//! - Different WARPS need explicit synchronization
//! - SIMT means all lanes do the same operation (or are masked)
//!
//! # Patterns Explored
//!
//! 1. **Lane-Parallel Borrows**: All lanes borrow in lockstep
//! 2. **Split Borrows**: Different lanes borrow different indices
//! 3. **Scoped Borrows**: RAII-style with sync at scope end
//! 4. **Lease/Return**: Explicit checkout and return

use std::marker::PhantomData;

// ============================================================================
// BASIC TYPES
// ============================================================================

pub trait ActiveSet: Copy + 'static {
    const MASK: u32;
}

#[derive(Copy, Clone)]
pub struct All;
impl ActiveSet for All {
    const MASK: u32 = 0xFFFFFFFF;
}

#[derive(Copy, Clone)]
pub struct Warp<S: ActiveSet> {
    _marker: PhantomData<S>,
}

impl<S: ActiveSet> Warp<S> {
    pub fn new() -> Self {
        Warp {
            _marker: PhantomData,
        }
    }
}

// ============================================================================
// SHARED MEMORY REGION
// ============================================================================

/// A region of shared memory with explicit owner
#[derive(Debug)]
pub struct SharedMem<T: Copy, const SIZE: usize> {
    data: [T; SIZE],
}

impl<T: Copy + Default, const SIZE: usize> SharedMem<T, SIZE> {
    pub fn new() -> Self {
        SharedMem {
            data: [T::default(); SIZE],
        }
    }

    pub fn from_slice(values: &[T]) -> Self
    where
        [T; SIZE]: Default,
    {
        let mut data = <[T; SIZE]>::default();
        for (i, v) in values.iter().take(SIZE).enumerate() {
            data[i] = *v;
        }
        SharedMem { data }
    }
}

// ============================================================================
// PATTERN 1: LANE-PARALLEL BORROWS
// ============================================================================

/// Lane-parallel borrowing: All lanes borrow the same index
///
/// In SIMT, all lanes execute the same instruction. When borrowing,
/// all lanes borrow the SAME logical slot but from their own perspective.
pub mod lane_parallel {
    use super::*;

    /// Immutable borrow - all lanes read the same value
    pub struct SharedRef<'a, T: Copy, const SIZE: usize> {
        mem: &'a SharedMem<T, SIZE>,
    }

    impl<'a, T: Copy, const SIZE: usize> SharedRef<'a, T, SIZE> {
        pub fn read(&self, index: usize) -> T {
            assert!(index < SIZE, "index out of bounds");
            self.mem.data[index]
        }
    }

    /// Mutable borrow - exclusive write access
    pub struct SharedMut<'a, T: Copy, const SIZE: usize> {
        mem: &'a mut SharedMem<T, SIZE>,
    }

    impl<'a, T: Copy, const SIZE: usize> SharedMut<'a, T, SIZE> {
        pub fn read(&self, index: usize) -> T {
            assert!(index < SIZE, "index out of bounds");
            self.mem.data[index]
        }

        pub fn write(&mut self, index: usize, value: T) {
            assert!(index < SIZE, "index out of bounds");
            self.mem.data[index] = value;
        }
    }

    /// Borrow shared memory for reading
    pub fn borrow_shared<'a, T: Copy, const SIZE: usize>(
        _warp: &Warp<All>,
        mem: &'a SharedMem<T, SIZE>,
    ) -> SharedRef<'a, T, SIZE> {
        SharedRef { mem }
    }

    /// Borrow shared memory for writing (exclusive)
    pub fn borrow_mut<'a, T: Copy, const SIZE: usize>(
        _warp: &Warp<All>,
        mem: &'a mut SharedMem<T, SIZE>,
    ) -> SharedMut<'a, T, SIZE> {
        SharedMut { mem }
    }

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

        #[test]
        fn test_shared_borrow() {
            let warp: Warp<All> = Warp::new();
            let mut mem: SharedMem<i32, 32> = SharedMem::new();

            // Write via mutable borrow
            {
                let mut borrow = borrow_mut(&warp, &mut mem);
                borrow.write(0, 42);
            }

            // Read via shared borrow
            {
                let borrow = borrow_shared(&warp, &mem);
                assert_eq!(borrow.read(0), 42);
            }
        }

        #[test]
        fn test_multiple_shared_borrows() {
            let warp: Warp<All> = Warp::new();
            let mem: SharedMem<i32, 32> = SharedMem::new();

            // Multiple shared borrows allowed
            let b1 = borrow_shared(&warp, &mem);
            let b2 = borrow_shared(&warp, &mem);

            assert_eq!(b1.read(0), b2.read(0));
        }
    }
}

// ============================================================================
// PATTERN 2: SPLIT BORROWS (Disjoint Index Sets)
// ============================================================================

/// Split borrows: Different lanes access different indices
///
/// Key insight: If lanes access DISJOINT indices, no race is possible.
/// We can grant "parallel exclusive" access where each lane owns its slot.
pub mod split {
    use super::*;

    /// Per-lane exclusive access to a single index
    pub struct LaneSlot<'a, T: Copy, const SIZE: usize> {
        mem: &'a mut SharedMem<T, SIZE>,
        owned_index: usize,
    }

    impl<'a, T: Copy, const SIZE: usize> LaneSlot<'a, T, SIZE> {
        pub fn read(&self) -> T {
            self.mem.data[self.owned_index]
        }

        pub fn write(&mut self, value: T) {
            self.mem.data[self.owned_index] = value;
        }

        pub fn index(&self) -> usize {
            self.owned_index
        }
    }

    /// Grant each lane exclusive access to its own slot
    ///
    /// Lane i gets exclusive access to index i.
    /// This is safe because indices are disjoint.
    pub fn split_by_lane<'a, T: Copy, const SIZE: usize>(
        _warp: &Warp<All>,
        mem: &'a mut SharedMem<T, SIZE>,
        lane: usize,
    ) -> LaneSlot<'a, T, SIZE> {
        assert!(lane < SIZE, "lane must be within memory size");
        LaneSlot {
            mem,
            owned_index: lane,
        }
    }

    /// Per-lane view of disjoint regions (immutable version)
    pub struct DisjointView<'a, T: Copy, const SIZE: usize> {
        mem: &'a SharedMem<T, SIZE>,
        owned_indices: Vec<usize>,
    }

    impl<'a, T: Copy, const SIZE: usize> DisjointView<'a, T, SIZE> {
        pub fn read(&self, local_index: usize) -> Option<T> {
            self.owned_indices
                .get(local_index)
                .map(|&i| self.mem.data[i])
        }
    }

    /// Split memory into disjoint views based on predicate
    pub fn split_by_predicate<'a, T: Copy, const SIZE: usize, F>(
        _warp: &Warp<All>,
        mem: &'a SharedMem<T, SIZE>,
        pred: F,
    ) -> (DisjointView<'a, T, SIZE>, DisjointView<'a, T, SIZE>)
    where
        F: Fn(usize) -> bool,
    {
        let mut true_indices = Vec::new();
        let mut false_indices = Vec::new();

        for i in 0..SIZE {
            if pred(i) {
                true_indices.push(i);
            } else {
                false_indices.push(i);
            }
        }

        (
            DisjointView {
                mem,
                owned_indices: true_indices,
            },
            DisjointView {
                mem,
                owned_indices: false_indices,
            },
        )
    }

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

        #[test]
        fn test_lane_exclusive_access() {
            let warp: Warp<All> = Warp::new();
            let mut mem: SharedMem<i32, 32> = SharedMem::new();

            // Each lane writes to its own slot
            for lane in 0..32 {
                let mut slot = split_by_lane(&warp, &mut mem, lane);
                slot.write(lane as i32 * 10);
            }

            // Verify each lane's value
            for lane in 0..32 {
                let slot = split_by_lane(&warp, &mut mem, lane);
                assert_eq!(slot.read(), lane as i32 * 10);
            }
        }

        #[test]
        fn test_disjoint_views() {
            let warp: Warp<All> = Warp::new();
            let mem: SharedMem<i32, 8> = SharedMem::from_slice(&[0, 1, 2, 3, 4, 5, 6, 7]);

            let (evens, odds) = split_by_predicate(&warp, &mem, |i| i % 2 == 0);

            // Even indices: 0, 2, 4, 6
            assert_eq!(evens.read(0), Some(0));
            assert_eq!(evens.read(1), Some(2));
            assert_eq!(evens.read(2), Some(4));
            assert_eq!(evens.read(3), Some(6));

            // Odd indices: 1, 3, 5, 7
            assert_eq!(odds.read(0), Some(1));
            assert_eq!(odds.read(1), Some(3));
        }
    }
}

// ============================================================================
// PATTERN 3: SCOPED BORROWS (RAII-style)
// ============================================================================

/// Scoped borrows: Borrow with automatic sync at scope end
///
/// In GPU programming, sync barriers are critical but easy to forget.
/// This pattern ties sync to scope exit.
pub mod scoped {
    use super::*;

    /// A scoped mutable borrow that syncs on drop
    pub struct ScopedMut<'a, T: Copy, const SIZE: usize> {
        mem: &'a mut SharedMem<T, SIZE>,
        needs_sync: bool,
    }

    impl<'a, T: Copy, const SIZE: usize> ScopedMut<'a, T, SIZE> {
        pub fn read(&self, index: usize) -> T {
            self.mem.data[index]
        }

        pub fn write(&mut self, index: usize, value: T) {
            self.mem.data[index] = value;
            self.needs_sync = true;
        }
    }

    impl<'a, T: Copy, const SIZE: usize> Drop for ScopedMut<'a, T, SIZE> {
        fn drop(&mut self) {
            if self.needs_sync {
                // In real GPU code: __syncthreads() or __threadfence_block()
                // Here we just mark that sync happened
                // println!("Sync barrier at scope exit");
            }
        }
    }

    /// Create a scoped mutable borrow
    pub fn scoped_borrow<'a, T: Copy, const SIZE: usize>(
        _warp: &Warp<All>,
        mem: &'a mut SharedMem<T, SIZE>,
    ) -> ScopedMut<'a, T, SIZE> {
        ScopedMut {
            mem,
            needs_sync: false,
        }
    }

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

        #[test]
        fn test_scoped_sync() {
            let warp: Warp<All> = Warp::new();
            let mut mem: SharedMem<i32, 32> = SharedMem::new();

            // Sync happens at end of scope
            {
                let mut borrow = scoped_borrow(&warp, &mut mem);
                borrow.write(0, 100);
                // Drop at end of scope triggers sync
            }

            assert_eq!(mem.data[0], 100);
        }
    }
}

// ============================================================================
// PATTERN 4: LEASE/RETURN
// ============================================================================

/// Lease/Return: Explicit checkout and return of memory regions
///
/// This is a more explicit form of borrowing where the return is
/// a separate operation (not tied to scope).
pub mod lease {

    /// A leased memory region (must be returned)
    pub struct Lease<T: Copy, const SIZE: usize> {
        data: [T; SIZE],
        source_id: usize, // Identifies which pool this came from
    }

    impl<T: Copy, const SIZE: usize> Lease<T, SIZE> {
        pub fn read(&self, index: usize) -> T {
            self.data[index]
        }

        pub fn write(&mut self, index: usize, value: T) {
            self.data[index] = value;
        }

        pub fn source_id(&self) -> usize {
            self.source_id
        }
    }

    /// A pool that manages leases
    pub struct LeasePool<T: Copy + Default, const SIZE: usize, const SLOTS: usize> {
        slots: [[T; SIZE]; SLOTS],
        leased: [bool; SLOTS],
    }

    impl<T: Copy + Default, const SIZE: usize, const SLOTS: usize> LeasePool<T, SIZE, SLOTS> {
        pub fn new() -> Self {
            LeasePool {
                slots: [[T::default(); SIZE]; SLOTS],
                leased: [false; SLOTS],
            }
        }

        /// Checkout a slot (returns None if all leased)
        pub fn checkout(&mut self) -> Option<Lease<T, SIZE>> {
            for (i, is_leased) in self.leased.iter_mut().enumerate() {
                if !*is_leased {
                    *is_leased = true;
                    return Some(Lease {
                        data: self.slots[i],
                        source_id: i,
                    });
                }
            }
            None
        }

        /// Return a lease
        pub fn return_lease(&mut self, lease: Lease<T, SIZE>) {
            let id = lease.source_id;
            assert!(self.leased[id], "Returning non-leased slot");
            self.slots[id] = lease.data;
            self.leased[id] = false;
        }

        /// Count available slots
        pub fn available(&self) -> usize {
            self.leased.iter().filter(|&&l| !l).count()
        }
    }

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

        #[test]
        fn test_lease_pool() {
            let mut pool: LeasePool<i32, 32, 4> = LeasePool::new();

            assert_eq!(pool.available(), 4);

            // Checkout a lease
            let mut lease = pool.checkout().unwrap();
            assert_eq!(pool.available(), 3);

            // Modify the lease
            lease.write(0, 42);

            // Return the lease
            pool.return_lease(lease);
            assert_eq!(pool.available(), 4);
        }

        #[test]
        fn test_lease_exhaustion() {
            let mut pool: LeasePool<i32, 8, 2> = LeasePool::new();

            let _l1 = pool.checkout().unwrap();
            let _l2 = pool.checkout().unwrap();

            // Pool exhausted
            assert!(pool.checkout().is_none());
        }
    }
}

// ============================================================================
// KEY FINDINGS
// ============================================================================

/// Summary: How to handle temporary shared access (borrowing)?
///
/// ## Patterns
///
/// | Pattern | Use Case | GPU Analog |
/// |---------|----------|------------|
/// | Lane-Parallel | All lanes access same index | Uniform load/store |
/// | Split | Each lane accesses own index | Coalesced access |
/// | Scoped | Sync needed at end | __syncthreads() |
/// | Lease/Return | Explicit lifetime management | Memory pools |
///
/// ## Key Insights
///
/// 1. **SIMT changes borrowing semantics**: In SIMT, all lanes do the same
///    operation. "Mutable borrow" means all lanes write - which is safe
///    if they write to DIFFERENT indices or the SAME value.
///
/// 2. **Disjoint access is key**: If we can prove indices are disjoint,
///    parallel mutable access is safe. This is like Rust's split_at_mut.
///
/// 3. **Sync ties to scope**: GPU sync barriers are critical. Tying them
///    to scope exit (RAII) prevents forgetting them.
///
/// 4. **Pools for dynamic allocation**: GPU shared memory is fixed size.
///    Lease pools provide dynamic-ish allocation with explicit return.
///
/// ## Comparison with CPU Rust
///
/// | CPU Rust | GPU Warp |
/// |----------|----------|
/// | Single owner | All lanes "own" together |
/// | &T / &mut T | SharedRef / SharedMut (per-warp) |
/// | split_at_mut | split_by_lane (per-index) |
/// | Drop | Drop + __syncthreads() |
///
/// The key difference: GPU "borrow" is always 32-way parallel. The type
/// system must track WHICH INDICES each lane can access, not just
/// read/write permissions.
pub const _FINDINGS: () = ();

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

    #[test]
    fn test_full_borrow_cycle() {
        let warp: Warp<All> = Warp::new();
        let mut mem: SharedMem<i32, 32> = SharedMem::new();

        // Phase 1: Lane-parallel write (all lanes write same index)
        {
            let mut borrow = lane_parallel::borrow_mut(&warp, &mut mem);
            borrow.write(0, 100); // All lanes write index 0
        }

        // Phase 2: Split access (each lane writes own index)
        for lane in 0..32 {
            let mut slot = split::split_by_lane(&warp, &mut mem, lane);
            slot.write(lane as i32);
        }

        // Verify
        for lane in 0..32 {
            let slot = split::split_by_lane(&warp, &mut mem, lane);
            if lane == 0 {
                // Index 0 was overwritten in phase 2
                assert_eq!(slot.read(), 0);
            } else {
                assert_eq!(slot.read(), lane as i32);
            }
        }
    }
}