freedreno: a2xx: fix HW binning for batches with >256K vertices
[mesa.git] / src / gallium / drivers / freedreno / a2xx / ir2_nir_lower_scalar.c
1 /*
2 * Copyright (C) 2018 Jonathan Marek <jonathan@marek.ca>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Jonathan Marek <jonathan@marek.ca>
25 */
26
27 /* some operations can only be scalar on a2xx:
28 * rsq, rcp, log2, exp2, cos, sin, sqrt
29 * mostly copy-pasted from nir_lower_alu_to_scalar.c
30 */
31
32 #include "ir2_private.h"
33 #include "compiler/nir/nir_builder.h"
34
35 static void
36 nir_alu_ssa_dest_init(nir_alu_instr * instr, unsigned num_components,
37 unsigned bit_size)
38 {
39 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components,
40 bit_size, NULL);
41 instr->dest.write_mask = (1 << num_components) - 1;
42 }
43
44 static void
45 lower_reduction(nir_alu_instr * instr, nir_op chan_op, nir_op merge_op,
46 nir_builder * builder)
47 {
48 unsigned num_components = nir_op_infos[instr->op].input_sizes[0];
49
50 nir_ssa_def *last = NULL;
51 for (unsigned i = 0; i < num_components; i++) {
52 nir_alu_instr *chan =
53 nir_alu_instr_create(builder->shader, chan_op);
54 nir_alu_ssa_dest_init(chan, 1, instr->dest.dest.ssa.bit_size);
55 nir_alu_src_copy(&chan->src[0], &instr->src[0], chan);
56 chan->src[0].swizzle[0] = chan->src[0].swizzle[i];
57 if (nir_op_infos[chan_op].num_inputs > 1) {
58 assert(nir_op_infos[chan_op].num_inputs == 2);
59 nir_alu_src_copy(&chan->src[1], &instr->src[1], chan);
60 chan->src[1].swizzle[0] = chan->src[1].swizzle[i];
61 }
62 chan->exact = instr->exact;
63
64 nir_builder_instr_insert(builder, &chan->instr);
65
66 if (i == 0) {
67 last = &chan->dest.dest.ssa;
68 } else {
69 last = nir_build_alu(builder, merge_op,
70 last, &chan->dest.dest.ssa, NULL, NULL);
71 }
72 }
73
74 assert(instr->dest.write_mask == 1);
75 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(last));
76 nir_instr_remove(&instr->instr);
77 }
78
79 static bool lower_scalar(nir_alu_instr * instr, nir_builder * b)
80 {
81 assert(instr->dest.dest.is_ssa);
82 assert(instr->dest.write_mask != 0);
83
84 b->cursor = nir_before_instr(&instr->instr);
85 b->exact = instr->exact;
86
87 #define LOWER_REDUCTION(name, chan, merge) \
88 case name##2: \
89 case name##3: \
90 case name##4: \
91 lower_reduction(instr, chan, merge, b); \
92 return true;
93
94 switch (instr->op) {
95 /* TODO: handle these instead of lowering */
96 LOWER_REDUCTION(nir_op_fall_equal, nir_op_seq, nir_op_fmin);
97 LOWER_REDUCTION(nir_op_fany_nequal, nir_op_sne, nir_op_fmax);
98
99 default:
100 return false;
101 case nir_op_frsq:
102 case nir_op_frcp:
103 case nir_op_flog2:
104 case nir_op_fexp2:
105 case nir_op_fcos:
106 case nir_op_fsin:
107 case nir_op_fsqrt:
108 break;
109 }
110
111 assert(nir_op_infos[instr->op].num_inputs == 1);
112
113 unsigned num_components = instr->dest.dest.ssa.num_components;
114 nir_ssa_def *comps[NIR_MAX_VEC_COMPONENTS] = { NULL };
115 unsigned chan;
116
117 if (num_components == 1)
118 return false;
119
120 for (chan = 0; chan < num_components; chan++) {
121 assert(instr->dest.write_mask & (1 << chan));
122
123 nir_alu_instr *lower = nir_alu_instr_create(b->shader, instr->op);
124
125 nir_alu_src_copy(&lower->src[0], &instr->src[0], lower);
126 lower->src[0].swizzle[0] = instr->src[0].swizzle[chan];
127
128 nir_alu_ssa_dest_init(lower, 1, instr->dest.dest.ssa.bit_size);
129 lower->dest.saturate = instr->dest.saturate;
130 comps[chan] = &lower->dest.dest.ssa;
131 lower->exact = instr->exact;
132
133 nir_builder_instr_insert(b, &lower->instr);
134 }
135
136 nir_ssa_def *vec = nir_vec(b, comps, num_components);
137
138 nir_ssa_def_rewrite_uses(&instr->dest.dest.ssa, nir_src_for_ssa(vec));
139
140 nir_instr_remove(&instr->instr);
141 return true;
142 }
143
144 static bool lower_scalar_impl(nir_function_impl * impl)
145 {
146 nir_builder builder;
147 nir_builder_init(&builder, impl);
148 bool progress = false;
149
150 nir_foreach_block(block, impl) {
151 nir_foreach_instr_safe(instr, block) {
152 if (instr->type == nir_instr_type_alu)
153 progress = lower_scalar(nir_instr_as_alu(instr), &builder)
154 || progress;
155 }
156 }
157
158 nir_metadata_preserve(impl, nir_metadata_block_index |
159 nir_metadata_dominance);
160
161 return progress;
162 }
163
164 bool ir2_nir_lower_scalar(nir_shader * shader)
165 {
166 bool progress = false;
167
168 nir_foreach_function(function, shader) {
169 if (function->impl)
170 progress = lower_scalar_impl(function->impl) || progress;
171 }
172
173 return progress;
174 }