-
Notifications
You must be signed in to change notification settings - Fork 242
Expand file tree
/
Copy pathwarp.rs
More file actions
840 lines (793 loc) · 25.9 KB
/
Copy pathwarp.rs
File metadata and controls
840 lines (793 loc) · 25.9 KB
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
//! Functions that work over warps of threads.
//!
//! Warps in CUDA are groups of 32 threads that are dispatched together inside of
//! thread blocks and execute in SIMT fashion.
use crate::gpu_only;
#[cfg(target_os = "cuda")]
use core::arch::asm;
use half::{bf16, f16};
/// Synchronizes all of the threads inside of this warp according to `mask`.
///
/// # Safety
///
/// The behavior of this function is undefined if:
/// - Any thread inside `mask` has exited.
/// - The executing thread is not inside of `mask`.
///
/// Moreover, on compute_62 and below, all of the threads inside `mask` must call
/// `sync` with the __exact same__ mask. Otherwise it is undefined behavior.
#[gpu_only]
#[inline(always)]
pub unsafe fn sync_warp(mask: u32) {
unsafe extern "C" {
#[link_name = "llvm.nvvm.bar.warp.sync"]
fn sync(mask: u32);
}
unsafe {
sync(mask);
}
}
/// Returns the thread's lane within its warp. This value ranges from `0` to `WARP_SIZE - 1` (`WARP_SIZE` is 32 on all
/// architectures currently).
#[gpu_only]
#[inline(always)]
pub fn lane_id() -> u32 {
let mut out;
unsafe {
asm!(
"mov.u32 {}, %laneid;",
out(reg32) out
);
}
out
}
/// Queries a mask of the active threads in the warp.
#[gpu_only]
#[inline(always)]
pub fn activemask() -> u32 {
let mut out;
unsafe {
asm!(
"activemask.b32 {};",
out(reg32) out
);
}
out
}
/// Synchronizes threads in a warp then performs a reduction operation.
///
/// `mask` is a bitmask indicating which threads in the warp should participate in the reduction.
/// The threads in `mask` will synchronize then perform the reduction op indicated by `op` and return the reduced value.
///
/// This intrinsic is only available on Compute Capabilities 8.x (Ampere) and above.
///
/// # Safety
///
/// Behavior is undefined if any thread specified inside of `mask` has exited. Additionally, every
/// thread must execute the function with the same mask.
#[gpu_only]
#[inline(always)]
pub unsafe fn warp_reduce<T: WarpReduceValue>(mask: u32, value: T, op: WarpReductionOp) -> T {
unsafe { T::reduce(mask, value, op) }
}
/// The type of operation to apply in a warp reduction.
#[repr(u32)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WarpReductionOp {
Add,
Min,
Max,
And,
Or,
Xor,
}
pub trait WarpReduceValue: Sized {
#[allow(clippy::missing_safety_doc)]
unsafe fn reduce(mask: u32, value: Self, op: WarpReductionOp) -> Self;
}
macro_rules! impl_reduce {
($($type:ty),* $(,)?) => {
$(
paste::paste! {
impl WarpReduceValue for $type {
unsafe fn reduce(mask: u32, value: Self, op: WarpReductionOp) -> Self {
unsafe { [<warp_reduce_ $type>](mask, value, op) }
}
}
}
)*
}
}
impl_reduce! {
i32,
u32,
}
#[gpu_only]
unsafe fn warp_reduce_32(mask: u32, value: u32, op: WarpReductionOp) -> u32 {
let out;
unsafe {
match op {
WarpReductionOp::And => {
asm!(
"redux.sync.and.b32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
WarpReductionOp::Or => {
asm!(
"redux.sync.or.b32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
WarpReductionOp::Xor => {
asm!(
"redux.sync.xor.b32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
_ => unreachable!(),
}
}
out
}
#[gpu_only]
unsafe fn warp_reduce_u32(mask: u32, value: u32, op: WarpReductionOp) -> u32 {
let out;
unsafe {
match op {
WarpReductionOp::Add => {
asm!(
"redux.sync.add.u32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
WarpReductionOp::Min => {
asm!(
"redux.sync.min.u32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
WarpReductionOp::Max => {
asm!(
"redux.sync.max.u32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
_ => out = warp_reduce_32(mask, value, op),
}
}
out
}
#[gpu_only]
unsafe fn warp_reduce_i32(mask: u32, value: i32, op: WarpReductionOp) -> i32 {
let out;
unsafe {
match op {
WarpReductionOp::Add => {
asm!(
"redux.sync.add.s32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
WarpReductionOp::Min => {
asm!(
"redux.sync.min.s32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
WarpReductionOp::Max => {
asm!(
"redux.sync.max.s32 {}, {}, {};",
out(reg32) out,
in(reg32) value,
in(reg32) mask
);
}
_ => out = warp_reduce_32(mask, value as u32, op) as i32,
}
}
out
}
/// Synchronizes threads in a warp and performs a broadcast-and-compare operation between them.
///
/// `mask` is a bitmask dictating what threads should participate in the operation. All the threads in
/// `mask` will synchronize then perform a warp match operation. The result is a bitmask of all the threads
/// which have the same `value`.
///
/// This intrinsic is only available on Compute Capabilities 7.x (Volta) and above.
///
/// # Safety
///
/// Behavior is undefined if any thread specified inside of `mask` has exited. Additionally, every
/// thread must execute the function with the same mask.
#[gpu_only]
#[inline(always)]
pub unsafe fn warp_match_any<T: WarpMatchValue>(mask: u32, value: T) -> u32 {
unsafe { T::match_any(mask, value) }
}
/// Synchronizes threads in a warp and performs a broadcast-and-compare operation between them.
///
/// `mask` is a bitmask dictating what threads should participate in the operation. All the threads in
/// `mask` will synchronize then perform a warp match operation. Returns `Some(mask)` if all threads in `mask` have
/// the same value for `value`, otherwise returns `None`.
///
/// This intrinsic is only available on Compute Capabilities 7.x (Volta) and above.
///
/// # Safety
///
/// Behavior is undefined if any thread specified inside of `mask` has exited. Additionally, every
/// thread must execute the function with the same mask.
#[gpu_only]
#[inline(always)]
pub unsafe fn warp_match_all<T: WarpMatchValue>(mask: u32, value: T) -> Option<u32> {
unsafe { T::match_all(mask, value) }
}
/// A value that can be used inside of a warp match.
pub trait WarpMatchValue: Sized {
#[allow(clippy::missing_safety_doc)]
unsafe fn match_any(mask: u32, value: Self) -> u32;
#[allow(clippy::missing_safety_doc)]
unsafe fn match_all(mask: u32, value: Self) -> Option<u32>;
}
macro_rules! impl_match {
($($type:ty, $width:literal),* $(,)?) => {
$(
paste::paste! {
impl WarpMatchValue for $type {
unsafe fn match_any(mask: u32, value: Self) -> u32 {
unsafe { [<match_any_ $width>](mask, value as [<u $width>]) }
}
unsafe fn match_all(mask: u32, value: Self) -> Option<u32> {
let (val, pred) = unsafe { [<match_all_ $width>](mask, value as [<u $width>]) };
pred.then(|| val)
}
}
}
)*
}
}
impl_match! {
i32, 32,
i64, 64,
u32, 32,
u64, 64,
f32, 32,
f64, 64,
}
#[gpu_only]
#[inline(always)]
unsafe fn match_any_32(mask: u32, value: u32) -> u32 {
unsafe extern "C" {
#[link_name = "llvm.nvvm.match.any.sync.i32"]
fn __nvvm_warp_match_any_32(mask: u32, value: u32) -> u32;
}
unsafe { __nvvm_warp_match_any_32(mask, value) }
}
#[gpu_only]
#[inline(always)]
unsafe fn match_any_64(mask: u32, value: u64) -> u32 {
unsafe extern "C" {
#[link_name = "llvm.nvvm.match.any.sync.i64"]
fn __nvvm_warp_match_any_64(mask: u32, value: u64) -> u32;
}
unsafe { __nvvm_warp_match_any_64(mask, value) }
}
#[gpu_only]
#[inline(always)]
unsafe fn match_all_32(mask: u32, value: u32) -> (u32, bool) {
unsafe extern "C" {
// see libintrinsics.ll — packs (value, predicate) into i64
fn __nvvm_warp_match_all_32(mask: u32, value: u32) -> u64;
}
unpack_warp_result(unsafe { __nvvm_warp_match_all_32(mask, value) })
}
#[gpu_only]
#[inline(always)]
unsafe fn match_all_64(mask: u32, value: u64) -> (u32, bool) {
unsafe extern "C" {
// see libintrinsics.ll — packs (value, predicate) into i64
fn __nvvm_warp_match_all_64(mask: u32, value: u64) -> u64;
}
unpack_warp_result(unsafe { __nvvm_warp_match_all_64(mask, value) })
}
/// Synchronizes a subset of threads in a warp then performs a reduce-and-broadcast
/// operation, returning `true` only if `predicate` evaluates to `true` for all threads
/// in the warp that are counted in `mask`. Mask is usually [`u32::MAX`].
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the vote has exited or the executing thread is not in
/// `mask`.
/// - For `compute_62` and below, all threads in `mask` must call this function in
/// convergence, and only threads belonging to the `mask` can be active when the
/// intrinsic is called.
/// - A thread tries to execute this function while not being present in `mask`.
#[gpu_only]
pub unsafe fn warp_vote_all(mask: u32, predicate: bool) -> bool {
let mut out: u32;
unsafe {
asm!(
"{{",
".reg .pred %p<3>;",
"setp.eq.u32 %p1, {}, 1;",
"vote.sync.all.pred %p2, %p1, {};",
"selp.u32 {}, 1, 0, %p2;",
"}}",
in(reg32) predicate as u32,
in(reg32) mask,
out(reg32) out
);
}
out != 0
}
/// Synchronizes a subset of threads in a warp then performs a reduce-and-broadcast
/// operation, returning `true` only if `predicate` evaluates to `true` for any of the
/// threads in the warp that are counted in `mask`. Mask is usually [`u32::MAX`].
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the vote has exited or the executing thread is not in
/// `mask`.
/// - For `compute_62` and below, all threads in `mask` must call this function in
/// convergence, and only threads belonging to the `mask` can be active when the
/// intrinsic is called.
/// - A thread tries to execute this function while not being present in `mask`.
#[gpu_only]
pub unsafe fn warp_vote_any(mask: u32, predicate: bool) -> bool {
let mut out: u32;
unsafe {
asm!(
"{{",
".reg .pred %p<3>;",
"setp.eq.u32 %p1, {}, 1;",
"vote.sync.any.pred %p2, %p1, {};",
"selp.u32 {}, 1, 0, %p2;",
"}}",
in(reg32) predicate as u32,
in(reg32) mask,
out(reg32) out
);
}
out != 0
}
/// Synchronizes a subset of threads in a warp then performs a reduce-and-broadcast
/// operation, returning an integer where every Nth bit is set only if the predicate
/// from the Nth thread is `true`. Inactive threads will be counted as a `0` in the mask
/// for that bit. Mask is usually [`u32::MAX`].
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the vote has exited or the executing thread is not in
/// `mask`.
/// - For `compute_62` and below, all threads in `mask` must call this function in
/// convergence, and only threads belonging to the `mask` can be active when the
/// intrinsic is called.
/// - A thread tries to execute this function while not being present in `mask`.
#[gpu_only]
pub unsafe fn warp_vote_ballot(mask: u32, predicate: bool) -> u32 {
let mut out: u32;
unsafe {
asm!(
"{{",
".reg .pred %p1;",
"setp.eq.u32 %p1, {}, 1;",
"vote.sync.ballot.b32 {}, %p1, {};",
"}}",
in(reg32) predicate as u32,
out(reg32) out,
in(reg32) mask,
);
}
out
}
/// Waits for threads in a warp to reach this point and shuffles a value across the
/// threads.
///
/// # Arguments
///
/// - `mask` dictates what threads will participate in the shuffle, usually
/// [`u32::MAX`] to indicate all threads.
/// - `value` is the value that will be shuffled across the threads. i.e. the value
/// that will be given to the thread that calculates this thread as its target lane.
/// - `delta` is the value that will be subtracted from the current thread's lane to
/// calculate the target lane.
/// - `width` dictates how to optionally split the warp into subsections, it must be
/// a power of two and lower than `32`. calculated source lane values will NOT
/// wrap around the value of `width`. Usually just `32`.
///
/// # Returns
///
/// Returns the value from the target lane and a bool indicating if the target lane was
/// active or not.
///
/// # Note
///
/// Shuffles are always 32-bit shuffles, shuffles less than 32 bits will still shuffle a
/// 32-bit value, and shuffles greater than 32 bits will perform `sizeof(T) / 4`
/// shuffles. Therefore it is better to pack multiple 16 or 8 bit values into one value
/// if you are doing multiple shuffles.
///
/// # Panics
///
/// Panics if `width` is not a power of two or higher than 32.
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the shuffle has exited or the executing thread is
/// not in `mask`.
/// - For `compute_62` and below, all threads in `mask` must call the same function in
/// convergence, and only the threads in `mask` can be active when the shuffle is
/// called.
///
/// The returned value returned is unspecified if the calculated target lane is
/// inactive.
pub unsafe fn warp_shuffle_down<T: WarpShuffleValue>(
mask: u32,
value: T,
delta: u32,
width: u32,
) -> (T, bool) {
unsafe { T::shuffle(WarpShuffleMode::Down, mask, value, delta, width) }
}
/// Waits for threads in a warp to reach this point and shuffles a value across the
/// threads.
///
/// # Arguments
///
/// - `mask` dictates what threads will participate in the shuffle, usually
/// [`u32::MAX`] to indicate all threads.
/// - `value` is the value that will be shuffled across the threads. i.e. the value
/// that will be given to the thread that calculates this thread as its target lane.
/// - `delta` is the value that will be added to the current thread's lane to
/// calculate the target lane.
/// - `width` dictates how to optionally split the warp into subsections, it must be a
/// power of two and lower than `32`. calculated source lane values will NOT wrap
/// around the value of `width`. Usually just `32`.
///
/// # Returns
///
/// Returns the value from the target lane and a bool indicating if the target lane was
/// active or not.
///
/// # Note
///
/// Shuffles are always 32-bit shuffles, shuffles less than 32 bits will still shuffle a
/// 32-bit value, and shuffles greater than 32 bits will perform `sizeof(T) / 4`
/// shuffles. Therefore it is better to pack multiple 16 or 8 bit values into one value
/// if you are doing multiple shuffles.
///
/// # Panics
///
/// Panics if `width` is not a power of two or higher than 32.
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the shuffle has exited or the executing thread is
/// not in `mask`.
/// - For `compute_62` and below, all threads in `mask` must call the same function in
/// convergence, and only the threads in `mask` can be active when the shuffle is
/// called.
///
/// The returned value returned is unspecified if the calculated target lane is
/// inactive.
pub unsafe fn warp_shuffle_up<T: WarpShuffleValue>(
mask: u32,
value: T,
delta: u32,
width: u32,
) -> (T, bool) {
unsafe { T::shuffle(WarpShuffleMode::Up, mask, value, delta, width) }
}
/// Waits for threads in a warp to reach this point and shuffles a value across the
/// threads.
///
/// # Arguments
///
/// - `mask` dictates what threads will participate in the shuffle, usually
/// [`u32::MAX`] to indicate all threads.
/// - `value` is the value that will be shuffled across the threads. i.e. the value
/// that will be given to the thread that calculates this thread as its target
/// lane.
/// - `idx` is the target lane that will be used as the source of this thread's
/// returned value.
/// - `width` dictates how to optionally split the warp into subsections, it must be a
/// power of two and lower than `32`. calculated source lane values will NOT wrap
/// around the value of `width`. Usually just `32`.
///
/// # Returns
///
/// Returns the value from the target lane and a bool indicating if the target lane was
/// active or not.
///
/// # Note
///
/// Shuffles are always 32-bit shuffles, shuffles less than 32 bits will still shuffle a
/// 32-bit value, and shuffles greater than 32 bits will perform `sizeof(T) / 4`
/// shuffles. Therefore it is better to pack multiple 16 or 8 bit values into one value
/// if you are doing multiple shuffles.
///
/// # Panics
///
/// Panics if `width` is not a power of two or higher than 32.
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the shuffle has exited or the executing thread is
/// not in `mask`.
/// - For `compute_62` and below, all threads in `mask` must call the same function in
/// convergence, and only the threads in `mask` can be active when the shuffle is
/// called.
///
/// The returned value returned is unspecified if the calculated target lane is
/// inactive.
pub unsafe fn warp_shuffle_idx<T: WarpShuffleValue>(
mask: u32,
value: T,
idx: u32,
width: u32,
) -> (T, bool) {
unsafe { T::shuffle(WarpShuffleMode::Idx, mask, value, idx, width) }
}
/// Waits for threads in a warp to reach this point and shuffles a value across the
/// threads.
///
/// # Arguments
///
/// - `mask` dictates what threads will participate in the shuffle, usually
/// [`u32::MAX`] to indicate all threads.
/// - `value` is the value that will be shuffled across the threads. i.e. the value
/// that will be given to the thread that calculates this thread as its target lane.
/// - `lane_mask` is the value that will be XOR'd by the current thread's lane id to
/// calculate the target lane. i.e. the target lane will be `lane_id ^ lane_mask`.
/// - `width` dictates how to optionally split the warp into subsections, it must be
/// a power of two and lower than `32`. calculated source lane values will NOT wrap
/// around the value of `width`. Usually just `32`.
///
/// # Returns
///
/// Returns the value from the target lane and a bool indicating if the target lane was
/// active or not.
///
/// # Note
///
/// Shuffles are always 32-bit shuffles, shuffles less than 32 bits will still shuffle a
/// 32-bit value, and shuffles greater than 32 bits will perform `sizeof(T) / 4`
/// shuffles. Therefore it is better to pack multiple 16 or 8 bit values into one value
/// if you are doing multiple shuffles.
///
/// # Panics
///
/// Panics if `width` is not a power of two or higher than 32.
///
/// # Safety
///
/// Behavior is undefined if:
/// - Any thread participating in the shuffle has exited or the executing thread is
/// not in `mask`.
/// - For `compute_62` and below, all threads in `mask` must call the same function in
/// convergence, and only the threads in `mask` can be active when the shuffle is
/// called.
///
/// The returned value returned is unspecified if the calculated target lane is
/// inactive.
pub unsafe fn warp_shuffle_xor<T: WarpShuffleValue>(
mask: u32,
value: T,
lane_mask: u32,
width: u32,
) -> (T, bool) {
unsafe { T::shuffle(WarpShuffleMode::Xor, mask, value, lane_mask, width) }
}
/// A value that can be used in a warp shuffle
pub trait WarpShuffleValue: Sized {
/// Executes the shuffle, note that `mode` must be a constant value.
#[allow(clippy::missing_safety_doc)]
unsafe fn shuffle(
mode: WarpShuffleMode,
mask: u32,
value: Self,
b: u32,
width: u32,
) -> (Self, bool);
}
macro_rules! impl_shuffle {
($($type:ty, $width:literal),*, $(,)?) => {
$(
paste::paste! {
impl WarpShuffleValue for $type {
unsafe fn shuffle(
mode: WarpShuffleMode,
mask: u32,
value: Self,
b: u32,
width: u32,
) -> (Self, bool) {
let (res, oob) = unsafe { [<warp_shuffle_ $width>](mode, mask, value as [<u $width>], b, width) };
(res as $type, oob)
}
}
}
)*
};
}
impl_shuffle! {
i8, 8,
i16, 16,
i32, 32,
i64, 64,
i128, 128,
u8, 8,
u16, 16,
u32, 32,
u64, 64,
u128, 128,
}
// special cases
impl WarpShuffleValue for f32 {
unsafe fn shuffle(
mode: WarpShuffleMode,
mask: u32,
value: Self,
b: u32,
width: u32,
) -> (Self, bool) {
let (res, oob) = unsafe { warp_shuffle_32(mode, mask, value.to_bits(), b, width) };
(f32::from_bits(res), oob)
}
}
impl WarpShuffleValue for f64 {
unsafe fn shuffle(
mode: WarpShuffleMode,
mask: u32,
value: Self,
b: u32,
width: u32,
) -> (Self, bool) {
let (res, oob) = unsafe { warp_shuffle_64(mode, mask, value.to_bits(), b, width) };
(f64::from_bits(res), oob)
}
}
impl WarpShuffleValue for f16 {
unsafe fn shuffle(
mode: WarpShuffleMode,
mask: u32,
value: Self,
b: u32,
width: u32,
) -> (Self, bool) {
let (res, oob) = unsafe { warp_shuffle_16(mode, mask, value.to_bits(), b, width) };
(f16::from_bits(res), oob)
}
}
impl WarpShuffleValue for bf16 {
unsafe fn shuffle(
mode: WarpShuffleMode,
mask: u32,
value: Self,
b: u32,
width: u32,
) -> (Self, bool) {
let (res, oob) = unsafe { warp_shuffle_16(mode, mask, value.to_bits(), b, width) };
(bf16::from_bits(res), oob)
}
}
#[doc(hidden)]
#[repr(u32)]
#[derive(Clone, Copy)]
pub enum WarpShuffleMode {
Idx = 0,
Up = 1,
Down = 2,
Xor = 3,
}
// The libintrinsics.ll wrappers pack their (value, predicate) result into a
// single i64: low 32 bits = value, bit 32 = predicate. Returning a primitive
// integer avoids the small-aggregate ABI path where rustc attaches `align N`
// to the call's return value — an attribute LLVM 19's verifier rejects on
// non-pointer returns.
// Unused on host targets — every caller is `#[gpu_only]`.
#[allow(dead_code)]
#[inline(always)]
fn unpack_warp_result(packed: u64) -> (u32, bool) {
(packed as u32, (packed >> 32) & 1 != 0)
}
#[gpu_only]
unsafe fn warp_shuffle_32(
mode: WarpShuffleMode,
mask: u32,
value: u32,
b: u32,
width: u32,
) -> (u32, bool) {
unsafe extern "C" {
// see libintrinsics.ll
fn __nvvm_warp_shuffle(mask: u32, mode: u32, a: u32, b: u32, c: u32) -> u64;
}
assert!(
!(width & (width - 1)) != 0 && width <= 32,
"width must be a power of 2 and less than or equal to 32"
);
// mimicking nvcc's behavior
let mut c = 0;
c |= 0b11111;
c |= (32 - width) << 8;
let result = unsafe { __nvvm_warp_shuffle(mask, mode as u32, value, b, c) };
unpack_warp_result(result)
}
unsafe fn warp_shuffle_128(
mode: WarpShuffleMode,
mask: u32,
value: u128,
b: u32,
width: u32,
) -> (u128, bool) {
let first_half = value as u64;
let second_half = (value >> 64) as u64;
// shuffle the first and second half of the value then recombine them
// this will perform 4 shuffles in total (4 32-bit shuffles)
let (new_first_half, oob) = unsafe { warp_shuffle_64(mode, mask, first_half, b, width) };
let (new_second_half, _) = unsafe { warp_shuffle_64(mode, mask, second_half, b, width) };
(
((new_second_half as u128) << 64) | (new_first_half as u128),
oob,
)
}
unsafe fn warp_shuffle_64(
mode: WarpShuffleMode,
mask: u32,
value: u64,
b: u32,
width: u32,
) -> (u64, bool) {
let first_half = value as u32;
let second_half = (value >> 32) as u32;
// shuffle the first and second half of the value then recombine them
let (new_first_half, oob) = unsafe { warp_shuffle_32(mode, mask, first_half, b, width) };
let (new_second_half, _) = unsafe { warp_shuffle_32(mode, mask, second_half, b, width) };
(
((new_second_half as u64) << 32) | (new_first_half as u64),
oob,
)
}
unsafe fn warp_shuffle_16(
mode: WarpShuffleMode,
mask: u32,
value: u16,
b: u32,
width: u32,
) -> (u16, bool) {
let (value, oob) = unsafe { warp_shuffle_32(mode, mask, value as u32, b, width) };
((value as u16), oob)
}
unsafe fn warp_shuffle_8(
mode: WarpShuffleMode,
mask: u32,
value: u8,
b: u32,
width: u32,
) -> (u8, bool) {
let (value, oob) = unsafe { warp_shuffle_32(mode, mask, value as u32, b, width) };
((value as u8), oob)
}