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