~mmach/netext73/mesa-ryzen

« back to all changes in this revision

Viewing changes to src/compiler/nir/nir_lower_to_source_mods.c

  • Committer: mmach
  • Date: 2023-11-02 21:31:35 UTC
  • Revision ID: netbit73@gmail.com-20231102213135-18d4tzh7tj0uz752
2023-11-02 22:11:57

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 */
23
23
 
24
24
#include "nir.h"
 
25
#include "nir_builder.h"
25
26
 
26
27
/*
27
28
 * This pass lowers the neg, abs, and sat operations to source modifiers on
44
45
}
45
46
 
46
47
static bool
47
 
nir_lower_to_source_mods_block(nir_block *block,
48
 
                               nir_lower_to_source_mods_flags options)
 
48
nir_lower_to_source_mods_instr(nir_builder *b, nir_instr *instr,
 
49
                               void *data)
49
50
{
 
51
   nir_lower_to_source_mods_flags options =
 
52
      *((nir_lower_to_source_mods_flags*) data);
 
53
 
50
54
   bool progress = false;
51
55
 
52
 
   nir_foreach_instr(instr, block) {
53
 
      if (instr->type != nir_instr_type_alu)
54
 
         continue;
55
 
 
56
 
      nir_alu_instr *alu = nir_instr_as_alu(instr);
57
 
 
58
 
      bool lower_abs = (nir_op_infos[alu->op].num_inputs < 3) ||
59
 
            (options & nir_lower_triop_abs);
60
 
 
61
 
      for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
62
 
         if (!alu->src[i].src.is_ssa)
63
 
            continue;
64
 
 
65
 
         if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
66
 
            continue;
67
 
 
68
 
         nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
69
 
 
70
 
         if (parent->dest.saturate)
71
 
            continue;
72
 
 
73
 
         switch (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i])) {
74
 
         case nir_type_float:
75
 
            if (!(options & nir_lower_float_source_mods))
76
 
               continue;
77
 
            if (!(parent->op == nir_op_fabs && (options & nir_lower_fabs_source_mods)) &&
78
 
                !(parent->op == nir_op_fneg && (options & nir_lower_fneg_source_mods))) {
79
 
               continue;
80
 
            }
81
 
            break;
82
 
         case nir_type_int:
83
 
            if (!(options & nir_lower_int_source_mods))
84
 
               continue;
85
 
            if (parent->op != nir_op_iabs && parent->op != nir_op_ineg)
86
 
               continue;
87
 
            break;
88
 
         default:
89
 
            continue;
90
 
         }
91
 
 
92
 
         if (nir_src_bit_size(alu->src[i].src) == 64 &&
93
 
             !(options & nir_lower_64bit_source_mods)) {
94
 
            continue;
95
 
         }
96
 
 
97
 
         /* We can only do a rewrite if the source we are copying is SSA.
98
 
          * Otherwise, moving the read might invalidly reorder reads/writes
99
 
          * on a register.
100
 
          */
101
 
         if (!parent->src[0].src.is_ssa)
102
 
            continue;
103
 
 
104
 
         if (!lower_abs && (parent->op == nir_op_fabs ||
105
 
                            parent->op == nir_op_iabs ||
106
 
                            parent->src[0].abs))
107
 
            continue;
108
 
 
109
 
         nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
110
 
 
111
 
         /* Apply any modifiers that come from the parent opcode */
112
 
         if (parent->op == nir_op_fneg || parent->op == nir_op_ineg)
113
 
            alu_src_consume_negate(&alu->src[i]);
114
 
         if (parent->op == nir_op_fabs || parent->op == nir_op_iabs)
115
 
            alu_src_consume_abs(&alu->src[i]);
116
 
 
117
 
         /* Apply modifiers from the parent source */
118
 
         if (parent->src[0].negate)
119
 
            alu_src_consume_negate(&alu->src[i]);
120
 
         if (parent->src[0].abs)
121
 
            alu_src_consume_abs(&alu->src[i]);
122
 
 
123
 
         for (int j = 0; j < 4; ++j) {
124
 
            if (!nir_alu_instr_channel_used(alu, i, j))
125
 
               continue;
126
 
            alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
127
 
         }
128
 
 
129
 
         if (nir_ssa_def_is_unused(&parent->dest.dest.ssa))
130
 
            nir_instr_remove(&parent->instr);
131
 
 
132
 
         progress = true;
 
56
   if (instr->type != nir_instr_type_alu)
 
57
      return false;
 
