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 nir_def *fmt_str_id = prntf->src[0].ssa;
38 nir_deref_instr *args = nir_src_as_deref(prntf->src[1]);
39 assert(args->deref_type == nir_deref_type_var);
40
41 const unsigned ptr_bit_size = nir_get_ptr_bitsize(b->shader);
42
43 /* Atomic add a buffer size counter to determine where to write. If
44 * overflowed, return -1, otherwise, store the arguments and return 0.
45 */
46 b->cursor = nir_before_instr(&prntf->instr);
47 nir_def *buffer_addr = nir_load_printf_buffer_address(b, ptr_bit_size);
48 nir_deref_instr *buffer =
49 nir_build_deref_cast(b, buffer_addr, nir_var_mem_global,
50 glsl_array_type(glsl_uint8_t_type(), 0, 4), 0);
51
52 /* Align the struct size to 4 */
53 assert(glsl_type_is_struct_or_ifc(args->type));
54 int args_size = align(glsl_get_cl_size(args->type), 4);
55 assert(fmt_str_id->bit_size == 32);
56 int fmt_str_id_size = 4;
57
58 /* Increment the counter at the beginning of the buffer */
59 const unsigned counter_size = 4;
60 nir_deref_instr *counter = nir_build_deref_array_imm(b, buffer, 0);
61 counter = nir_build_deref_cast(b, &counter->def,
62 nir_var_mem_global,
63 glsl_uint_type(), 0);
64 counter->cast.align_mul = 4;
65 nir_def *offset =
66 nir_deref_atomic(b, 32, &counter->def,
67 nir_imm_int(b, fmt_str_id_size + args_size),
68 .atomic_op = nir_atomic_op_iadd);
69
70 /* Check if we're still in-bounds */
71 const unsigned default_buffer_size = 1024 * 1024;
72 unsigned buffer_size = (options && options->max_buffer_size) ? options->max_buffer_size : default_buffer_size;
73 int max_valid_offset =
74 buffer_size - args_size - fmt_str_id_size - counter_size;
75 nir_push_if(b, nir_ilt_imm(b, offset, max_valid_offset));
76
77 nir_def *printf_succ_val = nir_imm_int(b, 0);
78
79 /* Write the format string ID */
80 nir_def *fmt_str_id_offset =
81 nir_i2iN(b, offset, ptr_bit_size);
82 nir_deref_instr *fmt_str_id_deref =
83 nir_build_deref_array(b, buffer, fmt_str_id_offset);
84 fmt_str_id_deref = nir_build_deref_cast(b, &fmt_str_id_deref->def,
85 nir_var_mem_global,
86 glsl_uint_type(), 0);
87 fmt_str_id_deref->cast.align_mul = 4;
88 nir_store_deref(b, fmt_str_id_deref, fmt_str_id, ~0);
89
90 /* Write the format args */
91 for (unsigned i = 0; i < glsl_get_length(args->type); ++i) {
92 nir_deref_instr *arg_deref = nir_build_deref_struct(b, args, i);
93 nir_def *arg = nir_load_deref(b, arg_deref);
94 const struct glsl_type *arg_type = arg_deref->type;
95
96 unsigned field_offset = glsl_get_struct_field_offset(args->type, i);
97 nir_def *arg_offset =
98 nir_i2iN(b, nir_iadd_imm(b, offset, fmt_str_id_size + field_offset),
99 ptr_bit_size);
100 nir_deref_instr *dst_arg_deref =
101 nir_build_deref_array(b, buffer, arg_offset);
102 dst_arg_deref = nir_build_deref_cast(b, &dst_arg_deref->def,
103 nir_var_mem_global, arg_type, 0);
104 assert(field_offset % 4 == 0);
105 dst_arg_deref->cast.align_mul = 4;
106 nir_store_deref(b, dst_arg_deref, arg, ~0);
107 }
108
109 nir_push_else(b, NULL);
110 nir_def *printf_fail_val = nir_imm_int(b, -1);
111 nir_pop_if(b, NULL);
112
113 nir_def *ret_val = nir_if_phi(b, printf_succ_val, printf_fail_val);
114 nir_def_rewrite_uses(&prntf->def, ret_val);
115 nir_instr_remove(&prntf->instr);
116
117 return true;
118 }
119
120 bool
nir_lower_printf(nir_shader * nir,const nir_lower_printf_options * options)121 nir_lower_printf(nir_shader *nir, const nir_lower_printf_options *options)
122 {
123 return nir_shader_intrinsics_pass(nir, lower_printf_intrin,
124 nir_metadata_none,
125 (void *)options);
126 }
127