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 "intel_nir.h"
25 #include "compiler/nir/nir_builder.h"
26
27 static void
split_conversion(nir_builder * b,nir_alu_instr * alu,nir_alu_type src_type,nir_alu_type tmp_type,nir_alu_type dst_type)28 split_conversion(nir_builder *b, nir_alu_instr *alu, nir_alu_type src_type,
29 nir_alu_type tmp_type, nir_alu_type dst_type)
30 {
31 b->cursor = nir_before_instr(&alu->instr);
32 nir_def *src = nir_ssa_for_alu_src(b, alu, 0);
33 nir_def *tmp = nir_type_convert(b, src, src_type, tmp_type, nir_rounding_mode_undef);
34 nir_def *res = nir_type_convert(b, tmp, tmp_type, dst_type, nir_rounding_mode_undef);
35 nir_def_replace(&alu->def, res);
36 }
37
38 static bool
lower_alu_instr(nir_builder * b,nir_alu_instr * alu)39 lower_alu_instr(nir_builder *b, nir_alu_instr *alu)
40 {
41 unsigned src_bit_size = nir_src_bit_size(alu->src[0].src);
42 nir_alu_type src_type = nir_op_infos[alu->op].input_types[0];
43 nir_alu_type src_full_type = (nir_alu_type) (src_type | src_bit_size);
44
45 unsigned dst_bit_size = alu->def.bit_size;
46 nir_alu_type dst_full_type = nir_op_infos[alu->op].output_type;
47 nir_alu_type dst_type = nir_alu_type_get_base_type(dst_full_type);
48
49 /* BDW PRM, vol02, Command Reference Instructions, mov - MOVE:
50 *
51 * "There is no direct conversion from HF to DF or DF to HF.
52 * Use two instructions and F (Float) as an intermediate type.
53 *
54 * There is no direct conversion from HF to Q/UQ or Q/UQ to HF.
55 * Use two instructions and F (Float) or a word integer type
56 * or a DWord integer type as an intermediate type."
57 *
58 * It is important that the intermediate conversion happens through a
59 * 32-bit float type so we don't lose range when we convert from
60 * a 64-bit integer.
61 */
62 if ((src_full_type == nir_type_float16 && dst_bit_size == 64) ||
63 (src_bit_size == 64 && dst_full_type == nir_type_float16)) {
64 split_conversion(b, alu, src_type, nir_type_float | 32,
65 dst_type | dst_bit_size);
66 return true;
67 }
68
69 /* SKL PRM, vol 02a, Command Reference: Instructions, Move:
70 *
71 * "There is no direct conversion from B/UB to DF or DF to B/UB. Use
72 * two instructions and a word or DWord intermediate type."
73 *
74 * "There is no direct conversion from B/UB to Q/UQ or Q/UQ to B/UB.
75 * Use two instructions and a word or DWord intermediate integer
76 * type."
77 *
78 * It is important that we use a 32-bit integer matching the sign of the
79 * destination as the intermediate type so we avoid any chance of rtne
80 * rounding happening before the conversion to integer (which is expected
81 * to round towards zero) in double to byte conversions.
82 */
83 if ((src_bit_size == 8 && dst_bit_size == 64) ||
84 (src_bit_size == 64 && dst_bit_size == 8)) {
85 split_conversion(b, alu, src_type, dst_type | 32, dst_type | dst_bit_size);
86 return true;
87 }
88
89 return false;
90 }
91
92 static bool
lower_instr(nir_builder * b,nir_instr * instr,UNUSED void * cb_data)93 lower_instr(nir_builder *b, nir_instr *instr, UNUSED void *cb_data)
94 {
95 if (instr->type != nir_instr_type_alu)
96 return false;
97
98 nir_alu_instr *alu = nir_instr_as_alu(instr);
99
100 if (!nir_op_infos[alu->op].is_conversion)
101 return false;
102
103 return lower_alu_instr(b, alu);
104 }
105
106 bool
intel_nir_lower_conversions(nir_shader * shader)107 intel_nir_lower_conversions(nir_shader *shader)
108 {
109 return nir_shader_instructions_pass(shader, lower_instr,
110 nir_metadata_control_flow,
111 NULL);
112 }
113