86a7ed3e13deacb42a6647da299e3efa8e132458
[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 #include "util/u_math.h"
28
29 #define NIR_SERIALIZE_FUNC_HAS_IMPL ((void *)(intptr_t)1)
30 #define MAX_OBJECT_IDS (1 << 20)
31
32 typedef struct {
33 size_t blob_offset;
34 nir_ssa_def *src;
35 nir_block *block;
36 } write_phi_fixup;
37
38 typedef struct {
39 const nir_shader *nir;
40
41 struct blob *blob;
42
43 /* maps pointer to index */
44 struct hash_table *remap_table;
45
46 /* the next index to assign to a NIR in-memory object */
47 uint32_t next_idx;
48
49 /* Array of write_phi_fixup structs representing phi sources that need to
50 * be resolved in the second pass.
51 */
52 struct util_dynarray phi_fixups;
53
54 /* The last serialized type. */
55 const struct glsl_type *last_type;
56 const struct glsl_type *last_interface_type;
57 struct nir_variable_data last_var_data;
58
59 /* Don't write optional data such as variable names. */
60 bool strip;
61 } write_ctx;
62
63 typedef struct {
64 nir_shader *nir;
65
66 struct blob_reader *blob;
67
68 /* the next index to assign to a NIR in-memory object */
69 uint32_t next_idx;
70
71 /* The length of the index -> object table */
72 uint32_t idx_table_len;
73
74 /* map from index to deserialized pointer */
75 void **idx_table;
76
77 /* List of phi sources. */
78 struct list_head phi_srcs;
79
80 /* The last deserialized type. */
81 const struct glsl_type *last_type;
82 const struct glsl_type *last_interface_type;
83 struct nir_variable_data last_var_data;
84 } read_ctx;
85
86 static void
87 write_add_object(write_ctx *ctx, const void *obj)
88 {
89 uint32_t index = ctx->next_idx++;
90 assert(index != MAX_OBJECT_IDS);
91 _mesa_hash_table_insert(ctx->remap_table, obj, (void *)(uintptr_t) index);
92 }
93
94 static uint32_t
95 write_lookup_object(write_ctx *ctx, const void *obj)
96 {
97 struct hash_entry *entry = _mesa_hash_table_search(ctx->remap_table, obj);
98 assert(entry);
99 return (uint32_t)(uintptr_t) entry->data;
100 }
101
102 static void
103 write_object(write_ctx *ctx, const void *obj)
104 {
105 blob_write_uint32(ctx->blob, write_lookup_object(ctx, obj));
106 }
107
108 static void
109 read_add_object(read_ctx *ctx, void *obj)
110 {
111 assert(ctx->next_idx < ctx->idx_table_len);
112 ctx->idx_table[ctx->next_idx++] = obj;
113 }
114
115 static void *
116 read_lookup_object(read_ctx *ctx, uint32_t idx)
117 {
118 assert(idx < ctx->idx_table_len);
119 return ctx->idx_table[idx];
120 }
121
122 static void *
123 read_object(read_ctx *ctx)
124 {
125 return read_lookup_object(ctx, blob_read_uint32(ctx->blob));
126 }
127
128 static uint32_t
129 encode_bit_size_3bits(uint8_t bit_size)
130 {
131 /* Encode values of 0, 1, 2, 4, 8, 16, 32, 64 in 3 bits. */
132 assert(bit_size <= 64 && util_is_power_of_two_or_zero(bit_size));
133 if (bit_size)
134 return util_logbase2(bit_size) + 1;
135 return 0;
136 }
137
138 static uint8_t
139 decode_bit_size_3bits(uint8_t bit_size)
140 {
141 if (bit_size)
142 return 1 << (bit_size - 1);
143 return 0;
144 }
145
146 static uint8_t
147 encode_num_components_in_3bits(uint8_t num_components)
148 {
149 if (num_components <= 4)
150 return num_components;
151 if (num_components == 8)
152 return 5;
153 if (num_components == 16)
154 return 6;
155
156 unreachable("invalid number in num_components");
157 return 0;
158 }
159
160 static uint8_t
161 decode_num_components_in_3bits(uint8_t value)
162 {
163 if (value <= 4)
164 return value;
165 if (value == 5)
166 return 8;
167 if (value == 6)
168 return 16;
169
170 unreachable("invalid num_components encoding");
171 return 0;
172 }
173
174 static void
175 write_constant(write_ctx *ctx, const nir_constant *c)
176 {
177 blob_write_bytes(ctx->blob, c->values, sizeof(c->values));
178 blob_write_uint32(ctx->blob, c->num_elements);
179 for (unsigned i = 0; i < c->num_elements; i++)
180 write_constant(ctx, c->elements[i]);
181 }
182
183 static nir_constant *
184 read_constant(read_ctx *ctx, nir_variable *nvar)
185 {
186 nir_constant *c = ralloc(nvar, nir_constant);
187
188 blob_copy_bytes(ctx->blob, (uint8_t *)c->values, sizeof(c->values));
189 c->num_elements = blob_read_uint32(ctx->blob);
190 c->elements = ralloc_array(nvar, nir_constant *, c->num_elements);
191 for (unsigned i = 0; i < c->num_elements; i++)
192 c->elements[i] = read_constant(ctx, nvar);
193
194 return c;
195 }
196
197 enum var_data_encoding {
198 var_encode_full,
199 var_encode_shader_temp,
200 var_encode_function_temp,
201 var_encode_location_diff,
202 };
203
204 union packed_var {
205 uint32_t u32;
206 struct {
207 unsigned has_name:1;
208 unsigned has_constant_initializer:1;
209 unsigned has_interface_type:1;
210 unsigned num_state_slots:7;
211 unsigned data_encoding:2;
212 unsigned type_same_as_last:1;
213 unsigned interface_type_same_as_last:1;
214 unsigned _pad:2;
215 unsigned num_members:16;
216 } u;
217 };
218
219 union packed_var_data_diff {
220 uint32_t u32;
221 struct {
222 int location:13;
223 int location_frac:3;
224 int driver_location:16;
225 } u;
226 };
227
228 static void
229 write_variable(write_ctx *ctx, const nir_variable *var)
230 {
231 write_add_object(ctx, var);
232
233 assert(var->num_state_slots < (1 << 7));
234 assert(var->num_members < (1 << 16));
235
236 STATIC_ASSERT(sizeof(union packed_var) == 4);
237 union packed_var flags;
238 flags.u32 = 0;
239
240 flags.u.has_name = !ctx->strip && var->name;
241 flags.u.has_constant_initializer = !!(var->constant_initializer);
242 flags.u.has_interface_type = !!(var->interface_type);
243 flags.u.type_same_as_last = var->type == ctx->last_type;
244 flags.u.interface_type_same_as_last =
245 var->interface_type && var->interface_type == ctx->last_interface_type;
246 flags.u.num_state_slots = var->num_state_slots;
247 flags.u.num_members = var->num_members;
248
249 struct nir_variable_data data = var->data;
250
251 /* When stripping, we expect that the location is no longer needed,
252 * which is typically after shaders are linked.
253 */
254 if (ctx->strip &&
255 data.mode != nir_var_shader_in &&
256 data.mode != nir_var_shader_out)
257 data.location = 0;
258
259 /* Temporary variables don't serialize var->data. */
260 if (data.mode == nir_var_shader_temp)
261 flags.u.data_encoding = var_encode_shader_temp;
262 else if (data.mode == nir_var_function_temp)
263 flags.u.data_encoding = var_encode_function_temp;
264 else {
265 struct nir_variable_data tmp = data;
266
267 tmp.location = ctx->last_var_data.location;
268 tmp.location_frac = ctx->last_var_data.location_frac;
269 tmp.driver_location = ctx->last_var_data.driver_location;
270
271 /* See if we can encode only the difference in locations from the last
272 * variable.
273 */
274 if (memcmp(&ctx->last_var_data, &tmp, sizeof(tmp)) == 0 &&
275 abs((int)data.location -
276 (int)ctx->last_var_data.location) < (1 << 12) &&
277 abs((int)data.driver_location -
278 (int)ctx->last_var_data.driver_location) < (1 << 15))
279 flags.u.data_encoding = var_encode_location_diff;
280 else
281 flags.u.data_encoding = var_encode_full;
282 }
283
284 blob_write_uint32(ctx->blob, flags.u32);
285
286 if (!flags.u.type_same_as_last) {
287 encode_type_to_blob(ctx->blob, var->type);
288 ctx->last_type = var->type;
289 }
290
291 if (var->interface_type && !flags.u.interface_type_same_as_last) {
292 encode_type_to_blob(ctx->blob, var->interface_type);
293 ctx->last_interface_type = var->interface_type;
294 }
295
296 if (flags.u.has_name)
297 blob_write_string(ctx->blob, var->name);
298
299 if (flags.u.data_encoding == var_encode_full ||
300 flags.u.data_encoding == var_encode_location_diff) {
301 if (flags.u.data_encoding == var_encode_full) {
302 blob_write_bytes(ctx->blob, &data, sizeof(data));
303 } else {
304 /* Serialize only the difference in locations from the last variable.
305 */
306 union packed_var_data_diff diff;
307
308 diff.u.location = data.location - ctx->last_var_data.location;
309 diff.u.location_frac = data.location_frac -
310 ctx->last_var_data.location_frac;
311 diff.u.driver_location = data.driver_location -
312 ctx->last_var_data.driver_location;
313
314 blob_write_uint32(ctx->blob, diff.u32);
315 }
316
317 ctx->last_var_data = data;
318 }
319
320 for (unsigned i = 0; i < var->num_state_slots; i++) {
321 blob_write_bytes(ctx->blob, &var->state_slots[i],
322 sizeof(var->state_slots[i]));
323 }
324 if (var->constant_initializer)
325 write_constant(ctx, var->constant_initializer);
326 if (var->num_members > 0) {
327 blob_write_bytes(ctx->blob, (uint8_t *) var->members,
328 var->num_members * sizeof(*var->members));
329 }
330 }
331
332 static nir_variable *
333 read_variable(read_ctx *ctx)
334 {
335 nir_variable *var = rzalloc(ctx->nir, nir_variable);
336 read_add_object(ctx, var);
337
338 union packed_var flags;
339 flags.u32 = blob_read_uint32(ctx->blob);
340
341 if (flags.u.type_same_as_last) {
342 var->type = ctx->last_type;
343 } else {
344 var->type = decode_type_from_blob(ctx->blob);
345 ctx->last_type = var->type;
346 }
347
348 if (flags.u.has_interface_type) {
349 if (flags.u.interface_type_same_as_last) {
350 var->interface_type = ctx->last_interface_type;
351 } else {
352 var->interface_type = decode_type_from_blob(ctx->blob);
353 ctx->last_interface_type = var->interface_type;
354 }
355 }
356
357 if (flags.u.has_name) {
358 const char *name = blob_read_string(ctx->blob);
359 var->name = ralloc_strdup(var, name);
360 } else {
361 var->name = NULL;
362 }
363
364 if (flags.u.data_encoding == var_encode_shader_temp)
365 var->data.mode = nir_var_shader_temp;
366 else if (flags.u.data_encoding == var_encode_function_temp)
367 var->data.mode = nir_var_function_temp;
368 else if (flags.u.data_encoding == var_encode_full) {
369 blob_copy_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
370 ctx->last_var_data = var->data;
371 } else { /* var_encode_location_diff */
372 union packed_var_data_diff diff;
373 diff.u32 = blob_read_uint32(ctx->blob);
374
375 var->data = ctx->last_var_data;
376 var->data.location += diff.u.location;
377 var->data.location_frac += diff.u.location_frac;
378 var->data.driver_location += diff.u.driver_location;
379
380 ctx->last_var_data = var->data;
381 }
382
383 var->num_state_slots = flags.u.num_state_slots;
384 if (var->num_state_slots != 0) {
385 var->state_slots = ralloc_array(var, nir_state_slot,
386 var->num_state_slots);
387 for (unsigned i = 0; i < var->num_state_slots; i++) {
388 blob_copy_bytes(ctx->blob, &var->state_slots[i],
389 sizeof(var->state_slots[i]));
390 }
391 }
392 if (flags.u.has_constant_initializer)
393 var->constant_initializer = read_constant(ctx, var);
394 else
395 var->constant_initializer = NULL;
396 var->num_members = flags.u.num_members;
397 if (var->num_members > 0) {
398 var->members = ralloc_array(var, struct nir_variable_data,
399 var->num_members);
400 blob_copy_bytes(ctx->blob, (uint8_t *) var->members,
401 var->num_members * sizeof(*var->members));
402 }
403
404 return var;
405 }
406
407 static void
408 write_var_list(write_ctx *ctx, const struct exec_list *src)
409 {
410 blob_write_uint32(ctx->blob, exec_list_length(src));
411 foreach_list_typed(nir_variable, var, node, src) {
412 write_variable(ctx, var);
413 }
414 }
415
416 static void
417 read_var_list(read_ctx *ctx, struct exec_list *dst)
418 {
419 exec_list_make_empty(dst);
420 unsigned num_vars = blob_read_uint32(ctx->blob);
421 for (unsigned i = 0; i < num_vars; i++) {
422 nir_variable *var = read_variable(ctx);
423 exec_list_push_tail(dst, &var->node);
424 }
425 }
426
427 static void
428 write_register(write_ctx *ctx, const nir_register *reg)
429 {
430 write_add_object(ctx, reg);
431 blob_write_uint32(ctx->blob, reg->num_components);
432 blob_write_uint32(ctx->blob, reg->bit_size);
433 blob_write_uint32(ctx->blob, reg->num_array_elems);
434 blob_write_uint32(ctx->blob, reg->index);
435 blob_write_uint32(ctx->blob, !ctx->strip && reg->name);
436 if (!ctx->strip && reg->name)
437 blob_write_string(ctx->blob, reg->name);
438 }
439
440 static nir_register *
441 read_register(read_ctx *ctx)
442 {
443 nir_register *reg = ralloc(ctx->nir, nir_register);
444 read_add_object(ctx, reg);
445 reg->num_components = blob_read_uint32(ctx->blob);
446 reg->bit_size = blob_read_uint32(ctx->blob);
447 reg->num_array_elems = blob_read_uint32(ctx->blob);
448 reg->index = blob_read_uint32(ctx->blob);
449 bool has_name = blob_read_uint32(ctx->blob);
450 if (has_name) {
451 const char *name = blob_read_string(ctx->blob);
452 reg->name = ralloc_strdup(reg, name);
453 } else {
454 reg->name = NULL;
455 }
456
457 list_inithead(&reg->uses);
458 list_inithead(&reg->defs);
459 list_inithead(&reg->if_uses);
460
461 return reg;
462 }
463
464 static void
465 write_reg_list(write_ctx *ctx, const struct exec_list *src)
466 {
467 blob_write_uint32(ctx->blob, exec_list_length(src));
468 foreach_list_typed(nir_register, reg, node, src)
469 write_register(ctx, reg);
470 }
471
472 static void
473 read_reg_list(read_ctx *ctx, struct exec_list *dst)
474 {
475 exec_list_make_empty(dst);
476 unsigned num_regs = blob_read_uint32(ctx->blob);
477 for (unsigned i = 0; i < num_regs; i++) {
478 nir_register *reg = read_register(ctx);
479 exec_list_push_tail(dst, &reg->node);
480 }
481 }
482
483 union packed_src {
484 uint32_t u32;
485 struct {
486 unsigned is_ssa:1; /* <-- Header */
487 unsigned is_indirect:1;
488 unsigned object_idx:20;
489 unsigned _footer:10; /* <-- Footer */
490 } any;
491 struct {
492 unsigned _header:22; /* <-- Header */
493 unsigned negate:1; /* <-- Footer */
494 unsigned abs:1;
495 unsigned swizzle_x:2;
496 unsigned swizzle_y:2;
497 unsigned swizzle_z:2;
498 unsigned swizzle_w:2;
499 } alu;
500 struct {
501 unsigned _header:22; /* <-- Header */
502 unsigned src_type:5; /* <-- Footer */
503 unsigned _pad:5;
504 } tex;
505 };
506
507 static void
508 write_src_full(write_ctx *ctx, const nir_src *src, union packed_src header)
509 {
510 /* Since sources are very frequent, we try to save some space when storing
511 * them. In particular, we store whether the source is a register and
512 * whether the register has an indirect index in the low two bits. We can
513 * assume that the high two bits of the index are zero, since otherwise our
514 * address space would've been exhausted allocating the remap table!
515 */
516 header.any.is_ssa = src->is_ssa;
517 if (src->is_ssa) {
518 header.any.object_idx = write_lookup_object(ctx, src->ssa);
519 blob_write_uint32(ctx->blob, header.u32);
520 } else {
521 header.any.object_idx = write_lookup_object(ctx, src->reg.reg);
522 header.any.is_indirect = !!src->reg.indirect;
523 blob_write_uint32(ctx->blob, header.u32);
524 blob_write_uint32(ctx->blob, src->reg.base_offset);
525 if (src->reg.indirect) {
526 union packed_src header = {0};
527 write_src_full(ctx, src->reg.indirect, header);
528 }
529 }
530 }
531
532 static void
533 write_src(write_ctx *ctx, const nir_src *src)
534 {
535 union packed_src header = {0};
536 write_src_full(ctx, src, header);
537 }
538
539 static union packed_src
540 read_src(read_ctx *ctx, nir_src *src, void *mem_ctx)
541 {
542 STATIC_ASSERT(sizeof(union packed_src) == 4);
543 union packed_src header;
544 header.u32 = blob_read_uint32(ctx->blob);
545
546 src->is_ssa = header.any.is_ssa;
547 if (src->is_ssa) {
548 src->ssa = read_lookup_object(ctx, header.any.object_idx);
549 } else {
550 src->reg.reg = read_lookup_object(ctx, header.any.object_idx);
551 src->reg.base_offset = blob_read_uint32(ctx->blob);
552 if (header.any.is_indirect) {
553 src->reg.indirect = ralloc(mem_ctx, nir_src);
554 read_src(ctx, src->reg.indirect, mem_ctx);
555 } else {
556 src->reg.indirect = NULL;
557 }
558 }
559 return header;
560 }
561
562 union packed_dest {
563 uint8_t u8;
564 struct {
565 uint8_t is_ssa:1;
566 uint8_t has_name:1;
567 uint8_t num_components:3;
568 uint8_t bit_size:3;
569 } ssa;
570 struct {
571 uint8_t is_ssa:1;
572 uint8_t is_indirect:1;
573 uint8_t _pad:6;
574 } reg;
575 };
576
577 enum intrinsic_const_indices_encoding {
578 /* Use the 6 bits of packed_const_indices to store 1-6 indices.
579 * 1 6-bit index, or 2 3-bit indices, or 3 2-bit indices, or
580 * 4-6 1-bit indices.
581 *
582 * The common case for load_ubo is 0, 0, 0, which is trivially represented.
583 * The common cases for load_interpolated_input also fit here, e.g.: 7, 3
584 */
585 const_indices_6bit_all_combined,
586
587 const_indices_8bit, /* 8 bits per element */
588 const_indices_16bit, /* 16 bits per element */
589 const_indices_32bit, /* 32 bits per element */
590 };
591
592 enum load_const_packing {
593 /* Constants are not packed and are stored in following dwords. */
594 load_const_full,
595
596 /* packed_value contains high 19 bits, low bits are 0,
597 * good for floating-point decimals
598 */
599 load_const_scalar_hi_19bits,
600
601 /* packed_value contains low 19 bits, high bits are sign-extended */
602 load_const_scalar_lo_19bits_sext,
603 };
604
605 union packed_instr {
606 uint32_t u32;
607 struct {
608 unsigned instr_type:4; /* always present */
609 unsigned _pad:20;
610 unsigned dest:8; /* always last */
611 } any;
612 struct {
613 unsigned instr_type:4;
614 unsigned exact:1;
615 unsigned no_signed_wrap:1;
616 unsigned no_unsigned_wrap:1;
617 unsigned saturate:1;
618 unsigned writemask:4;
619 unsigned op:9;
620 unsigned packed_src_ssa_16bit:1;
621 unsigned _pad:2;
622 unsigned dest:8;
623 } alu;
624 struct {
625 unsigned instr_type:4;
626 unsigned deref_type:3;
627 unsigned mode:10;
628 unsigned _pad:7;
629 unsigned dest:8;
630 } deref;
631 struct {
632 unsigned instr_type:4;
633 unsigned intrinsic:9;
634 unsigned num_components:3;
635 unsigned const_indices_encoding:2;
636 unsigned packed_const_indices:6;
637 unsigned dest:8;
638 } intrinsic;
639 struct {
640 unsigned instr_type:4;
641 unsigned last_component:4;
642 unsigned bit_size:3;
643 unsigned packing:2; /* enum load_const_packing */
644 unsigned packed_value:19; /* meaning determined by packing */
645 } load_const;
646 struct {
647 unsigned instr_type:4;
648 unsigned last_component:4;
649 unsigned bit_size:3;
650 unsigned _pad:21;
651 } undef;
652 struct {
653 unsigned instr_type:4;
654 unsigned num_srcs:4;
655 unsigned op:4;
656 unsigned texture_array_size:12;
657 unsigned dest:8;
658 } tex;
659 struct {
660 unsigned instr_type:4;
661 unsigned num_srcs:20;
662 unsigned dest:8;
663 } phi;
664 struct {
665 unsigned instr_type:4;
666 unsigned type:2;
667 unsigned _pad:26;
668 } jump;
669 };
670
671 /* Write "lo24" as low 24 bits in the first uint32. */
672 static void
673 write_dest(write_ctx *ctx, const nir_dest *dst, union packed_instr header)
674 {
675 STATIC_ASSERT(sizeof(union packed_dest) == 1);
676 union packed_dest dest;
677 dest.u8 = 0;
678
679 dest.ssa.is_ssa = dst->is_ssa;
680 if (dst->is_ssa) {
681 dest.ssa.has_name = !ctx->strip && dst->ssa.name;
682 dest.ssa.num_components =
683 encode_num_components_in_3bits(dst->ssa.num_components);
684 dest.ssa.bit_size = encode_bit_size_3bits(dst->ssa.bit_size);
685 } else {
686 dest.reg.is_indirect = !!(dst->reg.indirect);
687 }
688
689 header.any.dest = dest.u8;
690 blob_write_uint32(ctx->blob, header.u32);
691
692 if (dst->is_ssa) {
693 write_add_object(ctx, &dst->ssa);
694 if (dest.ssa.has_name)
695 blob_write_string(ctx->blob, dst->ssa.name);
696 } else {
697 blob_write_uint32(ctx->blob, write_lookup_object(ctx, dst->reg.reg));
698 blob_write_uint32(ctx->blob, dst->reg.base_offset);
699 if (dst->reg.indirect)
700 write_src(ctx, dst->reg.indirect);
701 }
702 }
703
704 static void
705 read_dest(read_ctx *ctx, nir_dest *dst, nir_instr *instr,
706 union packed_instr header)
707 {
708 union packed_dest dest;
709 dest.u8 = header.any.dest;
710
711 if (dest.ssa.is_ssa) {
712 unsigned bit_size = decode_bit_size_3bits(dest.ssa.bit_size);
713 unsigned num_components =
714 decode_num_components_in_3bits(dest.ssa.num_components);
715 char *name = dest.ssa.has_name ? blob_read_string(ctx->blob) : NULL;
716 nir_ssa_dest_init(instr, dst, num_components, bit_size, name);
717 read_add_object(ctx, &dst->ssa);
718 } else {
719 dst->reg.reg = read_object(ctx);
720 dst->reg.base_offset = blob_read_uint32(ctx->blob);
721 if (dest.reg.is_indirect) {
722 dst->reg.indirect = ralloc(instr, nir_src);
723 read_src(ctx, dst->reg.indirect, instr);
724 }
725 }
726 }
727
728 static bool
729 are_object_ids_16bit(write_ctx *ctx)
730 {
731 /* Check the highest object ID, because they are monotonic. */
732 return ctx->next_idx < (1 << 16);
733 }
734
735 static bool
736 is_alu_src_ssa_16bit(write_ctx *ctx, const nir_alu_instr *alu)
737 {
738 unsigned num_srcs = nir_op_infos[alu->op].num_inputs;
739
740 for (unsigned i = 0; i < num_srcs; i++) {
741 if (!alu->src[i].src.is_ssa || alu->src[i].abs || alu->src[i].negate)
742 return false;
743
744 unsigned src_components = nir_ssa_alu_instr_src_components(alu, i);
745
746 for (unsigned chan = 0; chan < src_components; chan++) {
747 if (alu->src[i].swizzle[chan] != chan)
748 return false;
749 }
750 }
751
752 return are_object_ids_16bit(ctx);
753 }
754
755 static void
756 write_alu(write_ctx *ctx, const nir_alu_instr *alu)
757 {
758 unsigned num_srcs = nir_op_infos[alu->op].num_inputs;
759 /* 9 bits for nir_op */
760 STATIC_ASSERT(nir_num_opcodes <= 512);
761 union packed_instr header;
762 header.u32 = 0;
763
764 header.alu.instr_type = alu->instr.type;
765 header.alu.exact = alu->exact;
766 header.alu.no_signed_wrap = alu->no_signed_wrap;
767 header.alu.no_unsigned_wrap = alu->no_unsigned_wrap;
768 header.alu.saturate = alu->dest.saturate;
769 header.alu.writemask = alu->dest.write_mask;
770 header.alu.op = alu->op;
771 header.alu.packed_src_ssa_16bit = is_alu_src_ssa_16bit(ctx, alu);
772
773 write_dest(ctx, &alu->dest.dest, header);
774
775 if (header.alu.packed_src_ssa_16bit) {
776 for (unsigned i = 0; i < num_srcs; i++) {
777 assert(alu->src[i].src.is_ssa);
778 unsigned idx = write_lookup_object(ctx, alu->src[i].src.ssa);
779 assert(idx < (1 << 16));
780 blob_write_uint16(ctx->blob, idx);
781 }
782 } else {
783 for (unsigned i = 0; i < num_srcs; i++) {
784 union packed_src src;
785 src.u32 = 0;
786
787 src.alu.negate = alu->src[i].negate;
788 src.alu.abs = alu->src[i].abs;
789 src.alu.swizzle_x = alu->src[i].swizzle[0];
790 src.alu.swizzle_y = alu->src[i].swizzle[1];
791 src.alu.swizzle_z = alu->src[i].swizzle[2];
792 src.alu.swizzle_w = alu->src[i].swizzle[3];
793
794 write_src_full(ctx, &alu->src[i].src, src);
795 }
796 }
797 }
798
799 static nir_alu_instr *
800 read_alu(read_ctx *ctx, union packed_instr header)
801 {
802 unsigned num_srcs = nir_op_infos[header.alu.op].num_inputs;
803 nir_alu_instr *alu = nir_alu_instr_create(ctx->nir, header.alu.op);
804
805 alu->exact = header.alu.exact;
806 alu->no_signed_wrap = header.alu.no_signed_wrap;
807 alu->no_unsigned_wrap = header.alu.no_unsigned_wrap;
808 alu->dest.saturate = header.alu.saturate;
809 alu->dest.write_mask = header.alu.writemask;
810
811 read_dest(ctx, &alu->dest.dest, &alu->instr, header);
812
813 if (header.alu.packed_src_ssa_16bit) {
814 for (unsigned i = 0; i < num_srcs; i++) {
815 nir_alu_src *src = &alu->src[i];
816 src->src.is_ssa = true;
817 src->src.ssa = read_lookup_object(ctx, blob_read_uint16(ctx->blob));
818
819 memset(&src->swizzle, 0, sizeof(src->swizzle));
820
821 unsigned src_components = nir_ssa_alu_instr_src_components(alu, i);
822
823 for (unsigned chan = 0; chan < src_components; chan++)
824 src->swizzle[chan] = chan;
825 }
826 } else {
827 for (unsigned i = 0; i < num_srcs; i++) {
828 union packed_src src = read_src(ctx, &alu->src[i].src, &alu->instr);
829
830 alu->src[i].negate = src.alu.negate;
831 alu->src[i].abs = src.alu.abs;
832 alu->src[i].swizzle[0] = src.alu.swizzle_x;
833 alu->src[i].swizzle[1] = src.alu.swizzle_y;
834 alu->src[i].swizzle[2] = src.alu.swizzle_z;
835 alu->src[i].swizzle[3] = src.alu.swizzle_w;
836 }
837 }
838
839 return alu;
840 }
841
842 static void
843 write_deref(write_ctx *ctx, const nir_deref_instr *deref)
844 {
845 assert(deref->deref_type < 8);
846 assert(deref->mode < (1 << 10));
847
848 union packed_instr header;
849 header.u32 = 0;
850
851 header.deref.instr_type = deref->instr.type;
852 header.deref.deref_type = deref->deref_type;
853 header.deref.mode = deref->mode;
854
855 write_dest(ctx, &deref->dest, header);
856 encode_type_to_blob(ctx->blob, deref->type);
857
858 if (deref->deref_type == nir_deref_type_var) {
859 write_object(ctx, deref->var);
860 return;
861 }
862
863 write_src(ctx, &deref->parent);
864
865 switch (deref->deref_type) {
866 case nir_deref_type_struct:
867 blob_write_uint32(ctx->blob, deref->strct.index);
868 break;
869
870 case nir_deref_type_array:
871 case nir_deref_type_ptr_as_array:
872 write_src(ctx, &deref->arr.index);
873 break;
874
875 case nir_deref_type_cast:
876 blob_write_uint32(ctx->blob, deref->cast.ptr_stride);
877 break;
878
879 case nir_deref_type_array_wildcard:
880 /* Nothing to do */
881 break;
882
883 default:
884 unreachable("Invalid deref type");
885 }
886 }
887
888 static nir_deref_instr *
889 read_deref(read_ctx *ctx, union packed_instr header)
890 {
891 nir_deref_type deref_type = header.deref.deref_type;
892 nir_deref_instr *deref = nir_deref_instr_create(ctx->nir, deref_type);
893
894 read_dest(ctx, &deref->dest, &deref->instr, header);
895
896 deref->mode = header.deref.mode;
897 deref->type = decode_type_from_blob(ctx->blob);
898
899 if (deref_type == nir_deref_type_var) {
900 deref->var = read_object(ctx);
901 return deref;
902 }
903
904 read_src(ctx, &deref->parent, &deref->instr);
905
906 switch (deref->deref_type) {
907 case nir_deref_type_struct:
908 deref->strct.index = blob_read_uint32(ctx->blob);
909 break;
910
911 case nir_deref_type_array:
912 case nir_deref_type_ptr_as_array:
913 read_src(ctx, &deref->arr.index, &deref->instr);
914 break;
915
916 case nir_deref_type_cast:
917 deref->cast.ptr_stride = blob_read_uint32(ctx->blob);
918 break;
919
920 case nir_deref_type_array_wildcard:
921 /* Nothing to do */
922 break;
923
924 default:
925 unreachable("Invalid deref type");
926 }
927
928 return deref;
929 }
930
931 static void
932 write_intrinsic(write_ctx *ctx, const nir_intrinsic_instr *intrin)
933 {
934 /* 9 bits for nir_intrinsic_op */
935 STATIC_ASSERT(nir_num_intrinsics <= 512);
936 unsigned num_srcs = nir_intrinsic_infos[intrin->intrinsic].num_srcs;
937 unsigned num_indices = nir_intrinsic_infos[intrin->intrinsic].num_indices;
938 assert(intrin->intrinsic < 512);
939
940 union packed_instr header;
941 header.u32 = 0;
942
943 header.intrinsic.instr_type = intrin->instr.type;
944 header.intrinsic.intrinsic = intrin->intrinsic;
945 header.intrinsic.num_components =
946 encode_num_components_in_3bits(intrin->num_components);
947
948 /* Analyze constant indices to decide how to encode them. */
949 if (num_indices) {
950 unsigned max_bits = 0;
951 for (unsigned i = 0; i < num_indices; i++) {
952 unsigned max = util_last_bit(intrin->const_index[i]);
953 max_bits = MAX2(max_bits, max);
954 }
955
956 if (max_bits * num_indices <= 6) {
957 header.intrinsic.const_indices_encoding = const_indices_6bit_all_combined;
958
959 /* Pack all const indices into 6 bits. */
960 unsigned bit_size = 6 / num_indices;
961 for (unsigned i = 0; i < num_indices; i++) {
962 header.intrinsic.packed_const_indices |=
963 intrin->const_index[i] << (i * bit_size);
964 }
965 } else if (max_bits <= 8)
966 header.intrinsic.const_indices_encoding = const_indices_8bit;
967 else if (max_bits <= 16)
968 header.intrinsic.const_indices_encoding = const_indices_16bit;
969 else
970 header.intrinsic.const_indices_encoding = const_indices_32bit;
971 }
972
973 if (nir_intrinsic_infos[intrin->intrinsic].has_dest)
974 write_dest(ctx, &intrin->dest, header);
975 else
976 blob_write_uint32(ctx->blob, header.u32);
977
978 for (unsigned i = 0; i < num_srcs; i++)
979 write_src(ctx, &intrin->src[i]);
980
981 if (num_indices) {
982 switch (header.intrinsic.const_indices_encoding) {
983 case const_indices_8bit:
984 for (unsigned i = 0; i < num_indices; i++)
985 blob_write_uint8(ctx->blob, intrin->const_index[i]);
986 break;
987 case const_indices_16bit:
988 for (unsigned i = 0; i < num_indices; i++)
989 blob_write_uint16(ctx->blob, intrin->const_index[i]);
990 break;
991 case const_indices_32bit:
992 for (unsigned i = 0; i < num_indices; i++)
993 blob_write_uint32(ctx->blob, intrin->const_index[i]);
994 break;
995 }
996 }
997 }
998
999 static nir_intrinsic_instr *
1000 read_intrinsic(read_ctx *ctx, union packed_instr header)
1001 {
1002 nir_intrinsic_op op = header.intrinsic.intrinsic;
1003 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(ctx->nir, op);
1004
1005 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
1006 unsigned num_indices = nir_intrinsic_infos[op].num_indices;
1007
1008 intrin->num_components =
1009 decode_num_components_in_3bits(header.intrinsic.num_components);
1010
1011 if (nir_intrinsic_infos[op].has_dest)
1012 read_dest(ctx, &intrin->dest, &intrin->instr, header);
1013
1014 for (unsigned i = 0; i < num_srcs; i++)
1015 read_src(ctx, &intrin->src[i], &intrin->instr);
1016
1017 if (num_indices) {
1018 switch (header.intrinsic.const_indices_encoding) {
1019 case const_indices_6bit_all_combined: {
1020 unsigned bit_size = 6 / num_indices;
1021 unsigned bit_mask = u_bit_consecutive(0, bit_size);
1022 for (unsigned i = 0; i < num_indices; i++) {
1023 intrin->const_index[i] =
1024 (header.intrinsic.packed_const_indices >> (i * bit_size)) &
1025 bit_mask;
1026 }
1027 break;
1028 }
1029 case const_indices_8bit:
1030 for (unsigned i = 0; i < num_indices; i++)
1031 intrin->const_index[i] = blob_read_uint8(ctx->blob);
1032 break;
1033 case const_indices_16bit:
1034 for (unsigned i = 0; i < num_indices; i++)
1035 intrin->const_index[i] = blob_read_uint16(ctx->blob);
1036 break;
1037 case const_indices_32bit:
1038 for (unsigned i = 0; i < num_indices; i++)
1039 intrin->const_index[i] = blob_read_uint32(ctx->blob);
1040 break;
1041 }
1042 }
1043
1044 return intrin;
1045 }
1046
1047 static void
1048 write_load_const(write_ctx *ctx, const nir_load_const_instr *lc)
1049 {
1050 assert(lc->def.num_components >= 1 && lc->def.num_components <= 16);
1051 union packed_instr header;
1052 header.u32 = 0;
1053
1054 header.load_const.instr_type = lc->instr.type;
1055 header.load_const.last_component = lc->def.num_components - 1;
1056 header.load_const.bit_size = encode_bit_size_3bits(lc->def.bit_size);
1057 header.load_const.packing = load_const_full;
1058
1059 /* Try to pack 1-component constants into the 19 free bits in the header. */
1060 if (lc->def.num_components == 1) {
1061 switch (lc->def.bit_size) {
1062 case 64:
1063 if ((lc->value[0].u64 & 0x1fffffffffffull) == 0) {
1064 /* packed_value contains high 19 bits, low bits are 0 */
1065 header.load_const.packing = load_const_scalar_hi_19bits;
1066 header.load_const.packed_value = lc->value[0].u64 >> 45;
1067 } else if (((lc->value[0].i64 << 45) >> 45) == lc->value[0].i64) {
1068 /* packed_value contains low 19 bits, high bits are sign-extended */
1069 header.load_const.packing = load_const_scalar_lo_19bits_sext;
1070 header.load_const.packed_value = lc->value[0].u64;
1071 }
1072 break;
1073
1074 case 32:
1075 if ((lc->value[0].u32 & 0x1fff) == 0) {
1076 header.load_const.packing = load_const_scalar_hi_19bits;
1077 header.load_const.packed_value = lc->value[0].u32 >> 13;
1078 } else if (((lc->value[0].i32 << 13) >> 13) == lc->value[0].i32) {
1079 header.load_const.packing = load_const_scalar_lo_19bits_sext;
1080 header.load_const.packed_value = lc->value[0].u32;
1081 }
1082 break;
1083
1084 case 16:
1085 header.load_const.packing = load_const_scalar_lo_19bits_sext;
1086 header.load_const.packed_value = lc->value[0].u16;
1087 break;
1088 case 8:
1089 header.load_const.packing = load_const_scalar_lo_19bits_sext;
1090 header.load_const.packed_value = lc->value[0].u8;
1091 break;
1092 case 1:
1093 header.load_const.packing = load_const_scalar_lo_19bits_sext;
1094 header.load_const.packed_value = lc->value[0].b;
1095 break;
1096 default:
1097 unreachable("invalid bit_size");
1098 }
1099 }
1100
1101 blob_write_uint32(ctx->blob, header.u32);
1102
1103 if (header.load_const.packing == load_const_full) {
1104 switch (lc->def.bit_size) {
1105 case 64:
1106 blob_write_bytes(ctx->blob, lc->value,
1107 sizeof(*lc->value) * lc->def.num_components);
1108 break;
1109
1110 case 32:
1111 for (unsigned i = 0; i < lc->def.num_components; i++)
1112 blob_write_uint32(ctx->blob, lc->value[i].u32);
1113 break;
1114
1115 case 16:
1116 for (unsigned i = 0; i < lc->def.num_components; i++)
1117 blob_write_uint16(ctx->blob, lc->value[i].u16);
1118 break;
1119
1120 default:
1121 assert(lc->def.bit_size <= 8);
1122 for (unsigned i = 0; i < lc->def.num_components; i++)
1123 blob_write_uint8(ctx->blob, lc->value[i].u8);
1124 break;
1125 }
1126 }
1127
1128 write_add_object(ctx, &lc->def);
1129 }
1130
1131 static nir_load_const_instr *
1132 read_load_const(read_ctx *ctx, union packed_instr header)
1133 {
1134 nir_load_const_instr *lc =
1135 nir_load_const_instr_create(ctx->nir, header.load_const.last_component + 1,
1136 decode_bit_size_3bits(header.load_const.bit_size));
1137
1138 switch (header.load_const.packing) {
1139 case load_const_scalar_hi_19bits:
1140 switch (lc->def.bit_size) {
1141 case 64:
1142 lc->value[0].u64 = (uint64_t)header.load_const.packed_value << 45;
1143 break;
1144 case 32:
1145 lc->value[0].u32 = (uint64_t)header.load_const.packed_value << 13;
1146 break;
1147 default:
1148 unreachable("invalid bit_size");
1149 }
1150 break;
1151
1152 case load_const_scalar_lo_19bits_sext:
1153 switch (lc->def.bit_size) {
1154 case 64:
1155 lc->value[0].i64 = ((int64_t)header.load_const.packed_value << 45) >> 45;
1156 break;
1157 case 32:
1158 lc->value[0].i32 = ((int32_t)header.load_const.packed_value << 13) >> 13;
1159 break;
1160 case 16:
1161 lc->value[0].u16 = header.load_const.packed_value;
1162 break;
1163 case 8:
1164 lc->value[0].u8 = header.load_const.packed_value;
1165 break;
1166 case 1:
1167 lc->value[0].b = header.load_const.packed_value;
1168 break;
1169 default:
1170 unreachable("invalid bit_size");
1171 }
1172 break;
1173
1174 case load_const_full:
1175 switch (lc->def.bit_size) {
1176 case 64:
1177 blob_copy_bytes(ctx->blob, lc->value, sizeof(*lc->value) * lc->def.num_components);
1178 break;
1179
1180 case 32:
1181 for (unsigned i = 0; i < lc->def.num_components; i++)
1182 lc->value[i].u32 = blob_read_uint32(ctx->blob);
1183 break;
1184
1185 case 16:
1186 for (unsigned i = 0; i < lc->def.num_components; i++)
1187 lc->value[i].u16 = blob_read_uint16(ctx->blob);
1188 break;
1189
1190 default:
1191 assert(lc->def.bit_size <= 8);
1192 for (unsigned i = 0; i < lc->def.num_components; i++)
1193 lc->value[i].u8 = blob_read_uint8(ctx->blob);
1194 break;
1195 }
1196 break;
1197 }
1198
1199 read_add_object(ctx, &lc->def);
1200 return lc;
1201 }
1202
1203 static void
1204 write_ssa_undef(write_ctx *ctx, const nir_ssa_undef_instr *undef)
1205 {
1206 assert(undef->def.num_components >= 1 && undef->def.num_components <= 16);
1207
1208 union packed_instr header;
1209 header.u32 = 0;
1210
1211 header.undef.instr_type = undef->instr.type;
1212 header.undef.last_component = undef->def.num_components - 1;
1213 header.undef.bit_size = encode_bit_size_3bits(undef->def.bit_size);
1214
1215 blob_write_uint32(ctx->blob, header.u32);
1216 write_add_object(ctx, &undef->def);
1217 }
1218
1219 static nir_ssa_undef_instr *
1220 read_ssa_undef(read_ctx *ctx, union packed_instr header)
1221 {
1222 nir_ssa_undef_instr *undef =
1223 nir_ssa_undef_instr_create(ctx->nir, header.undef.last_component + 1,
1224 decode_bit_size_3bits(header.undef.bit_size));
1225
1226 read_add_object(ctx, &undef->def);
1227 return undef;
1228 }
1229
1230 union packed_tex_data {
1231 uint32_t u32;
1232 struct {
1233 enum glsl_sampler_dim sampler_dim:4;
1234 nir_alu_type dest_type:8;
1235 unsigned coord_components:3;
1236 unsigned is_array:1;
1237 unsigned is_shadow:1;
1238 unsigned is_new_style_shadow:1;
1239 unsigned component:2;
1240 unsigned unused:10; /* Mark unused for valgrind. */
1241 } u;
1242 };
1243
1244 static void
1245 write_tex(write_ctx *ctx, const nir_tex_instr *tex)
1246 {
1247 assert(tex->num_srcs < 16);
1248 assert(tex->op < 16);
1249 assert(tex->texture_array_size < 1024);
1250
1251 union packed_instr header;
1252 header.u32 = 0;
1253
1254 header.tex.instr_type = tex->instr.type;
1255 header.tex.num_srcs = tex->num_srcs;
1256 header.tex.op = tex->op;
1257 header.tex.texture_array_size = tex->texture_array_size;
1258
1259 write_dest(ctx, &tex->dest, header);
1260
1261 blob_write_uint32(ctx->blob, tex->texture_index);
1262 blob_write_uint32(ctx->blob, tex->sampler_index);
1263 if (tex->op == nir_texop_tg4)
1264 blob_write_bytes(ctx->blob, tex->tg4_offsets, sizeof(tex->tg4_offsets));
1265
1266 STATIC_ASSERT(sizeof(union packed_tex_data) == sizeof(uint32_t));
1267 union packed_tex_data packed = {
1268 .u.sampler_dim = tex->sampler_dim,
1269 .u.dest_type = tex->dest_type,
1270 .u.coord_components = tex->coord_components,
1271 .u.is_array = tex->is_array,
1272 .u.is_shadow = tex->is_shadow,
1273 .u.is_new_style_shadow = tex->is_new_style_shadow,
1274 .u.component = tex->component,
1275 };
1276 blob_write_uint32(ctx->blob, packed.u32);
1277
1278 for (unsigned i = 0; i < tex->num_srcs; i++) {
1279 union packed_src src;
1280 src.u32 = 0;
1281 src.tex.src_type = tex->src[i].src_type;
1282 write_src_full(ctx, &tex->src[i].src, src);
1283 }
1284 }
1285
1286 static nir_tex_instr *
1287 read_tex(read_ctx *ctx, union packed_instr header)
1288 {
1289 nir_tex_instr *tex = nir_tex_instr_create(ctx->nir, header.tex.num_srcs);
1290
1291 read_dest(ctx, &tex->dest, &tex->instr, header);
1292
1293 tex->op = header.tex.op;
1294 tex->texture_index = blob_read_uint32(ctx->blob);
1295 tex->texture_array_size = header.tex.texture_array_size;
1296 tex->sampler_index = blob_read_uint32(ctx->blob);
1297 if (tex->op == nir_texop_tg4)
1298 blob_copy_bytes(ctx->blob, tex->tg4_offsets, sizeof(tex->tg4_offsets));
1299
1300 union packed_tex_data packed;
1301 packed.u32 = blob_read_uint32(ctx->blob);
1302 tex->sampler_dim = packed.u.sampler_dim;
1303 tex->dest_type = packed.u.dest_type;
1304 tex->coord_components = packed.u.coord_components;
1305 tex->is_array = packed.u.is_array;
1306 tex->is_shadow = packed.u.is_shadow;
1307 tex->is_new_style_shadow = packed.u.is_new_style_shadow;
1308 tex->component = packed.u.component;
1309
1310 for (unsigned i = 0; i < tex->num_srcs; i++) {
1311 union packed_src src = read_src(ctx, &tex->src[i].src, &tex->instr);
1312 tex->src[i].src_type = src.tex.src_type;
1313 }
1314
1315 return tex;
1316 }
1317
1318 static void
1319 write_phi(write_ctx *ctx, const nir_phi_instr *phi)
1320 {
1321 union packed_instr header;
1322 header.u32 = 0;
1323
1324 header.phi.instr_type = phi->instr.type;
1325 header.phi.num_srcs = exec_list_length(&phi->srcs);
1326
1327 /* Phi nodes are special, since they may reference SSA definitions and
1328 * basic blocks that don't exist yet. We leave two empty uint32_t's here,
1329 * and then store enough information so that a later fixup pass can fill
1330 * them in correctly.
1331 */
1332 write_dest(ctx, &phi->dest, header);
1333
1334 nir_foreach_phi_src(src, phi) {
1335 assert(src->src.is_ssa);
1336 size_t blob_offset = blob_reserve_uint32(ctx->blob);
1337 ASSERTED size_t blob_offset2 = blob_reserve_uint32(ctx->blob);
1338 assert(blob_offset + sizeof(uint32_t) == blob_offset2);
1339 write_phi_fixup fixup = {
1340 .blob_offset = blob_offset,
1341 .src = src->src.ssa,
1342 .block = src->pred,
1343 };
1344 util_dynarray_append(&ctx->phi_fixups, write_phi_fixup, fixup);
1345 }
1346 }
1347
1348 static void
1349 write_fixup_phis(write_ctx *ctx)
1350 {
1351 util_dynarray_foreach(&ctx->phi_fixups, write_phi_fixup, fixup) {
1352 uint32_t *blob_ptr = (uint32_t *)(ctx->blob->data + fixup->blob_offset);
1353 blob_ptr[0] = write_lookup_object(ctx, fixup->src);
1354 blob_ptr[1] = write_lookup_object(ctx, fixup->block);
1355 }
1356
1357 util_dynarray_clear(&ctx->phi_fixups);
1358 }
1359
1360 static nir_phi_instr *
1361 read_phi(read_ctx *ctx, nir_block *blk, union packed_instr header)
1362 {
1363 nir_phi_instr *phi = nir_phi_instr_create(ctx->nir);
1364
1365 read_dest(ctx, &phi->dest, &phi->instr, header);
1366
1367 /* For similar reasons as before, we just store the index directly into the
1368 * pointer, and let a later pass resolve the phi sources.
1369 *
1370 * In order to ensure that the copied sources (which are just the indices
1371 * from the blob for now) don't get inserted into the old shader's use-def
1372 * lists, we have to add the phi instruction *before* we set up its
1373 * sources.
1374 */
1375 nir_instr_insert_after_block(blk, &phi->instr);
1376
1377 for (unsigned i = 0; i < header.phi.num_srcs; i++) {
1378 nir_phi_src *src = ralloc(phi, nir_phi_src);
1379
1380 src->src.is_ssa = true;
1381 src->src.ssa = (nir_ssa_def *)(uintptr_t) blob_read_uint32(ctx->blob);
1382 src->pred = (nir_block *)(uintptr_t) blob_read_uint32(ctx->blob);
1383
1384 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
1385 * we have to set the parent_instr manually. It doesn't really matter
1386 * when we do it, so we might as well do it here.
1387 */
1388 src->src.parent_instr = &phi->instr;
1389
1390 /* Stash it in the list of phi sources. We'll walk this list and fix up
1391 * sources at the very end of read_function_impl.
1392 */
1393 list_add(&src->src.use_link, &ctx->phi_srcs);
1394
1395 exec_list_push_tail(&phi->srcs, &src->node);
1396 }
1397
1398 return phi;
1399 }
1400
1401 static void
1402 read_fixup_phis(read_ctx *ctx)
1403 {
1404 list_for_each_entry_safe(nir_phi_src, src, &ctx->phi_srcs, src.use_link) {
1405 src->pred = read_lookup_object(ctx, (uintptr_t)src->pred);
1406 src->src.ssa = read_lookup_object(ctx, (uintptr_t)src->src.ssa);
1407
1408 /* Remove from this list */
1409 list_del(&src->src.use_link);
1410
1411 list_addtail(&src->src.use_link, &src->src.ssa->uses);
1412 }
1413 assert(list_is_empty(&ctx->phi_srcs));
1414 }
1415
1416 static void
1417 write_jump(write_ctx *ctx, const nir_jump_instr *jmp)
1418 {
1419 assert(jmp->type < 4);
1420
1421 union packed_instr header;
1422 header.u32 = 0;
1423
1424 header.jump.instr_type = jmp->instr.type;
1425 header.jump.type = jmp->type;
1426
1427 blob_write_uint32(ctx->blob, header.u32);
1428 }
1429
1430 static nir_jump_instr *
1431 read_jump(read_ctx *ctx, union packed_instr header)
1432 {
1433 nir_jump_instr *jmp = nir_jump_instr_create(ctx->nir, header.jump.type);
1434 return jmp;
1435 }
1436
1437 static void
1438 write_call(write_ctx *ctx, const nir_call_instr *call)
1439 {
1440 blob_write_uint32(ctx->blob, write_lookup_object(ctx, call->callee));
1441
1442 for (unsigned i = 0; i < call->num_params; i++)
1443 write_src(ctx, &call->params[i]);
1444 }
1445
1446 static nir_call_instr *
1447 read_call(read_ctx *ctx)
1448 {
1449 nir_function *callee = read_object(ctx);
1450 nir_call_instr *call = nir_call_instr_create(ctx->nir, callee);
1451
1452 for (unsigned i = 0; i < call->num_params; i++)
1453 read_src(ctx, &call->params[i], call);
1454
1455 return call;
1456 }
1457
1458 static void
1459 write_instr(write_ctx *ctx, const nir_instr *instr)
1460 {
1461 /* We have only 4 bits for the instruction type. */
1462 assert(instr->type < 16);
1463
1464 switch (instr->type) {
1465 case nir_instr_type_alu:
1466 write_alu(ctx, nir_instr_as_alu(instr));
1467 break;
1468 case nir_instr_type_deref:
1469 write_deref(ctx, nir_instr_as_deref(instr));
1470 break;
1471 case nir_instr_type_intrinsic:
1472 write_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1473 break;
1474 case nir_instr_type_load_const:
1475 write_load_const(ctx, nir_instr_as_load_const(instr));
1476 break;
1477 case nir_instr_type_ssa_undef:
1478 write_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
1479 break;
1480 case nir_instr_type_tex:
1481 write_tex(ctx, nir_instr_as_tex(instr));
1482 break;
1483 case nir_instr_type_phi:
1484 write_phi(ctx, nir_instr_as_phi(instr));
1485 break;
1486 case nir_instr_type_jump:
1487 write_jump(ctx, nir_instr_as_jump(instr));
1488 break;
1489 case nir_instr_type_call:
1490 blob_write_uint32(ctx->blob, instr->type);
1491 write_call(ctx, nir_instr_as_call(instr));
1492 break;
1493 case nir_instr_type_parallel_copy:
1494 unreachable("Cannot write parallel copies");
1495 default:
1496 unreachable("bad instr type");
1497 }
1498 }
1499
1500 static void
1501 read_instr(read_ctx *ctx, nir_block *block)
1502 {
1503 STATIC_ASSERT(sizeof(union packed_instr) == 4);
1504 union packed_instr header;
1505 header.u32 = blob_read_uint32(ctx->blob);
1506 nir_instr *instr;
1507
1508 switch (header.any.instr_type) {
1509 case nir_instr_type_alu:
1510 instr = &read_alu(ctx, header)->instr;
1511 break;
1512 case nir_instr_type_deref:
1513 instr = &read_deref(ctx, header)->instr;
1514 break;
1515 case nir_instr_type_intrinsic:
1516 instr = &read_intrinsic(ctx, header)->instr;
1517 break;
1518 case nir_instr_type_load_const:
1519 instr = &read_load_const(ctx, header)->instr;
1520 break;
1521 case nir_instr_type_ssa_undef:
1522 instr = &read_ssa_undef(ctx, header)->instr;
1523 break;
1524 case nir_instr_type_tex:
1525 instr = &read_tex(ctx, header)->instr;
1526 break;
1527 case nir_instr_type_phi:
1528 /* Phi instructions are a bit of a special case when reading because we
1529 * don't want inserting the instruction to automatically handle use/defs
1530 * for us. Instead, we need to wait until all the blocks/instructions
1531 * are read so that we can set their sources up.
1532 */
1533 read_phi(ctx, block, header);
1534 return;
1535 case nir_instr_type_jump:
1536 instr = &read_jump(ctx, header)->instr;
1537 break;
1538 case nir_instr_type_call:
1539 instr = &read_call(ctx)->instr;
1540 break;
1541 case nir_instr_type_parallel_copy:
1542 unreachable("Cannot read parallel copies");
1543 default:
1544 unreachable("bad instr type");
1545 }
1546
1547 nir_instr_insert_after_block(block, instr);
1548 }
1549
1550 static void
1551 write_block(write_ctx *ctx, const nir_block *block)
1552 {
1553 write_add_object(ctx, block);
1554 blob_write_uint32(ctx->blob, exec_list_length(&block->instr_list));
1555 nir_foreach_instr(instr, block)
1556 write_instr(ctx, instr);
1557 }
1558
1559 static void
1560 read_block(read_ctx *ctx, struct exec_list *cf_list)
1561 {
1562 /* Don't actually create a new block. Just use the one from the tail of
1563 * the list. NIR guarantees that the tail of the list is a block and that
1564 * no two blocks are side-by-side in the IR; It should be empty.
1565 */
1566 nir_block *block =
1567 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
1568
1569 read_add_object(ctx, block);
1570 unsigned num_instrs = blob_read_uint32(ctx->blob);
1571 for (unsigned i = 0; i < num_instrs; i++) {
1572 read_instr(ctx, block);
1573 }
1574 }
1575
1576 static void
1577 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list);
1578
1579 static void
1580 read_cf_list(read_ctx *ctx, struct exec_list *cf_list);
1581
1582 static void
1583 write_if(write_ctx *ctx, nir_if *nif)
1584 {
1585 write_src(ctx, &nif->condition);
1586
1587 write_cf_list(ctx, &nif->then_list);
1588 write_cf_list(ctx, &nif->else_list);
1589 }
1590
1591 static void
1592 read_if(read_ctx *ctx, struct exec_list *cf_list)
1593 {
1594 nir_if *nif = nir_if_create(ctx->nir);
1595
1596 read_src(ctx, &nif->condition, nif);
1597
1598 nir_cf_node_insert_end(cf_list, &nif->cf_node);
1599
1600 read_cf_list(ctx, &nif->then_list);
1601 read_cf_list(ctx, &nif->else_list);
1602 }
1603
1604 static void
1605 write_loop(write_ctx *ctx, nir_loop *loop)
1606 {
1607 write_cf_list(ctx, &loop->body);
1608 }
1609
1610 static void
1611 read_loop(read_ctx *ctx, struct exec_list *cf_list)
1612 {
1613 nir_loop *loop = nir_loop_create(ctx->nir);
1614
1615 nir_cf_node_insert_end(cf_list, &loop->cf_node);
1616
1617 read_cf_list(ctx, &loop->body);
1618 }
1619
1620 static void
1621 write_cf_node(write_ctx *ctx, nir_cf_node *cf)
1622 {
1623 blob_write_uint32(ctx->blob, cf->type);
1624
1625 switch (cf->type) {
1626 case nir_cf_node_block:
1627 write_block(ctx, nir_cf_node_as_block(cf));
1628 break;
1629 case nir_cf_node_if:
1630 write_if(ctx, nir_cf_node_as_if(cf));
1631 break;
1632 case nir_cf_node_loop:
1633 write_loop(ctx, nir_cf_node_as_loop(cf));
1634 break;
1635 default:
1636 unreachable("bad cf type");
1637 }
1638 }
1639
1640 static void
1641 read_cf_node(read_ctx *ctx, struct exec_list *list)
1642 {
1643 nir_cf_node_type type = blob_read_uint32(ctx->blob);
1644
1645 switch (type) {
1646 case nir_cf_node_block:
1647 read_block(ctx, list);
1648 break;
1649 case nir_cf_node_if:
1650 read_if(ctx, list);
1651 break;
1652 case nir_cf_node_loop:
1653 read_loop(ctx, list);
1654 break;
1655 default:
1656 unreachable("bad cf type");
1657 }
1658 }
1659
1660 static void
1661 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list)
1662 {
1663 blob_write_uint32(ctx->blob, exec_list_length(cf_list));
1664 foreach_list_typed(nir_cf_node, cf, node, cf_list) {
1665 write_cf_node(ctx, cf);
1666 }
1667 }
1668
1669 static void
1670 read_cf_list(read_ctx *ctx, struct exec_list *cf_list)
1671 {
1672 uint32_t num_cf_nodes = blob_read_uint32(ctx->blob);
1673 for (unsigned i = 0; i < num_cf_nodes; i++)
1674 read_cf_node(ctx, cf_list);
1675 }
1676
1677 static void
1678 write_function_impl(write_ctx *ctx, const nir_function_impl *fi)
1679 {
1680 write_var_list(ctx, &fi->locals);
1681 write_reg_list(ctx, &fi->registers);
1682 blob_write_uint32(ctx->blob, fi->reg_alloc);
1683
1684 write_cf_list(ctx, &fi->body);
1685 write_fixup_phis(ctx);
1686 }
1687
1688 static nir_function_impl *
1689 read_function_impl(read_ctx *ctx, nir_function *fxn)
1690 {
1691 nir_function_impl *fi = nir_function_impl_create_bare(ctx->nir);
1692 fi->function = fxn;
1693
1694 read_var_list(ctx, &fi->locals);
1695 read_reg_list(ctx, &fi->registers);
1696 fi->reg_alloc = blob_read_uint32(ctx->blob);
1697
1698 read_cf_list(ctx, &fi->body);
1699 read_fixup_phis(ctx);
1700
1701 fi->valid_metadata = 0;
1702
1703 return fi;
1704 }
1705
1706 static void
1707 write_function(write_ctx *ctx, const nir_function *fxn)
1708 {
1709 uint32_t flags = fxn->is_entrypoint;
1710 if (fxn->name)
1711 flags |= 0x2;
1712 if (fxn->impl)
1713 flags |= 0x4;
1714 blob_write_uint32(ctx->blob, flags);
1715 if (fxn->name)
1716 blob_write_string(ctx->blob, fxn->name);
1717
1718 write_add_object(ctx, fxn);
1719
1720 blob_write_uint32(ctx->blob, fxn->num_params);
1721 for (unsigned i = 0; i < fxn->num_params; i++) {
1722 uint32_t val =
1723 ((uint32_t)fxn->params[i].num_components) |
1724 ((uint32_t)fxn->params[i].bit_size) << 8;
1725 blob_write_uint32(ctx->blob, val);
1726 }
1727
1728 /* At first glance, it looks like we should write the function_impl here.
1729 * However, call instructions need to be able to reference at least the
1730 * function and those will get processed as we write the function_impls.
1731 * We stop here and write function_impls as a second pass.
1732 */
1733 }
1734
1735 static void
1736 read_function(read_ctx *ctx)
1737 {
1738 uint32_t flags = blob_read_uint32(ctx->blob);
1739 bool has_name = flags & 0x2;
1740 char *name = has_name ? blob_read_string(ctx->blob) : NULL;
1741
1742 nir_function *fxn = nir_function_create(ctx->nir, name);
1743
1744 read_add_object(ctx, fxn);
1745
1746 fxn->num_params = blob_read_uint32(ctx->blob);
1747 fxn->params = ralloc_array(fxn, nir_parameter, fxn->num_params);
1748 for (unsigned i = 0; i < fxn->num_params; i++) {
1749 uint32_t val = blob_read_uint32(ctx->blob);
1750 fxn->params[i].num_components = val & 0xff;
1751 fxn->params[i].bit_size = (val >> 8) & 0xff;
1752 }
1753
1754 fxn->is_entrypoint = flags & 0x1;
1755 if (flags & 0x4)
1756 fxn->impl = NIR_SERIALIZE_FUNC_HAS_IMPL;
1757 }
1758
1759 /**
1760 * Serialize NIR into a binary blob.
1761 *
1762 * \param strip Don't serialize information only useful for debugging,
1763 * such as variable names, making cache hits from similar
1764 * shaders more likely.
1765 */
1766 void
1767 nir_serialize(struct blob *blob, const nir_shader *nir, bool strip)
1768 {
1769 write_ctx ctx = {0};
1770 ctx.remap_table = _mesa_pointer_hash_table_create(NULL);
1771 ctx.blob = blob;
1772 ctx.nir = nir;
1773 ctx.strip = strip;
1774 util_dynarray_init(&ctx.phi_fixups, NULL);
1775
1776 size_t idx_size_offset = blob_reserve_uint32(blob);
1777
1778 struct shader_info info = nir->info;
1779 uint32_t strings = 0;
1780 if (!strip && info.name)
1781 strings |= 0x1;
1782 if (!strip && info.label)
1783 strings |= 0x2;
1784 blob_write_uint32(blob, strings);
1785 if (!strip && info.name)
1786 blob_write_string(blob, info.name);
1787 if (!strip && info.label)
1788 blob_write_string(blob, info.label);
1789 info.name = info.label = NULL;
1790 blob_write_bytes(blob, (uint8_t *) &info, sizeof(info));
1791
1792 write_var_list(&ctx, &nir->uniforms);
1793 write_var_list(&ctx, &nir->inputs);
1794 write_var_list(&ctx, &nir->outputs);
1795 write_var_list(&ctx, &nir->shared);
1796 write_var_list(&ctx, &nir->globals);
1797 write_var_list(&ctx, &nir->system_values);
1798
1799 blob_write_uint32(blob, nir->num_inputs);
1800 blob_write_uint32(blob, nir->num_uniforms);
1801 blob_write_uint32(blob, nir->num_outputs);
1802 blob_write_uint32(blob, nir->num_shared);
1803 blob_write_uint32(blob, nir->scratch_size);
1804
1805 blob_write_uint32(blob, exec_list_length(&nir->functions));
1806 nir_foreach_function(fxn, nir) {
1807 write_function(&ctx, fxn);
1808 }
1809
1810 nir_foreach_function(fxn, nir) {
1811 if (fxn->impl)
1812 write_function_impl(&ctx, fxn->impl);
1813 }
1814
1815 blob_write_uint32(blob, nir->constant_data_size);
1816 if (nir->constant_data_size > 0)
1817 blob_write_bytes(blob, nir->constant_data, nir->constant_data_size);
1818
1819 *(uint32_t *)(blob->data + idx_size_offset) = ctx.next_idx;
1820
1821 _mesa_hash_table_destroy(ctx.remap_table, NULL);
1822 util_dynarray_fini(&ctx.phi_fixups);
1823 }
1824
1825 nir_shader *
1826 nir_deserialize(void *mem_ctx,
1827 const struct nir_shader_compiler_options *options,
1828 struct blob_reader *blob)
1829 {
1830 read_ctx ctx = {0};
1831 ctx.blob = blob;
1832 list_inithead(&ctx.phi_srcs);
1833 ctx.idx_table_len = blob_read_uint32(blob);
1834 ctx.idx_table = calloc(ctx.idx_table_len, sizeof(uintptr_t));
1835
1836 uint32_t strings = blob_read_uint32(blob);
1837 char *name = (strings & 0x1) ? blob_read_string(blob) : NULL;
1838 char *label = (strings & 0x2) ? blob_read_string(blob) : NULL;
1839
1840 struct shader_info info;
1841 blob_copy_bytes(blob, (uint8_t *) &info, sizeof(info));
1842
1843 ctx.nir = nir_shader_create(mem_ctx, info.stage, options, NULL);
1844
1845 info.name = name ? ralloc_strdup(ctx.nir, name) : NULL;
1846 info.label = label ? ralloc_strdup(ctx.nir, label) : NULL;
1847
1848 ctx.nir->info = info;
1849
1850 read_var_list(&ctx, &ctx.nir->uniforms);
1851 read_var_list(&ctx, &ctx.nir->inputs);
1852 read_var_list(&ctx, &ctx.nir->outputs);
1853 read_var_list(&ctx, &ctx.nir->shared);
1854 read_var_list(&ctx, &ctx.nir->globals);
1855 read_var_list(&ctx, &ctx.nir->system_values);
1856
1857 ctx.nir->num_inputs = blob_read_uint32(blob);
1858 ctx.nir->num_uniforms = blob_read_uint32(blob);
1859 ctx.nir->num_outputs = blob_read_uint32(blob);
1860 ctx.nir->num_shared = blob_read_uint32(blob);
1861 ctx.nir->scratch_size = blob_read_uint32(blob);
1862
1863 unsigned num_functions = blob_read_uint32(blob);
1864 for (unsigned i = 0; i < num_functions; i++)
1865 read_function(&ctx);
1866
1867 nir_foreach_function(fxn, ctx.nir) {
1868 if (fxn->impl == NIR_SERIALIZE_FUNC_HAS_IMPL)
1869 fxn->impl = read_function_impl(&ctx, fxn);
1870 }
1871
1872 ctx.nir->constant_data_size = blob_read_uint32(blob);
1873 if (ctx.nir->constant_data_size > 0) {
1874 ctx.nir->constant_data =
1875 ralloc_size(ctx.nir, ctx.nir->constant_data_size);
1876 blob_copy_bytes(blob, ctx.nir->constant_data,
1877 ctx.nir->constant_data_size);
1878 }
1879
1880 free(ctx.idx_table);
1881
1882 return ctx.nir;
1883 }
1884
1885 void
1886 nir_shader_serialize_deserialize(nir_shader *shader)
1887 {
1888 const struct nir_shader_compiler_options *options = shader->options;
1889
1890 struct blob writer;
1891 blob_init(&writer);
1892 nir_serialize(&writer, shader, false);
1893
1894 /* Delete all of dest's ralloc children but leave dest alone */
1895 void *dead_ctx = ralloc_context(NULL);
1896 ralloc_adopt(dead_ctx, shader);
1897 ralloc_free(dead_ctx);
1898
1899 dead_ctx = ralloc_context(NULL);
1900
1901 struct blob_reader reader;
1902 blob_reader_init(&reader, writer.data, writer.size);
1903 nir_shader *copy = nir_deserialize(dead_ctx, options, &reader);
1904
1905 blob_finish(&writer);
1906
1907 nir_shader_replace(shader, copy);
1908 ralloc_free(dead_ctx);
1909 }