1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Jason Ekstrand (jason@jlekstrand.net)
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "nir.h"
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci/*
30bf215546Sopenharmony_ci * Handles management of the metadata.
31bf215546Sopenharmony_ci */
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_civoid
34bf215546Sopenharmony_cinir_metadata_require(nir_function_impl *impl, nir_metadata required, ...)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci#define NEEDS_UPDATE(X) ((required & ~impl->valid_metadata) & (X))
37bf215546Sopenharmony_ci
38bf215546Sopenharmony_ci   if (NEEDS_UPDATE(nir_metadata_block_index))
39bf215546Sopenharmony_ci      nir_index_blocks(impl);
40bf215546Sopenharmony_ci   if (NEEDS_UPDATE(nir_metadata_instr_index))
41bf215546Sopenharmony_ci      nir_index_instrs(impl);
42bf215546Sopenharmony_ci   if (NEEDS_UPDATE(nir_metadata_dominance))
43bf215546Sopenharmony_ci      nir_calc_dominance_impl(impl);
44bf215546Sopenharmony_ci   if (NEEDS_UPDATE(nir_metadata_live_ssa_defs))
45bf215546Sopenharmony_ci      nir_live_ssa_defs_impl(impl);
46bf215546Sopenharmony_ci   if (NEEDS_UPDATE(nir_metadata_loop_analysis)) {
47bf215546Sopenharmony_ci      va_list ap;
48bf215546Sopenharmony_ci      va_start(ap, required);
49bf215546Sopenharmony_ci      /* !! Warning !! Do not move these va_arg() call directly to
50bf215546Sopenharmony_ci       * nir_loop_analyze_impl() as parameters because the execution order will
51bf215546Sopenharmony_ci       * become undefined.
52bf215546Sopenharmony_ci       */
53bf215546Sopenharmony_ci      nir_variable_mode mode = va_arg(ap, nir_variable_mode);
54bf215546Sopenharmony_ci      int force_unroll_sampler_indirect = va_arg(ap, int);
55bf215546Sopenharmony_ci      nir_loop_analyze_impl(impl, mode, force_unroll_sampler_indirect);
56bf215546Sopenharmony_ci      va_end(ap);
57bf215546Sopenharmony_ci   }
58bf215546Sopenharmony_ci
59bf215546Sopenharmony_ci#undef NEEDS_UPDATE
60bf215546Sopenharmony_ci
61bf215546Sopenharmony_ci   impl->valid_metadata |= required;
62bf215546Sopenharmony_ci}
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_civoid
65bf215546Sopenharmony_cinir_metadata_preserve(nir_function_impl *impl, nir_metadata preserved)
66bf215546Sopenharmony_ci{
67bf215546Sopenharmony_ci   impl->valid_metadata &= preserved;
68bf215546Sopenharmony_ci}
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_civoid
71bf215546Sopenharmony_cinir_shader_preserve_all_metadata(nir_shader *shader)
72bf215546Sopenharmony_ci{
73bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
74bf215546Sopenharmony_ci      if (function->impl)
75bf215546Sopenharmony_ci         nir_metadata_preserve(function->impl, nir_metadata_all);
76bf215546Sopenharmony_ci   }
77bf215546Sopenharmony_ci}
78bf215546Sopenharmony_ci
79bf215546Sopenharmony_ci#ifndef NDEBUG
80bf215546Sopenharmony_ci/**
81bf215546Sopenharmony_ci * Make sure passes properly invalidate metadata (part 1).
82bf215546Sopenharmony_ci *
83bf215546Sopenharmony_ci * Call this before running a pass to set a bogus metadata flag, which will
84bf215546Sopenharmony_ci * only be preserved if the pass forgets to call nir_metadata_preserve().
85bf215546Sopenharmony_ci */
86bf215546Sopenharmony_civoid
87bf215546Sopenharmony_cinir_metadata_set_validation_flag(nir_shader *shader)
88bf215546Sopenharmony_ci{
89bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
90bf215546Sopenharmony_ci      if (function->impl) {
91bf215546Sopenharmony_ci         function->impl->valid_metadata |= nir_metadata_not_properly_reset;
92bf215546Sopenharmony_ci      }
93bf215546Sopenharmony_ci   }
94bf215546Sopenharmony_ci}
95bf215546Sopenharmony_ci
96bf215546Sopenharmony_ci/**
97bf215546Sopenharmony_ci * Make sure passes properly invalidate metadata (part 2).
98bf215546Sopenharmony_ci *
99bf215546Sopenharmony_ci * Call this after a pass makes progress to verify that the bogus metadata set by
100bf215546Sopenharmony_ci * the earlier function was properly thrown away.  Note that passes may not call
101bf215546Sopenharmony_ci * nir_metadata_preserve() if they don't actually make any changes at all.
102bf215546Sopenharmony_ci */
103bf215546Sopenharmony_civoid
104bf215546Sopenharmony_cinir_metadata_check_validation_flag(nir_shader *shader)
105bf215546Sopenharmony_ci{
106bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
107bf215546Sopenharmony_ci      if (function->impl) {
108bf215546Sopenharmony_ci         assert(!(function->impl->valid_metadata &
109bf215546Sopenharmony_ci                  nir_metadata_not_properly_reset));
110bf215546Sopenharmony_ci      }
111bf215546Sopenharmony_ci   }
112bf215546Sopenharmony_ci}
113bf215546Sopenharmony_ci#endif
114