nir: allow builin arrays to be lowered
[mesa.git] / src / compiler / nir / nir_serialize.c
1 /*
2 * Copyright © 2017 Connor Abbott
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 "nir_serialize.h"
25 #include "nir_control_flow.h"
26 #include "util/u_dynarray.h"
27
28 typedef struct {
29 size_t blob_offset;
30 nir_ssa_def *src;
31 nir_block *block;
32 } write_phi_fixup;
33
34 typedef struct {
35 const nir_shader *nir;
36
37 struct blob *blob;
38
39 /* maps pointer to index */
40 struct hash_table *remap_table;
41
42 /* the next index to assign to a NIR in-memory object */
43 uintptr_t next_idx;
44
45 /* Array of write_phi_fixup structs representing phi sources that need to
46 * be resolved in the second pass.
47 */
48 struct util_dynarray phi_fixups;
49 } write_ctx;
50
51 typedef struct {
52 nir_shader *nir;
53
54 struct blob_reader *blob;
55
56 /* the next index to assign to a NIR in-memory object */
57 uintptr_t next_idx;
58
59 /* The length of the index -> object table */
60 uintptr_t idx_table_len;
61
62 /* map from index to deserialized pointer */
63 void **idx_table;
64
65 /* List of phi sources. */
66 struct list_head phi_srcs;
67
68 } read_ctx;
69
70 static void
71 write_add_object(write_ctx *ctx, const void *obj)
72 {
73 uintptr_t index = ctx->next_idx++;
74 _mesa_hash_table_insert(ctx->remap_table, obj, (void *) index);
75 }
76
77 static uintptr_t
78 write_lookup_object(write_ctx *ctx, const void *obj)
79 {
80 struct hash_entry *entry = _mesa_hash_table_search(ctx->remap_table, obj);
81 assert(entry);
82 return (uintptr_t) entry->data;
83 }
84
85 static void
86 write_object(write_ctx *ctx, const void *obj)
87 {
88 blob_write_intptr(ctx->blob, write_lookup_object(ctx, obj));
89 }
90
91 static void
92 read_add_object(read_ctx *ctx, void *obj)
93 {
94 assert(ctx->next_idx < ctx->idx_table_len);
95 ctx->idx_table[ctx->next_idx++] = obj;
96 }
97
98 static void *
99 read_lookup_object(read_ctx *ctx, uintptr_t idx)
100 {
101 assert(idx < ctx->idx_table_len);
102 return ctx->idx_table[idx];
103 }
104
105 static void *
106 read_object(read_ctx *ctx)
107 {
108 return read_lookup_object(ctx, blob_read_intptr(ctx->blob));
109 }
110
111 static void
112 write_constant(write_ctx *ctx, const nir_constant *c)
113 {
114 blob_write_bytes(ctx->blob, c->values, sizeof(c->values));
115 blob_write_uint32(ctx->blob, c->num_elements);
116 for (unsigned i = 0; i < c->num_elements; i++)
117 write_constant(ctx, c->elements[i]);
118 }
119
120 static nir_constant *
121 read_constant(read_ctx *ctx, nir_variable *nvar)
122 {
123 nir_constant *c = ralloc(nvar, nir_constant);
124
125 blob_copy_bytes(ctx->blob, (uint8_t *)c->values, sizeof(c->values));
126 c->num_elements = blob_read_uint32(ctx->blob);
127 c->elements = ralloc_array(ctx->nir, nir_constant *, c->num_elements);
128 for (unsigned i = 0; i < c->num_elements; i++)
129 c->elements[i] = read_constant(ctx, nvar);
130
131 return c;
132 }
133
134 static void
135 write_variable(write_ctx *ctx, const nir_variable *var)
136 {
137 write_add_object(ctx, var);
138 encode_type_to_blob(ctx->blob, var->type);
139 blob_write_uint32(ctx->blob, !!(var->name));
140 blob_write_string(ctx->blob, var->name);
141 blob_write_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
142 blob_write_uint32(ctx->blob, var->num_state_slots);
143 blob_write_bytes(ctx->blob, (uint8_t *) var->state_slots,
144 var->num_state_slots * sizeof(nir_state_slot));
145 blob_write_uint32(ctx->blob, !!(var->constant_initializer));
146 if (var->constant_initializer)
147 write_constant(ctx, var->constant_initializer);
148 blob_write_uint32(ctx->blob, !!(var->interface_type));
149 if (var->interface_type)
150 encode_type_to_blob(ctx->blob, var->interface_type);
151 }
152
153 static nir_variable *
154 read_variable(read_ctx *ctx)
155 {
156 nir_variable *var = rzalloc(ctx->nir, nir_variable);
157 read_add_object(ctx, var);
158
159 var->type = decode_type_from_blob(ctx->blob);
160 bool has_name = blob_read_uint32(ctx->blob);
161 if (has_name) {
162 const char *name = blob_read_string(ctx->blob);
163 var->name = ralloc_strdup(var, name);
164 } else {
165 var->name = NULL;
166 }
167 blob_copy_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
168 var->num_state_slots = blob_read_uint32(ctx->blob);
169 var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots);
170 blob_copy_bytes(ctx->blob, (uint8_t *) var->state_slots,
171 var->num_state_slots * sizeof(nir_state_slot));
172 bool has_const_initializer = blob_read_uint32(ctx->blob);
173 if (has_const_initializer)
174 var->constant_initializer = read_constant(ctx, var);
175 else
176 var->constant_initializer = NULL;
177 bool has_interface_type = blob_read_uint32(ctx->blob);
178 if (has_interface_type)
179 var->interface_type = decode_type_from_blob(ctx->blob);
180 else
181 var->interface_type = NULL;
182
183 return var;
184 }
185
186 static void
187 write_var_list(write_ctx *ctx, const struct exec_list *src)
188 {
189 blob_write_uint32(ctx->blob, exec_list_length(src));
190 foreach_list_typed(nir_variable, var, node, src) {
191 write_variable(ctx, var);
192 }
193 }
194
195 static void
196 read_var_list(read_ctx *ctx, struct exec_list *dst)
197 {
198 exec_list_make_empty(dst);
199 unsigned num_vars = blob_read_uint32(ctx->blob);
200 for (unsigned i = 0; i < num_vars; i++) {
201 nir_variable *var = read_variable(ctx);
202 exec_list_push_tail(dst, &var->node);
203 }
204 }
205
206 static void
207 write_register(write_ctx *ctx, const nir_register *reg)
208 {
209 write_add_object(ctx, reg);
210 blob_write_uint32(ctx->blob, reg->num_components);
211 blob_write_uint32(ctx->blob, reg->bit_size);
212 blob_write_uint32(ctx->blob, reg->num_array_elems);
213 blob_write_uint32(ctx->blob, reg->index);
214 blob_write_uint32(ctx->blob, !!(reg->name));
215 if (reg->name)
216 blob_write_string(ctx->blob, reg->name);
217 blob_write_uint32(ctx->blob, reg->is_global << 1 | reg->is_packed);
218 }
219
220 static nir_register *
221 read_register(read_ctx *ctx)
222 {
223 nir_register *reg = ralloc(ctx->nir, nir_register);
224 read_add_object(ctx, reg);
225 reg->num_components = blob_read_uint32(ctx->blob);
226 reg->bit_size = blob_read_uint32(ctx->blob);
227 reg->num_array_elems = blob_read_uint32(ctx->blob);
228 reg->index = blob_read_uint32(ctx->blob);
229 bool has_name = blob_read_uint32(ctx->blob);
230 if (has_name) {
231 const char *name = blob_read_string(ctx->blob);
232 reg->name = ralloc_strdup(reg, name);
233 } else {
234 reg->name = NULL;
235 }
236 unsigned flags = blob_read_uint32(ctx->blob);
237 reg->is_global = flags & 0x2;
238 reg->is_packed = flags & 0x1;
239
240 list_inithead(&reg->uses);
241 list_inithead(&reg->defs);
242 list_inithead(&reg->if_uses);
243
244 return reg;
245 }
246
247 static void
248 write_reg_list(write_ctx *ctx, const struct exec_list *src)
249 {
250 blob_write_uint32(ctx->blob, exec_list_length(src));
251 foreach_list_typed(nir_register, reg, node, src)
252 write_register(ctx, reg);
253 }
254
255 static void
256 read_reg_list(read_ctx *ctx, struct exec_list *dst)
257 {
258 exec_list_make_empty(dst);
259 unsigned num_regs = blob_read_uint32(ctx->blob);
260 for (unsigned i = 0; i < num_regs; i++) {
261 nir_register *reg = read_register(ctx);
262 exec_list_push_tail(dst, &reg->node);
263 }
264 }
265
266 static void
267 write_src(write_ctx *ctx, const nir_src *src)
268 {
269 /* Since sources are very frequent, we try to save some space when storing
270 * them. In particular, we store whether the source is a register and
271 * whether the register has an indirect index in the low two bits. We can
272 * assume that the high two bits of the index are zero, since otherwise our
273 * address space would've been exhausted allocating the remap table!
274 */
275 if (src->is_ssa) {
276 uintptr_t idx = write_lookup_object(ctx, src->ssa) << 2;
277 idx |= 1;
278 blob_write_intptr(ctx->blob, idx);
279 } else {
280 uintptr_t idx = write_lookup_object(ctx, src->reg.reg) << 2;
281 if (src->reg.indirect)
282 idx |= 2;
283 blob_write_intptr(ctx->blob, idx);
284 blob_write_uint32(ctx->blob, src->reg.base_offset);
285 if (src->reg.indirect) {
286 write_src(ctx, src->reg.indirect);
287 }
288 }
289 }
290
291 static void
292 read_src(read_ctx *ctx, nir_src *src, void *mem_ctx)
293 {
294 uintptr_t val = blob_read_intptr(ctx->blob);
295 uintptr_t idx = val >> 2;
296 src->is_ssa = val & 0x1;
297 if (src->is_ssa) {
298 src->ssa = read_lookup_object(ctx, idx);
299 } else {
300 bool is_indirect = val & 0x2;
301 src->reg.reg = read_lookup_object(ctx, idx);
302 src->reg.base_offset = blob_read_uint32(ctx->blob);
303 if (is_indirect) {
304 src->reg.indirect = ralloc(mem_ctx, nir_src);
305 read_src(ctx, src->reg.indirect, mem_ctx);
306 } else {
307 src->reg.indirect = NULL;
308 }
309 }
310 }
311
312 static void
313 write_dest(write_ctx *ctx, const nir_dest *dst)
314 {
315 uint32_t val = dst->is_ssa;
316 if (dst->is_ssa) {
317 val |= !!(dst->ssa.name) << 1;
318 val |= dst->ssa.num_components << 2;
319 val |= dst->ssa.bit_size << 5;
320 } else {
321 val |= !!(dst->reg.indirect) << 1;
322 }
323 blob_write_uint32(ctx->blob, val);
324 if (dst->is_ssa) {
325 write_add_object(ctx, &dst->ssa);
326 if (dst->ssa.name)
327 blob_write_string(ctx->blob, dst->ssa.name);
328 } else {
329 blob_write_intptr(ctx->blob, write_lookup_object(ctx, dst->reg.reg));
330 blob_write_uint32(ctx->blob, dst->reg.base_offset);
331 if (dst->reg.indirect)
332 write_src(ctx, dst->reg.indirect);
333 }
334 }
335
336 static void
337 read_dest(read_ctx *ctx, nir_dest *dst, nir_instr *instr)
338 {
339 uint32_t val = blob_read_uint32(ctx->blob);
340 bool is_ssa = val & 0x1;
341 if (is_ssa) {
342 bool has_name = val & 0x2;
343 unsigned num_components = (val >> 2) & 0x7;
344 unsigned bit_size = val >> 5;
345 char *name = has_name ? blob_read_string(ctx->blob) : NULL;
346 nir_ssa_dest_init(instr, dst, num_components, bit_size, name);
347 read_add_object(ctx, &dst->ssa);
348 } else {
349 bool is_indirect = val & 0x2;
350 dst->reg.reg = read_object(ctx);
351 dst->reg.base_offset = blob_read_uint32(ctx->blob);
352 if (is_indirect) {
353 dst->reg.indirect = ralloc(instr, nir_src);
354 read_src(ctx, dst->reg.indirect, instr);
355 }
356 }
357 }
358
359 static void
360 write_deref_chain(write_ctx *ctx, const nir_deref_var *deref_var)
361 {
362 write_object(ctx, deref_var->var);
363
364 uint32_t len = 0;
365 for (const nir_deref *d = deref_var->deref.child; d; d = d->child)
366 len++;
367 blob_write_uint32(ctx->blob, len);
368
369 for (const nir_deref *d = deref_var->deref.child; d; d = d->child) {
370 blob_write_uint32(ctx->blob, d->deref_type);
371 switch (d->deref_type) {
372 case nir_deref_type_array: {
373 const nir_deref_array *deref_array = nir_deref_as_array(d);
374 blob_write_uint32(ctx->blob, deref_array->deref_array_type);
375 blob_write_uint32(ctx->blob, deref_array->base_offset);
376 if (deref_array->deref_array_type == nir_deref_array_type_indirect)
377 write_src(ctx, &deref_array->indirect);
378 break;
379 }
380 case nir_deref_type_struct: {
381 const nir_deref_struct *deref_struct = nir_deref_as_struct(d);
382 blob_write_uint32(ctx->blob, deref_struct->index);
383 break;
384 }
385 case nir_deref_type_var:
386 unreachable("Invalid deref type");
387 }
388
389 encode_type_to_blob(ctx->blob, d->type);
390 }
391 }
392
393 static nir_deref_var *
394 read_deref_chain(read_ctx *ctx, void *mem_ctx)
395 {
396 nir_variable *var = read_object(ctx);
397 nir_deref_var *deref_var = nir_deref_var_create(mem_ctx, var);
398
399 uint32_t len = blob_read_uint32(ctx->blob);
400
401 nir_deref *tail = &deref_var->deref;
402 for (uint32_t i = 0; i < len; i++) {
403 nir_deref_type deref_type = blob_read_uint32(ctx->blob);
404 nir_deref *deref = NULL;
405 switch (deref_type) {
406 case nir_deref_type_array: {
407 nir_deref_array *deref_array = nir_deref_array_create(tail);
408 deref_array->deref_array_type = blob_read_uint32(ctx->blob);
409 deref_array->base_offset = blob_read_uint32(ctx->blob);
410 if (deref_array->deref_array_type == nir_deref_array_type_indirect)
411 read_src(ctx, &deref_array->indirect, mem_ctx);
412 deref = &deref_array->deref;
413 break;
414 }
415 case nir_deref_type_struct: {
416 uint32_t index = blob_read_uint32(ctx->blob);
417 nir_deref_struct *deref_struct = nir_deref_struct_create(tail, index);
418 deref = &deref_struct->deref;
419 break;
420 }
421 case nir_deref_type_var:
422 unreachable("Invalid deref type");
423 }
424
425 deref->type = decode_type_from_blob(ctx->blob);
426
427 tail->child = deref;
428 tail = deref;
429 }
430
431 return deref_var;
432 }
433
434 static void
435 write_alu(write_ctx *ctx, const nir_alu_instr *alu)
436 {
437 blob_write_uint32(ctx->blob, alu->op);
438 uint32_t flags = alu->exact;
439 flags |= alu->dest.saturate << 1;
440 flags |= alu->dest.write_mask << 2;
441 blob_write_uint32(ctx->blob, flags);
442
443 write_dest(ctx, &alu->dest.dest);
444
445 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
446 write_src(ctx, &alu->src[i].src);
447 flags = alu->src[i].negate;
448 flags |= alu->src[i].abs << 1;
449 for (unsigned j = 0; j < 4; j++)
450 flags |= alu->src[i].swizzle[j] << (2 + 2 * j);
451 blob_write_uint32(ctx->blob, flags);
452 }
453 }
454
455 static nir_alu_instr *
456 read_alu(read_ctx *ctx)
457 {
458 nir_op op = blob_read_uint32(ctx->blob);
459 nir_alu_instr *alu = nir_alu_instr_create(ctx->nir, op);
460
461 uint32_t flags = blob_read_uint32(ctx->blob);
462 alu->exact = flags & 1;
463 alu->dest.saturate = flags & 2;
464 alu->dest.write_mask = flags >> 2;
465
466 read_dest(ctx, &alu->dest.dest, &alu->instr);
467
468 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++) {
469 read_src(ctx, &alu->src[i].src, &alu->instr);
470 flags = blob_read_uint32(ctx->blob);
471 alu->src[i].negate = flags & 1;
472 alu->src[i].abs = flags & 2;
473 for (unsigned j = 0; j < 4; j++)
474 alu->src[i].swizzle[j] = (flags >> (2 * j + 2)) & 3;
475 }
476
477 return alu;
478 }
479
480 static void
481 write_intrinsic(write_ctx *ctx, const nir_intrinsic_instr *intrin)
482 {
483 blob_write_uint32(ctx->blob, intrin->intrinsic);
484
485 unsigned num_variables = nir_intrinsic_infos[intrin->intrinsic].num_variables;
486 unsigned num_srcs = nir_intrinsic_infos[intrin->intrinsic].num_srcs;
487 unsigned num_indices = nir_intrinsic_infos[intrin->intrinsic].num_indices;
488
489 blob_write_uint32(ctx->blob, intrin->num_components);
490
491 if (nir_intrinsic_infos[intrin->intrinsic].has_dest)
492 write_dest(ctx, &intrin->dest);
493
494 for (unsigned i = 0; i < num_variables; i++)
495 write_deref_chain(ctx, intrin->variables[i]);
496
497 for (unsigned i = 0; i < num_srcs; i++)
498 write_src(ctx, &intrin->src[i]);
499
500 for (unsigned i = 0; i < num_indices; i++)
501 blob_write_uint32(ctx->blob, intrin->const_index[i]);
502 }
503
504 static nir_intrinsic_instr *
505 read_intrinsic(read_ctx *ctx)
506 {
507 nir_intrinsic_op op = blob_read_uint32(ctx->blob);
508
509 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(ctx->nir, op);
510
511 unsigned num_variables = nir_intrinsic_infos[op].num_variables;
512 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
513 unsigned num_indices = nir_intrinsic_infos[op].num_indices;
514
515 intrin->num_components = blob_read_uint32(ctx->blob);
516
517 if (nir_intrinsic_infos[op].has_dest)
518 read_dest(ctx, &intrin->dest, &intrin->instr);
519
520 for (unsigned i = 0; i < num_variables; i++)
521 intrin->variables[i] = read_deref_chain(ctx, &intrin->instr);
522
523 for (unsigned i = 0; i < num_srcs; i++)
524 read_src(ctx, &intrin->src[i], &intrin->instr);
525
526 for (unsigned i = 0; i < num_indices; i++)
527 intrin->const_index[i] = blob_read_uint32(ctx->blob);
528
529 return intrin;
530 }
531
532 static void
533 write_load_const(write_ctx *ctx, const nir_load_const_instr *lc)
534 {
535 uint32_t val = lc->def.num_components;
536 val |= lc->def.bit_size << 3;
537 blob_write_uint32(ctx->blob, val);
538 blob_write_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));
539 write_add_object(ctx, &lc->def);
540 }
541
542 static nir_load_const_instr *
543 read_load_const(read_ctx *ctx)
544 {
545 uint32_t val = blob_read_uint32(ctx->blob);
546
547 nir_load_const_instr *lc =
548 nir_load_const_instr_create(ctx->nir, val & 0x7, val >> 3);
549
550 blob_copy_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));
551 read_add_object(ctx, &lc->def);
552 return lc;
553 }
554
555 static void
556 write_ssa_undef(write_ctx *ctx, const nir_ssa_undef_instr *undef)
557 {
558 uint32_t val = undef->def.num_components;
559 val |= undef->def.bit_size << 3;
560 blob_write_uint32(ctx->blob, val);
561 write_add_object(ctx, &undef->def);
562 }
563
564 static nir_ssa_undef_instr *
565 read_ssa_undef(read_ctx *ctx)
566 {
567 uint32_t val = blob_read_uint32(ctx->blob);
568
569 nir_ssa_undef_instr *undef =
570 nir_ssa_undef_instr_create(ctx->nir, val & 0x7, val >> 3);
571
572 read_add_object(ctx, &undef->def);
573 return undef;
574 }
575
576 union packed_tex_data {
577 uint32_t u32;
578 struct {
579 enum glsl_sampler_dim sampler_dim:4;
580 nir_alu_type dest_type:8;
581 unsigned coord_components:3;
582 unsigned is_array:1;
583 unsigned is_shadow:1;
584 unsigned is_new_style_shadow:1;
585 unsigned component:2;
586 unsigned has_texture_deref:1;
587 unsigned has_sampler_deref:1;
588 } u;
589 };
590
591 static void
592 write_tex(write_ctx *ctx, const nir_tex_instr *tex)
593 {
594 blob_write_uint32(ctx->blob, tex->num_srcs);
595 blob_write_uint32(ctx->blob, tex->op);
596 blob_write_uint32(ctx->blob, tex->texture_index);
597 blob_write_uint32(ctx->blob, tex->texture_array_size);
598 blob_write_uint32(ctx->blob, tex->sampler_index);
599
600 STATIC_ASSERT(sizeof(union packed_tex_data) == sizeof(uint32_t));
601 union packed_tex_data packed = {
602 .u.sampler_dim = tex->sampler_dim,
603 .u.dest_type = tex->dest_type,
604 .u.coord_components = tex->coord_components,
605 .u.is_array = tex->is_array,
606 .u.is_shadow = tex->is_shadow,
607 .u.is_new_style_shadow = tex->is_new_style_shadow,
608 .u.component = tex->component,
609 .u.has_texture_deref = tex->texture != NULL,
610 .u.has_sampler_deref = tex->sampler != NULL,
611 };
612 blob_write_uint32(ctx->blob, packed.u32);
613
614 write_dest(ctx, &tex->dest);
615 for (unsigned i = 0; i < tex->num_srcs; i++) {
616 blob_write_uint32(ctx->blob, tex->src[i].src_type);
617 write_src(ctx, &tex->src[i].src);
618 }
619
620 if (tex->texture)
621 write_deref_chain(ctx, tex->texture);
622 if (tex->sampler)
623 write_deref_chain(ctx, tex->sampler);
624 }
625
626 static nir_tex_instr *
627 read_tex(read_ctx *ctx)
628 {
629 unsigned num_srcs = blob_read_uint32(ctx->blob);
630 nir_tex_instr *tex = nir_tex_instr_create(ctx->nir, num_srcs);
631
632 tex->op = blob_read_uint32(ctx->blob);
633 tex->texture_index = blob_read_uint32(ctx->blob);
634 tex->texture_array_size = blob_read_uint32(ctx->blob);
635 tex->sampler_index = blob_read_uint32(ctx->blob);
636
637 union packed_tex_data packed;
638 packed.u32 = blob_read_uint32(ctx->blob);
639 tex->sampler_dim = packed.u.sampler_dim;
640 tex->dest_type = packed.u.dest_type;
641 tex->coord_components = packed.u.coord_components;
642 tex->is_array = packed.u.is_array;
643 tex->is_shadow = packed.u.is_shadow;
644 tex->is_new_style_shadow = packed.u.is_new_style_shadow;
645 tex->component = packed.u.component;
646
647 read_dest(ctx, &tex->dest, &tex->instr);
648 for (unsigned i = 0; i < tex->num_srcs; i++) {
649 tex->src[i].src_type = blob_read_uint32(ctx->blob);
650 read_src(ctx, &tex->src[i].src, &tex->instr);
651 }
652
653 tex->texture = packed.u.has_texture_deref ?
654 read_deref_chain(ctx, &tex->instr) : NULL;
655 tex->sampler = packed.u.has_sampler_deref ?
656 read_deref_chain(ctx, &tex->instr) : NULL;
657
658 return tex;
659 }
660
661 static void
662 write_phi(write_ctx *ctx, const nir_phi_instr *phi)
663 {
664 /* Phi nodes are special, since they may reference SSA definitions and
665 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
666 * and then store enough information so that a later fixup pass can fill
667 * them in correctly.
668 */
669 write_dest(ctx, &phi->dest);
670
671 blob_write_uint32(ctx->blob, exec_list_length(&phi->srcs));
672
673 nir_foreach_phi_src(src, phi) {
674 assert(src->src.is_ssa);
675 size_t blob_offset = blob_reserve_intptr(ctx->blob);
676 MAYBE_UNUSED size_t blob_offset2 = blob_reserve_intptr(ctx->blob);
677 assert(blob_offset + sizeof(uintptr_t) == blob_offset2);
678 write_phi_fixup fixup = {
679 .blob_offset = blob_offset,
680 .src = src->src.ssa,
681 .block = src->pred,
682 };
683 util_dynarray_append(&ctx->phi_fixups, write_phi_fixup, fixup);
684 }
685 }
686
687 static void
688 write_fixup_phis(write_ctx *ctx)
689 {
690 util_dynarray_foreach(&ctx->phi_fixups, write_phi_fixup, fixup) {
691 uintptr_t *blob_ptr = (uintptr_t *)(ctx->blob->data + fixup->blob_offset);
692 blob_ptr[0] = write_lookup_object(ctx, fixup->src);
693 blob_ptr[1] = write_lookup_object(ctx, fixup->block);
694 }
695
696 util_dynarray_clear(&ctx->phi_fixups);
697 }
698
699 static nir_phi_instr *
700 read_phi(read_ctx *ctx, nir_block *blk)
701 {
702 nir_phi_instr *phi = nir_phi_instr_create(ctx->nir);
703
704 read_dest(ctx, &phi->dest, &phi->instr);
705
706 unsigned num_srcs = blob_read_uint32(ctx->blob);
707
708 /* For similar reasons as before, we just store the index directly into the
709 * pointer, and let a later pass resolve the phi sources.
710 *
711 * In order to ensure that the copied sources (which are just the indices
712 * from the blob for now) don't get inserted into the old shader's use-def
713 * lists, we have to add the phi instruction *before* we set up its
714 * sources.
715 */
716 nir_instr_insert_after_block(blk, &phi->instr);
717
718 for (unsigned i = 0; i < num_srcs; i++) {
719 nir_phi_src *src = ralloc(phi, nir_phi_src);
720
721 src->src.is_ssa = true;
722 src->src.ssa = (nir_ssa_def *) blob_read_intptr(ctx->blob);
723 src->pred = (nir_block *) blob_read_intptr(ctx->blob);
724
725 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
726 * we have to set the parent_instr manually. It doesn't really matter
727 * when we do it, so we might as well do it here.
728 */
729 src->src.parent_instr = &phi->instr;
730
731 /* Stash it in the list of phi sources. We'll walk this list and fix up
732 * sources at the very end of read_function_impl.
733 */
734 list_add(&src->src.use_link, &ctx->phi_srcs);
735
736 exec_list_push_tail(&phi->srcs, &src->node);
737 }
738
739 return phi;
740 }
741
742 static void
743 read_fixup_phis(read_ctx *ctx)
744 {
745 list_for_each_entry_safe(nir_phi_src, src, &ctx->phi_srcs, src.use_link) {
746 src->pred = read_lookup_object(ctx, (uintptr_t)src->pred);
747 src->src.ssa = read_lookup_object(ctx, (uintptr_t)src->src.ssa);
748
749 /* Remove from this list */
750 list_del(&src->src.use_link);
751
752 list_addtail(&src->src.use_link, &src->src.ssa->uses);
753 }
754 assert(list_empty(&ctx->phi_srcs));
755 }
756
757 static void
758 write_jump(write_ctx *ctx, const nir_jump_instr *jmp)
759 {
760 blob_write_uint32(ctx->blob, jmp->type);
761 }
762
763 static nir_jump_instr *
764 read_jump(read_ctx *ctx)
765 {
766 nir_jump_type type = blob_read_uint32(ctx->blob);
767 nir_jump_instr *jmp = nir_jump_instr_create(ctx->nir, type);
768 return jmp;
769 }
770
771 static void
772 write_call(write_ctx *ctx, const nir_call_instr *call)
773 {
774 blob_write_intptr(ctx->blob, write_lookup_object(ctx, call->callee));
775
776 for (unsigned i = 0; i < call->num_params; i++)
777 write_deref_chain(ctx, call->params[i]);
778
779 write_deref_chain(ctx, call->return_deref);
780 }
781
782 static nir_call_instr *
783 read_call(read_ctx *ctx)
784 {
785 nir_function *callee = read_object(ctx);
786 nir_call_instr *call = nir_call_instr_create(ctx->nir, callee);
787
788 for (unsigned i = 0; i < call->num_params; i++)
789 call->params[i] = read_deref_chain(ctx, &call->instr);
790
791 call->return_deref = read_deref_chain(ctx, &call->instr);
792
793 return call;
794 }
795
796 static void
797 write_instr(write_ctx *ctx, const nir_instr *instr)
798 {
799 blob_write_uint32(ctx->blob, instr->type);
800 switch (instr->type) {
801 case nir_instr_type_alu:
802 write_alu(ctx, nir_instr_as_alu(instr));
803 break;
804 case nir_instr_type_intrinsic:
805 write_intrinsic(ctx, nir_instr_as_intrinsic(instr));
806 break;
807 case nir_instr_type_load_const:
808 write_load_const(ctx, nir_instr_as_load_const(instr));
809 break;
810 case nir_instr_type_ssa_undef:
811 write_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
812 break;
813 case nir_instr_type_tex:
814 write_tex(ctx, nir_instr_as_tex(instr));
815 break;
816 case nir_instr_type_phi:
817 write_phi(ctx, nir_instr_as_phi(instr));
818 break;
819 case nir_instr_type_jump:
820 write_jump(ctx, nir_instr_as_jump(instr));
821 break;
822 case nir_instr_type_call:
823 write_call(ctx, nir_instr_as_call(instr));
824 break;
825 case nir_instr_type_parallel_copy:
826 unreachable("Cannot write parallel copies");
827 default:
828 unreachable("bad instr type");
829 }
830 }
831
832 static void
833 read_instr(read_ctx *ctx, nir_block *block)
834 {
835 nir_instr_type type = blob_read_uint32(ctx->blob);
836 nir_instr *instr;
837 switch (type) {
838 case nir_instr_type_alu:
839 instr = &read_alu(ctx)->instr;
840 break;
841 case nir_instr_type_intrinsic:
842 instr = &read_intrinsic(ctx)->instr;
843 break;
844 case nir_instr_type_load_const:
845 instr = &read_load_const(ctx)->instr;
846 break;
847 case nir_instr_type_ssa_undef:
848 instr = &read_ssa_undef(ctx)->instr;
849 break;
850 case nir_instr_type_tex:
851 instr = &read_tex(ctx)->instr;
852 break;
853 case nir_instr_type_phi:
854 /* Phi instructions are a bit of a special case when reading because we
855 * don't want inserting the instruction to automatically handle use/defs
856 * for us. Instead, we need to wait until all the blocks/instructions
857 * are read so that we can set their sources up.
858 */
859 read_phi(ctx, block);
860 return;
861 case nir_instr_type_jump:
862 instr = &read_jump(ctx)->instr;
863 break;
864 case nir_instr_type_call:
865 instr = &read_call(ctx)->instr;
866 break;
867 case nir_instr_type_parallel_copy:
868 unreachable("Cannot read parallel copies");
869 default:
870 unreachable("bad instr type");
871 }
872
873 nir_instr_insert_after_block(block, instr);
874 }
875
876 static void
877 write_block(write_ctx *ctx, const nir_block *block)
878 {
879 write_add_object(ctx, block);
880 blob_write_uint32(ctx->blob, exec_list_length(&block->instr_list));
881 nir_foreach_instr(instr, block)
882 write_instr(ctx, instr);
883 }
884
885 static void
886 read_block(read_ctx *ctx, struct exec_list *cf_list)
887 {
888 /* Don't actually create a new block. Just use the one from the tail of
889 * the list. NIR guarantees that the tail of the list is a block and that
890 * no two blocks are side-by-side in the IR; It should be empty.
891 */
892 nir_block *block =
893 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
894
895 read_add_object(ctx, block);
896 unsigned num_instrs = blob_read_uint32(ctx->blob);
897 for (unsigned i = 0; i < num_instrs; i++) {
898 read_instr(ctx, block);
899 }
900 }
901
902 static void
903 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list);
904
905 static void
906 read_cf_list(read_ctx *ctx, struct exec_list *cf_list);
907
908 static void
909 write_if(write_ctx *ctx, nir_if *nif)
910 {
911 write_src(ctx, &nif->condition);
912
913 write_cf_list(ctx, &nif->then_list);
914 write_cf_list(ctx, &nif->else_list);
915 }
916
917 static void
918 read_if(read_ctx *ctx, struct exec_list *cf_list)
919 {
920 nir_if *nif = nir_if_create(ctx->nir);
921
922 read_src(ctx, &nif->condition, nif);
923
924 nir_cf_node_insert_end(cf_list, &nif->cf_node);
925
926 read_cf_list(ctx, &nif->then_list);
927 read_cf_list(ctx, &nif->else_list);
928 }
929
930 static void
931 write_loop(write_ctx *ctx, nir_loop *loop)
932 {
933 write_cf_list(ctx, &loop->body);
934 }
935
936 static void
937 read_loop(read_ctx *ctx, struct exec_list *cf_list)
938 {
939 nir_loop *loop = nir_loop_create(ctx->nir);
940
941 nir_cf_node_insert_end(cf_list, &loop->cf_node);
942
943 read_cf_list(ctx, &loop->body);
944 }
945
946 static void
947 write_cf_node(write_ctx *ctx, nir_cf_node *cf)
948 {
949 blob_write_uint32(ctx->blob, cf->type);
950
951 switch (cf->type) {
952 case nir_cf_node_block:
953 write_block(ctx, nir_cf_node_as_block(cf));
954 break;
955 case nir_cf_node_if:
956 write_if(ctx, nir_cf_node_as_if(cf));
957 break;
958 case nir_cf_node_loop:
959 write_loop(ctx, nir_cf_node_as_loop(cf));
960 break;
961 default:
962 unreachable("bad cf type");
963 }
964 }
965
966 static void
967 read_cf_node(read_ctx *ctx, struct exec_list *list)
968 {
969 nir_cf_node_type type = blob_read_uint32(ctx->blob);
970
971 switch (type) {
972 case nir_cf_node_block:
973 read_block(ctx, list);
974 break;
975 case nir_cf_node_if:
976 read_if(ctx, list);
977 break;
978 case nir_cf_node_loop:
979 read_loop(ctx, list);
980 break;
981 default:
982 unreachable("bad cf type");
983 }
984 }
985
986 static void
987 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list)
988 {
989 blob_write_uint32(ctx->blob, exec_list_length(cf_list));
990 foreach_list_typed(nir_cf_node, cf, node, cf_list) {
991 write_cf_node(ctx, cf);
992 }
993 }
994
995 static void
996 read_cf_list(read_ctx *ctx, struct exec_list *cf_list)
997 {
998 uint32_t num_cf_nodes = blob_read_uint32(ctx->blob);
999 for (unsigned i = 0; i < num_cf_nodes; i++)
1000 read_cf_node(ctx, cf_list);
1001 }
1002
1003 static void
1004 write_function_impl(write_ctx *ctx, const nir_function_impl *fi)
1005 {
1006 write_var_list(ctx, &fi->locals);
1007 write_reg_list(ctx, &fi->registers);
1008 blob_write_uint32(ctx->blob, fi->reg_alloc);
1009
1010 blob_write_uint32(ctx->blob, fi->num_params);
1011 for (unsigned i = 0; i < fi->num_params; i++) {
1012 write_variable(ctx, fi->params[i]);
1013 }
1014
1015 blob_write_uint32(ctx->blob, !!(fi->return_var));
1016 if (fi->return_var)
1017 write_variable(ctx, fi->return_var);
1018
1019 write_cf_list(ctx, &fi->body);
1020 write_fixup_phis(ctx);
1021 }
1022
1023 static nir_function_impl *
1024 read_function_impl(read_ctx *ctx, nir_function *fxn)
1025 {
1026 nir_function_impl *fi = nir_function_impl_create_bare(ctx->nir);
1027 fi->function = fxn;
1028
1029 read_var_list(ctx, &fi->locals);
1030 read_reg_list(ctx, &fi->registers);
1031 fi->reg_alloc = blob_read_uint32(ctx->blob);
1032
1033 fi->num_params = blob_read_uint32(ctx->blob);
1034 for (unsigned i = 0; i < fi->num_params; i++) {
1035 fi->params[i] = read_variable(ctx);
1036 }
1037
1038 bool has_return = blob_read_uint32(ctx->blob);
1039 if (has_return)
1040 fi->return_var = read_variable(ctx);
1041 else
1042 fi->return_var = NULL;
1043
1044 read_cf_list(ctx, &fi->body);
1045 read_fixup_phis(ctx);
1046
1047 fi->valid_metadata = 0;
1048
1049 return fi;
1050 }
1051
1052 static void
1053 write_function(write_ctx *ctx, const nir_function *fxn)
1054 {
1055 blob_write_uint32(ctx->blob, !!(fxn->name));
1056 if (fxn->name)
1057 blob_write_string(ctx->blob, fxn->name);
1058
1059 write_add_object(ctx, fxn);
1060
1061 blob_write_uint32(ctx->blob, fxn->num_params);
1062 for (unsigned i = 0; i < fxn->num_params; i++) {
1063 blob_write_uint32(ctx->blob, fxn->params[i].param_type);
1064 encode_type_to_blob(ctx->blob, fxn->params[i].type);
1065 }
1066
1067 encode_type_to_blob(ctx->blob, fxn->return_type);
1068
1069 /* At first glance, it looks like we should write the function_impl here.
1070 * However, call instructions need to be able to reference at least the
1071 * function and those will get processed as we write the function_impls.
1072 * We stop here and write function_impls as a second pass.
1073 */
1074 }
1075
1076 static void
1077 read_function(read_ctx *ctx)
1078 {
1079 bool has_name = blob_read_uint32(ctx->blob);
1080 char *name = has_name ? blob_read_string(ctx->blob) : NULL;
1081
1082 nir_function *fxn = nir_function_create(ctx->nir, name);
1083
1084 read_add_object(ctx, fxn);
1085
1086 fxn->num_params = blob_read_uint32(ctx->blob);
1087 for (unsigned i = 0; i < fxn->num_params; i++) {
1088 fxn->params[i].param_type = blob_read_uint32(ctx->blob);
1089 fxn->params[i].type = decode_type_from_blob(ctx->blob);
1090 }
1091
1092 fxn->return_type = decode_type_from_blob(ctx->blob);
1093 }
1094
1095 void
1096 nir_serialize(struct blob *blob, const nir_shader *nir)
1097 {
1098 write_ctx ctx;
1099 ctx.remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1100 _mesa_key_pointer_equal);
1101 ctx.next_idx = 0;
1102 ctx.blob = blob;
1103 ctx.nir = nir;
1104 util_dynarray_init(&ctx.phi_fixups, NULL);
1105
1106 size_t idx_size_offset = blob_reserve_intptr(blob);
1107
1108 struct shader_info info = nir->info;
1109 uint32_t strings = 0;
1110 if (info.name)
1111 strings |= 0x1;
1112 if (info.label)
1113 strings |= 0x2;
1114 blob_write_uint32(blob, strings);
1115 if (info.name)
1116 blob_write_string(blob, info.name);
1117 if (info.label)
1118 blob_write_string(blob, info.label);
1119 info.name = info.label = NULL;
1120 blob_write_bytes(blob, (uint8_t *) &info, sizeof(info));
1121
1122 write_var_list(&ctx, &nir->uniforms);
1123 write_var_list(&ctx, &nir->inputs);
1124 write_var_list(&ctx, &nir->outputs);
1125 write_var_list(&ctx, &nir->shared);
1126 write_var_list(&ctx, &nir->globals);
1127 write_var_list(&ctx, &nir->system_values);
1128
1129 write_reg_list(&ctx, &nir->registers);
1130 blob_write_uint32(blob, nir->reg_alloc);
1131 blob_write_uint32(blob, nir->num_inputs);
1132 blob_write_uint32(blob, nir->num_uniforms);
1133 blob_write_uint32(blob, nir->num_outputs);
1134 blob_write_uint32(blob, nir->num_shared);
1135
1136 blob_write_uint32(blob, exec_list_length(&nir->functions));
1137 nir_foreach_function(fxn, nir) {
1138 write_function(&ctx, fxn);
1139 }
1140
1141 nir_foreach_function(fxn, nir) {
1142 write_function_impl(&ctx, fxn->impl);
1143 }
1144
1145 *(uintptr_t *)(blob->data + idx_size_offset) = ctx.next_idx;
1146
1147 _mesa_hash_table_destroy(ctx.remap_table, NULL);
1148 util_dynarray_fini(&ctx.phi_fixups);
1149 }
1150
1151 nir_shader *
1152 nir_deserialize(void *mem_ctx,
1153 const struct nir_shader_compiler_options *options,
1154 struct blob_reader *blob)
1155 {
1156 read_ctx ctx;
1157 ctx.blob = blob;
1158 list_inithead(&ctx.phi_srcs);
1159 ctx.idx_table_len = blob_read_intptr(blob);
1160 ctx.idx_table = calloc(ctx.idx_table_len, sizeof(uintptr_t));
1161 ctx.next_idx = 0;
1162
1163 uint32_t strings = blob_read_uint32(blob);
1164 char *name = (strings & 0x1) ? blob_read_string(blob) : NULL;
1165 char *label = (strings & 0x2) ? blob_read_string(blob) : NULL;
1166
1167 struct shader_info info;
1168 blob_copy_bytes(blob, (uint8_t *) &info, sizeof(info));
1169
1170 ctx.nir = nir_shader_create(mem_ctx, info.stage, options, NULL);
1171
1172 info.name = name ? ralloc_strdup(ctx.nir, name) : NULL;
1173 info.label = label ? ralloc_strdup(ctx.nir, label) : NULL;
1174
1175 ctx.nir->info = info;
1176
1177 read_var_list(&ctx, &ctx.nir->uniforms);
1178 read_var_list(&ctx, &ctx.nir->inputs);
1179 read_var_list(&ctx, &ctx.nir->outputs);
1180 read_var_list(&ctx, &ctx.nir->shared);
1181 read_var_list(&ctx, &ctx.nir->globals);
1182 read_var_list(&ctx, &ctx.nir->system_values);
1183
1184 read_reg_list(&ctx, &ctx.nir->registers);
1185 ctx.nir->reg_alloc = blob_read_uint32(blob);
1186 ctx.nir->num_inputs = blob_read_uint32(blob);
1187 ctx.nir->num_uniforms = blob_read_uint32(blob);
1188 ctx.nir->num_outputs = blob_read_uint32(blob);
1189 ctx.nir->num_shared = blob_read_uint32(blob);
1190
1191 unsigned num_functions = blob_read_uint32(blob);
1192 for (unsigned i = 0; i < num_functions; i++)
1193 read_function(&ctx);
1194
1195 nir_foreach_function(fxn, ctx.nir)
1196 fxn->impl = read_function_impl(&ctx, fxn);
1197
1198 free(ctx.idx_table);
1199
1200 return ctx.nir;
1201 }
1202
1203 nir_shader *
1204 nir_shader_serialize_deserialize(void *mem_ctx, nir_shader *s)
1205 {
1206 const struct nir_shader_compiler_options *options = s->options;
1207
1208 struct blob writer;
1209 blob_init(&writer);
1210 nir_serialize(&writer, s);
1211 ralloc_free(s);
1212
1213 struct blob_reader reader;
1214 blob_reader_init(&reader, writer.data, writer.size);
1215 nir_shader *ns = nir_deserialize(mem_ctx, options, &reader);
1216
1217 blob_finish(&writer);
1218
1219 return ns;
1220 }