~mmach/netext73/mesa-haswell

« back to all changes in this revision

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

  • Committer: mmach
  • Date: 2022-09-22 19:56:13 UTC
  • Revision ID: netbit73@gmail.com-20220922195613-wtik9mmy20tmor0i
2022-09-22 21:17:09

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright © 2020 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 "nir.h"
25
 
#include "nir_builder.h"
26
 
#include "nir_conversion_builder.h"
27
 
 
28
 
static bool
29
 
try_simplify_convert_intrin(nir_intrinsic_instr *conv)
30
 
{
31
 
   bool progress = false;
32
 
 
33
 
   nir_alu_type src_type = nir_intrinsic_src_type(conv);
34
 
   nir_alu_type dest_type = nir_intrinsic_dest_type(conv);
35
 
 
36
 
   nir_rounding_mode rounding = nir_intrinsic_rounding_mode(conv);
37
 
   nir_rounding_mode simple_rounding =
38
 
      nir_simplify_conversion_rounding(src_type, dest_type, rounding);
39
 
   if (rounding != simple_rounding) {
40
 
      nir_intrinsic_set_rounding_mode(conv, simple_rounding);
41
 
      progress = true;
42
 
   }
43
 
 
44
 
   if (nir_intrinsic_saturate(conv) &&
45
 
       nir_alu_type_range_contains_type_range(dest_type, src_type)) {
46
 
      nir_intrinsic_set_saturate(conv, false);
47
 
      progress = true;
48
 
   }
49
 
 
50
 
   return progress;
51
 
}
52
 
 
53
 
static void
54
 
lower_convert_alu_types_instr(nir_builder *b, nir_intrinsic_instr *conv)
55
 
{
56
 
   assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
57
 
         assert(conv->src[0].is_ssa && conv->dest.is_ssa);
58
 
 
59
 
   b->cursor = nir_instr_remove(&conv->instr);
60
 
   nir_ssa_def *val =
61
 
      nir_convert_with_rounding(b, conv->src[0].ssa,
62
 
                                nir_intrinsic_src_type(conv),
63
 
                                nir_intrinsic_dest_type(conv),
64
 
                                nir_intrinsic_rounding_mode(conv),
65
 
                                nir_intrinsic_saturate(conv));
66
 
   nir_ssa_def_rewrite_uses(&conv->dest.ssa, val);
67
 
}
68
 
 
69
 
static bool
70
 
opt_simplify_convert_alu_types_impl(nir_function_impl *impl)
71
 
{
72
 
   bool progress = false;
73
 
   bool lowered_instr = false;
74
 
 
75
 
   nir_builder b;
76
 
   nir_builder_init(&b, impl);
77
 
 
78
 
   nir_foreach_block(block, impl) {
79
 
      nir_foreach_instr_safe(instr, block) {
80
 
         if (instr->type != nir_instr_type_intrinsic)
81
 
            continue;
82
 
 
83
 
         nir_intrinsic_instr *conv = nir_instr_as_intrinsic(instr);
84
 
         if (conv->intrinsic != nir_intrinsic_convert_alu_types)
85
 
            continue;
86
 
 
87
 
         if (try_simplify_convert_intrin(conv))
88
 
            progress = true;
89
 
 
90
 
         if (nir_intrinsic_rounding_mode(conv) == nir_rounding_mode_undef &&
91
 
             !nir_intrinsic_saturate(conv)) {
92
 
            lower_convert_alu_types_instr(&b, conv);
93
 
            lowered_instr = true;
94
 
         }
95
 
      }
96
 
   }
97
 
 
98
 
   if (lowered_instr) {
99
 
      nir_metadata_preserve(impl, nir_metadata_block_index |
100
 
                                  nir_metadata_dominance);
101
 
   } else {
102
 
      nir_metadata_preserve(impl, nir_metadata_all);
103
 
   }
104
 
 
105
 
   return progress;
106
 
}
107
 
 
108
 
bool
109
 
nir_opt_simplify_convert_alu_types(nir_shader *shader)
110
 
