lima/ppir: don't lower phis to scalar
[mesa.git] / src / gallium / drivers / lima / lima_program.c
1 /*
2 * Copyright (c) 2017-2019 Lima Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 #include "util/u_memory.h"
26 #include "util/ralloc.h"
27 #include "util/u_debug.h"
28
29 #include "tgsi/tgsi_dump.h"
30 #include "compiler/nir/nir.h"
31 #include "nir/tgsi_to_nir.h"
32
33 #include "pipe/p_state.h"
34
35 #include "lima_screen.h"
36 #include "lima_context.h"
37 #include "lima_program.h"
38 #include "lima_bo.h"
39 #include "ir/lima_ir.h"
40
41 static const nir_shader_compiler_options vs_nir_options = {
42 .lower_ffma = true,
43 .lower_fpow = true,
44 .lower_ffract = true,
45 .lower_fdiv = true,
46 .lower_fmod = true,
47 .lower_fsqrt = true,
48 .lower_sub = true,
49 .lower_flrp32 = true,
50 .lower_flrp64 = true,
51 .lower_ftrunc = true,
52 /* could be implemented by clamp */
53 .lower_fsat = true,
54 .lower_bitops = true,
55 .lower_rotate = true,
56 .lower_sincos = true,
57 };
58
59 static const nir_shader_compiler_options fs_nir_options = {
60 .lower_ffma = true,
61 .lower_fpow = true,
62 .lower_fdiv = true,
63 .lower_fmod = true,
64 .lower_sub = true,
65 .lower_flrp32 = true,
66 .lower_flrp64 = true,
67 .lower_fsign = true,
68 .lower_rotate = true,
69 .lower_fdot = true,
70 .lower_bitops = true,
71 .lower_vector_cmp = true,
72 };
73
74 static const struct nir_lower_tex_options tex_options = {
75 .lower_txp = ~0u,
76 };
77
78 const void *
79 lima_program_get_compiler_options(enum pipe_shader_type shader)
80 {
81 switch (shader) {
82 case PIPE_SHADER_VERTEX:
83 return &vs_nir_options;
84 case PIPE_SHADER_FRAGMENT:
85 return &fs_nir_options;
86 default:
87 return NULL;
88 }
89 }
90
91 static int
92 type_size(const struct glsl_type *type, bool bindless)
93 {
94 return glsl_count_attribute_slots(type, false);
95 }
96
97 void
98 lima_program_optimize_vs_nir(struct nir_shader *s)
99 {
100 bool progress;
101
102 NIR_PASS_V(s, nir_lower_viewport_transform);
103 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
104 NIR_PASS_V(s, nir_lower_load_const_to_scalar);
105 NIR_PASS_V(s, lima_nir_lower_uniform_to_scalar);
106 NIR_PASS_V(s, nir_lower_io_to_scalar,
107 nir_var_shader_in|nir_var_shader_out);
108
109 do {
110 progress = false;
111
112 NIR_PASS_V(s, nir_lower_vars_to_ssa);
113 NIR_PASS(progress, s, nir_lower_alu_to_scalar, NULL, NULL);
114 NIR_PASS(progress, s, nir_lower_phis_to_scalar);
115 NIR_PASS(progress, s, nir_copy_prop);
116 NIR_PASS(progress, s, nir_opt_remove_phis);
117 NIR_PASS(progress, s, nir_opt_dce);
118 NIR_PASS(progress, s, nir_opt_dead_cf);
119 NIR_PASS(progress, s, nir_opt_cse);
120 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
121 NIR_PASS(progress, s, nir_opt_algebraic);
122 NIR_PASS(progress, s, nir_opt_constant_folding);
123 NIR_PASS(progress, s, nir_opt_undef);
124 NIR_PASS(progress, s, nir_opt_loop_unroll,
125 nir_var_shader_in |
126 nir_var_shader_out |
127 nir_var_function_temp);
128 } while (progress);
129
130 NIR_PASS_V(s, nir_lower_int_to_float);
131 NIR_PASS_V(s, nir_lower_bool_to_float);
132
133 /* Some ops must be lowered after being converted from int ops,
134 * so re-run nir_opt_algebraic after int lowering. */
135 do {
136 progress = false;
137 NIR_PASS(progress, s, nir_opt_algebraic);
138 } while (progress);
139
140 NIR_PASS_V(s, nir_copy_prop);
141 NIR_PASS_V(s, nir_opt_dce);
142 NIR_PASS_V(s, nir_lower_locals_to_regs);
143 NIR_PASS_V(s, nir_convert_from_ssa, true);
144 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
145 nir_sweep(s);
146 }
147
148 static bool
149 lima_alu_to_scalar_filter_cb(const nir_instr *instr, const void *data)
150 {
151 if (instr->type != nir_instr_type_alu)
152 return false;
153
154 nir_alu_instr *alu = nir_instr_as_alu(instr);
155 switch (alu->op) {
156 case nir_op_frcp:
157 case nir_op_frsq:
158 case nir_op_flog2:
159 case nir_op_fexp2:
160 case nir_op_fsqrt:
161 case nir_op_fsin:
162 case nir_op_fcos:
163 return true;
164 default:
165 break;
166 }
167
168 /* nir vec4 fcsel assumes that each component of the condition will be
169 * used to select the same component from the two options, but Utgard PP
170 * has only 1 component condition. If all condition components are not the
171 * same we need to lower it to scalar.
172 */
173 switch (alu->op) {
174 case nir_op_bcsel:
175 case nir_op_fcsel:
176 break;
177 default:
178 return false;
179 }
180
181 int num_components = nir_dest_num_components(alu->dest.dest);
182
183 uint8_t swizzle = alu->src[0].swizzle[0];
184
185 for (int i = 1; i < num_components; i++)
186 if (alu->src[0].swizzle[i] != swizzle)
187 return true;
188
189 return false;
190 }
191
192 void
193 lima_program_optimize_fs_nir(struct nir_shader *s)
194 {
195 bool progress;
196
197 NIR_PASS_V(s, nir_lower_fragcoord_wtrans);
198 NIR_PASS_V(s, nir_lower_io, nir_var_all, type_size, 0);
199 NIR_PASS_V(s, nir_lower_regs_to_ssa);
200 NIR_PASS_V(s, nir_lower_tex, &tex_options);
201
202 do {
203 progress = false;
204 NIR_PASS(progress, s, nir_opt_vectorize);
205 } while (progress);
206
207 do {
208 progress = false;
209
210 NIR_PASS_V(s, nir_lower_vars_to_ssa);
211 NIR_PASS(progress, s, nir_lower_alu_to_scalar, lima_alu_to_scalar_filter_cb, NULL);
212 NIR_PASS(progress, s, nir_copy_prop);
213 NIR_PASS(progress, s, nir_opt_remove_phis);
214 NIR_PASS(progress, s, nir_opt_dce);
215 NIR_PASS(progress, s, nir_opt_dead_cf);
216 NIR_PASS(progress, s, nir_opt_cse);
217 NIR_PASS(progress, s, nir_opt_peephole_select, 8, true, true);
218 NIR_PASS(progress, s, nir_opt_algebraic);
219 NIR_PASS(progress, s, nir_opt_constant_folding);
220 NIR_PASS(progress, s, nir_opt_undef);
221 NIR_PASS(progress, s, nir_opt_loop_unroll,
222 nir_var_shader_in |
223 nir_var_shader_out |
224 nir_var_function_temp);
225 } while (progress);
226
227 NIR_PASS_V(s, nir_lower_int_to_float);
228 NIR_PASS_V(s, nir_lower_bool_to_float);
229
230 /* Some ops must be lowered after being converted from int ops,
231 * so re-run nir_opt_algebraic after int lowering. */
232 do {
233 progress = false;
234 NIR_PASS(progress, s, nir_opt_algebraic);
235 } while (progress);
236
237 /* Must be run after optimization loop */
238 NIR_PASS_V(s, lima_nir_scale_trig);
239
240 /* Lower modifiers */
241 NIR_PASS_V(s, nir_lower_to_source_mods, nir_lower_all_source_mods);
242 NIR_PASS_V(s, nir_copy_prop);
243 NIR_PASS_V(s, nir_opt_dce);
244
245 NIR_PASS_V(s, nir_lower_locals_to_regs);
246 NIR_PASS_V(s, nir_convert_from_ssa, true);
247 NIR_PASS_V(s, nir_remove_dead_variables, nir_var_function_temp);
248
249 NIR_PASS_V(s, nir_move_vec_src_uses_to_dest);
250 NIR_PASS_V(s, nir_lower_vec_to_movs);
251
252 nir_sweep(s);
253 }
254
255 static void *
256 lima_create_fs_state(struct pipe_context *pctx,
257 const struct pipe_shader_state *cso)
258 {
259 struct lima_context *ctx = lima_context(pctx);
260 struct lima_screen *screen = lima_screen(pctx->screen);
261 struct lima_fs_shader_state *so = rzalloc(NULL, struct lima_fs_shader_state);
262
263 if (!so)
264 return NULL;
265
266 nir_shader *nir;
267 if (cso->type == PIPE_SHADER_IR_NIR)
268 nir = cso->ir.nir;
269 else {
270 assert(cso->type == PIPE_SHADER_IR_TGSI);
271
272 nir = tgsi_to_nir(cso->tokens, pctx->screen);
273 }
274
275 lima_program_optimize_fs_nir(nir);
276
277 if (lima_debug & LIMA_DEBUG_PP)
278 nir_print_shader(nir, stdout);
279
280 if (!ppir_compile_nir(so, nir, screen->pp_ra, &ctx->debug)) {
281 ralloc_free(so);
282 return NULL;
283 }
284
285 return so;
286 }
287
288 static void
289 lima_bind_fs_state(struct pipe_context *pctx, void *hwcso)
290 {
291 struct lima_context *ctx = lima_context(pctx);
292
293 ctx->fs = hwcso;
294 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_FRAG;
295 }
296
297 static void
298 lima_delete_fs_state(struct pipe_context *pctx, void *hwcso)
299 {
300 struct lima_fs_shader_state *so = hwcso;
301
302 if (so->bo)
303 lima_bo_free(so->bo);
304
305 ralloc_free(so);
306 }
307
308 bool
309 lima_update_vs_state(struct lima_context *ctx)
310 {
311 struct lima_vs_shader_state *vs = ctx->vs;
312 if (!vs->bo) {
313 struct lima_screen *screen = lima_screen(ctx->base.screen);
314 vs->bo = lima_bo_create(screen, vs->shader_size, 0);
315 if (!vs->bo) {
316 fprintf(stderr, "lima: create vs shader bo fail\n");
317 return false;
318 }
319
320 memcpy(lima_bo_map(vs->bo), vs->shader, vs->shader_size);
321 ralloc_free(vs->shader);
322 vs->shader = NULL;
323 }
324
325 return true;
326 }
327
328 bool
329 lima_update_fs_state(struct lima_context *ctx)
330 {
331 struct lima_fs_shader_state *fs = ctx->fs;
332 if (!fs->bo) {
333 struct lima_screen *screen = lima_screen(ctx->base.screen);
334 fs->bo = lima_bo_create(screen, fs->shader_size, 0);
335 if (!fs->bo) {
336 fprintf(stderr, "lima: create fs shader bo fail\n");
337 return false;
338 }
339
340 memcpy(lima_bo_map(fs->bo), fs->shader, fs->shader_size);
341 ralloc_free(fs->shader);
342 fs->shader = NULL;
343 }
344
345 ctx->pp_max_stack_size = MAX2(ctx->pp_max_stack_size, ctx->fs->stack_size);
346
347 return true;
348 }
349
350 static void *
351 lima_create_vs_state(struct pipe_context *pctx,
352 const struct pipe_shader_state *cso)
353 {
354 struct lima_context *ctx = lima_context(pctx);
355 struct lima_vs_shader_state *so = rzalloc(NULL, struct lima_vs_shader_state);
356
357 if (!so)
358 return NULL;
359
360 nir_shader *nir;
361 if (cso->type == PIPE_SHADER_IR_NIR)
362 nir = cso->ir.nir;
363 else {
364 assert(cso->type == PIPE_SHADER_IR_TGSI);
365
366 nir = tgsi_to_nir(cso->tokens, pctx->screen);
367 }
368
369 lima_program_optimize_vs_nir(nir);
370
371 if (lima_debug & LIMA_DEBUG_GP)
372 nir_print_shader(nir, stdout);
373
374 if (!gpir_compile_nir(so, nir, &ctx->debug)) {
375 ralloc_free(so);
376 return NULL;
377 }
378
379 return so;
380 }
381
382 static void
383 lima_bind_vs_state(struct pipe_context *pctx, void *hwcso)
384 {
385 struct lima_context *ctx = lima_context(pctx);
386
387 ctx->vs = hwcso;
388 ctx->dirty |= LIMA_CONTEXT_DIRTY_SHADER_VERT;
389 }
390
391 static void
392 lima_delete_vs_state(struct pipe_context *pctx, void *hwcso)
393 {
394 struct lima_vs_shader_state *so = hwcso;
395
396 if (so->bo)
397 lima_bo_free(so->bo);
398
399 ralloc_free(so);
400 }
401
402 void
403 lima_program_init(struct lima_context *ctx)
404 {
405 ctx->base.create_fs_state = lima_create_fs_state;
406 ctx->base.bind_fs_state = lima_bind_fs_state;
407 ctx->base.delete_fs_state = lima_delete_fs_state;
408
409 ctx->base.create_vs_state = lima_create_vs_state;
410 ctx->base.bind_vs_state = lima_bind_vs_state;
411 ctx->base.delete_vs_state = lima_delete_vs_state;
412 }