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_instr(nir_builder * b,nir_instr * instr,UNUSED void * cb_data)34 lower_instr(nir_builder *b, nir_instr *instr, UNUSED void *cb_data)
35 {
36    if (instr->type != nir_instr_type_intrinsic)
37       return false;
38 
39    nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
40 
41    if (intr->intrinsic != nir_intrinsic_scoped_barrier ||
42        nir_intrinsic_execution_scope(intr) == NIR_SCOPE_NONE)
43       return false;
44 
45    if (nir_intrinsic_execution_scope(intr) == NIR_SCOPE_WORKGROUP) {
46       b->cursor = nir_after_instr(&intr->instr);
47       nir_control_barrier(b);
48    }
49 
50    nir_intrinsic_set_execution_scope(intr, NIR_SCOPE_NONE);
51    return true;
52 }
53 
54 bool
brw_nir_lower_scoped_barriers(nir_shader * nir)55 brw_nir_lower_scoped_barriers(nir_shader *nir)
56 {
57    return nir_shader_instructions_pass(nir, lower_instr,
58                                        nir_metadata_block_index |
59                                        nir_metadata_dominance,
60                                        NULL);
61 }
62