xref: /aosp_15_r20/external/mesa3d/src/nouveau/compiler/nak_nir_split_64bit_conversions.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2024 Collabora, Ltd.
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 /* Adapted from intel_nir_lower_conversions.c */
25 
26 #include "nak_private.h"
27 #include "nir_builder.h"
28 
29 static nir_rounding_mode
op_rounding_mode(nir_op op)30 op_rounding_mode(nir_op op)
31 {
32    switch (op) {
33    case nir_op_f2f16_rtne: return nir_rounding_mode_rtne;
34    case nir_op_f2f16_rtz: return nir_rounding_mode_rtz;
35    default: return nir_rounding_mode_undef;
36    }
37 }
38 
39 static bool
split_64bit_conversion(nir_builder * b,nir_instr * instr,UNUSED void * _data)40 split_64bit_conversion(nir_builder *b, nir_instr *instr, UNUSED void *_data)
41 {
42    if (instr->type != nir_instr_type_alu)
43       return false;
44 
45    nir_alu_instr *alu = nir_instr_as_alu(instr);
46 
47    if (!nir_op_infos[alu->op].is_conversion)
48       return false;
49 
50    unsigned src_bit_size = nir_src_bit_size(alu->src[0].src);
51    nir_alu_type src_type = nir_op_infos[alu->op].input_types[0];
52    nir_alu_type src_full_type = (nir_alu_type) (src_type | src_bit_size);
53 
54    unsigned dst_bit_size = alu->def.bit_size;
55    nir_alu_type dst_full_type = nir_op_infos[alu->op].output_type;
56    assert(nir_alu_type_get_type_size(dst_full_type) == dst_bit_size);
57    nir_alu_type dst_type = nir_alu_type_get_base_type(dst_full_type);
58 
59    /* We can't cross the 64-bit boundary in one conversion */
60    if ((src_bit_size <= 32 && dst_bit_size <= 32) ||
61        (src_bit_size >= 32 && dst_bit_size >= 32))
62       return false;
63 
64    nir_alu_type tmp_type;
65    if ((src_full_type == nir_type_float16 && dst_bit_size == 64) ||
66        (src_bit_size == 64 && dst_full_type == nir_type_float16)) {
67       /* It is important that the intermediate conversion happens through a
68        * 32-bit float type so we don't lose range when we convert to/from
69        * a 64-bit integer.
70        */
71       tmp_type = nir_type_float32;
72    } else {
73       /* For fp64 to integer conversions, using an integer intermediate type
74        * ensures that rounding happens as part of the first conversion,
75        * avoiding any chance of rtne rounding happening before the conversion
76        * to integer (which is expected to round towards zero).
77        *
78        * NOTE: NVIDIA hardware saturates conversions by default and the second
79        * conversion will not saturate in this case.  However, GLSL makes OOB
80        * values in conversions undefiend.
81        *
82        * For all other conversions, the conversion from int to int is either
83        * lossless or just as lossy as the final conversion.
84        */
85       tmp_type = dst_type | 32;
86    }
87 
88    b->cursor = nir_before_instr(&alu->instr);
89    nir_def *src = nir_ssa_for_alu_src(b, alu, 0);
90    nir_def *tmp = nir_type_convert(b, src, src_type, tmp_type,
91                                    nir_rounding_mode_undef);
92    nir_def *res = nir_type_convert(b, tmp, tmp_type, dst_full_type,
93                                    op_rounding_mode(alu->op));
94    nir_def_replace(&alu->def, res);
95 
96    return true;
97 }
98 
99 bool
nak_nir_split_64bit_conversions(nir_shader * nir)100 nak_nir_split_64bit_conversions(nir_shader *nir)
101 {
102    return nir_shader_instructions_pass(nir, split_64bit_conversion,
103                                        nir_metadata_control_flow,
104                                        NULL);
105 }
106