summaryrefslogtreecommitdiff
path: root/prebuilt-intermediates/ir3/ir3_nir_trig.c
blob: 3d6358a6d0ff477039d64715847c3e9e1272f54d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include "ir3_nir.h"

#include "nir.h"
#include "nir_builder.h"
#include "nir_search.h"
#include "nir_search_helpers.h"

#ifndef NIR_OPT_ALGEBRAIC_STRUCT_DEFS
#define NIR_OPT_ALGEBRAIC_STRUCT_DEFS

struct transform {
   const nir_search_expression *search;
   const nir_search_value *replace;
   unsigned condition_offset;
};

#endif

   
static const nir_search_variable search0_0 = {
   { nir_search_value_variable, -1 },
   0, /* x */
   false,
   nir_type_invalid,
   NULL,
};
static const nir_search_expression search0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fsin,
   { &search0_0.value },
   NULL,
};
   
static const nir_search_constant replace0_0_0_0 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x401921fb3fa6defc /* 6.283185 */ },
};

static const nir_search_constant replace0_0_0_1_0_0_0 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x3fc45f30e7ff583a /* 0.159155 */ },
};

static const nir_search_variable replace0_0_0_1_0_0_1 = {
   { nir_search_value_variable, -1 },
   0, /* x */
   false,
   nir_type_invalid,
   NULL,
};
static const nir_search_expression replace0_0_0_1_0_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fmul,
   { &replace0_0_0_1_0_0_0.value, &replace0_0_0_1_0_0_1.value },
   NULL,
};

static const nir_search_constant replace0_0_0_1_0_1 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x3fe0000000000000 /* 0.5 */ },
};
static const nir_search_expression replace0_0_0_1_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fadd,
   { &replace0_0_0_1_0_0.value, &replace0_0_0_1_0_1.value },
   NULL,
};
static const nir_search_expression replace0_0_0_1 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_ffract,
   { &replace0_0_0_1_0.value },
   NULL,
};
static const nir_search_expression replace0_0_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fmul,
   { &replace0_0_0_0.value, &replace0_0_0_1.value },
   NULL,
};

static const nir_search_constant replace0_0_1 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x400921fb82c2bd7f /* 3.141593 */ },
};
static const nir_search_expression replace0_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fsub,
   { &replace0_0_0.value, &replace0_0_1.value },
   NULL,
};
static const nir_search_expression replace0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fsin,
   { &replace0_0.value },
   NULL,
};
   
static const nir_search_variable search1_0 = {
   { nir_search_value_variable, -1 },
   0, /* x */
   false,
   nir_type_invalid,
   NULL,
};
static const nir_search_expression search1 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fcos,
   { &search1_0.value },
   NULL,
};
   
static const nir_search_constant replace1_0_0_0 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x401921fb3fa6defc /* 6.283185 */ },
};

static const nir_search_constant replace1_0_0_1_0_0_0 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x3fc45f30e7ff583a /* 0.159155 */ },
};

static const nir_search_variable replace1_0_0_1_0_0_1 = {
   { nir_search_value_variable, -1 },
   0, /* x */
   false,
   nir_type_invalid,
   NULL,
};
static const nir_search_expression replace1_0_0_1_0_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fmul,
   { &replace1_0_0_1_0_0_0.value, &replace1_0_0_1_0_0_1.value },
   NULL,
};

static const nir_search_constant replace1_0_0_1_0_1 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x3fe0000000000000 /* 0.5 */ },
};
static const nir_search_expression replace1_0_0_1_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fadd,
   { &replace1_0_0_1_0_0.value, &replace1_0_0_1_0_1.value },
   NULL,
};
static const nir_search_expression replace1_0_0_1 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_ffract,
   { &replace1_0_0_1_0.value },
   NULL,
};
static const nir_search_expression replace1_0_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fmul,
   { &replace1_0_0_0.value, &replace1_0_0_1.value },
   NULL,
};

static const nir_search_constant replace1_0_1 = {
   { nir_search_value_constant, -1 },
   nir_type_float, { 0x400921fb82c2bd7f /* 3.141593 */ },
};
static const nir_search_expression replace1_0 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fsub,
   { &replace1_0_0.value, &replace1_0_1.value },
   NULL,
};
static const nir_search_expression replace1 = {
   { nir_search_value_expression, -1 },
   false,
   nir_op_fcos,
   { &replace1_0.value },
   NULL,
};

static const struct transform ir3_nir_apply_trig_workarounds_fcos_xforms[] = {
   { &search1, &replace1.value, 0 },
};
static const struct transform ir3_nir_apply_trig_workarounds_fsin_xforms[] = {
   { &search0, &replace0.value, 0 },
};

static bool
ir3_nir_apply_trig_workarounds_block(nir_builder *build, nir_block *block,
                   const bool *condition_flags)
{
   bool progress = false;

   nir_foreach_instr_reverse_safe(instr, block) {
      if (instr->type != nir_instr_type_alu)
         continue;

      nir_alu_instr *alu = nir_instr_as_alu(instr);
      if (!alu->dest.dest.is_ssa)
         continue;

      switch (alu->op) {
      case nir_op_fcos:
         for (unsigned i = 0; i < ARRAY_SIZE(ir3_nir_apply_trig_workarounds_fcos_xforms); i++) {
            const struct transform *xform = &ir3_nir_apply_trig_workarounds_fcos_xforms[i];
            if (condition_flags[xform->condition_offset] &&
                nir_replace_instr(build, alu, xform->search, xform->replace)) {
               progress = true;
               break;
            }
         }
         break;
      case nir_op_fsin:
         for (unsigned i = 0; i < ARRAY_SIZE(ir3_nir_apply_trig_workarounds_fsin_xforms); i++) {
            const struct transform *xform = &ir3_nir_apply_trig_workarounds_fsin_xforms[i];
            if (condition_flags[xform->condition_offset] &&
                nir_replace_instr(build, alu, xform->search, xform->replace)) {
               progress = true;
               break;
            }
         }
         break;
      default:
         break;
      }
   }

   return progress;
}

static bool
ir3_nir_apply_trig_workarounds_impl(nir_function_impl *impl, const bool *condition_flags)
{
   bool progress = false;

   nir_builder build;
   nir_builder_init(&build, impl);

   nir_foreach_block_reverse(block, impl) {
      progress |= ir3_nir_apply_trig_workarounds_block(&build, block, condition_flags);
   }

   if (progress) {
      nir_metadata_preserve(impl, nir_metadata_block_index |
                                  nir_metadata_dominance);
    } else {
#ifndef NDEBUG
      impl->valid_metadata &= ~nir_metadata_not_properly_reset;
#endif
    }

   return progress;
}


bool
ir3_nir_apply_trig_workarounds(nir_shader *shader)
{
   bool progress = false;
   bool condition_flags[1];
   const nir_shader_compiler_options *options = shader->options;
   (void) options;

   condition_flags[0] = true;

   nir_foreach_function(function, shader) {
      if (function->impl)
         progress |= ir3_nir_apply_trig_workarounds_impl(function->impl, condition_flags);
   }

   return progress;
}