1 /*
2 * Copyright (c) 2020 Intel Corporation
3 * Copyright (c) 2020 Collabora Ltd
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25 /*
26 * Lower scoped barriers embedding a control barrier (execusion_scope != NONE)
27 * to scoped_barriers-without-control-barrier + control_barrier.
28 */
29
30 #include "brw_nir.h"
31 #include "compiler/nir/nir_builder.h"
32
33 static bool
lower_impl(nir_function_impl * impl)34 lower_impl(nir_function_impl *impl)
35 {
36 nir_builder b;
37 nir_builder_init(&b, impl);
38 bool progress = false;
39
40 nir_foreach_block(block, impl) {
41 nir_foreach_instr_safe(instr, block) {
42 if (instr->type != nir_instr_type_intrinsic)
43 continue;
44
45 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
46
47 if (intr->intrinsic != nir_intrinsic_scoped_barrier ||
48 nir_intrinsic_execution_scope(intr) == NIR_SCOPE_NONE)
49 continue;
50
51 if (nir_intrinsic_execution_scope(intr) == NIR_SCOPE_WORKGROUP) {
52 b.cursor = nir_after_instr(&intr->instr);
53 nir_intrinsic_instr *cbarrier =
54 nir_intrinsic_instr_create(b.shader,
55 nir_intrinsic_control_barrier);
56 nir_builder_instr_insert(&b, &cbarrier->instr);
57 }
58
59 nir_intrinsic_set_execution_scope(intr, NIR_SCOPE_NONE);
60 progress = true;
61 }
62 }
63
64 if (progress) {
65 nir_metadata_preserve(impl, nir_metadata_block_index |
66 nir_metadata_dominance);
67 } else {
68 nir_metadata_preserve(impl, nir_metadata_all);
69 }
70
71 return progress;
72 }
73
74 bool
brw_nir_lower_scoped_barriers(nir_shader * nir)75 brw_nir_lower_scoped_barriers(nir_shader *nir)
76 {
77 bool progress = false;
78
79 nir_foreach_function(function, nir) {
80 if (function->impl)
81 progress |= lower_impl(function->impl);
82 }
83
84 return progress;
85 }
86