2 * Copyright © 2017 Intel Corporation
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
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
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
26 #include "pipe/p_defines.h"
27 #include "pipe/p_state.h"
28 #include "pipe/p_context.h"
29 #include "pipe/p_screen.h"
30 #include "util/u_atomic.h"
31 #include "compiler/nir/nir.h"
32 #include "compiler/nir/nir_builder.h"
33 #include "intel/compiler/brw_compiler.h"
34 #include "intel/compiler/brw_eu.h"
35 #include "intel/compiler/brw_nir.h"
36 #include "iris_context.h"
40 enum iris_program_cache_id cache_id
;
44 static struct keybox
*
45 make_keybox(struct iris_program_cache
*cache
,
46 enum iris_program_cache_id cache_id
,
49 static const unsigned key_sizes
[] = {
50 [IRIS_CACHE_VS
] = sizeof(struct brw_vs_prog_key
),
51 [IRIS_CACHE_TCS
] = sizeof(struct brw_tcs_prog_key
),
52 [IRIS_CACHE_TES
] = sizeof(struct brw_tes_prog_key
),
53 [IRIS_CACHE_GS
] = sizeof(struct brw_gs_prog_key
),
54 [IRIS_CACHE_FS
] = sizeof(struct brw_wm_prog_key
),
55 [IRIS_CACHE_CS
] = sizeof(struct brw_cs_prog_key
),
56 //[IRIS_CACHE_BLORP_BLIT] = sizeof(struct brw_blorp_blit_prog_key),
59 struct keybox
*keybox
=
60 ralloc_size(cache
->table
, sizeof(struct keybox
) + key_sizes
[cache_id
]);
62 keybox
->cache_id
= cache_id
;
63 keybox
->size
= key_sizes
[cache_id
];
64 memcpy(keybox
->data
, key
, key_sizes
[cache_id
]);
70 keybox_hash(const void *void_key
)
72 const struct keybox
*key
= void_key
;
73 return _mesa_hash_data(&key
->cache_id
, key
->size
+ sizeof(key
->cache_id
));
77 keybox_equals(const void *void_a
, const void *void_b
)
79 const struct keybox
*a
= void_a
, *b
= void_b
;
80 if (a
->size
!= b
->size
)
83 return memcmp(a
->data
, b
->data
, a
->size
) == 0;
87 dirty_flag_for_cache(enum iris_program_cache_id cache_id
)
89 assert(cache_id
<= MESA_SHADER_STAGES
);
90 return IRIS_DIRTY_VS
<< cache_id
;
94 get_program_string_id(enum iris_program_cache_id cache_id
, const void *key
)
98 return ((struct brw_vs_prog_key
*) key
)->program_string_id
;
100 return ((struct brw_tcs_prog_key
*) key
)->program_string_id
;
102 return ((struct brw_tes_prog_key
*) key
)->program_string_id
;
104 return ((struct brw_gs_prog_key
*) key
)->program_string_id
;
106 return ((struct brw_cs_prog_key
*) key
)->program_string_id
;
108 return ((struct brw_wm_prog_key
*) key
)->program_string_id
;
110 unreachable("no program string id for this kind of program");
115 * Looks for a program in the cache and binds it.
117 * If no program was found, returns false and leaves the binding alone.
120 iris_bind_cached_shader(struct iris_context
*ice
,
121 enum iris_program_cache_id cache_id
,
124 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
126 struct keybox
*keybox
= make_keybox(cache
, cache_id
, key
);
128 struct hash_entry
*entry
= _mesa_hash_table_search(cache
->table
, keybox
);
133 struct iris_compiled_shader
*shader
= entry
->data
;
135 if (cache_id
<= MESA_SHADER_STAGES
&&
136 memcmp(shader
, ice
->shaders
.prog
[cache_id
], sizeof(*shader
)) != 0) {
137 ice
->shaders
.prog
[cache_id
] = shader
;
138 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
145 recreate_cache_bo(struct iris_context
*ice
, uint32_t size
)
147 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
148 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
149 struct iris_bo
*old_bo
= cache
->bo
;
150 void *old_map
= cache
->map
;
152 cache
->bo
= iris_bo_alloc(screen
->bufmgr
, "program cache", size
, 64);
153 cache
->bo
->kflags
= EXEC_OBJECT_CAPTURE
| EXEC_OBJECT_PINNED
;
154 cache
->map
= iris_bo_map(&ice
->dbg
, cache
->bo
,
155 MAP_READ
| MAP_WRITE
| MAP_ASYNC
| MAP_PERSISTENT
);
157 /* Copy any existing data that needs to be saved. */
159 perf_debug(&ice
->dbg
,
160 "Copying to larger program cache: %u kB -> %u kB\n",
161 (unsigned) old_bo
->size
/ 1024,
162 (unsigned) cache
->bo
->size
/ 1024);
164 /* Put the new BO just past the old one */
165 cache
->bo
->gtt_offset
= ALIGN(old_bo
->gtt_offset
+ old_bo
->size
, 4096);
167 memcpy(cache
->map
, old_map
, cache
->next_offset
);
169 iris_bo_unreference(old_bo
);
170 iris_bo_unmap(old_bo
);
172 /* Put the initial cache BO...somewhere. */
173 cache
->bo
->gtt_offset
= 4096 * 10;
176 ice
->state
.dirty
|= IRIS_DIRTY_STATE_BASE_ADDRESS
;
180 iris_find_previous_compile(struct iris_program_cache
*cache
,
181 enum iris_program_cache_id cache_id
,
182 unsigned program_string_id
)
184 hash_table_foreach(cache
->table
, entry
) {
185 const struct keybox
*keybox
= entry
->key
;
186 if (keybox
->cache_id
== cache_id
&&
187 get_program_string_id(cache_id
, keybox
->data
) == program_string_id
) {
196 * Look for an existing entry in the cache that has identical assembly code.
198 * This is useful for programs generating shaders at runtime, where multiple
199 * distinct shaders (from an API perspective) may compile to the same assembly
200 * in our backend. This saves space in the program cache buffer.
202 static const struct iris_compiled_shader
*
203 find_existing_assembly(const struct iris_program_cache
*cache
,
204 const void *assembly
,
205 unsigned assembly_size
)
207 hash_table_foreach(cache
->table
, entry
) {
208 const struct iris_compiled_shader
*existing
= entry
->data
;
209 if (existing
->prog_data
->program_size
== assembly_size
&&
210 memcmp(cache
->map
+ existing
->prog_offset
,
211 assembly
, assembly_size
) == 0)
218 upload_new_assembly(struct iris_context
*ice
,
219 const void *assembly
,
222 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
224 /* Allocate space in the cache BO for our new program. */
225 if (cache
->next_offset
+ size
> cache
->bo
->size
) {
226 uint32_t new_size
= cache
->bo
->size
* 2;
228 while (cache
->next_offset
+ size
> new_size
)
231 recreate_cache_bo(ice
, new_size
);
234 uint32_t offset
= cache
->next_offset
;
236 /* Programs are always 64-byte aligned, so set up the next one now */
237 cache
->next_offset
= ALIGN(offset
+ size
, 64);
239 /* Copy data to the buffer */
240 memcpy(cache
->map
+ offset
, assembly
, size
);
246 * Upload a new shader to the program cache, and bind it for use.
248 * \param prog_data must be ralloc'd and will be stolen.
251 iris_upload_and_bind_shader(struct iris_context
*ice
,
252 enum iris_program_cache_id cache_id
,
254 const void *assembly
,
255 struct brw_stage_prog_data
*prog_data
)
257 struct iris_screen
*screen
= (void *) ice
->ctx
.screen
;
258 struct gen_device_info
*devinfo
= &screen
->devinfo
;
259 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
260 struct iris_compiled_shader
*shader
=
261 ralloc_size(cache
->table
, sizeof(struct iris_compiled_shader
) +
262 ice
->state
.derived_program_state_size(cache_id
));
263 const struct iris_compiled_shader
*existing
=
264 find_existing_assembly(cache
, assembly
, prog_data
->program_size
);
266 /* If we can find a matching prog in the cache already, then reuse the
267 * existing stuff without creating new copy into the underlying buffer
268 * object. This is notably useful for programs generating shaders at
269 * runtime, where multiple shaders may compile to the same thing in our
273 shader
->prog_offset
= existing
->prog_offset
;
275 shader
->prog_offset
=
276 upload_new_assembly(ice
, assembly
, prog_data
->program_size
);
279 shader
->prog_data
= prog_data
;
281 ralloc_steal(shader
, shader
->prog_data
);
282 ralloc_steal(shader
->prog_data
, prog_data
->param
);
283 ralloc_steal(shader
->prog_data
, prog_data
->pull_param
);
285 /* Store the 3DSTATE shader packets and other derived state. */
286 ice
->state
.set_derived_program_state(devinfo
, cache_id
, shader
);
288 struct keybox
*keybox
= make_keybox(cache
, cache_id
, key
);
289 _mesa_hash_table_insert(cache
->table
, keybox
, shader
);
291 if (cache_id
<= MESA_SHADER_STAGES
) {
292 ice
->shaders
.prog
[cache_id
] = shader
;
293 ice
->state
.dirty
|= dirty_flag_for_cache(cache_id
);
298 iris_init_program_cache(struct iris_context
*ice
)
300 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
302 cache
->table
= _mesa_hash_table_create(ice
, keybox_hash
, keybox_equals
);
304 recreate_cache_bo(ice
, 16384);
308 iris_destroy_program_cache(struct iris_context
*ice
)
310 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
312 /* This can be NULL if context creation failed early on. */
314 iris_bo_unmap(cache
->bo
);
315 iris_bo_unreference(cache
->bo
);
320 cache
->next_offset
= 0;
322 for (int i
= 0; i
< MESA_SHADER_STAGES
; i
++) {
323 ice
->shaders
.prog
[i
] = NULL
;
326 ralloc_free(cache
->table
);
330 cache_name(enum iris_program_cache_id cache_id
)
332 if (cache_id
== IRIS_CACHE_BLORP_BLIT
)
335 return _mesa_shader_stage_to_string(cache_id
);
339 iris_print_program_cache(struct iris_context
*ice
)
341 struct iris_program_cache
*cache
= &ice
->shaders
.cache
;
342 struct iris_screen
*screen
= (struct iris_screen
*)ice
->ctx
.screen
;
343 const struct gen_device_info
*devinfo
= &screen
->devinfo
;
345 hash_table_foreach(cache
->table
, entry
) {
346 const struct keybox
*keybox
= entry
->key
;
347 struct iris_compiled_shader
*shader
= entry
->data
;
348 fprintf(stderr
, "%s:\n", cache_name(keybox
->cache_id
));
349 brw_disassemble(devinfo
, cache
->map
, shader
->prog_offset
,
350 shader
->prog_data
->program_size
, stderr
);