intel/blorp: Plumb the stage through blorp upload_shader
[mesa.git] / src / gallium / drivers / iris / iris_program_cache.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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 /**
24 * @file iris_program_cache.c
25 *
26 * The in-memory program cache. This is basically a hash table mapping
27 * API-specified shaders and a state key to a compiled variant. It also
28 * takes care of uploading shader assembly into a BO for use on the GPU.
29 */
30
31 #include <stdio.h>
32 #include <errno.h>
33 #include "pipe/p_defines.h"
34 #include "pipe/p_state.h"
35 #include "pipe/p_context.h"
36 #include "pipe/p_screen.h"
37 #include "util/u_atomic.h"
38 #include "util/u_upload_mgr.h"
39 #include "compiler/nir/nir.h"
40 #include "compiler/nir/nir_builder.h"
41 #include "intel/compiler/brw_compiler.h"
42 #include "intel/compiler/brw_eu.h"
43 #include "intel/compiler/brw_nir.h"
44 #include "iris_context.h"
45 #include "iris_resource.h"
46
47 struct keybox {
48 uint16_t size;
49 enum iris_program_cache_id cache_id;
50 uint8_t data[0];
51 };
52
53 static struct keybox *
54 make_keybox(void *mem_ctx,
55 enum iris_program_cache_id cache_id,
56 const void *key,
57 uint32_t key_size)
58 {
59 struct keybox *keybox =
60 ralloc_size(mem_ctx, sizeof(struct keybox) + key_size);
61
62 keybox->cache_id = cache_id;
63 keybox->size = key_size;
64 memcpy(keybox->data, key, key_size);
65
66 return keybox;
67 }
68
69 static uint32_t
70 keybox_hash(const void *void_key)
71 {
72 const struct keybox *key = void_key;
73 return _mesa_hash_data(&key->cache_id, key->size + sizeof(key->cache_id));
74 }
75
76 static bool
77 keybox_equals(const void *void_a, const void *void_b)
78 {
79 const struct keybox *a = void_a, *b = void_b;
80 if (a->size != b->size)
81 return false;
82
83 return memcmp(a->data, b->data, a->size) == 0;
84 }
85
86 struct iris_compiled_shader *
87 iris_find_cached_shader(struct iris_context *ice,
88 enum iris_program_cache_id cache_id,
89 uint32_t key_size,
90 const void *key)
91 {
92 struct keybox *keybox = make_keybox(NULL, cache_id, key, key_size);
93 struct hash_entry *entry =
94 _mesa_hash_table_search(ice->shaders.cache, keybox);
95
96 ralloc_free(keybox);
97
98 return entry ? entry->data : NULL;
99 }
100
101 const void *
102 iris_find_previous_compile(const struct iris_context *ice,
103 enum iris_program_cache_id cache_id,
104 unsigned program_string_id)
105 {
106 hash_table_foreach(ice->shaders.cache, entry) {
107 const struct keybox *keybox = entry->key;
108 const struct brw_base_prog_key *key = (const void *)keybox->data;
109 if (keybox->cache_id == cache_id &&
110 key->program_string_id == program_string_id) {
111 return keybox->data;
112 }
113 }
114
115 return NULL;
116 }
117
118 /**
119 * Look for an existing entry in the cache that has identical assembly code.
120 *
121 * This is useful for programs generating shaders at runtime, where multiple
122 * distinct shaders (from an API perspective) may compile to the same assembly
123 * in our backend. This saves space in the program cache buffer.
124 */
125 static const struct iris_compiled_shader *
126 find_existing_assembly(struct hash_table *cache,
127 const void *assembly,
128 unsigned assembly_size)
129 {
130 hash_table_foreach(cache, entry) {
131 const struct iris_compiled_shader *existing = entry->data;
132 if (existing->prog_data->program_size == assembly_size &&
133 memcmp(existing->map, assembly, assembly_size) == 0)
134 return existing;
135 }
136 return NULL;
137 }
138
139 struct iris_compiled_shader *
140 iris_upload_shader(struct iris_context *ice,
141 enum iris_program_cache_id cache_id,
142 uint32_t key_size,
143 const void *key,
144 const void *assembly,
145 struct brw_stage_prog_data *prog_data,
146 uint32_t *streamout,
147 enum brw_param_builtin *system_values,
148 unsigned num_system_values,
149 unsigned num_cbufs,
150 const struct iris_binding_table *bt)
151 {
152 struct hash_table *cache = ice->shaders.cache;
153 struct iris_compiled_shader *shader =
154 rzalloc_size(cache, sizeof(struct iris_compiled_shader) +
155 ice->vtbl.derived_program_state_size(cache_id));
156 const struct iris_compiled_shader *existing =
157 find_existing_assembly(cache, assembly, prog_data->program_size);
158
159 /* If we can find a matching prog in the cache already, then reuse the
160 * existing stuff without creating new copy into the underlying buffer
161 * object. This is notably useful for programs generating shaders at
162 * runtime, where multiple shaders may compile to the same thing in our
163 * backend.
164 */
165 if (existing) {
166 pipe_resource_reference(&shader->assembly.res, existing->assembly.res);
167 shader->assembly.offset = existing->assembly.offset;
168 shader->map = existing->map;
169 } else {
170 shader->assembly.res = NULL;
171 u_upload_alloc(ice->shaders.uploader, 0, prog_data->program_size, 64,
172 &shader->assembly.offset, &shader->assembly.res,
173 &shader->map);
174 memcpy(shader->map, assembly, prog_data->program_size);
175 }
176
177 shader->prog_data = prog_data;
178 shader->streamout = streamout;
179 shader->system_values = system_values;
180 shader->num_system_values = num_system_values;
181 shader->num_cbufs = num_cbufs;
182 shader->bt = *bt;
183
184 ralloc_steal(shader, shader->prog_data);
185 ralloc_steal(shader->prog_data, prog_data->param);
186 ralloc_steal(shader->prog_data, prog_data->pull_param);
187 ralloc_steal(shader, shader->streamout);
188 ralloc_steal(shader, shader->system_values);
189
190 /* Store the 3DSTATE shader packets and other derived state. */
191 ice->vtbl.store_derived_program_state(ice, cache_id, shader);
192
193 struct keybox *keybox = make_keybox(shader, cache_id, key, key_size);
194 _mesa_hash_table_insert(ice->shaders.cache, keybox, shader);
195
196 return shader;
197 }
198
199 bool
200 iris_blorp_lookup_shader(struct blorp_batch *blorp_batch,
201 const void *key, uint32_t key_size,
202 uint32_t *kernel_out, void *prog_data_out)
203 {
204 struct blorp_context *blorp = blorp_batch->blorp;
205 struct iris_context *ice = blorp->driver_ctx;
206 struct iris_batch *batch = blorp_batch->driver_batch;
207 struct iris_compiled_shader *shader =
208 iris_find_cached_shader(ice, IRIS_CACHE_BLORP, key_size, key);
209
210 if (!shader)
211 return false;
212
213 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
214 *kernel_out =
215 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
216 *((void **) prog_data_out) = shader->prog_data;
217
218 iris_use_pinned_bo(batch, bo, false);
219
220 return true;
221 }
222
223 bool
224 iris_blorp_upload_shader(struct blorp_batch *blorp_batch, uint32_t stage,
225 const void *key, uint32_t key_size,
226 const void *kernel, UNUSED uint32_t kernel_size,
227 const struct brw_stage_prog_data *prog_data_templ,
228 UNUSED uint32_t prog_data_size,
229 uint32_t *kernel_out, void *prog_data_out)
230 {
231 struct blorp_context *blorp = blorp_batch->blorp;
232 struct iris_context *ice = blorp->driver_ctx;
233 struct iris_batch *batch = blorp_batch->driver_batch;
234
235 void *prog_data = ralloc_size(NULL, prog_data_size);
236 memcpy(prog_data, prog_data_templ, prog_data_size);
237
238 struct iris_binding_table bt;
239 memset(&bt, 0, sizeof(bt));
240
241 struct iris_compiled_shader *shader =
242 iris_upload_shader(ice, IRIS_CACHE_BLORP, key_size, key, kernel,
243 prog_data, NULL, NULL, 0, 0, &bt);
244
245 struct iris_bo *bo = iris_resource_bo(shader->assembly.res);
246 *kernel_out =
247 iris_bo_offset_from_base_address(bo) + shader->assembly.offset;
248 *((void **) prog_data_out) = shader->prog_data;
249
250 iris_use_pinned_bo(batch, bo, false);
251
252 return true;
253 }
254
255 void
256 iris_init_program_cache(struct iris_context *ice)
257 {
258 ice->shaders.cache =
259 _mesa_hash_table_create(ice, keybox_hash, keybox_equals);
260
261 ice->shaders.uploader =
262 u_upload_create(&ice->ctx, 16384, PIPE_BIND_CUSTOM, PIPE_USAGE_IMMUTABLE,
263 IRIS_RESOURCE_FLAG_SHADER_MEMZONE);
264 }
265
266 void
267 iris_destroy_program_cache(struct iris_context *ice)
268 {
269 for (int i = 0; i < MESA_SHADER_STAGES; i++) {
270 ice->shaders.prog[i] = NULL;
271 }
272
273 hash_table_foreach(ice->shaders.cache, entry) {
274 struct iris_compiled_shader *shader = entry->data;
275 pipe_resource_reference(&shader->assembly.res, NULL);
276 }
277
278 u_upload_destroy(ice->shaders.uploader);
279
280 ralloc_free(ice->shaders.cache);
281 }
282
283 static const char *
284 cache_name(enum iris_program_cache_id cache_id)
285 {
286 if (cache_id == IRIS_CACHE_BLORP)
287 return "BLORP";
288
289 return _mesa_shader_stage_to_string(cache_id);
290 }
291
292 void
293 iris_print_program_cache(struct iris_context *ice)
294 {
295 struct iris_screen *screen = (struct iris_screen *)ice->ctx.screen;
296 const struct gen_device_info *devinfo = &screen->devinfo;
297
298 hash_table_foreach(ice->shaders.cache, entry) {
299 const struct keybox *keybox = entry->key;
300 struct iris_compiled_shader *shader = entry->data;
301 fprintf(stderr, "%s:\n", cache_name(keybox->cache_id));
302 brw_disassemble(devinfo, shader->map, 0,
303 shader->prog_data->program_size, stderr);
304 }
305 }