58
 
 
59
   nir_alu_instr *alu = nir_instr_as_alu(instr);
 
60
 
 
61
   bool lower_abs = (nir_op_infos[alu->op].num_inputs < 3) ||
 
62
         (options & nir_lower_triop_abs);
 
63
 
 
64
   for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
 
65
      if (!alu->src[i].src.is_ssa)
 
66
         continue;
 
67
 
 
68
      if (alu->src[i].src.ssa->parent_instr->type != nir_instr_type_alu)
 
69
         continue;
 
70
 
 
71
      nir_alu_instr *parent = nir_instr_as_alu(alu->src[i].src.ssa->parent_instr);
 
72
 
 
73
      if (parent->dest.saturate)
 
74
         continue;
 
75
 
 
76
      if (nir_alu_type_get_base_type(nir_op_infos[alu->op].input_types[i]) != nir_type_float)
 
77
         continue;
 
78
 
 
79
      if (!(parent->op == nir_op_fabs && (options & nir_lower_fabs_source_mods)) &&
 
80
          !(parent->op == nir_op_fneg && (options & nir_lower_fneg_source_mods))) {
 
81
         continue;
133
82
      }
134
83
 
135
 
      /* We've covered sources.  Now we're going to try and saturate the
136
 
       * destination if we can.
 
84
      if (nir_src_bit_size(alu->src[i].src) == 64)
 
85
         continue;
 
86
 
 
87
      /* We can only do a rewrite if the source we are copying is SSA.
 
88
       * Otherwise, moving the read might invalidly reorder reads/writes
 
89
       * on a register.
137
90
       */
138
 
 
139
 
      if (!alu->dest.dest.is_ssa)
140
 
         continue;
141
 
 
142
 
      if (nir_dest_bit_size(alu->dest.dest) == 64 &&
143
 
          !(options & nir_lower_64bit_source_mods)) {
144
 
         continue;
145
 
      }
146
 
 
147
 
      /* We can only saturate float destinations */
148
 
      if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
149
 
          nir_type_float)
150
 
         continue;
151
 
 
152
 
      if (!(options & nir_lower_float_source_mods))
153
 
         continue;
154
 
 
155
 
      bool all_children_are_sat = true;
156
 
      nir_foreach_use_including_if(child_src, &alu->dest.dest.ssa) {
157
 
         if (child_src->is_if) {
158
 
            all_children_are_sat = false;
159
 
            break;
160
 
         }
161
 
 
162
 
         assert(child_src->is_ssa);
163
 
         nir_instr *child = child_src->parent_instr;
164
 
         if (child->type != nir_instr_type_alu) {
165
 
            all_children_are_sat = false;
166
 
            continue;
167
 
         }
168
 
 
169
 
         nir_alu_instr *child_alu = nir_instr_as_alu(child);
170
 
         if (child_alu->src[0].negate || child_alu->src[0].abs) {
171
 
            all_children_are_sat = false;
172
 
            continue;
173
 
         }
174
 
 
175
 
         if (child_alu->op != nir_op_fsat) {
176
 
            all_children_are_sat = false;
177
 
            continue;
178
 
         }
179
 
      }
180
 
 
181
 
      if (!all_children_are_sat)
182
 
         continue;
183
 
 
184
 
      alu->dest.saturate = true;
 
91
      if (!parent->src[0].src.is_ssa)
 
92
         continue;
 
93
 
 
94
      if (!lower_abs && (parent->op == nir_op_fabs || parent->src[0].abs))
 
95
         continue;
 
96
 
 
97
      nir_instr_rewrite_src(instr, &alu->src[i].src, parent->src[0].src);
 
98
 
 
99
      /* Apply any modifiers that come from the parent opcode */
 
100
      if (parent->op == nir_op_fneg)
 
101
         alu_src_consume_negate(&alu->src[i]);
 
102
      if (parent->op == nir_op_fabs)
 
103
         alu_src_consume_abs(&alu->src[i]);
 
104
 
 
105
      /* Apply modifiers from the parent source */
 
106
      if (parent->src[0].negate)
 
107
         alu_src_consume_negate(&alu->src[i]);
 
108
      if (parent->src[0].abs)
 
109
         alu_src_consume_abs(&alu->src[i]);
 
110
 
 
111
      for (int j = 0; j < 4; ++j) {
 
112
         if (!nir_alu_instr_channel_used(alu, i, j))
 
113
            continue;
 
114
         alu->src[i].swizzle[j] = parent->src[0].swizzle[alu->src[i].swizzle[j]];
 
115
      }
 
