1 /*
2 * Copyright 2023 Rose Hudson
3 * Copyright 2022 Amazon.com, Inc. or its affiliates.
4 * Copyright 2018 Intel Corporation
5 * SPDX-License-Identifier: MIT
6 */
7
8 #include <assert.h>
9 #include <stdio.h>
10
11 #include "asahi/compiler/agx_debug.h"
12 #include "compiler/shader_enums.h"
13 #include "util/blob.h"
14 #include "util/build_id.h"
15 #include "util/disk_cache.h"
16 #include "util/mesa-sha1.h"
17 #include "agx_bo.h"
18 #include "agx_device.h"
19 #include "agx_disk_cache.h"
20 #include "agx_state.h"
21
22 /* Flags that are allowed and do not disable the disk cache */
23 #define ALLOWED_FLAGS (AGX_DBG_NO16 | AGX_DBG_COMPBLIT)
24
25 /**
26 * Compute a disk cache key for the given uncompiled shader and shader key.
27 */
28 static void
agx_disk_cache_compute_key(struct disk_cache * cache,const struct agx_uncompiled_shader * uncompiled,const union asahi_shader_key * shader_key,cache_key cache_key)29 agx_disk_cache_compute_key(struct disk_cache *cache,
30 const struct agx_uncompiled_shader *uncompiled,
31 const union asahi_shader_key *shader_key,
32 cache_key cache_key)
33 {
34 uint8_t data[sizeof(uncompiled->nir_sha1) + sizeof(*shader_key)];
35 int hash_size = sizeof(uncompiled->nir_sha1);
36 int key_size;
37 if (uncompiled->type == PIPE_SHADER_VERTEX ||
38 uncompiled->type == PIPE_SHADER_TESS_EVAL)
39 key_size = sizeof(shader_key->vs);
40 else if (uncompiled->type == PIPE_SHADER_GEOMETRY)
41 key_size = sizeof(shader_key->gs);
42 else if (uncompiled->type == PIPE_SHADER_TESS_CTRL)
43 key_size = sizeof(shader_key->tcs);
44 else if (uncompiled->type == PIPE_SHADER_FRAGMENT)
45 key_size = sizeof(shader_key->fs);
46 else if (uncompiled->type == PIPE_SHADER_COMPUTE)
47 key_size = 0;
48 else
49 unreachable("Unsupported shader stage");
50
51 memcpy(data, uncompiled->nir_sha1, hash_size);
52
53 if (key_size)
54 memcpy(data + hash_size, shader_key, key_size);
55
56 disk_cache_compute_key(cache, data, hash_size + key_size, cache_key);
57 }
58
59 static void
write_shader(struct blob * blob,const struct agx_compiled_shader * binary,bool is_root_gs)60 write_shader(struct blob *blob, const struct agx_compiled_shader *binary,
61 bool is_root_gs)
62 {
63 uint32_t shader_size = binary->bo->size;
64 blob_write_uint32(blob, shader_size);
65 blob_write_bytes(blob, binary->bo->ptr.cpu, shader_size);
66 blob_write_bytes(blob, &binary->info, sizeof(binary->info));
67 blob_write_uint32(blob, binary->push_range_count);
68 blob_write_bytes(blob, binary->push,
69 sizeof(binary->push[0]) * binary->push_range_count);
70
71 if (is_root_gs) {
72 blob_write_uint32(blob, binary->gs_count_words);
73 blob_write_uint32(blob, binary->gs_output_mode);
74 write_shader(blob, binary->pre_gs, false);
75
76 blob_write_uint8(blob, binary->gs_copy != NULL);
77 if (binary->gs_copy)
78 write_shader(blob, binary->gs_copy, false);
79
80 blob_write_uint8(blob, binary->gs_count != NULL);
81 if (binary->gs_count)
82 write_shader(blob, binary->gs_count, false);
83 }
84 }
85
86 static struct agx_compiled_shader *
read_shader(struct agx_screen * screen,struct blob_reader * blob,const struct agx_uncompiled_shader * uncompiled,bool is_root)87 read_shader(struct agx_screen *screen, struct blob_reader *blob,
88 const struct agx_uncompiled_shader *uncompiled, bool is_root)
89 {
90 struct agx_compiled_shader *binary = CALLOC_STRUCT(agx_compiled_shader);
91 binary->stage = uncompiled->type;
92 binary->so = uncompiled;
93
94 uint32_t binary_size = blob_read_uint32(blob);
95 binary->bo = agx_bo_create(&screen->dev, binary_size,
96 AGX_BO_EXEC | AGX_BO_LOW_VA, "Executable");
97 blob_copy_bytes(blob, binary->bo->ptr.cpu, binary_size);
98
99 blob_copy_bytes(blob, &binary->info, sizeof(binary->info));
100 binary->push_range_count = blob_read_uint32(blob);
101 blob_copy_bytes(blob, binary->push,
102 sizeof(binary->push[0]) * binary->push_range_count);
103
104 if (is_root && uncompiled->type == PIPE_SHADER_GEOMETRY) {
105 binary->gs_count_words = blob_read_uint32(blob);
106 binary->gs_output_mode = blob_read_uint32(blob);
107 binary->pre_gs = read_shader(screen, blob, uncompiled, false);
108
109 if (blob_read_uint8(blob))
110 binary->gs_copy = read_shader(screen, blob, uncompiled, false);
111
112 if (blob_read_uint8(blob))
113 binary->gs_count = read_shader(screen, blob, uncompiled, false);
114 }
115
116 return binary;
117 }
118
119 /**
120 * Store the given compiled shader in the disk cache.
121 *
122 * This should only be called on newly compiled shaders. No checking is
123 * done to prevent repeated stores of the same shader.
124 */
125 void
agx_disk_cache_store(struct disk_cache * cache,const struct agx_uncompiled_shader * uncompiled,const union asahi_shader_key * key,const struct agx_compiled_shader * binary)126 agx_disk_cache_store(struct disk_cache *cache,
127 const struct agx_uncompiled_shader *uncompiled,
128 const union asahi_shader_key *key,
129 const struct agx_compiled_shader *binary)
130 {
131 #ifdef ENABLE_SHADER_CACHE
132 if (!cache)
133 return;
134
135 assert(binary->bo->ptr.cpu != NULL && "shaders must be CPU mapped");
136
137 cache_key cache_key;
138 agx_disk_cache_compute_key(cache, uncompiled, key, cache_key);
139
140 struct blob blob;
141 blob_init(&blob);
142
143 write_shader(&blob, binary, uncompiled->type == PIPE_SHADER_GEOMETRY);
144
145 disk_cache_put(cache, cache_key, blob.data, blob.size, NULL);
146 blob_finish(&blob);
147 #endif
148 }
149
150 /**
151 * Search for a compiled shader in the disk cache.
152 */
153 struct agx_compiled_shader *
agx_disk_cache_retrieve(struct agx_screen * screen,const struct agx_uncompiled_shader * uncompiled,const union asahi_shader_key * key)154 agx_disk_cache_retrieve(struct agx_screen *screen,
155 const struct agx_uncompiled_shader *uncompiled,
156 const union asahi_shader_key *key)
157 {
158 #ifdef ENABLE_SHADER_CACHE
159 struct disk_cache *cache = screen->disk_cache;
160 if (!cache)
161 return NULL;
162
163 cache_key cache_key;
164 agx_disk_cache_compute_key(cache, uncompiled, key, cache_key);
165
166 size_t size;
167 void *buffer = disk_cache_get(cache, cache_key, &size);
168 if (!buffer)
169 return NULL;
170
171 struct blob_reader blob;
172 blob_reader_init(&blob, buffer, size);
173
174 struct agx_compiled_shader *binary =
175 read_shader(screen, &blob, uncompiled, true);
176
177 free(buffer);
178 return binary;
179 #else
180 return NULL;
181 #endif
182 }
183
184 /**
185 * Initialise the on-disk shader cache.
186 */
187 void
agx_disk_cache_init(struct agx_screen * screen)188 agx_disk_cache_init(struct agx_screen *screen)
189 {
190 #ifdef ENABLE_SHADER_CACHE
191 if (agx_get_compiler_debug() || (screen->dev.debug & ~ALLOWED_FLAGS))
192 return;
193
194 const char *renderer = screen->pscreen.get_name(&screen->pscreen);
195
196 const struct build_id_note *note =
197 build_id_find_nhdr_for_addr(agx_disk_cache_init);
198 assert(note && build_id_length(note) == 20);
199
200 const uint8_t *id_sha1 = build_id_data(note);
201 assert(id_sha1);
202
203 char timestamp[41];
204 _mesa_sha1_format(timestamp, id_sha1);
205
206 uint64_t driver_flags = screen->dev.debug;
207 screen->disk_cache = disk_cache_create(renderer, timestamp, driver_flags);
208 #endif
209 }
210