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
30static bool
31lower_printf_instr(nir_builder *b, nir_instr *instr, void *_options)
32{
33   const nir_lower_printf_options *options = _options;
34   if (instr->type != nir_instr_type_intrinsic)
35      return false;
36
37   nir_intrinsic_instr *prntf = nir_instr_as_intrinsic(instr);
38   if (prntf->intrinsic != nir_intrinsic_printf)
39      return false;
40
41   nir_ssa_def *fmt_str_id = prntf->src[0].ssa;
42   nir_deref_instr *args = nir_src_as_deref(prntf->src[1]);
43   assert(args->deref_type == nir_deref_type_var);
44
45   const unsigned ptr_bit_size = nir_get_ptr_bitsize(b->shader);
46
47   /* Atomic add a buffer size counter to determine where to write.  If
48    * overflowed, return -1, otherwise, store the arguments and return 0.
49    */
50   b->cursor = nir_before_instr(&prntf->instr);
51   nir_ssa_def *buffer_addr = nir_load_printf_buffer_address(b, ptr_bit_size);
52   nir_deref_instr *buffer =
53      nir_build_deref_cast(b, buffer_addr, nir_var_mem_global,
54                           glsl_array_type(glsl_uint8_t_type(), 0, 4), 0);
55
56   /* Align the struct size to 4 */
57   assert(glsl_type_is_struct_or_ifc(args->type));
58   int args_size = align(glsl_get_cl_size(args->type), 4);
59   assert(fmt_str_id->bit_size == 32);
60   int fmt_str_id_size = 4;
61
62   /* Increment the counter at the beginning of the buffer */
63   const unsigned counter_size = 4;
64   nir_deref_instr *counter = nir_build_deref_array_imm(b, buffer, 0);
65   counter = nir_build_deref_cast(b, &counter->dest.ssa,
66                                  nir_var_mem_global,
67                                  glsl_uint_type(), 0);
68   counter->cast.align_mul = 4;
69   nir_ssa_def *offset =
70      nir_deref_atomic_add(b, 32, &counter->dest.ssa,
71                           nir_imm_int(b, fmt_str_id_size + args_size));
72
73   /* Check if we're still in-bounds */
74   const unsigned default_buffer_size = 1024 * 1024;
75   unsigned buffer_size = (options && options->max_buffer_size) ?
76                          options->max_buffer_size : default_buffer_size;
77   int max_valid_offset =
78      buffer_size - args_size - fmt_str_id_size - counter_size;
79   nir_push_if(b, nir_ilt(b, offset, nir_imm_int(b, max_valid_offset)));
80
81   nir_ssa_def *printf_succ_val = nir_imm_int(b, 0);
82
83   /* Write the format string ID */
84   nir_ssa_def *fmt_str_id_offset =
85      nir_i2i(b, offset, ptr_bit_size);
86   nir_deref_instr *fmt_str_id_deref =
87      nir_build_deref_array(b, buffer, fmt_str_id_offset);
88   fmt_str_id_deref = nir_build_deref_cast(b, &fmt_str_id_deref->dest.ssa,
89                                           nir_var_mem_global,
90                                           glsl_uint_type(), 0);
91   fmt_str_id_deref->cast.align_mul = 4;
92   nir_store_deref(b, fmt_str_id_deref, fmt_str_id, ~0);
93
94   /* Write the format args */
95   for (unsigned i = 0; i < glsl_get_length(args->type); ++i) {
96      nir_deref_instr *arg_deref = nir_build_deref_struct(b, args, i);
97      nir_ssa_def *arg = nir_load_deref(b, arg_deref);
98      const struct glsl_type *arg_type = arg_deref->type;
99
100      /* Clang does promotion of arguments to their "native" size. That means
101       * that any floats have been converted to doubles for the call to
102       * printf. Since doubles are optional, some drivers might not support
103       * them. For those drivers, convert them back to float before writing.
104       * Copy prop and other optimizations should remove all hints of doubles.
105       */
106      if (glsl_get_base_type(arg_type) == GLSL_TYPE_DOUBLE &&
107          options && options->treat_doubles_as_floats) {
108         arg = nir_f2f32(b, arg);
109         arg_type = glsl_float_type();
110      }
111
112      unsigned field_offset = glsl_get_struct_field_offset(args->type, i);
113      nir_ssa_def *arg_offset =
114         nir_i2i(b, nir_iadd_imm(b, offset,
115                                 fmt_str_id_size + field_offset),
116                 ptr_bit_size);
117      nir_deref_instr *dst_arg_deref =
118         nir_build_deref_array(b, buffer, arg_offset);
119      dst_arg_deref = nir_build_deref_cast(b, &dst_arg_deref->dest.ssa,
120                                           nir_var_mem_global, arg_type, 0);
121      assert(field_offset % 4 == 0);
122      dst_arg_deref->cast.align_mul = 4;
123      nir_store_deref(b, dst_arg_deref, arg, ~0);
124   }
125
126   nir_push_else(b, NULL);
127   nir_ssa_def *printf_fail_val = nir_imm_int(b, -1);
128   nir_pop_if(b, NULL);
129
130   nir_ssa_def *ret_val = nir_if_phi(b, printf_succ_val, printf_fail_val);
131   nir_ssa_def_rewrite_uses(&prntf->dest.ssa, ret_val);
132   nir_instr_remove(&prntf->instr);
133
134   return true;
135}
136
137bool
138nir_lower_printf(nir_shader *nir, const nir_lower_printf_options *options)
139{
140   return nir_shader_instructions_pass(nir, lower_printf_instr,
141                                       nir_metadata_none,
142                                       (void *)options);
143}
144