Debian Codegraph @

src/kosmickrisp/compiler/msl_nir_lower_subgroups.c

raw symbols SCIP
1 /*
2 * Copyright 2023 Valve Corporation
3 * Copyright 2025 LunarG, Inc.
4 * Copyright 2025 Google LLC
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include "msl_private.h"
9 #include "nir.h"
10 #include "nir_builder.h"
11
12 static bool
13 needs_bool_widening(nir_intrinsic_instr *intrin)
14 {
15 switch (intrin->intrinsic) {
16 case nir_intrinsic_read_invocation:
17 case nir_intrinsic_read_first_invocation:
18 case nir_intrinsic_reduce:
19 case nir_intrinsic_quad_broadcast:
20 case nir_intrinsic_quad_swap_horizontal:
21 case nir_intrinsic_quad_swap_vertical:
22 case nir_intrinsic_quad_swap_diagonal:
23 case nir_intrinsic_shuffle:
24 case nir_intrinsic_shuffle_down:
25 case nir_intrinsic_shuffle_up:
26 case nir_intrinsic_shuffle_xor:
27 return true;
28 default:
29 return false;
30 }
31 }
32
33 static bool
34 lower_bool_ops(nir_builder *b, nir_intrinsic_instr *intrin, void *_unused)
35 {
36 if (!needs_bool_widening(intrin))
37 return false;
38
39 if (intrin->def.bit_size != 1)
40 return false;
41
42 b->cursor = nir_before_instr(&intrin->instr);
43 nir_def *widen = nir_b2i32(b, intrin->src[0].ssa);
44 nir_src_rewrite(&intrin->src[0], widen);
45 intrin->def.bit_size = 32;
46 b->cursor = nir_after_instr(&intrin->instr);
47 nir_def *narrow = nir_b2b1(b, &intrin->def);
48 nir_def_rewrite_uses_after(&intrin->def, narrow);
49
50 return true;
51 }
52
53 static bool
54 lower(nir_builder *b, nir_intrinsic_instr *intr, void *data)
55 {
56 b->cursor = nir_before_instr(&intr->instr);
57
58 switch (intr->intrinsic) {
59 case nir_intrinsic_vote_any: {
60 /* We don't have vote instructions, but we have efficient ballots */
61 nir_def *ballot = nir_ballot(b, 1, 32, intr->src[0].ssa);
62 nir_def_rewrite_uses(&intr->def, nir_ine_imm(b, ballot, 0));
63 return true;
64 }
65
66 case nir_intrinsic_vote_all: {
67 nir_def *ballot = nir_ballot(b, 1, 32, nir_inot(b, intr->src[0].ssa));
68 nir_def_rewrite_uses(&intr->def, nir_ieq_imm(b, ballot, 0));
69 return true;
70 }
71
72 default:
73 return false;
74 }
75 }
76
77 void
78 msl_nir_lower_subgroups(nir_shader *nir)
79 {
80 const nir_lower_subgroups_options subgroups_options = {
81 .subgroup_size = 32,
82 .ballot_bit_size = 32,
83 .ballot_components = 1,
84 .lower_subgroup_masks = true,
85 .lower_vote_ieq = true,
86 .lower_vote_feq = true,
87 .lower_vote_bool_eq = true,
88 .lower_inverse_ballot = true,
89 .lower_relative_shuffle = true,
90 .lower_quad = true,
91 .lower_reduce = true,
92 };
93 NIR_PASS(_, nir, nir_lower_subgroups, &subgroups_options);
94 NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower,
95 nir_metadata_control_flow, NULL);
96 NIR_PASS(_, nir, nir_shader_intrinsics_pass, lower_bool_ops,
97 nir_metadata_control_flow, NULL);
98 }