2 * Copyright © 2018 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 * 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:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
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.
24 * @file iris_disk_cache.c
26 * Functions for interacting with the on-disk shader cache.
34 #include "compiler/blob.h"
35 #include "compiler/nir/nir.h"
36 #include "util/build_id.h"
37 #include "util/disk_cache.h"
38 #include "util/mesa-sha1.h"
40 #include "iris_context.h"
42 static bool debug
= false;
45 * Compute a disk cache key for the given uncompiled shader and NOS key.
48 iris_disk_cache_compute_key(struct disk_cache
*cache
,
49 const struct iris_uncompiled_shader
*ish
,
50 const void *orig_prog_key
,
51 uint32_t prog_key_size
,
54 gl_shader_stage stage
= ish
->nir
->info
.stage
;
56 /* Create a copy of the program key with program_string_id zeroed out.
57 * It's essentially random data which we don't want to include in our
58 * hashing and comparisons. We'll set a proper value on a cache hit.
60 union brw_any_prog_key prog_key
;
61 memcpy(&prog_key
, orig_prog_key
, prog_key_size
);
62 brw_prog_key_set_id(&prog_key
, stage
, 0);
64 uint8_t data
[sizeof(prog_key
) + sizeof(ish
->nir_sha1
)];
65 uint32_t data_size
= prog_key_size
+ sizeof(ish
->nir_sha1
);
67 memcpy(data
, ish
->nir_sha1
, sizeof(ish
->nir_sha1
));
68 memcpy(data
+ sizeof(ish
->nir_sha1
), &prog_key
, prog_key_size
);
70 disk_cache_compute_key(cache
, data
, data_size
, cache_key
);
74 * Store the given compiled shader in the disk cache.
76 * This should only be called on newly compiled shaders. No checking is
77 * done to prevent repeated stores of the same shader.
80 iris_disk_cache_store(struct disk_cache
*cache
,
81 const struct iris_uncompiled_shader
*ish
,
82 const struct iris_compiled_shader
*shader
,
84 uint32_t prog_key_size
)
86 #ifdef ENABLE_SHADER_CACHE
90 gl_shader_stage stage
= ish
->nir
->info
.stage
;
91 const struct brw_stage_prog_data
*prog_data
= shader
->prog_data
;
94 iris_disk_cache_compute_key(cache
, ish
, prog_key
, prog_key_size
, cache_key
);
98 _mesa_sha1_format(sha1
, cache_key
);
99 fprintf(stderr
, "[mesa disk cache] storing %s\n", sha1
);
105 /* We write the following data to the cache blob:
107 * 1. Prog data (must come first because it has the assembly size)
109 * 3. Number of entries in the system value array
110 * 4. System value array
111 * 5. Legacy param array (only used for compute workgroup ID)
113 blob_write_bytes(&blob
, shader
->prog_data
, brw_prog_data_size(stage
));
114 blob_write_bytes(&blob
, shader
->map
, shader
->prog_data
->program_size
);
115 blob_write_bytes(&blob
, &shader
->num_system_values
, sizeof(unsigned));
116 blob_write_bytes(&blob
, shader
->system_values
,
117 shader
->num_system_values
* sizeof(enum brw_param_builtin
));
118 blob_write_bytes(&blob
, prog_data
->param
,
119 prog_data
->nr_params
* sizeof(uint32_t));
121 disk_cache_put(cache
, cache_key
, blob
.data
, blob
.size
, NULL
);
127 * Search for a compiled shader in the disk cache. If found, upload it
128 * to the in-memory program cache so we can use it.
130 struct iris_compiled_shader
*
131 iris_disk_cache_retrieve(struct iris_context
*ice
,
132 const struct iris_uncompiled_shader
*ish
,
133 const void *prog_key
,
136 #ifdef ENABLE_SHADER_CACHE
137 struct iris_screen
*screen
= (void *) ice
->ctx
.screen
;
138 struct disk_cache
*cache
= screen
->disk_cache
;
139 gl_shader_stage stage
= ish
->nir
->info
.stage
;
145 iris_disk_cache_compute_key(cache
, ish
, prog_key
, key_size
, cache_key
);
149 _mesa_sha1_format(sha1
, cache_key
);
150 fprintf(stderr
, "[mesa disk cache] retrieving %s: ", sha1
);
154 void *buffer
= disk_cache_get(screen
->disk_cache
, cache_key
, &size
);
157 fprintf(stderr
, "%s\n", buffer
? "found" : "missing");
162 const uint32_t prog_data_size
= brw_prog_data_size(stage
);
164 struct brw_stage_prog_data
*prog_data
= ralloc_size(NULL
, prog_data_size
);
165 const void *assembly
;
166 uint32_t num_system_values
;
167 uint32_t *system_values
= NULL
;
168 uint32_t *so_decls
= NULL
;
170 struct blob_reader blob
;
171 blob_reader_init(&blob
, buffer
, size
);
172 blob_copy_bytes(&blob
, prog_data
, prog_data_size
);
173 assembly
= blob_read_bytes(&blob
, prog_data
->program_size
);
174 num_system_values
= blob_read_uint32(&blob
);
175 if (num_system_values
) {
177 ralloc_array(NULL
, enum brw_param_builtin
, num_system_values
);
178 blob_copy_bytes(&blob
, system_values
,
179 num_system_values
* sizeof(enum brw_param_builtin
));
182 prog_data
->param
= NULL
;
183 prog_data
->pull_param
= NULL
;
184 assert(prog_data
->nr_pull_params
== 0);
186 if (prog_data
->nr_params
) {
187 prog_data
->param
= ralloc_array(NULL
, uint32_t, prog_data
->nr_params
);
188 blob_copy_bytes(&blob
, prog_data
->param
,
189 prog_data
->nr_params
* sizeof(uint32_t));
192 if (stage
== MESA_SHADER_VERTEX
||
193 stage
== MESA_SHADER_TESS_EVAL
||
194 stage
== MESA_SHADER_GEOMETRY
) {
195 struct brw_vue_prog_data
*vue_prog_data
= (void *) prog_data
;
196 so_decls
= ice
->vtbl
.create_so_decl_list(&ish
->stream_output
,
197 &vue_prog_data
->vue_map
);
200 /* System values and uniforms are stored in constant buffer 0, the
201 * user-facing UBOs are indexed by one. So if any constant buffer is
202 * needed, the constant buffer 0 will be needed, so account for it.
204 unsigned num_cbufs
= ish
->nir
->info
.num_ubos
;
205 if (num_cbufs
|| num_system_values
|| ish
->nir
->num_uniforms
)
208 /* Upload our newly read shader to the in-memory program cache and
209 * return it to the caller.
211 return iris_upload_shader(ice
, stage
, key_size
, prog_key
, assembly
,
212 prog_data
, so_decls
, system_values
,
213 num_system_values
, num_cbufs
);
220 * Initialize the on-disk shader cache.
223 iris_disk_cache_init(struct iris_screen
*screen
)
225 #ifdef ENABLE_SHADER_CACHE
226 if (INTEL_DEBUG
& DEBUG_DISK_CACHE_DISABLE_MASK
)
229 /* array length = print length + nul char + 1 extra to verify it's unused */
232 snprintf(renderer
, sizeof(renderer
), "iris_%04x", screen
->pci_id
);
233 assert(len
== sizeof(renderer
) - 2);
235 const struct build_id_note
*note
=
236 build_id_find_nhdr_for_addr(iris_disk_cache_init
);
237 assert(note
&& build_id_length(note
) == 20); /* sha1 */
239 const uint8_t *id_sha1
= build_id_data(note
);
243 _mesa_sha1_format(timestamp
, id_sha1
);
245 const uint64_t driver_flags
=
246 brw_get_compiler_config_value(screen
->compiler
);
247 screen
->disk_cache
= disk_cache_create(renderer
, timestamp
, driver_flags
);