2 * Copyright © 2017 Connor Abbott
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
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
24 #include "nir_serialize.h"
25 #include "nir_control_flow.h"
26 #include "util/u_dynarray.h"
27 #include "util/u_math.h"
29 #define NIR_SERIALIZE_FUNC_HAS_IMPL ((void *)(intptr_t)1)
30 #define MAX_OBJECT_IDS (1 << 20)
39 const nir_shader
*nir
;
43 /* maps pointer to index */
44 struct hash_table
*remap_table
;
46 /* the next index to assign to a NIR in-memory object */
49 /* Array of write_phi_fixup structs representing phi sources that need to
50 * be resolved in the second pass.
52 struct util_dynarray phi_fixups
;
54 /* Don't write optional data such as variable names. */
61 struct blob_reader
*blob
;
63 /* the next index to assign to a NIR in-memory object */
66 /* The length of the index -> object table */
67 uint32_t idx_table_len
;
69 /* map from index to deserialized pointer */
72 /* List of phi sources. */
73 struct list_head phi_srcs
;
78 write_add_object(write_ctx
*ctx
, const void *obj
)
80 uint32_t index
= ctx
->next_idx
++;
81 assert(index
!= MAX_OBJECT_IDS
);
82 _mesa_hash_table_insert(ctx
->remap_table
, obj
, (void *)(uintptr_t) index
);
86 write_lookup_object(write_ctx
*ctx
, const void *obj
)
88 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->remap_table
, obj
);
90 return (uint32_t)(uintptr_t) entry
->data
;
94 write_object(write_ctx
*ctx
, const void *obj
)
96 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, obj
));
100 read_add_object(read_ctx
*ctx
, void *obj
)
102 assert(ctx
->next_idx
< ctx
->idx_table_len
);
103 ctx
->idx_table
[ctx
->next_idx
++] = obj
;
107 read_lookup_object(read_ctx
*ctx
, uint32_t idx
)
109 assert(idx
< ctx
->idx_table_len
);
110 return ctx
->idx_table
[idx
];
114 read_object(read_ctx
*ctx
)
116 return read_lookup_object(ctx
, blob_read_uint32(ctx
->blob
));
120 encode_bit_size_3bits(uint8_t bit_size
)
122 /* Encode values of 0, 1, 2, 4, 8, 16, 32, 64 in 3 bits. */
123 assert(bit_size
<= 64 && util_is_power_of_two_or_zero(bit_size
));
125 return util_logbase2(bit_size
) + 1;
130 decode_bit_size_3bits(uint8_t bit_size
)
133 return 1 << (bit_size
- 1);
138 encode_num_components_in_3bits(uint8_t num_components
)
140 if (num_components
<= 4)
141 return num_components
;
142 if (num_components
== 8)
144 if (num_components
== 16)
147 unreachable("invalid number in num_components");
152 decode_num_components_in_3bits(uint8_t value
)
161 unreachable("invalid num_components encoding");
166 write_constant(write_ctx
*ctx
, const nir_constant
*c
)
168 blob_write_bytes(ctx
->blob
, c
->values
, sizeof(c
->values
));
169 blob_write_uint32(ctx
->blob
, c
->num_elements
);
170 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
171 write_constant(ctx
, c
->elements
[i
]);
174 static nir_constant
*
175 read_constant(read_ctx
*ctx
, nir_variable
*nvar
)
177 nir_constant
*c
= ralloc(nvar
, nir_constant
);
179 blob_copy_bytes(ctx
->blob
, (uint8_t *)c
->values
, sizeof(c
->values
));
180 c
->num_elements
= blob_read_uint32(ctx
->blob
);
181 c
->elements
= ralloc_array(nvar
, nir_constant
*, c
->num_elements
);
182 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
183 c
->elements
[i
] = read_constant(ctx
, nvar
);
192 unsigned has_constant_initializer
:1;
193 unsigned has_interface_type
:1;
194 unsigned num_state_slots
:13;
195 unsigned num_members
:16;
200 write_variable(write_ctx
*ctx
, const nir_variable
*var
)
202 write_add_object(ctx
, var
);
203 encode_type_to_blob(ctx
->blob
, var
->type
);
205 assert(var
->num_state_slots
< (1 << 13));
206 assert(var
->num_members
< (1 << 16));
208 STATIC_ASSERT(sizeof(union packed_var
) == 4);
209 union packed_var flags
;
212 flags
.u
.has_name
= !ctx
->strip
&& var
->name
;
213 flags
.u
.has_constant_initializer
= !!(var
->constant_initializer
);
214 flags
.u
.has_interface_type
= !!(var
->interface_type
);
215 flags
.u
.num_state_slots
= var
->num_state_slots
;
216 flags
.u
.num_members
= var
->num_members
;
218 blob_write_uint32(ctx
->blob
, flags
.u32
);
220 if (flags
.u
.has_name
)
221 blob_write_string(ctx
->blob
, var
->name
);
223 struct nir_variable_data data
= var
->data
;
225 /* When stripping, we expect that the location is no longer needed,
226 * which is typically after shaders are linked.
229 data
.mode
!= nir_var_shader_in
&&
230 data
.mode
!= nir_var_shader_out
)
233 blob_write_bytes(ctx
->blob
, &data
, sizeof(data
));
235 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
236 blob_write_bytes(ctx
->blob
, &var
->state_slots
[i
],
237 sizeof(var
->state_slots
[i
]));
239 if (var
->constant_initializer
)
240 write_constant(ctx
, var
->constant_initializer
);
241 if (var
->interface_type
)
242 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
243 if (var
->num_members
> 0) {
244 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->members
,
245 var
->num_members
* sizeof(*var
->members
));
249 static nir_variable
*
250 read_variable(read_ctx
*ctx
)
252 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
253 read_add_object(ctx
, var
);
255 var
->type
= decode_type_from_blob(ctx
->blob
);
257 union packed_var flags
;
258 flags
.u32
= blob_read_uint32(ctx
->blob
);
260 if (flags
.u
.has_name
) {
261 const char *name
= blob_read_string(ctx
->blob
);
262 var
->name
= ralloc_strdup(var
, name
);
266 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
267 var
->num_state_slots
= flags
.u
.num_state_slots
;
268 if (var
->num_state_slots
!= 0) {
269 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
270 var
->num_state_slots
);
271 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
272 blob_copy_bytes(ctx
->blob
, &var
->state_slots
[i
],
273 sizeof(var
->state_slots
[i
]));
276 if (flags
.u
.has_constant_initializer
)
277 var
->constant_initializer
= read_constant(ctx
, var
);
279 var
->constant_initializer
= NULL
;
280 if (flags
.u
.has_interface_type
)
281 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
283 var
->interface_type
= NULL
;
284 var
->num_members
= flags
.u
.num_members
;
285 if (var
->num_members
> 0) {
286 var
->members
= ralloc_array(var
, struct nir_variable_data
,
288 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->members
,
289 var
->num_members
* sizeof(*var
->members
));
296 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
298 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
299 foreach_list_typed(nir_variable
, var
, node
, src
) {
300 write_variable(ctx
, var
);
305 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
307 exec_list_make_empty(dst
);
308 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
309 for (unsigned i
= 0; i
< num_vars
; i
++) {
310 nir_variable
*var
= read_variable(ctx
);
311 exec_list_push_tail(dst
, &var
->node
);
316 write_register(write_ctx
*ctx
, const nir_register
*reg
)
318 write_add_object(ctx
, reg
);
319 blob_write_uint32(ctx
->blob
, reg
->num_components
);
320 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
321 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
322 blob_write_uint32(ctx
->blob
, reg
->index
);
323 blob_write_uint32(ctx
->blob
, !ctx
->strip
&& reg
->name
);
324 if (!ctx
->strip
&& reg
->name
)
325 blob_write_string(ctx
->blob
, reg
->name
);
328 static nir_register
*
329 read_register(read_ctx
*ctx
)
331 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
332 read_add_object(ctx
, reg
);
333 reg
->num_components
= blob_read_uint32(ctx
->blob
);
334 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
335 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
336 reg
->index
= blob_read_uint32(ctx
->blob
);
337 bool has_name
= blob_read_uint32(ctx
->blob
);
339 const char *name
= blob_read_string(ctx
->blob
);
340 reg
->name
= ralloc_strdup(reg
, name
);
345 list_inithead(®
->uses
);
346 list_inithead(®
->defs
);
347 list_inithead(®
->if_uses
);
353 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
355 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
356 foreach_list_typed(nir_register
, reg
, node
, src
)
357 write_register(ctx
, reg
);
361 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
363 exec_list_make_empty(dst
);
364 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
365 for (unsigned i
= 0; i
< num_regs
; i
++) {
366 nir_register
*reg
= read_register(ctx
);
367 exec_list_push_tail(dst
, ®
->node
);
374 unsigned is_ssa
:1; /* <-- Header */
375 unsigned is_indirect
:1;
376 unsigned object_idx
:20;
377 unsigned _footer
:10; /* <-- Footer */
380 unsigned _header
:22; /* <-- Header */
381 unsigned negate
:1; /* <-- Footer */
383 unsigned swizzle_x
:2;
384 unsigned swizzle_y
:2;
385 unsigned swizzle_z
:2;
386 unsigned swizzle_w
:2;
389 unsigned _header
:22; /* <-- Header */
390 unsigned src_type
:5; /* <-- Footer */
396 write_src_full(write_ctx
*ctx
, const nir_src
*src
, union packed_src header
)
398 /* Since sources are very frequent, we try to save some space when storing
399 * them. In particular, we store whether the source is a register and
400 * whether the register has an indirect index in the low two bits. We can
401 * assume that the high two bits of the index are zero, since otherwise our
402 * address space would've been exhausted allocating the remap table!
404 header
.any
.is_ssa
= src
->is_ssa
;
406 header
.any
.object_idx
= write_lookup_object(ctx
, src
->ssa
);
407 blob_write_uint32(ctx
->blob
, header
.u32
);
409 header
.any
.object_idx
= write_lookup_object(ctx
, src
->reg
.reg
);
410 header
.any
.is_indirect
= !!src
->reg
.indirect
;
411 blob_write_uint32(ctx
->blob
, header
.u32
);
412 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
413 if (src
->reg
.indirect
) {
414 union packed_src header
= {0};
415 write_src_full(ctx
, src
->reg
.indirect
, header
);
421 write_src(write_ctx
*ctx
, const nir_src
*src
)
423 union packed_src header
= {0};
424 write_src_full(ctx
, src
, header
);
427 static union packed_src
428 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
430 STATIC_ASSERT(sizeof(union packed_src
) == 4);
431 union packed_src header
;
432 header
.u32
= blob_read_uint32(ctx
->blob
);
434 src
->is_ssa
= header
.any
.is_ssa
;
436 src
->ssa
= read_lookup_object(ctx
, header
.any
.object_idx
);
438 src
->reg
.reg
= read_lookup_object(ctx
, header
.any
.object_idx
);
439 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
440 if (header
.any
.is_indirect
) {
441 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
442 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
444 src
->reg
.indirect
= NULL
;
455 uint8_t num_components
:3;
460 uint8_t is_indirect
:1;
468 unsigned instr_type
:4; /* always present */
470 unsigned dest
:8; /* always last */
473 unsigned instr_type
:4;
475 unsigned no_signed_wrap
:1;
476 unsigned no_unsigned_wrap
:1;
478 unsigned writemask
:4;
484 unsigned instr_type
:4;
485 unsigned deref_type
:3;
491 unsigned instr_type
:4;
492 unsigned intrinsic
:9;
493 unsigned num_components
:3;
498 unsigned instr_type
:4;
499 unsigned last_component
:4;
504 unsigned instr_type
:4;
505 unsigned last_component
:4;
510 unsigned instr_type
:4;
513 unsigned texture_array_size
:12;
517 unsigned instr_type
:4;
518 unsigned num_srcs
:20;
522 unsigned instr_type
:4;
528 /* Write "lo24" as low 24 bits in the first uint32. */
530 write_dest(write_ctx
*ctx
, const nir_dest
*dst
, union packed_instr header
)
532 STATIC_ASSERT(sizeof(union packed_dest
) == 1);
533 union packed_dest dest
;
536 dest
.ssa
.is_ssa
= dst
->is_ssa
;
538 dest
.ssa
.has_name
= !ctx
->strip
&& dst
->ssa
.name
;
539 dest
.ssa
.num_components
=
540 encode_num_components_in_3bits(dst
->ssa
.num_components
);
541 dest
.ssa
.bit_size
= encode_bit_size_3bits(dst
->ssa
.bit_size
);
543 dest
.reg
.is_indirect
= !!(dst
->reg
.indirect
);
546 header
.any
.dest
= dest
.u8
;
547 blob_write_uint32(ctx
->blob
, header
.u32
);
550 write_add_object(ctx
, &dst
->ssa
);
551 if (dest
.ssa
.has_name
)
552 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
554 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
555 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
556 if (dst
->reg
.indirect
)
557 write_src(ctx
, dst
->reg
.indirect
);
562 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
,
563 union packed_instr header
)
565 union packed_dest dest
;
566 dest
.u8
= header
.any
.dest
;
568 if (dest
.ssa
.is_ssa
) {
569 unsigned bit_size
= decode_bit_size_3bits(dest
.ssa
.bit_size
);
570 unsigned num_components
=
571 decode_num_components_in_3bits(dest
.ssa
.num_components
);
572 char *name
= dest
.ssa
.has_name
? blob_read_string(ctx
->blob
) : NULL
;
573 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
574 read_add_object(ctx
, &dst
->ssa
);
576 dst
->reg
.reg
= read_object(ctx
);
577 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
578 if (dest
.reg
.is_indirect
) {
579 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
580 read_src(ctx
, dst
->reg
.indirect
, instr
);
586 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
588 /* 9 bits for nir_op */
589 STATIC_ASSERT(nir_num_opcodes
<= 512);
590 union packed_instr header
;
593 header
.alu
.instr_type
= alu
->instr
.type
;
594 header
.alu
.exact
= alu
->exact
;
595 header
.alu
.no_signed_wrap
= alu
->no_signed_wrap
;
596 header
.alu
.no_unsigned_wrap
= alu
->no_unsigned_wrap
;
597 header
.alu
.saturate
= alu
->dest
.saturate
;
598 header
.alu
.writemask
= alu
->dest
.write_mask
;
599 header
.alu
.op
= alu
->op
;
601 write_dest(ctx
, &alu
->dest
.dest
, header
);
603 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
604 union packed_src src
;
607 src
.alu
.negate
= alu
->src
[i
].negate
;
608 src
.alu
.abs
= alu
->src
[i
].abs
;
609 src
.alu
.swizzle_x
= alu
->src
[i
].swizzle
[0];
610 src
.alu
.swizzle_y
= alu
->src
[i
].swizzle
[1];
611 src
.alu
.swizzle_z
= alu
->src
[i
].swizzle
[2];
612 src
.alu
.swizzle_w
= alu
->src
[i
].swizzle
[3];
614 write_src_full(ctx
, &alu
->src
[i
].src
, src
);
618 static nir_alu_instr
*
619 read_alu(read_ctx
*ctx
, union packed_instr header
)
621 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, header
.alu
.op
);
623 alu
->exact
= header
.alu
.exact
;
624 alu
->no_signed_wrap
= header
.alu
.no_signed_wrap
;
625 alu
->no_unsigned_wrap
= header
.alu
.no_unsigned_wrap
;
626 alu
->dest
.saturate
= header
.alu
.saturate
;
627 alu
->dest
.write_mask
= header
.alu
.writemask
;
629 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
, header
);
631 for (unsigned i
= 0; i
< nir_op_infos
[header
.alu
.op
].num_inputs
; i
++) {
632 union packed_src src
= read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
634 alu
->src
[i
].negate
= src
.alu
.negate
;
635 alu
->src
[i
].abs
= src
.alu
.abs
;
636 alu
->src
[i
].swizzle
[0] = src
.alu
.swizzle_x
;
637 alu
->src
[i
].swizzle
[1] = src
.alu
.swizzle_y
;
638 alu
->src
[i
].swizzle
[2] = src
.alu
.swizzle_z
;
639 alu
->src
[i
].swizzle
[3] = src
.alu
.swizzle_w
;
646 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
648 assert(deref
->deref_type
< 8);
649 assert(deref
->mode
< (1 << 10));
651 union packed_instr header
;
654 header
.deref
.instr_type
= deref
->instr
.type
;
655 header
.deref
.deref_type
= deref
->deref_type
;
656 header
.deref
.mode
= deref
->mode
;
658 write_dest(ctx
, &deref
->dest
, header
);
659 encode_type_to_blob(ctx
->blob
, deref
->type
);
661 if (deref
->deref_type
== nir_deref_type_var
) {
662 write_object(ctx
, deref
->var
);
666 write_src(ctx
, &deref
->parent
);
668 switch (deref
->deref_type
) {
669 case nir_deref_type_struct
:
670 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
673 case nir_deref_type_array
:
674 case nir_deref_type_ptr_as_array
:
675 write_src(ctx
, &deref
->arr
.index
);
678 case nir_deref_type_cast
:
679 blob_write_uint32(ctx
->blob
, deref
->cast
.ptr_stride
);
682 case nir_deref_type_array_wildcard
:
687 unreachable("Invalid deref type");
691 static nir_deref_instr
*
692 read_deref(read_ctx
*ctx
, union packed_instr header
)
694 nir_deref_type deref_type
= header
.deref
.deref_type
;
695 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
697 read_dest(ctx
, &deref
->dest
, &deref
->instr
, header
);
699 deref
->mode
= header
.deref
.mode
;
700 deref
->type
= decode_type_from_blob(ctx
->blob
);
702 if (deref_type
== nir_deref_type_var
) {
703 deref
->var
= read_object(ctx
);
707 read_src(ctx
, &deref
->parent
, &deref
->instr
);
709 switch (deref
->deref_type
) {
710 case nir_deref_type_struct
:
711 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
714 case nir_deref_type_array
:
715 case nir_deref_type_ptr_as_array
:
716 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
719 case nir_deref_type_cast
:
720 deref
->cast
.ptr_stride
= blob_read_uint32(ctx
->blob
);
723 case nir_deref_type_array_wildcard
:
728 unreachable("Invalid deref type");
735 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
737 /* 9 bits for nir_intrinsic_op */
738 STATIC_ASSERT(nir_num_intrinsics
<= 512);
739 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
740 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
741 assert(intrin
->intrinsic
< 512);
743 union packed_instr header
;
746 header
.intrinsic
.instr_type
= intrin
->instr
.type
;
747 header
.intrinsic
.intrinsic
= intrin
->intrinsic
;
748 header
.intrinsic
.num_components
=
749 encode_num_components_in_3bits(intrin
->num_components
);
751 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
752 write_dest(ctx
, &intrin
->dest
, header
);
754 blob_write_uint32(ctx
->blob
, header
.u32
);
756 for (unsigned i
= 0; i
< num_srcs
; i
++)
757 write_src(ctx
, &intrin
->src
[i
]);
759 for (unsigned i
= 0; i
< num_indices
; i
++)
760 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
763 static nir_intrinsic_instr
*
764 read_intrinsic(read_ctx
*ctx
, union packed_instr header
)
766 nir_intrinsic_op op
= header
.intrinsic
.intrinsic
;
767 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
769 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
770 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
772 intrin
->num_components
=
773 decode_num_components_in_3bits(header
.intrinsic
.num_components
);
775 if (nir_intrinsic_infos
[op
].has_dest
)
776 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
, header
);
778 for (unsigned i
= 0; i
< num_srcs
; i
++)
779 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
781 for (unsigned i
= 0; i
< num_indices
; i
++)
782 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
788 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
790 assert(lc
->def
.num_components
>= 1 && lc
->def
.num_components
<= 16);
791 union packed_instr header
;
794 header
.load_const
.instr_type
= lc
->instr
.type
;
795 header
.load_const
.last_component
= lc
->def
.num_components
- 1;
796 header
.load_const
.bit_size
= encode_bit_size_3bits(lc
->def
.bit_size
);
798 blob_write_uint32(ctx
->blob
, header
.u32
);
799 blob_write_bytes(ctx
->blob
, lc
->value
, sizeof(*lc
->value
) * lc
->def
.num_components
);
800 write_add_object(ctx
, &lc
->def
);
803 static nir_load_const_instr
*
804 read_load_const(read_ctx
*ctx
, union packed_instr header
)
806 nir_load_const_instr
*lc
=
807 nir_load_const_instr_create(ctx
->nir
, header
.load_const
.last_component
+ 1,
808 decode_bit_size_3bits(header
.load_const
.bit_size
));
810 blob_copy_bytes(ctx
->blob
, lc
->value
, sizeof(*lc
->value
) * lc
->def
.num_components
);
811 read_add_object(ctx
, &lc
->def
);
816 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
818 assert(undef
->def
.num_components
>= 1 && undef
->def
.num_components
<= 16);
820 union packed_instr header
;
823 header
.undef
.instr_type
= undef
->instr
.type
;
824 header
.undef
.last_component
= undef
->def
.num_components
- 1;
825 header
.undef
.bit_size
= encode_bit_size_3bits(undef
->def
.bit_size
);
827 blob_write_uint32(ctx
->blob
, header
.u32
);
828 write_add_object(ctx
, &undef
->def
);
831 static nir_ssa_undef_instr
*
832 read_ssa_undef(read_ctx
*ctx
, union packed_instr header
)
834 nir_ssa_undef_instr
*undef
=
835 nir_ssa_undef_instr_create(ctx
->nir
, header
.undef
.last_component
+ 1,
836 decode_bit_size_3bits(header
.undef
.bit_size
));
838 read_add_object(ctx
, &undef
->def
);
842 union packed_tex_data
{
845 enum glsl_sampler_dim sampler_dim
:4;
846 nir_alu_type dest_type
:8;
847 unsigned coord_components
:3;
849 unsigned is_shadow
:1;
850 unsigned is_new_style_shadow
:1;
851 unsigned component
:2;
852 unsigned unused
:10; /* Mark unused for valgrind. */
857 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
859 assert(tex
->num_srcs
< 16);
860 assert(tex
->op
< 16);
861 assert(tex
->texture_array_size
< 1024);
863 union packed_instr header
;
866 header
.tex
.instr_type
= tex
->instr
.type
;
867 header
.tex
.num_srcs
= tex
->num_srcs
;
868 header
.tex
.op
= tex
->op
;
869 header
.tex
.texture_array_size
= tex
->texture_array_size
;
871 write_dest(ctx
, &tex
->dest
, header
);
873 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
874 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
875 if (tex
->op
== nir_texop_tg4
)
876 blob_write_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
878 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
879 union packed_tex_data packed
= {
880 .u
.sampler_dim
= tex
->sampler_dim
,
881 .u
.dest_type
= tex
->dest_type
,
882 .u
.coord_components
= tex
->coord_components
,
883 .u
.is_array
= tex
->is_array
,
884 .u
.is_shadow
= tex
->is_shadow
,
885 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
886 .u
.component
= tex
->component
,
888 blob_write_uint32(ctx
->blob
, packed
.u32
);
890 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
891 union packed_src src
;
893 src
.tex
.src_type
= tex
->src
[i
].src_type
;
894 write_src_full(ctx
, &tex
->src
[i
].src
, src
);
898 static nir_tex_instr
*
899 read_tex(read_ctx
*ctx
, union packed_instr header
)
901 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, header
.tex
.num_srcs
);
903 read_dest(ctx
, &tex
->dest
, &tex
->instr
, header
);
905 tex
->op
= header
.tex
.op
;
906 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
907 tex
->texture_array_size
= header
.tex
.texture_array_size
;
908 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
909 if (tex
->op
== nir_texop_tg4
)
910 blob_copy_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
912 union packed_tex_data packed
;
913 packed
.u32
= blob_read_uint32(ctx
->blob
);
914 tex
->sampler_dim
= packed
.u
.sampler_dim
;
915 tex
->dest_type
= packed
.u
.dest_type
;
916 tex
->coord_components
= packed
.u
.coord_components
;
917 tex
->is_array
= packed
.u
.is_array
;
918 tex
->is_shadow
= packed
.u
.is_shadow
;
919 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
920 tex
->component
= packed
.u
.component
;
922 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
923 union packed_src src
= read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
924 tex
->src
[i
].src_type
= src
.tex
.src_type
;
931 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
933 union packed_instr header
;
936 header
.phi
.instr_type
= phi
->instr
.type
;
937 header
.phi
.num_srcs
= exec_list_length(&phi
->srcs
);
939 /* Phi nodes are special, since they may reference SSA definitions and
940 * basic blocks that don't exist yet. We leave two empty uint32_t's here,
941 * and then store enough information so that a later fixup pass can fill
944 write_dest(ctx
, &phi
->dest
, header
);
946 nir_foreach_phi_src(src
, phi
) {
947 assert(src
->src
.is_ssa
);
948 size_t blob_offset
= blob_reserve_uint32(ctx
->blob
);
949 ASSERTED
size_t blob_offset2
= blob_reserve_uint32(ctx
->blob
);
950 assert(blob_offset
+ sizeof(uint32_t) == blob_offset2
);
951 write_phi_fixup fixup
= {
952 .blob_offset
= blob_offset
,
956 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
961 write_fixup_phis(write_ctx
*ctx
)
963 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
964 uint32_t *blob_ptr
= (uint32_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
965 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
966 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
969 util_dynarray_clear(&ctx
->phi_fixups
);
972 static nir_phi_instr
*
973 read_phi(read_ctx
*ctx
, nir_block
*blk
, union packed_instr header
)
975 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
977 read_dest(ctx
, &phi
->dest
, &phi
->instr
, header
);
979 /* For similar reasons as before, we just store the index directly into the
980 * pointer, and let a later pass resolve the phi sources.
982 * In order to ensure that the copied sources (which are just the indices
983 * from the blob for now) don't get inserted into the old shader's use-def
984 * lists, we have to add the phi instruction *before* we set up its
987 nir_instr_insert_after_block(blk
, &phi
->instr
);
989 for (unsigned i
= 0; i
< header
.phi
.num_srcs
; i
++) {
990 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
992 src
->src
.is_ssa
= true;
993 src
->src
.ssa
= (nir_ssa_def
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
994 src
->pred
= (nir_block
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
996 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
997 * we have to set the parent_instr manually. It doesn't really matter
998 * when we do it, so we might as well do it here.
1000 src
->src
.parent_instr
= &phi
->instr
;
1002 /* Stash it in the list of phi sources. We'll walk this list and fix up
1003 * sources at the very end of read_function_impl.
1005 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
1007 exec_list_push_tail(&phi
->srcs
, &src
->node
);
1014 read_fixup_phis(read_ctx
*ctx
)
1016 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
1017 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
1018 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
1020 /* Remove from this list */
1021 list_del(&src
->src
.use_link
);
1023 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
1025 assert(list_is_empty(&ctx
->phi_srcs
));
1029 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
1031 assert(jmp
->type
< 4);
1033 union packed_instr header
;
1036 header
.jump
.instr_type
= jmp
->instr
.type
;
1037 header
.jump
.type
= jmp
->type
;
1039 blob_write_uint32(ctx
->blob
, header
.u32
);
1042 static nir_jump_instr
*
1043 read_jump(read_ctx
*ctx
, union packed_instr header
)
1045 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, header
.jump
.type
);
1050 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
1052 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
1054 for (unsigned i
= 0; i
< call
->num_params
; i
++)
1055 write_src(ctx
, &call
->params
[i
]);
1058 static nir_call_instr
*
1059 read_call(read_ctx
*ctx
)
1061 nir_function
*callee
= read_object(ctx
);
1062 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
1064 for (unsigned i
= 0; i
< call
->num_params
; i
++)
1065 read_src(ctx
, &call
->params
[i
], call
);
1071 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
1073 /* We have only 4 bits for the instruction type. */
1074 assert(instr
->type
< 16);
1076 switch (instr
->type
) {
1077 case nir_instr_type_alu
:
1078 write_alu(ctx
, nir_instr_as_alu(instr
));
1080 case nir_instr_type_deref
:
1081 write_deref(ctx
, nir_instr_as_deref(instr
));
1083 case nir_instr_type_intrinsic
:
1084 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1086 case nir_instr_type_load_const
:
1087 write_load_const(ctx
, nir_instr_as_load_const(instr
));
1089 case nir_instr_type_ssa_undef
:
1090 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1092 case nir_instr_type_tex
:
1093 write_tex(ctx
, nir_instr_as_tex(instr
));
1095 case nir_instr_type_phi
:
1096 write_phi(ctx
, nir_instr_as_phi(instr
));
1098 case nir_instr_type_jump
:
1099 write_jump(ctx
, nir_instr_as_jump(instr
));
1101 case nir_instr_type_call
:
1102 blob_write_uint32(ctx
->blob
, instr
->type
);
1103 write_call(ctx
, nir_instr_as_call(instr
));
1105 case nir_instr_type_parallel_copy
:
1106 unreachable("Cannot write parallel copies");
1108 unreachable("bad instr type");
1113 read_instr(read_ctx
*ctx
, nir_block
*block
)
1115 STATIC_ASSERT(sizeof(union packed_instr
) == 4);
1116 union packed_instr header
;
1117 header
.u32
= blob_read_uint32(ctx
->blob
);
1120 switch (header
.any
.instr_type
) {
1121 case nir_instr_type_alu
:
1122 instr
= &read_alu(ctx
, header
)->instr
;
1124 case nir_instr_type_deref
:
1125 instr
= &read_deref(ctx
, header
)->instr
;
1127 case nir_instr_type_intrinsic
:
1128 instr
= &read_intrinsic(ctx
, header
)->instr
;
1130 case nir_instr_type_load_const
:
1131 instr
= &read_load_const(ctx
, header
)->instr
;
1133 case nir_instr_type_ssa_undef
:
1134 instr
= &read_ssa_undef(ctx
, header
)->instr
;
1136 case nir_instr_type_tex
:
1137 instr
= &read_tex(ctx
, header
)->instr
;
1139 case nir_instr_type_phi
:
1140 /* Phi instructions are a bit of a special case when reading because we
1141 * don't want inserting the instruction to automatically handle use/defs
1142 * for us. Instead, we need to wait until all the blocks/instructions
1143 * are read so that we can set their sources up.
1145 read_phi(ctx
, block
, header
);
1147 case nir_instr_type_jump
:
1148 instr
= &read_jump(ctx
, header
)->instr
;
1150 case nir_instr_type_call
:
1151 instr
= &read_call(ctx
)->instr
;
1153 case nir_instr_type_parallel_copy
:
1154 unreachable("Cannot read parallel copies");
1156 unreachable("bad instr type");
1159 nir_instr_insert_after_block(block
, instr
);
1163 write_block(write_ctx
*ctx
, const nir_block
*block
)
1165 write_add_object(ctx
, block
);
1166 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
1167 nir_foreach_instr(instr
, block
)
1168 write_instr(ctx
, instr
);
1172 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
1174 /* Don't actually create a new block. Just use the one from the tail of
1175 * the list. NIR guarantees that the tail of the list is a block and that
1176 * no two blocks are side-by-side in the IR; It should be empty.
1179 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
1181 read_add_object(ctx
, block
);
1182 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
1183 for (unsigned i
= 0; i
< num_instrs
; i
++) {
1184 read_instr(ctx
, block
);
1189 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
1192 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
1195 write_if(write_ctx
*ctx
, nir_if
*nif
)
1197 write_src(ctx
, &nif
->condition
);
1199 write_cf_list(ctx
, &nif
->then_list
);
1200 write_cf_list(ctx
, &nif
->else_list
);
1204 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
1206 nir_if
*nif
= nir_if_create(ctx
->nir
);
1208 read_src(ctx
, &nif
->condition
, nif
);
1210 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
1212 read_cf_list(ctx
, &nif
->then_list
);
1213 read_cf_list(ctx
, &nif
->else_list
);
1217 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
1219 write_cf_list(ctx
, &loop
->body
);
1223 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
1225 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
1227 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
1229 read_cf_list(ctx
, &loop
->body
);
1233 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
1235 blob_write_uint32(ctx
->blob
, cf
->type
);
1238 case nir_cf_node_block
:
1239 write_block(ctx
, nir_cf_node_as_block(cf
));
1241 case nir_cf_node_if
:
1242 write_if(ctx
, nir_cf_node_as_if(cf
));
1244 case nir_cf_node_loop
:
1245 write_loop(ctx
, nir_cf_node_as_loop(cf
));
1248 unreachable("bad cf type");
1253 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
1255 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
1258 case nir_cf_node_block
:
1259 read_block(ctx
, list
);
1261 case nir_cf_node_if
:
1264 case nir_cf_node_loop
:
1265 read_loop(ctx
, list
);
1268 unreachable("bad cf type");
1273 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
1275 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
1276 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
1277 write_cf_node(ctx
, cf
);
1282 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1284 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1285 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1286 read_cf_node(ctx
, cf_list
);
1290 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1292 write_var_list(ctx
, &fi
->locals
);
1293 write_reg_list(ctx
, &fi
->registers
);
1294 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1296 write_cf_list(ctx
, &fi
->body
);
1297 write_fixup_phis(ctx
);
1300 static nir_function_impl
*
1301 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1303 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1306 read_var_list(ctx
, &fi
->locals
);
1307 read_reg_list(ctx
, &fi
->registers
);
1308 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1310 read_cf_list(ctx
, &fi
->body
);
1311 read_fixup_phis(ctx
);
1313 fi
->valid_metadata
= 0;
1319 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1321 uint32_t flags
= fxn
->is_entrypoint
;
1326 blob_write_uint32(ctx
->blob
, flags
);
1328 blob_write_string(ctx
->blob
, fxn
->name
);
1330 write_add_object(ctx
, fxn
);
1332 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1333 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1335 ((uint32_t)fxn
->params
[i
].num_components
) |
1336 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1337 blob_write_uint32(ctx
->blob
, val
);
1340 /* At first glance, it looks like we should write the function_impl here.
1341 * However, call instructions need to be able to reference at least the
1342 * function and those will get processed as we write the function_impls.
1343 * We stop here and write function_impls as a second pass.
1348 read_function(read_ctx
*ctx
)
1350 uint32_t flags
= blob_read_uint32(ctx
->blob
);
1351 bool has_name
= flags
& 0x2;
1352 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1354 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1356 read_add_object(ctx
, fxn
);
1358 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1359 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1360 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1361 uint32_t val
= blob_read_uint32(ctx
->blob
);
1362 fxn
->params
[i
].num_components
= val
& 0xff;
1363 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1366 fxn
->is_entrypoint
= flags
& 0x1;
1368 fxn
->impl
= NIR_SERIALIZE_FUNC_HAS_IMPL
;
1372 * Serialize NIR into a binary blob.
1374 * \param strip Don't serialize information only useful for debugging,
1375 * such as variable names, making cache hits from similar
1376 * shaders more likely.
1379 nir_serialize(struct blob
*blob
, const nir_shader
*nir
, bool strip
)
1381 write_ctx ctx
= {0};
1382 ctx
.remap_table
= _mesa_pointer_hash_table_create(NULL
);
1386 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1388 size_t idx_size_offset
= blob_reserve_uint32(blob
);
1390 struct shader_info info
= nir
->info
;
1391 uint32_t strings
= 0;
1392 if (!strip
&& info
.name
)
1394 if (!strip
&& info
.label
)
1396 blob_write_uint32(blob
, strings
);
1397 if (!strip
&& info
.name
)
1398 blob_write_string(blob
, info
.name
);
1399 if (!strip
&& info
.label
)
1400 blob_write_string(blob
, info
.label
);
1401 info
.name
= info
.label
= NULL
;
1402 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1404 write_var_list(&ctx
, &nir
->uniforms
);
1405 write_var_list(&ctx
, &nir
->inputs
);
1406 write_var_list(&ctx
, &nir
->outputs
);
1407 write_var_list(&ctx
, &nir
->shared
);
1408 write_var_list(&ctx
, &nir
->globals
);
1409 write_var_list(&ctx
, &nir
->system_values
);
1411 blob_write_uint32(blob
, nir
->num_inputs
);
1412 blob_write_uint32(blob
, nir
->num_uniforms
);
1413 blob_write_uint32(blob
, nir
->num_outputs
);
1414 blob_write_uint32(blob
, nir
->num_shared
);
1415 blob_write_uint32(blob
, nir
->scratch_size
);
1417 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1418 nir_foreach_function(fxn
, nir
) {
1419 write_function(&ctx
, fxn
);
1422 nir_foreach_function(fxn
, nir
) {
1424 write_function_impl(&ctx
, fxn
->impl
);
1427 blob_write_uint32(blob
, nir
->constant_data_size
);
1428 if (nir
->constant_data_size
> 0)
1429 blob_write_bytes(blob
, nir
->constant_data
, nir
->constant_data_size
);
1431 *(uint32_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1433 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1434 util_dynarray_fini(&ctx
.phi_fixups
);
1438 nir_deserialize(void *mem_ctx
,
1439 const struct nir_shader_compiler_options
*options
,
1440 struct blob_reader
*blob
)
1444 list_inithead(&ctx
.phi_srcs
);
1445 ctx
.idx_table_len
= blob_read_uint32(blob
);
1446 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1448 uint32_t strings
= blob_read_uint32(blob
);
1449 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1450 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1452 struct shader_info info
;
1453 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1455 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1457 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1458 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1460 ctx
.nir
->info
= info
;
1462 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1463 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1464 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1465 read_var_list(&ctx
, &ctx
.nir
->shared
);
1466 read_var_list(&ctx
, &ctx
.nir
->globals
);
1467 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1469 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1470 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1471 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1472 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1473 ctx
.nir
->scratch_size
= blob_read_uint32(blob
);
1475 unsigned num_functions
= blob_read_uint32(blob
);
1476 for (unsigned i
= 0; i
< num_functions
; i
++)
1477 read_function(&ctx
);
1479 nir_foreach_function(fxn
, ctx
.nir
) {
1480 if (fxn
->impl
== NIR_SERIALIZE_FUNC_HAS_IMPL
)
1481 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1484 ctx
.nir
->constant_data_size
= blob_read_uint32(blob
);
1485 if (ctx
.nir
->constant_data_size
> 0) {
1486 ctx
.nir
->constant_data
=
1487 ralloc_size(ctx
.nir
, ctx
.nir
->constant_data_size
);
1488 blob_copy_bytes(blob
, ctx
.nir
->constant_data
,
1489 ctx
.nir
->constant_data_size
);
1492 free(ctx
.idx_table
);
1498 nir_shader_serialize_deserialize(nir_shader
*shader
)
1500 const struct nir_shader_compiler_options
*options
= shader
->options
;
1504 nir_serialize(&writer
, shader
, false);
1506 /* Delete all of dest's ralloc children but leave dest alone */
1507 void *dead_ctx
= ralloc_context(NULL
);
1508 ralloc_adopt(dead_ctx
, shader
);
1509 ralloc_free(dead_ctx
);
1511 dead_ctx
= ralloc_context(NULL
);
1513 struct blob_reader reader
;
1514 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1515 nir_shader
*copy
= nir_deserialize(dead_ctx
, options
, &reader
);
1517 blob_finish(&writer
);
1519 nir_shader_replace(shader
, copy
);
1520 ralloc_free(dead_ctx
);