xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/lima/ir/lima_nir_split_load_input.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2019 Vasily Khoruzhick <[email protected]>
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 
27 #include "lima_ir.h"
28 
29 static bool
lima_nir_split_load_input_instr(nir_builder * b,nir_instr * instr,UNUSED void * cb_data)30 lima_nir_split_load_input_instr(nir_builder *b,
31                                 nir_instr *instr,
32                                 UNUSED void *cb_data)
33 {
34    if (instr->type != nir_instr_type_alu)
35       return false;
36 
37    nir_alu_instr *alu = nir_instr_as_alu(instr);
38    if (alu->op != nir_op_mov)
39       return false;
40 
41    nir_def *ssa = alu->src[0].src.ssa;
42    if (ssa->parent_instr->type != nir_instr_type_intrinsic)
43       return false;
44 
45    nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(ssa->parent_instr);
46    if (intrin->intrinsic != nir_intrinsic_load_input)
47       return false;
48 
49    uint8_t swizzle = alu->src[0].swizzle[0];
50    int i;
51 
52    for (i = 1; i < alu->def.num_components; i++)
53       if (alu->src[0].swizzle[i] != (swizzle + i))
54          break;
55 
56    if (i != alu->def.num_components)
57       return false;
58 
59    /* mali4xx can't access unaligned vec3, don't split load input */
60    if (alu->def.num_components == 3 && swizzle > 0)
61       return false;
62 
63    /* mali4xx can't access unaligned vec2, don't split load input */
64    if (alu->def.num_components == 2 &&
65        swizzle != 0 && swizzle != 2)
66       return false;
67 
68    b->cursor = nir_before_instr(&intrin->instr);
69    nir_intrinsic_instr *new_intrin = nir_intrinsic_instr_create(
70                                           b->shader,
71                                           intrin->intrinsic);
72    nir_def_init(&new_intrin->instr, &new_intrin->def,
73                 alu->def.num_components, ssa->bit_size);
74    new_intrin->num_components = alu->def.num_components;
75    nir_intrinsic_set_base(new_intrin, nir_intrinsic_base(intrin));
76    nir_intrinsic_set_component(new_intrin, nir_intrinsic_component(intrin) + swizzle);
77    nir_intrinsic_set_dest_type(new_intrin, nir_intrinsic_dest_type(intrin));
78 
79    /* offset */
80    new_intrin->src[0] = nir_src_for_ssa(intrin->src[0].ssa);
81 
82    nir_builder_instr_insert(b, &new_intrin->instr);
83    nir_def_replace(&alu->def, &new_intrin->def);
84    return true;
85 }
86 
87 /* Replaces a single load of several packed varyings and number of movs with
88  * a number of loads of smaller size
89  */
90 bool
lima_nir_split_load_input(nir_shader * shader)91 lima_nir_split_load_input(nir_shader *shader)
92 {
93    return nir_shader_instructions_pass(shader, lima_nir_split_load_input_instr,
94                                        nir_metadata_control_flow,
95                                        NULL);
96 }
97