xref: /aosp_15_r20/external/mesa3d/src/compiler/nir/nir_lower_printf.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2020 Microsoft 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
21  * DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include "nir.h"
25 #include "nir_builder.h"
26 #include "nir_builder_opcodes.h"
27 
28 #include "util/u_math.h"
29 
30 static bool
lower_printf_intrin(nir_builder * b,nir_intrinsic_instr * prntf,void * _options)31 lower_printf_intrin(nir_builder *b, nir_intrinsic_instr *prntf, void *_options)
32 {
33    const nir_lower_printf_options *options = _options;
34    if (prntf->intrinsic != nir_intrinsic_printf)
35       return false;
36 
37    b->cursor = nir_before_instr(&prntf->instr);
38 
39    nir_def *fmt_str_id = prntf->src[0].ssa;
40    if (options && options->use_printf_base_identifier) {
41       fmt_str_id = nir_iadd(b,
42                             nir_load_printf_base_identifier(b),
43                             fmt_str_id);
44    }
45 
46    nir_deref_instr *args = nir_src_as_deref(prntf->src[1]);
47    assert(args->deref_type == nir_deref_type_var);
48 
49    const unsigned ptr_bit_size = options->ptr_bit_size != 0 ?
50       options->ptr_bit_size : nir_get_ptr_bitsize(b->shader);
51 
52    /* Atomic add a buffer size counter to determine where to write.  If
53     * overflowed, return -1, otherwise, store the arguments and return 0.
54     */
55    nir_def *buffer_addr = nir_load_printf_buffer_address(b, ptr_bit_size);
56    nir_deref_instr *buffer =
57       nir_build_deref_cast(b, buffer_addr, nir_var_mem_global,
58                            glsl_array_type(glsl_uint8_t_type(), 0, 4), 0);
59 
60    /* Align the struct size to 4 */
61    assert(glsl_type_is_struct_or_ifc(args->type));
62    int args_size = align(glsl_get_cl_size(args->type), 4);
63    assert(fmt_str_id->bit_size == 32);
64    int fmt_str_id_size = 4;
65 
66    /* Increment the counter at the beginning of the buffer */
67    const unsigned counter_size = 4;
68    nir_deref_instr *counter = nir_build_deref_array_imm(b, buffer, 0);
69    counter = nir_build_deref_cast(b, &counter->def,
70                                   nir_var_mem_global,
71                                   glsl_uint_type(), 0);
72    counter->cast.align_mul = 4;
73    nir_def *offset =
74       nir_deref_atomic(b, 32, &counter->def,
75                        nir_imm_int(b, fmt_str_id_size + args_size),
76                        .atomic_op = nir_atomic_op_iadd);
77 
78    /* Check if we're still in-bounds */
79    const unsigned default_buffer_size = 1024 * 1024;
80    unsigned buffer_size = (options && options->max_buffer_size) ? options->max_buffer_size : default_buffer_size;
81    int max_valid_offset =
82       buffer_size - args_size - fmt_str_id_size - counter_size;
83    nir_push_if(b, nir_ilt_imm(b, offset, max_valid_offset));
84 
85    nir_def *printf_succ_val = nir_imm_int(b, 0);
86 
87    /* Write the format string ID */
88    nir_def *fmt_str_id_offset =
89       nir_i2iN(b, offset, ptr_bit_size);
90    nir_deref_instr *fmt_str_id_deref =
91       nir_build_deref_array(b, buffer, fmt_str_id_offset);
92    fmt_str_id_deref = nir_build_deref_cast(b, &fmt_str_id_deref->def,
93                                            nir_var_mem_global,
94                                            glsl_uint_type(), 0);
95    fmt_str_id_deref->cast.align_mul = 4;
96    nir_store_deref(b, fmt_str_id_deref, fmt_str_id, ~0);
97 
98    /* Write the format args */
99    for (unsigned i = 0; i < glsl_get_length(args->type); ++i) {
100       nir_deref_instr *arg_deref = nir_build_deref_struct(b, args, i);
101       nir_def *arg = nir_load_deref(b, arg_deref);
102       const struct glsl_type *arg_type = arg_deref->type;
103 
104       unsigned field_offset = glsl_get_struct_field_offset(args->type, i);
105       nir_def *arg_offset =
106          nir_i2iN(b, nir_iadd_imm(b, offset, fmt_str_id_size + field_offset),
107                   ptr_bit_size);
108       nir_deref_instr *dst_arg_deref =
109          nir_build_deref_array(b, buffer, arg_offset);
110       dst_arg_deref = nir_build_deref_cast(b, &dst_arg_deref->def,
111                                            nir_var_mem_global, arg_type, 0);
112       assert(field_offset % 4 == 0);
113       dst_arg_deref->cast.align_mul = 4;
114       nir_store_deref(b, dst_arg_deref, arg, ~0);
115    }
116 
117    nir_push_else(b, NULL);
118    nir_def *printf_fail_val = nir_imm_int(b, -1);
119    nir_pop_if(b, NULL);
120 
121    nir_def *ret_val = nir_if_phi(b, printf_succ_val, printf_fail_val);
122    nir_def_replace(&prntf->def, ret_val);
123 
124    return true;
125 }
126 
127 bool
nir_lower_printf(nir_shader * nir,const nir_lower_printf_options * options)128 nir_lower_printf(nir_shader *nir, const nir_lower_printf_options *options)
129 {
130    return nir_shader_intrinsics_pass(nir, lower_printf_intrin,
131                                      nir_metadata_none,
132                                      (void *)options);
133 }
134 
135 void
nir_printf_fmt(nir_builder * b,bool use_printf_base_identifier,unsigned ptr_bit_size,const char * fmt,...)136 nir_printf_fmt(nir_builder *b,
137                bool use_printf_base_identifier,
138                unsigned ptr_bit_size,
139                const char *fmt, ...)
140 {
141    b->shader->printf_info_count++;
142    b->shader->printf_info = reralloc(b->shader,
143                                      b->shader->printf_info,
144                                      u_printf_info,
145                                      b->shader->printf_info_count);
146 
147    u_printf_info *info =
148       &b->shader->printf_info[b->shader->printf_info_count - 1];
149 
150    *info = (u_printf_info) {
151       .strings = ralloc_strdup(b->shader, fmt),
152       .string_size = strlen(fmt) + 1,
153    };
154 
155    va_list ap;
156    size_t pos = 0;
157    size_t args_size = 0;
158 
159    va_start(ap, fmt);
160    while ((pos = util_printf_next_spec_pos(fmt, pos)) != -1) {
161       unsigned arg_size;
162       switch (fmt[pos]) {
163       case 'c': arg_size = 1; break;
164       case 'd': arg_size = 4; break;
165       case 'e': arg_size = 4; break;
166       case 'E': arg_size = 4; break;
167       case 'f': arg_size = 4; break;
168       case 'F': arg_size = 4; break;
169       case 'G': arg_size = 4; break;
170       case 'a': arg_size = 4; break;
171       case 'A': arg_size = 4; break;
172       case 'i': arg_size = 4; break;
173       case 'u': arg_size = 4; break;
174       case 'x': arg_size = 4; break;
175       case 'X': arg_size = 4; break;
176       case 'p': arg_size = 8; break;
177       default:  unreachable("invalid");
178       }
179 
180       ASSERTED nir_def *def = va_arg(ap, nir_def*);
181       assert(def->bit_size / 8 == arg_size);
182 
183       info->num_args++;
184       info->arg_sizes = reralloc(b->shader,
185                                  info->arg_sizes,
186                                  unsigned, info->num_args);
187       info->arg_sizes[info->num_args - 1] = arg_size;
188 
189       args_size += arg_size;
190    }
191    va_end(ap);
192 
193    nir_def *buffer_addr =
194       nir_load_printf_buffer_address(
195          b, ptr_bit_size ? ptr_bit_size : nir_get_ptr_bitsize(b->shader));
196    nir_def *buffer_offset =
197       nir_global_atomic(b, 32, buffer_addr,
198                         nir_imm_int(b, args_size + sizeof(uint32_t)),
199                         .atomic_op = nir_atomic_op_iadd);
200 
201    /* Identifier */
202    nir_def *identifier =
203       use_printf_base_identifier ?
204       nir_iadd_imm(b,
205                    nir_load_printf_base_identifier(b),
206                    b->shader->printf_info_count) :
207       nir_imm_int(b, b->shader->printf_info_count);
208 
209    nir_def *store_addr =
210       nir_iadd(b, buffer_addr, nir_i2iN(b, buffer_offset,
211                                            buffer_addr->bit_size));
212    nir_store_global(b, store_addr, 4, identifier, 0x1);
213 
214    /* Arguments */
215    va_start(ap, fmt);
216    unsigned store_offset = sizeof(uint32_t);
217    for (unsigned a = 0; a < info->num_args; a++) {
218       nir_def *def = va_arg(ap, nir_def*);
219 
220       nir_store_global(b, nir_iadd_imm(b, store_addr, store_offset),
221                        4, def, 0x1);
222 
223       store_offset += info->arg_sizes[a];
224    }
225    va_end(ap);
226 }
227