1 /*
2 * Copyright © 2020 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_builder.h"
26
27 static bool
lower_ubo_load_instr(nir_builder * b,nir_intrinsic_instr * load,UNUSED void * _data)28 lower_ubo_load_instr(nir_builder *b, nir_intrinsic_instr *load,
29 UNUSED void *_data)
30 {
31 if (load->intrinsic != nir_intrinsic_load_global_constant_offset &&
32 load->intrinsic != nir_intrinsic_load_global_constant_bounded)
33 return false;
34
35 b->cursor = nir_before_instr(&load->instr);
36
37 nir_def *base_addr = load->src[0].ssa;
38 nir_def *bound = NULL;
39 if (load->intrinsic == nir_intrinsic_load_global_constant_bounded)
40 bound = load->src[2].ssa;
41
42 unsigned bit_size = load->def.bit_size;
43 assert(bit_size >= 8 && bit_size % 8 == 0);
44 unsigned byte_size = bit_size / 8;
45
46 nir_def *val;
47 if (!nir_src_is_divergent(load->src[0]) && nir_src_is_const(load->src[1])) {
48 uint32_t offset = nir_src_as_uint(load->src[1]);
49
50 /* Things should be component-aligned. */
51 assert(offset % byte_size == 0);
52
53 assert(ANV_UBO_ALIGNMENT == 64);
54
55 unsigned suboffset = offset % 64;
56 uint64_t aligned_offset = offset - suboffset;
57
58 /* Load two just in case we go over a 64B boundary */
59 nir_def *data[2];
60 for (unsigned i = 0; i < 2; i++) {
61 nir_def *pred;
62 if (bound) {
63 pred = nir_igt_imm(b, bound, aligned_offset + i * 64 + 63);
64 } else {
65 pred = nir_imm_true(b);
66 }
67
68 nir_def *addr = nir_iadd_imm(b, base_addr,
69 aligned_offset + i * 64);
70
71 data[i] = nir_load_global_const_block_intel(b, 16, addr, pred);
72 }
73
74 val = nir_extract_bits(b, data, 2, suboffset * 8,
75 load->num_components, bit_size);
76 } else {
77 nir_def *offset = load->src[1].ssa;
78 nir_def *addr = nir_iadd(b, base_addr, nir_u2u64(b, offset));
79
80 if (bound) {
81 nir_def *zero = nir_imm_zero(b, load->num_components, bit_size);
82
83 unsigned load_size = byte_size * load->num_components;
84 nir_def *in_bounds =
85 nir_ilt(b, nir_iadd_imm(b, offset, load_size - 1), bound);
86
87 nir_push_if(b, in_bounds);
88
89 nir_def *load_val =
90 nir_build_load_global_constant(b, load->def.num_components,
91 load->def.bit_size, addr,
92 .access = nir_intrinsic_access(load),
93 .align_mul = nir_intrinsic_align_mul(load),
94 .align_offset = nir_intrinsic_align_offset(load));
95
96 nir_pop_if(b, NULL);
97
98 val = nir_if_phi(b, load_val, zero);
99 } else {
100 val = nir_build_load_global_constant(b, load->def.num_components,
101 load->def.bit_size, addr,
102 .access = nir_intrinsic_access(load),
103 .align_mul = nir_intrinsic_align_mul(load),
104 .align_offset = nir_intrinsic_align_offset(load));
105 }
106 }
107
108 nir_def_rewrite_uses(&load->def, val);
109 nir_instr_remove(&load->instr);
110
111 return true;
112 }
113
114 bool
anv_nir_lower_ubo_loads(nir_shader * shader)115 anv_nir_lower_ubo_loads(nir_shader *shader)
116 {
117 return nir_shader_intrinsics_pass(shader, lower_ubo_load_instr,
118 nir_metadata_none,
119 NULL);
120 }
121