glx/test: meson: assorted include fixes
[mesa.git] / src / intel / compiler / brw_nir_lower_cs_intrinsics.c
1 /*
2 * Copyright (c) 2016 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 struct lower_intrinsics_state {
28 nir_shader *nir;
29 unsigned dispatch_width;
30 nir_function_impl *impl;
31 bool progress;
32 nir_builder builder;
33 unsigned local_workgroup_size;
34 };
35
36 static bool
37 lower_cs_intrinsics_convert_block(struct lower_intrinsics_state *state,
38 nir_block *block)
39 {
40 bool progress = false;
41 nir_builder *b = &state->builder;
42 nir_shader *nir = state->nir;
43
44 nir_foreach_instr_safe(instr, block) {
45 if (instr->type != nir_instr_type_intrinsic)
46 continue;
47
48 nir_intrinsic_instr *intrinsic = nir_instr_as_intrinsic(instr);
49
50 b->cursor = nir_after_instr(&intrinsic->instr);
51
52 nir_ssa_def *sysval;
53 switch (intrinsic->intrinsic) {
54 case nir_intrinsic_load_local_invocation_index: {
55 /* We construct the local invocation index from:
56 *
57 * gl_LocalInvocationIndex =
58 * cs_thread_local_id + subgroup_invocation;
59 */
60 nir_ssa_def *subgroup_id;
61 if (state->local_workgroup_size <= state->dispatch_width)
62 subgroup_id = nir_imm_int(b, 0);
63 else
64 subgroup_id = nir_load_subgroup_id(b);
65
66 nir_ssa_def *thread_local_id =
67 nir_imul(b, subgroup_id, nir_imm_int(b, state->dispatch_width));
68 nir_ssa_def *channel = nir_load_subgroup_invocation(b);
69 sysval = nir_iadd(b, channel, thread_local_id);
70 break;
71 }
72
73 case nir_intrinsic_load_subgroup_id:
74 if (state->local_workgroup_size > 8)
75 continue;
76
77 /* For small workgroup sizes, we know subgroup_id will be zero */
78 sysval = nir_imm_int(b, 0);
79 break;
80
81 case nir_intrinsic_load_num_subgroups: {
82 unsigned local_workgroup_size =
83 nir->info.cs.local_size[0] * nir->info.cs.local_size[1] *
84 nir->info.cs.local_size[2];
85 unsigned num_subgroups =
86 DIV_ROUND_UP(local_workgroup_size, state->dispatch_width);
87 sysval = nir_imm_int(b, num_subgroups);
88 break;
89 }
90
91 default:
92 continue;
93 }
94
95 nir_ssa_def_rewrite_uses(&intrinsic->dest.ssa, nir_src_for_ssa(sysval));
96 nir_instr_remove(&intrinsic->instr);
97
98 state->progress = true;
99 }
100
101 return progress;
102 }
103
104 static void
105 lower_cs_intrinsics_convert_impl(struct lower_intrinsics_state *state)
106 {
107 nir_builder_init(&state->builder, state->impl);
108
109 nir_foreach_block(block, state->impl) {
110 lower_cs_intrinsics_convert_block(state, block);
111 }
112
113 nir_metadata_preserve(state->impl,
114 nir_metadata_block_index | nir_metadata_dominance);
115 }
116
117 bool
118 brw_nir_lower_cs_intrinsics(nir_shader *nir,
119 unsigned dispatch_width)
120 {
121 assert(nir->info.stage == MESA_SHADER_COMPUTE);
122
123 bool progress = false;
124 struct lower_intrinsics_state state;
125 memset(&state, 0, sizeof(state));
126 state.nir = nir;
127 state.dispatch_width = dispatch_width;
128 state.local_workgroup_size = nir->info.cs.local_size[0] *
129 nir->info.cs.local_size[1] *
130 nir->info.cs.local_size[2];
131
132 do {
133 state.progress = false;
134 nir_foreach_function(function, nir) {
135 if (function->impl) {
136 state.impl = function->impl;
137 lower_cs_intrinsics_convert_impl(&state);
138 }
139 }
140 progress |= state.progress;
141 } while (state.progress);
142
143 return progress;
144 }