intel/assembler: Add labels support
[mesa.git] / src / intel / vulkan / anv_nir_add_base_work_group_id.c
1 /*
2 * Copyright © 2017 Intel 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 #include "anv_nir.h"
25 #include "nir/nir_builder.h"
26 #include "compiler/brw_compiler.h"
27
28 bool
29 anv_nir_add_base_work_group_id(nir_shader *shader)
30 {
31 assert(shader->info.stage == MESA_SHADER_COMPUTE);
32
33 nir_builder b;
34 bool progress = false;
35 nir_foreach_function(function, shader) {
36 if (!function->impl)
37 continue;
38
39 nir_builder_init(&b, function->impl);
40
41 nir_foreach_block(block, function->impl) {
42 nir_foreach_instr_safe(instr, block) {
43 if (instr->type != nir_instr_type_intrinsic)
44 continue;
45
46 nir_intrinsic_instr *load_id = nir_instr_as_intrinsic(instr);
47 if (load_id->intrinsic != nir_intrinsic_load_work_group_id)
48 continue;
49
50 b.cursor = nir_after_instr(&load_id->instr);
51
52 nir_intrinsic_instr *load_base =
53 nir_intrinsic_instr_create(shader, nir_intrinsic_load_push_constant);
54 load_base->num_components = 3;
55 load_base->src[0] = nir_src_for_ssa(nir_imm_int(&b, 0));
56 nir_ssa_dest_init(&load_base->instr, &load_base->dest, 3, 32, NULL);
57 nir_intrinsic_set_base(load_base,
58 offsetof(struct anv_push_constants,
59 cs.base_work_group_id));
60 nir_intrinsic_set_range(load_base, 3 * sizeof(uint32_t));
61 nir_builder_instr_insert(&b, &load_base->instr);
62
63 nir_ssa_def *id = nir_iadd(&b, &load_id->dest.ssa,
64 &load_base->dest.ssa);
65
66 nir_ssa_def_rewrite_uses_after(&load_id->dest.ssa,
67 nir_src_for_ssa(id),
68 id->parent_instr);
69 progress = true;
70 }
71 }
72
73 nir_metadata_preserve(function->impl, nir_metadata_block_index |
74 nir_metadata_dominance);
75 }
76
77 return progress;
78 }