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