nir: Add a new memory_barrier_tcs_patch intrinsic
[mesa.git] / src / compiler / nir / nir_lower_bit_size.c
1 /*
2 * Copyright © 2018 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_builder.h"
25
26 /**
27 * Some ALU operations may not be supported in hardware in specific bit-sizes.
28 * This pass allows implementations to selectively lower such operations to
29 * a bit-size that is supported natively and then converts the result back to
30 * the original bit-size.
31 */
32 static nir_ssa_def *
33 convert_to_bit_size(nir_builder *bld,
34 nir_ssa_def *src,
35 nir_alu_type type,
36 unsigned bit_size)
37 {
38 nir_alu_type base_type = nir_alu_type_get_base_type(type);
39 nir_alu_type lowered_type = bit_size | base_type;
40
41 nir_op opcode =
42 nir_type_conversion_op(type, lowered_type, nir_rounding_mode_undef);
43
44 return nir_build_alu(bld, opcode, src, NULL, NULL, NULL);
45 }
46
47 static void
48 lower_instr(nir_builder *bld, nir_alu_instr *alu, unsigned bit_size)
49 {
50 const nir_op op = alu->op;
51
52 bld->cursor = nir_before_instr(&alu->instr);
53
54 /* Convert each source to the requested bit-size */
55 nir_ssa_def *srcs[4] = { NULL, NULL, NULL, NULL };
56 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++) {
57 nir_ssa_def *src = nir_ssa_for_alu_src(bld, alu, i);
58
59 nir_alu_type type = nir_op_infos[op].input_types[i];
60 if (nir_alu_type_get_type_size(type) == 0)
61 srcs[i] = convert_to_bit_size(bld, src, type, bit_size);
62 else
63 srcs[i] = src;
64 }
65
66 /* Emit the lowered ALU instruction */
67 nir_ssa_def *lowered_dst =
68 nir_build_alu(bld, op, srcs[0], srcs[1], srcs[2], srcs[3]);
69
70 /* Convert result back to the original bit-size */
71 unsigned dst_bit_size = alu->dest.dest.ssa.bit_size;
72 nir_alu_type type = nir_op_infos[op].output_type;
73 nir_ssa_def *dst = convert_to_bit_size(bld, lowered_dst, type, dst_bit_size);
74 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(dst));
75 }
76
77 static bool
78 lower_impl(nir_function_impl *impl,
79 nir_lower_bit_size_callback callback,
80 void *callback_data)
81 {
82 nir_builder b;
83 nir_builder_init(&b, impl);
84 bool progress = false;
85
86 nir_foreach_block(block, impl) {
87 nir_foreach_instr_safe(instr, block) {
88 if (instr->type != nir_instr_type_alu)
89 continue;
90
91 nir_alu_instr *alu = nir_instr_as_alu(instr);
92 assert(alu->dest.dest.is_ssa);
93
94 unsigned lower_bit_size = callback(alu, callback_data);
95 if (lower_bit_size == 0)
96 continue;
97
98 assert(lower_bit_size != alu->dest.dest.ssa.bit_size);
99
100 lower_instr(&b, alu, lower_bit_size);
101 progress = true;
102 }
103 }
104
105 if (progress) {
106 nir_metadata_preserve(impl, nir_metadata_block_index |
107 nir_metadata_dominance);
108 }
109
110 return progress;
111 }
112
113 bool
114 nir_lower_bit_size(nir_shader *shader,
115 nir_lower_bit_size_callback callback,
116 void *callback_data)
117 {
118 bool progress = false;
119
120 nir_foreach_function(function, shader) {
121 if (function->impl)
122 progress |= lower_impl(function->impl, callback, callback_data);
123 }
124
125 return progress;
126 }