{
111
 
   bool progress = false;
112
 
 
113
 
   nir_foreach_function(func, shader) {
114
 
      if (func->impl && opt_simplify_convert_alu_types_impl(func->impl))
115
 
         progress = true;
116
 
   }
117
 
 
118
 
   return progress;
119
 
}
120
 
 
121
 
static bool
122
 
lower_convert_alu_types_impl(nir_function_impl *impl,
123
 
                             bool (*should_lower)(nir_intrinsic_instr *))
124
 
{
125
 
   bool progress = false;
126
 
 
127
 
   nir_builder b;
128
 
   nir_builder_init(&b, impl);
129
 
 
130
 
   nir_foreach_block(block, impl) {
131
 
      nir_foreach_instr_safe(instr, block) {
132
 
         if (instr->type != nir_instr_type_intrinsic)
133
 
            continue;
134
 
 
135
 
         nir_intrinsic_instr *conv = nir_instr_as_intrinsic(instr);
136
 
         if (conv->intrinsic != nir_intrinsic_convert_alu_types)
137
 
            continue;
138
 
 
139
 
         if (should_lower != NULL && !should_lower(conv))
140
 
            continue;
141
 
 
142
 
         lower_convert_alu_types_instr(&b, conv);
143
 
         progress = true;
144
 
      }
145
 
   }
146
 
 
147
 
   if (progress) {
148
 
      nir_metadata_preserve(impl, nir_metadata_block_index |
149
 
                                  nir_metadata_dominance);
150
 
   } else {
151
 
      nir_metadata_preserve(impl, nir_metadata_all);
152
 
   }
153
 
 
154
 
   return progress;
155
 
}
156
 
 
157
 
bool
158
 
nir_lower_convert_alu_types(nir_shader *shader,
159
 
                            bool (*should_lower)(nir_intrinsic_instr *))
160
 
{
161
 
   bool progress = false;
162
 
 
163
 
   nir_foreach_function(func, shader) {
164
 
      if (func->impl && lower_convert_alu_types_impl(func->impl, should_lower))
165
 
         progress = true;
166
 
   }
167
 
 
168
 
   return progress;
169
 
}
170
 
 
171
 
static bool
172
 
is_constant(nir_intrinsic_instr *conv)
173
 
{
174
 
   assert(conv->intrinsic == nir_intrinsic_convert_alu_types);
175
 
   return nir_src_is_const(conv->src[0]);
176
 
}
177
 
 
178
 
bool
179
 
nir_lower_constant_convert_alu_types(nir_shader *shader)
180
 
{
181
 
   return nir_lower_convert_alu_types(shader, is_constant);
182
 
}
183
 
 
184
 
static bool
185
 
is_alu_conversion(const nir_instr *instr, UNUSED const void *_data)
186
 
{
187
 
   return instr->type == nir_instr_type_alu &&
188
 
          nir_op_infos[nir_instr_as_alu(instr)->op].is_conversion;
189
 
}
190
 
 
191
 
static nir_ssa_def *
192
 
lower_alu_conversion(nir_builder *b, nir_instr *instr, UNUSED void *_data)
193
 
{
194
 
   nir_alu_instr *alu = nir_instr_as_alu(instr);
195
 
   nir_ssa_def *src = nir_ssa_for_alu_src(b, alu, 0);
196
 
   nir_alu_type src_type = nir_op_infos[alu->op].input_types[0] | src->bit_size;
197
 
   nir_alu_type dst_type = nir_op_infos[alu->op].output_type;
198
 
   return nir_convert_alu_types(b, alu->dest.dest.ssa.bit_size, src,
199
 
                                .src_type = src_type, .dest_type = dst_type,
200
 
                                .rounding_mode = nir_rounding_mode_undef,
201
 
                                .saturate = false);
202
 
}
203
 
 
204
 
bool
205
 
nir_lower_alu_conversion_to_intrinsic(nir_shader *shader)
206
 
{
207
 
   return nir_shader_lower_instructions(shader, is_alu_conversion,
208
 
                                        lower_alu_conversion, NULL);
209
 
}