ptx_parser/parser/instruction/
red.rs

1//! Original PTX specification:
2//!
3//! // Reduction operation with scalar type:
4//! red.op{.space}{.sem}{.scope}{.level::cache_hint}.type          [a], b{, cache-policy};
5//! red.add{.space}{.sem}{.scope}.noftz{.level::cache_hint}.f16    [a], b{, cache-policy};
6//! red.add{.space}{.sem}{.scope}.noftz{.level::cache_hint}.f16x2  [a], b{, cache-policy};
7//! red.add{.space}{.sem}{.scope}.noftz{.level::cache_hint}.bf16   [a], b{, cache-policy};
8//! red.add{.space}{.sem}{.scope}.noftz{.level::cache_hint}.bf16x2 [a], b{, cache-policy};
9//! .space =              { .global, .shared, .shared::cta, .shared::cluster};
10//! .sem =                {.relaxed, .release};
11//! .scope =              {.cta, .cluster, .gpu, .sys};
12//! .op =                 { .and, .or, .xor, .add, .inc, .dec, .min, .max };
13//! .level::cache_hint =  { .L2::cache_hint };
14//! .type =               { .b32, .b64, .u32, .u64, .s32, .s64, .f32, .f64 };
15//! ------------------------------------------------------------------
16//! // Reduction operation with vector type:
17//! red.add{.space}{.sem}{.scope}{.level::cache_hint}.vec_32_bit.f32 [a], b{, cache-policy};
18//! red.op{.space}{.sem}{.scope}.noftz{.level::cache_hint}. vec_16_bit.half_word_type [a], b{, cache-policy};
19//! red.op{.space}{.sem}{.scope}.noftz{.level::cache_hint}.vec_32_bit.packed_type [a], b {, cache-policy};
20//! .sem =                { .relaxed, .release };
21//! .scope =              { .cta, .cluster, .gpu, .sys };
22//! .op =                 { .add, .min, .max };
23//! .half_word_type =     { .f16, .bf16 };
24//! .packed_type =        { .f16x2,.bf16x2 };
25//! .vec_16_bit =         { .v2, .v4, .v8 };
26//! .vec_32_bit =         { .v2, .v4 };
27//! .level::cache_hint =  { .L2::cache_hint };
28
29#![allow(unused)]
30
31use crate::parser::{
32    PtxParseError, PtxParser, PtxTokenStream, Span,
33    util::{
34        between, comma_p, directive_p, exclamation_p, lbracket_p, lparen_p, map, minus_p, optional,
35        pipe_p, rbracket_p, rparen_p, semicolon_p, sep_by, string_p, try_map,
36    },
37};
38use crate::r#type::common::*;
39use crate::{alt, ok, seq_n};
40
41pub mod section_0 {
42    use super::*;
43    use crate::r#type::instruction::red::section_0::*;
44
45    // ============================================================================
46    // Generated enum parsers
47    // ============================================================================
48
49    impl PtxParser for LevelCacheHint {
50        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
51            alt!(map(string_p(".L2::cache_hint"), |_, _span| {
52                LevelCacheHint::L2CacheHint
53            }))
54        }
55    }
56
57    impl PtxParser for Op {
58        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
59            alt!(
60                map(string_p(".and"), |_, _span| Op::And),
61                map(string_p(".xor"), |_, _span| Op::Xor),
62                map(string_p(".add"), |_, _span| Op::Add),
63                map(string_p(".inc"), |_, _span| Op::Inc),
64                map(string_p(".dec"), |_, _span| Op::Dec),
65                map(string_p(".min"), |_, _span| Op::Min),
66                map(string_p(".max"), |_, _span| Op::Max),
67                map(string_p(".or"), |_, _span| Op::Or)
68            )
69        }
70    }
71
72    impl PtxParser for Scope {
73        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
74            alt!(
75                map(string_p(".cluster"), |_, _span| Scope::Cluster),
76                map(string_p(".cta"), |_, _span| Scope::Cta),
77                map(string_p(".gpu"), |_, _span| Scope::Gpu),
78                map(string_p(".sys"), |_, _span| Scope::Sys)
79            )
80        }
81    }
82
83    impl PtxParser for Sem {
84        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
85            alt!(
86                map(string_p(".relaxed"), |_, _span| Sem::Relaxed),
87                map(string_p(".release"), |_, _span| Sem::Release)
88            )
89        }
90    }
91
92    impl PtxParser for Space {
93        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
94            alt!(
95                map(string_p(".shared::cluster"), |_, _span| {
96                    Space::SharedCluster
97                }),
98                map(string_p(".shared::cta"), |_, _span| Space::SharedCta),
99                map(string_p(".global"), |_, _span| Space::Global),
100                map(string_p(".shared"), |_, _span| Space::Shared)
101            )
102        }
103    }
104
105    impl PtxParser for Type {
106        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
107            alt!(
108                map(string_p(".b32"), |_, _span| Type::B32),
109                map(string_p(".b64"), |_, _span| Type::B64),
110                map(string_p(".u32"), |_, _span| Type::U32),
111                map(string_p(".u64"), |_, _span| Type::U64),
112                map(string_p(".s32"), |_, _span| Type::S32),
113                map(string_p(".s64"), |_, _span| Type::S64),
114                map(string_p(".f32"), |_, _span| Type::F32),
115                map(string_p(".f64"), |_, _span| Type::F64)
116            )
117        }
118    }
119
120    impl PtxParser for RedOpSpaceSemScopeLevelCacheHintType {
121        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
122            try_map(
123                seq_n!(
124                    string_p("red"),
125                    Op::parse(),
126                    optional(Space::parse()),
127                    optional(Sem::parse()),
128                    optional(Scope::parse()),
129                    optional(LevelCacheHint::parse()),
130                    Type::parse(),
131                    AddressOperand::parse(),
132                    comma_p(),
133                    GeneralOperand::parse(),
134                    map(
135                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
136                        |value, _| value.map(|(_, operand)| operand)
137                    ),
138                    semicolon_p()
139                ),
140                |(_, op, space, sem, scope, level_cache_hint, type_, a, _, b, cache_policy, _),
141                 span| {
142                    ok!(RedOpSpaceSemScopeLevelCacheHintType {
143                        op = op,
144                        space = space,
145                        sem = sem,
146                        scope = scope,
147                        level_cache_hint = level_cache_hint,
148                        type_ = type_,
149                        a = a,
150                        b = b,
151                        cache_policy = cache_policy,
152
153                    })
154                },
155            )
156        }
157    }
158
159    impl PtxParser for RedAddSpaceSemScopeNoftzLevelCacheHintF16 {
160        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
161            try_map(
162                seq_n!(
163                    string_p("red"),
164                    string_p(".add"),
165                    optional(Space::parse()),
166                    optional(Sem::parse()),
167                    optional(Scope::parse()),
168                    string_p(".noftz"),
169                    optional(LevelCacheHint::parse()),
170                    string_p(".f16"),
171                    AddressOperand::parse(),
172                    comma_p(),
173                    GeneralOperand::parse(),
174                    map(
175                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
176                        |value, _| value.map(|(_, operand)| operand)
177                    ),
178                    semicolon_p()
179                ),
180                |(
181                    _,
182                    add,
183                    space,
184                    sem,
185                    scope,
186                    noftz,
187                    level_cache_hint,
188                    f16,
189                    a,
190                    _,
191                    b,
192                    cache_policy,
193                    _,
194                ),
195                 span| {
196                    ok!(RedAddSpaceSemScopeNoftzLevelCacheHintF16 {
197                        add = add,
198                        space = space,
199                        sem = sem,
200                        scope = scope,
201                        noftz = noftz,
202                        level_cache_hint = level_cache_hint,
203                        f16 = f16,
204                        a = a,
205                        b = b,
206                        cache_policy = cache_policy,
207
208                    })
209                },
210            )
211        }
212    }
213
214    impl PtxParser for RedAddSpaceSemScopeNoftzLevelCacheHintF16x2 {
215        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
216            try_map(
217                seq_n!(
218                    string_p("red"),
219                    string_p(".add"),
220                    optional(Space::parse()),
221                    optional(Sem::parse()),
222                    optional(Scope::parse()),
223                    string_p(".noftz"),
224                    optional(LevelCacheHint::parse()),
225                    string_p(".f16x2"),
226                    AddressOperand::parse(),
227                    comma_p(),
228                    GeneralOperand::parse(),
229                    map(
230                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
231                        |value, _| value.map(|(_, operand)| operand)
232                    ),
233                    semicolon_p()
234                ),
235                |(
236                    _,
237                    add,
238                    space,
239                    sem,
240                    scope,
241                    noftz,
242                    level_cache_hint,
243                    f16x2,
244                    a,
245                    _,
246                    b,
247                    cache_policy,
248                    _,
249                ),
250                 span| {
251                    ok!(RedAddSpaceSemScopeNoftzLevelCacheHintF16x2 {
252                        add = add,
253                        space = space,
254                        sem = sem,
255                        scope = scope,
256                        noftz = noftz,
257                        level_cache_hint = level_cache_hint,
258                        f16x2 = f16x2,
259                        a = a,
260                        b = b,
261                        cache_policy = cache_policy,
262
263                    })
264                },
265            )
266        }
267    }
268
269    impl PtxParser for RedAddSpaceSemScopeNoftzLevelCacheHintBf16 {
270        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
271            try_map(
272                seq_n!(
273                    string_p("red"),
274                    string_p(".add"),
275                    optional(Space::parse()),
276                    optional(Sem::parse()),
277                    optional(Scope::parse()),
278                    string_p(".noftz"),
279                    optional(LevelCacheHint::parse()),
280                    string_p(".bf16"),
281                    AddressOperand::parse(),
282                    comma_p(),
283                    GeneralOperand::parse(),
284                    map(
285                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
286                        |value, _| value.map(|(_, operand)| operand)
287                    ),
288                    semicolon_p()
289                ),
290                |(
291                    _,
292                    add,
293                    space,
294                    sem,
295                    scope,
296                    noftz,
297                    level_cache_hint,
298                    bf16,
299                    a,
300                    _,
301                    b,
302                    cache_policy,
303                    _,
304                ),
305                 span| {
306                    ok!(RedAddSpaceSemScopeNoftzLevelCacheHintBf16 {
307                        add = add,
308                        space = space,
309                        sem = sem,
310                        scope = scope,
311                        noftz = noftz,
312                        level_cache_hint = level_cache_hint,
313                        bf16 = bf16,
314                        a = a,
315                        b = b,
316                        cache_policy = cache_policy,
317
318                    })
319                },
320            )
321        }
322    }
323
324    impl PtxParser for RedAddSpaceSemScopeNoftzLevelCacheHintBf16x2 {
325        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
326            try_map(
327                seq_n!(
328                    string_p("red"),
329                    string_p(".add"),
330                    optional(Space::parse()),
331                    optional(Sem::parse()),
332                    optional(Scope::parse()),
333                    string_p(".noftz"),
334                    optional(LevelCacheHint::parse()),
335                    string_p(".bf16x2"),
336                    AddressOperand::parse(),
337                    comma_p(),
338                    GeneralOperand::parse(),
339                    map(
340                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
341                        |value, _| value.map(|(_, operand)| operand)
342                    ),
343                    semicolon_p()
344                ),
345                |(
346                    _,
347                    add,
348                    space,
349                    sem,
350                    scope,
351                    noftz,
352                    level_cache_hint,
353                    bf16x2,
354                    a,
355                    _,
356                    b,
357                    cache_policy,
358                    _,
359                ),
360                 span| {
361                    ok!(RedAddSpaceSemScopeNoftzLevelCacheHintBf16x2 {
362                        add = add,
363                        space = space,
364                        sem = sem,
365                        scope = scope,
366                        noftz = noftz,
367                        level_cache_hint = level_cache_hint,
368                        bf16x2 = bf16x2,
369                        a = a,
370                        b = b,
371                        cache_policy = cache_policy,
372
373                    })
374                },
375            )
376        }
377    }
378}
379
380pub mod section_1 {
381    use super::*;
382    use crate::r#type::instruction::red::section_1::*;
383
384    // ============================================================================
385    // Generated enum parsers
386    // ============================================================================
387
388    impl PtxParser for HalfWordType {
389        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
390            alt!(
391                map(string_p(".bf16"), |_, _span| HalfWordType::Bf16),
392                map(string_p(".f16"), |_, _span| HalfWordType::F16)
393            )
394        }
395    }
396
397    impl PtxParser for LevelCacheHint {
398        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
399            alt!(map(string_p(".L2::cache_hint"), |_, _span| {
400                LevelCacheHint::L2CacheHint
401            }))
402        }
403    }
404
405    impl PtxParser for Op {
406        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
407            alt!(
408                map(string_p(".add"), |_, _span| Op::Add),
409                map(string_p(".min"), |_, _span| Op::Min),
410                map(string_p(".max"), |_, _span| Op::Max)
411            )
412        }
413    }
414
415    impl PtxParser for PackedType {
416        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
417            alt!(
418                map(string_p(".bf16x2"), |_, _span| PackedType::Bf16x2),
419                map(string_p(".f16x2"), |_, _span| PackedType::F16x2)
420            )
421        }
422    }
423
424    impl PtxParser for Scope {
425        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
426            alt!(
427                map(string_p(".cluster"), |_, _span| Scope::Cluster),
428                map(string_p(".cta"), |_, _span| Scope::Cta),
429                map(string_p(".gpu"), |_, _span| Scope::Gpu),
430                map(string_p(".sys"), |_, _span| Scope::Sys)
431            )
432        }
433    }
434
435    impl PtxParser for Sem {
436        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
437            alt!(
438                map(string_p(".relaxed"), |_, _span| Sem::Relaxed),
439                map(string_p(".release"), |_, _span| Sem::Release)
440            )
441        }
442    }
443
444    impl PtxParser for Space {
445        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
446            alt!(
447                map(string_p(".shared::cluster"), |_, _span| {
448                    Space::SharedCluster
449                }),
450                map(string_p(".shared::cta"), |_, _span| Space::SharedCta),
451                map(string_p(".global"), |_, _span| Space::Global),
452                map(string_p(".shared"), |_, _span| Space::Shared)
453            )
454        }
455    }
456
457    impl PtxParser for Vec16Bit {
458        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
459            alt!(
460                map(string_p(".v2"), |_, _span| Vec16Bit::V2),
461                map(string_p(".v4"), |_, _span| Vec16Bit::V4),
462                map(string_p(".v8"), |_, _span| Vec16Bit::V8)
463            )
464        }
465    }
466
467    impl PtxParser for Vec32Bit {
468        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
469            alt!(
470                map(string_p(".v2"), |_, _span| Vec32Bit::V2),
471                map(string_p(".v4"), |_, _span| Vec32Bit::V4)
472            )
473        }
474    }
475
476    impl PtxParser for RedAddSpaceSemScopeLevelCacheHintVec32BitF32 {
477        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
478            try_map(
479                seq_n!(
480                    string_p("red"),
481                    string_p(".add"),
482                    optional(Space::parse()),
483                    optional(Sem::parse()),
484                    optional(Scope::parse()),
485                    optional(LevelCacheHint::parse()),
486                    Vec32Bit::parse(),
487                    string_p(".f32"),
488                    AddressOperand::parse(),
489                    comma_p(),
490                    GeneralOperand::parse(),
491                    map(
492                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
493                        |value, _| value.map(|(_, operand)| operand)
494                    ),
495                    semicolon_p()
496                ),
497                |(
498                    _,
499                    add,
500                    space,
501                    sem,
502                    scope,
503                    level_cache_hint,
504                    vec_32_bit,
505                    f32,
506                    a,
507                    _,
508                    b,
509                    cache_policy,
510                    _,
511                ),
512                 span| {
513                    ok!(RedAddSpaceSemScopeLevelCacheHintVec32BitF32 {
514                        add = add,
515                        space = space,
516                        sem = sem,
517                        scope = scope,
518                        level_cache_hint = level_cache_hint,
519                        vec_32_bit = vec_32_bit,
520                        f32 = f32,
521                        a = a,
522                        b = b,
523                        cache_policy = cache_policy,
524
525                    })
526                },
527            )
528        }
529    }
530
531    impl PtxParser for RedOpSpaceSemScopeNoftzLevelCacheHintVec16BitHalfWordType {
532        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
533            try_map(
534                seq_n!(
535                    string_p("red"),
536                    Op::parse(),
537                    optional(Space::parse()),
538                    optional(Sem::parse()),
539                    optional(Scope::parse()),
540                    string_p(".noftz"),
541                    optional(LevelCacheHint::parse()),
542                    Vec16Bit::parse(),
543                    HalfWordType::parse(),
544                    AddressOperand::parse(),
545                    comma_p(),
546                    GeneralOperand::parse(),
547                    map(
548                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
549                        |value, _| value.map(|(_, operand)| operand)
550                    ),
551                    semicolon_p()
552                ),
553                |(
554                    _,
555                    op,
556                    space,
557                    sem,
558                    scope,
559                    noftz,
560                    level_cache_hint,
561                    vec_16_bit,
562                    half_word_type,
563                    a,
564                    _,
565                    b,
566                    cache_policy,
567                    _,
568                ),
569                 span| {
570                    ok!(RedOpSpaceSemScopeNoftzLevelCacheHintVec16BitHalfWordType {
571                        op = op,
572                        space = space,
573                        sem = sem,
574                        scope = scope,
575                        noftz = noftz,
576                        level_cache_hint = level_cache_hint,
577                        vec_16_bit = vec_16_bit,
578                        half_word_type = half_word_type,
579                        a = a,
580                        b = b,
581                        cache_policy = cache_policy,
582
583                    })
584                },
585            )
586        }
587    }
588
589    impl PtxParser for RedOpSpaceSemScopeNoftzLevelCacheHintVec32BitPackedType {
590        fn parse() -> impl Fn(&mut PtxTokenStream) -> Result<(Self, Span), PtxParseError> {
591            try_map(
592                seq_n!(
593                    string_p("red"),
594                    Op::parse(),
595                    optional(Space::parse()),
596                    optional(Sem::parse()),
597                    optional(Scope::parse()),
598                    string_p(".noftz"),
599                    optional(LevelCacheHint::parse()),
600                    Vec32Bit::parse(),
601                    PackedType::parse(),
602                    AddressOperand::parse(),
603                    comma_p(),
604                    GeneralOperand::parse(),
605                    map(
606                        optional(seq_n!(comma_p(), GeneralOperand::parse())),
607                        |value, _| value.map(|(_, operand)| operand)
608                    ),
609                    semicolon_p()
610                ),
611                |(
612                    _,
613                    op,
614                    space,
615                    sem,
616                    scope,
617                    noftz,
618                    level_cache_hint,
619                    vec_32_bit,
620                    packed_type,
621                    a,
622                    _,
623                    b,
624                    cache_policy,
625                    _,
626                ),
627                 span| {
628                    ok!(RedOpSpaceSemScopeNoftzLevelCacheHintVec32BitPackedType {
629                        op = op,
630                        space = space,
631                        sem = sem,
632                        scope = scope,
633                        noftz = noftz,
634                        level_cache_hint = level_cache_hint,
635                        vec_32_bit = vec_32_bit,
636                        packed_type = packed_type,
637                        a = a,
638                        b = b,
639                        cache_policy = cache_policy,
640
641                    })
642                },
643            )
644        }
645    }
646}