116
 
 
117
      if (nir_ssa_def_is_unused(&parent->dest.dest.ssa))
 
118
         nir_instr_remove(&parent->instr);
 
119
 
185
120
      progress = true;
186
 
 
187
 
      nir_foreach_use(child_src, &alu->dest.dest.ssa) {
188
 
         assert(child_src->is_ssa);
189
 
         nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
190
 
 
191
 
         child_alu->op = nir_op_mov;
192
 
         child_alu->dest.saturate = false;
193
 
         /* We could propagate the dest of our instruction to the
194
 
          * destinations of the uses here.  However, one quick round of
195
 
          * copy propagation will clean that all up and then we don't have
196
 
          * the complexity.
197
 
          */
198
 
      }
199
 
   }
200
 
 
201
 
   return progress;
202
 
}
203
 
 
204
 
static bool
205
 
nir_lower_to_source_mods_impl(nir_function_impl *impl,
206
 
                              nir_lower_to_source_mods_flags options)
207
 
{
208
 
   bool progress = false;
209
 
 
210
 
   nir_foreach_block(block, impl) {
211
 
      progress |= nir_lower_to_source_mods_block(block, options);
212
 
   }
213
 
 
214
 
   if (progress)
215
 
      nir_metadata_preserve(impl, nir_metadata_block_index |
216
 
                                  nir_metadata_dominance);
 
121
   }
 
122
 
 
123
   /* We've covered sources.  Now we're going to try and saturate the
 
124
    * destination if we can.
 
125
    */
 
126
 
 
127
   if (!alu->dest.dest.is_ssa)
 
128
      return progress;
 
129
 
 
130
   if (nir_dest_bit_size(alu->dest.dest) == 64)
 
131
      return progress;
 
132
 
 
133
   /* We can only saturate float destinations */
 
134
   if (nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type) !=
 
135
       nir_type_float)
 
136
      return progress;
 
137
 
 
138
   bool all_children_are_sat = true;
 
139
   nir_foreach_use_including_if(child_src, &alu->dest.dest.ssa) {
 
140
      if (child_src->is_if) {
 
141
         all_children_are_sat = false;
 
142
         break;
 
143
      }
 
144
  
 
145
      assert(child_src->is_ssa);
 
146
      nir_instr *child = child_src->parent_instr;
 
147
      if (child->type != nir_instr_type_alu) {
 
148
         all_children_are_sat = false;
 
149
         continue;
 
150
      }
 
151
 
 
152
      nir_alu_instr *child_alu = nir_instr_as_alu(child);
 
153
      if (child_alu->src[0].negate || child_alu->src[0].abs) {
 
154
         all_children_are_sat = false;
 
155
         continue;
 
156
      }
 
157
 
 
158
      if (child_alu->op != nir_op_fsat) {
 
159
         all_children_are_sat = false;
 
160
         continue;
 
161
      }
 
162
   }
 
163
 
 
164
   if (!all_children_are_sat)
 
165
      return progress;
 
166
 
 
167
   alu->dest.saturate = true;
 
168
   progress = true;
 
169
 
 
170
   nir_foreach_use(child_src, &alu->dest.dest.ssa) {
 
171
      assert(child_src->is_ssa);
 
172
      nir_alu_instr *child_alu = nir_instr_as_alu(child_src->parent_instr);
 
173
 
 
174
      child_alu->op = nir_op_mov;
 
175
      child_alu->dest.saturate = false;
 
176
      /* We could propagate the dest of our instruction to the
 
177
       * destinations of the uses here.  However, one quick round of
 
178
       * copy propagation will clean that all up and then we don't have
 
179
       * the complexity.
 
180
       */
 
181
   }
217
182
 
218
183
   return progress;
219
184
}
222
187
nir_lower_to_source_mods(nir_shader *shader,
223
188
                         nir_lower_to_source_mods_flags options)
224
189
{
225
 
   bool progress = false;
226
 
 
227
 
   nir_foreach_function(function, shader) {
228
 
      if (function->impl) {
229
 
         progress |= nir_lower_to_source_mods_impl(function->impl, options);
230
 
      }
231
 
   }
232
 
 
233
 
   return progress;
 
190
   return nir_shader_instructions_pass(shader,
 
191
                                       nir_lower_to_source_mods_instr,
 
192
                                       nir_metadata_block_index |
 
193
                                       nir_metadata_dominance,
 
194
                                       &options);
234
195
}