1/*
2 * Copyright © 2019 Valve 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 */
24
25#include "nir.h"
26#include "util/debug.h"
27
28/* This pass removes information which is only useful for debugging,
29 * making cache hits from similar shaders more likely.
30 */
31
32static void
33strip_variable(nir_variable *var)
34{
35   var->name = NULL;
36
37   if (var->data.mode != nir_var_shader_in &&
38       var->data.mode != nir_var_shader_out) {
39      /* We assume that this is called after nir_lower_io(), at which point
40       * the original user-facing location is irrelevant except for inputs and
41       * outputs.
42       */
43      var->data.location = 0;
44   }
45}
46
47static void
48strip_register(nir_register *reg)
49{
50   reg->name = NULL;
51}
52
53static bool
54strip_def(nir_ssa_def *def, void *_unused)
55{
56   (void) _unused;
57   def->name = NULL;
58   return true;
59}
60
61static void
62strip_impl(nir_function_impl *impl)
63{
64   nir_index_ssa_defs(impl);
65
66   nir_foreach_variable(var, &impl->locals)
67      strip_variable(var);
68   nir_foreach_register(reg, &impl->registers)
69      strip_register(reg);
70   nir_foreach_block(block, impl) {
71      nir_foreach_instr(instr, block) {
72         nir_foreach_ssa_def(instr, strip_def, NULL);
73      }
74   }
75}
76
77void
78nir_strip(nir_shader *shader)
79{
80   static int should_strip = -1;
81   if (should_strip < 0)
82      should_strip = env_var_as_boolean("NIR_STRIP", true);
83   if (!should_strip)
84      return;
85
86   shader->info.name = NULL;
87   shader->info.label = NULL;
88
89   nir_foreach_variable(var, &shader->uniforms)
90      strip_variable(var);
91   nir_foreach_variable(var, &shader->inputs)
92      strip_variable(var);
93   nir_foreach_variable(var, &shader->outputs)
94      strip_variable(var);
95   nir_foreach_variable(var, &shader->system_values)
96      strip_variable(var);
97   nir_foreach_variable(var, &shader->globals)
98      strip_variable(var);
99
100   nir_foreach_function(func, shader) {
101      if (func->impl)
102         strip_impl(func->impl);
103   }
104}
105