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"
28 #define MAX_OBJECT_IDS (1 << 30)
37 const nir_shader
*nir
;
41 /* maps pointer to index */
42 struct hash_table
*remap_table
;
44 /* the next index to assign to a NIR in-memory object */
47 /* Array of write_phi_fixup structs representing phi sources that need to
48 * be resolved in the second pass.
50 struct util_dynarray phi_fixups
;
56 struct blob_reader
*blob
;
58 /* the next index to assign to a NIR in-memory object */
61 /* The length of the index -> object table */
62 uint32_t idx_table_len
;
64 /* map from index to deserialized pointer */
67 /* List of phi sources. */
68 struct list_head phi_srcs
;
73 write_add_object(write_ctx
*ctx
, const void *obj
)
75 uint32_t index
= ctx
->next_idx
++;
76 assert(index
!= MAX_OBJECT_IDS
);
77 _mesa_hash_table_insert(ctx
->remap_table
, obj
, (void *)(uintptr_t) index
);
81 write_lookup_object(write_ctx
*ctx
, const void *obj
)
83 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->remap_table
, obj
);
85 return (uint32_t)(uintptr_t) entry
->data
;
89 write_object(write_ctx
*ctx
, const void *obj
)
91 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, obj
));
95 read_add_object(read_ctx
*ctx
, void *obj
)
97 assert(ctx
->next_idx
< ctx
->idx_table_len
);
98 ctx
->idx_table
[ctx
->next_idx
++] = obj
;
102 read_lookup_object(read_ctx
*ctx
, uint32_t idx
)
104 assert(idx
< ctx
->idx_table_len
);
105 return ctx
->idx_table
[idx
];
109 read_object(read_ctx
*ctx
)
111 return read_lookup_object(ctx
, blob_read_uint32(ctx
->blob
));
115 write_constant(write_ctx
*ctx
, const nir_constant
*c
)
117 blob_write_bytes(ctx
->blob
, c
->values
, sizeof(c
->values
));
118 blob_write_uint32(ctx
->blob
, c
->num_elements
);
119 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
120 write_constant(ctx
, c
->elements
[i
]);
123 static nir_constant
*
124 read_constant(read_ctx
*ctx
, nir_variable
*nvar
)
126 nir_constant
*c
= ralloc(nvar
, nir_constant
);
128 blob_copy_bytes(ctx
->blob
, (uint8_t *)c
->values
, sizeof(c
->values
));
129 c
->num_elements
= blob_read_uint32(ctx
->blob
);
130 c
->elements
= ralloc_array(nvar
, nir_constant
*, c
->num_elements
);
131 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
132 c
->elements
[i
] = read_constant(ctx
, nvar
);
138 write_variable(write_ctx
*ctx
, const nir_variable
*var
)
140 write_add_object(ctx
, var
);
141 encode_type_to_blob(ctx
->blob
, var
->type
);
142 blob_write_uint32(ctx
->blob
, !!(var
->name
));
144 blob_write_string(ctx
->blob
, var
->name
);
145 blob_write_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
146 blob_write_uint32(ctx
->blob
, var
->num_state_slots
);
147 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
148 blob_write_bytes(ctx
->blob
, &var
->state_slots
[i
],
149 sizeof(var
->state_slots
[i
]));
151 blob_write_uint32(ctx
->blob
, !!(var
->constant_initializer
));
152 if (var
->constant_initializer
)
153 write_constant(ctx
, var
->constant_initializer
);
154 blob_write_uint32(ctx
->blob
, !!(var
->interface_type
));
155 if (var
->interface_type
)
156 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
157 blob_write_uint32(ctx
->blob
, var
->num_members
);
158 if (var
->num_members
> 0) {
159 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->members
,
160 var
->num_members
* sizeof(*var
->members
));
164 static nir_variable
*
165 read_variable(read_ctx
*ctx
)
167 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
168 read_add_object(ctx
, var
);
170 var
->type
= decode_type_from_blob(ctx
->blob
);
171 bool has_name
= blob_read_uint32(ctx
->blob
);
173 const char *name
= blob_read_string(ctx
->blob
);
174 var
->name
= ralloc_strdup(var
, name
);
178 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
179 var
->num_state_slots
= blob_read_uint32(ctx
->blob
);
180 if (var
->num_state_slots
!= 0) {
181 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
182 var
->num_state_slots
);
183 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
184 blob_copy_bytes(ctx
->blob
, &var
->state_slots
[i
],
185 sizeof(var
->state_slots
[i
]));
188 bool has_const_initializer
= blob_read_uint32(ctx
->blob
);
189 if (has_const_initializer
)
190 var
->constant_initializer
= read_constant(ctx
, var
);
192 var
->constant_initializer
= NULL
;
193 bool has_interface_type
= blob_read_uint32(ctx
->blob
);
194 if (has_interface_type
)
195 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
197 var
->interface_type
= NULL
;
198 var
->num_members
= blob_read_uint32(ctx
->blob
);
199 if (var
->num_members
> 0) {
200 var
->members
= ralloc_array(var
, struct nir_variable_data
,
202 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->members
,
203 var
->num_members
* sizeof(*var
->members
));
210 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
212 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
213 foreach_list_typed(nir_variable
, var
, node
, src
) {
214 write_variable(ctx
, var
);
219 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
221 exec_list_make_empty(dst
);
222 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
223 for (unsigned i
= 0; i
< num_vars
; i
++) {
224 nir_variable
*var
= read_variable(ctx
);
225 exec_list_push_tail(dst
, &var
->node
);
230 write_register(write_ctx
*ctx
, const nir_register
*reg
)
232 write_add_object(ctx
, reg
);
233 blob_write_uint32(ctx
->blob
, reg
->num_components
);
234 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
235 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
236 blob_write_uint32(ctx
->blob
, reg
->index
);
237 blob_write_uint32(ctx
->blob
, !!(reg
->name
));
239 blob_write_string(ctx
->blob
, reg
->name
);
242 static nir_register
*
243 read_register(read_ctx
*ctx
)
245 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
246 read_add_object(ctx
, reg
);
247 reg
->num_components
= blob_read_uint32(ctx
->blob
);
248 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
249 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
250 reg
->index
= blob_read_uint32(ctx
->blob
);
251 bool has_name
= blob_read_uint32(ctx
->blob
);
253 const char *name
= blob_read_string(ctx
->blob
);
254 reg
->name
= ralloc_strdup(reg
, name
);
259 list_inithead(®
->uses
);
260 list_inithead(®
->defs
);
261 list_inithead(®
->if_uses
);
267 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
269 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
270 foreach_list_typed(nir_register
, reg
, node
, src
)
271 write_register(ctx
, reg
);
275 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
277 exec_list_make_empty(dst
);
278 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
279 for (unsigned i
= 0; i
< num_regs
; i
++) {
280 nir_register
*reg
= read_register(ctx
);
281 exec_list_push_tail(dst
, ®
->node
);
286 write_src(write_ctx
*ctx
, const nir_src
*src
)
288 /* Since sources are very frequent, we try to save some space when storing
289 * them. In particular, we store whether the source is a register and
290 * whether the register has an indirect index in the low two bits. We can
291 * assume that the high two bits of the index are zero, since otherwise our
292 * address space would've been exhausted allocating the remap table!
295 uint32_t idx
= write_lookup_object(ctx
, src
->ssa
) << 2;
297 blob_write_uint32(ctx
->blob
, idx
);
299 uint32_t idx
= write_lookup_object(ctx
, src
->reg
.reg
) << 2;
300 if (src
->reg
.indirect
)
302 blob_write_uint32(ctx
->blob
, idx
);
303 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
304 if (src
->reg
.indirect
) {
305 write_src(ctx
, src
->reg
.indirect
);
311 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
313 uint32_t val
= blob_read_uint32(ctx
->blob
);
314 uint32_t idx
= val
>> 2;
315 src
->is_ssa
= val
& 0x1;
317 src
->ssa
= read_lookup_object(ctx
, idx
);
319 bool is_indirect
= val
& 0x2;
320 src
->reg
.reg
= read_lookup_object(ctx
, idx
);
321 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
323 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
324 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
326 src
->reg
.indirect
= NULL
;
332 write_dest(write_ctx
*ctx
, const nir_dest
*dst
)
334 uint32_t val
= dst
->is_ssa
;
336 val
|= !!(dst
->ssa
.name
) << 1;
337 val
|= dst
->ssa
.num_components
<< 2;
338 val
|= dst
->ssa
.bit_size
<< 5;
340 val
|= !!(dst
->reg
.indirect
) << 1;
342 blob_write_uint32(ctx
->blob
, val
);
344 write_add_object(ctx
, &dst
->ssa
);
346 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
348 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
349 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
350 if (dst
->reg
.indirect
)
351 write_src(ctx
, dst
->reg
.indirect
);
356 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
)
358 uint32_t val
= blob_read_uint32(ctx
->blob
);
359 bool is_ssa
= val
& 0x1;
361 bool has_name
= val
& 0x2;
362 unsigned num_components
= (val
>> 2) & 0x7;
363 unsigned bit_size
= val
>> 5;
364 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
365 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
366 read_add_object(ctx
, &dst
->ssa
);
368 bool is_indirect
= val
& 0x2;
369 dst
->reg
.reg
= read_object(ctx
);
370 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
372 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
373 read_src(ctx
, dst
->reg
.indirect
, instr
);
379 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
381 blob_write_uint32(ctx
->blob
, alu
->op
);
382 uint32_t flags
= alu
->exact
;
383 flags
|= alu
->no_signed_wrap
<< 1;
384 flags
|= alu
->no_unsigned_wrap
<< 2;
385 flags
|= alu
->dest
.saturate
<< 3;
386 flags
|= alu
->dest
.write_mask
<< 4;
387 blob_write_uint32(ctx
->blob
, flags
);
389 write_dest(ctx
, &alu
->dest
.dest
);
391 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
392 write_src(ctx
, &alu
->src
[i
].src
);
393 flags
= alu
->src
[i
].negate
;
394 flags
|= alu
->src
[i
].abs
<< 1;
395 for (unsigned j
= 0; j
< 4; j
++)
396 flags
|= alu
->src
[i
].swizzle
[j
] << (2 + 2 * j
);
397 blob_write_uint32(ctx
->blob
, flags
);
401 static nir_alu_instr
*
402 read_alu(read_ctx
*ctx
)
404 nir_op op
= blob_read_uint32(ctx
->blob
);
405 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, op
);
407 uint32_t flags
= blob_read_uint32(ctx
->blob
);
408 alu
->exact
= flags
& 1;
409 alu
->no_signed_wrap
= flags
& 2;
410 alu
->no_unsigned_wrap
= flags
& 4;
411 alu
->dest
.saturate
= flags
& 8;
412 alu
->dest
.write_mask
= flags
>> 4;
414 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
);
416 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
417 read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
418 flags
= blob_read_uint32(ctx
->blob
);
419 alu
->src
[i
].negate
= flags
& 1;
420 alu
->src
[i
].abs
= flags
& 2;
421 for (unsigned j
= 0; j
< 4; j
++)
422 alu
->src
[i
].swizzle
[j
] = (flags
>> (2 * j
+ 2)) & 3;
429 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
431 blob_write_uint32(ctx
->blob
, deref
->deref_type
);
433 blob_write_uint32(ctx
->blob
, deref
->mode
);
434 encode_type_to_blob(ctx
->blob
, deref
->type
);
436 write_dest(ctx
, &deref
->dest
);
438 if (deref
->deref_type
== nir_deref_type_var
) {
439 write_object(ctx
, deref
->var
);
443 write_src(ctx
, &deref
->parent
);
445 switch (deref
->deref_type
) {
446 case nir_deref_type_struct
:
447 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
450 case nir_deref_type_array
:
451 case nir_deref_type_ptr_as_array
:
452 write_src(ctx
, &deref
->arr
.index
);
455 case nir_deref_type_cast
:
456 blob_write_uint32(ctx
->blob
, deref
->cast
.ptr_stride
);
459 case nir_deref_type_array_wildcard
:
464 unreachable("Invalid deref type");
468 static nir_deref_instr
*
469 read_deref(read_ctx
*ctx
)
471 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
472 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
474 deref
->mode
= blob_read_uint32(ctx
->blob
);
475 deref
->type
= decode_type_from_blob(ctx
->blob
);
477 read_dest(ctx
, &deref
->dest
, &deref
->instr
);
479 if (deref_type
== nir_deref_type_var
) {
480 deref
->var
= read_object(ctx
);
484 read_src(ctx
, &deref
->parent
, &deref
->instr
);
486 switch (deref
->deref_type
) {
487 case nir_deref_type_struct
:
488 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
491 case nir_deref_type_array
:
492 case nir_deref_type_ptr_as_array
:
493 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
496 case nir_deref_type_cast
:
497 deref
->cast
.ptr_stride
= blob_read_uint32(ctx
->blob
);
500 case nir_deref_type_array_wildcard
:
505 unreachable("Invalid deref type");
512 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
514 blob_write_uint32(ctx
->blob
, intrin
->intrinsic
);
516 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
517 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
519 blob_write_uint32(ctx
->blob
, intrin
->num_components
);
521 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
522 write_dest(ctx
, &intrin
->dest
);
524 for (unsigned i
= 0; i
< num_srcs
; i
++)
525 write_src(ctx
, &intrin
->src
[i
]);
527 for (unsigned i
= 0; i
< num_indices
; i
++)
528 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
531 static nir_intrinsic_instr
*
532 read_intrinsic(read_ctx
*ctx
)
534 nir_intrinsic_op op
= blob_read_uint32(ctx
->blob
);
536 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
538 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
539 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
541 intrin
->num_components
= blob_read_uint32(ctx
->blob
);
543 if (nir_intrinsic_infos
[op
].has_dest
)
544 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
);
546 for (unsigned i
= 0; i
< num_srcs
; i
++)
547 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
549 for (unsigned i
= 0; i
< num_indices
; i
++)
550 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
556 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
558 uint32_t val
= lc
->def
.num_components
;
559 val
|= lc
->def
.bit_size
<< 3;
560 blob_write_uint32(ctx
->blob
, val
);
561 blob_write_bytes(ctx
->blob
, lc
->value
, sizeof(*lc
->value
) * lc
->def
.num_components
);
562 write_add_object(ctx
, &lc
->def
);
565 static nir_load_const_instr
*
566 read_load_const(read_ctx
*ctx
)
568 uint32_t val
= blob_read_uint32(ctx
->blob
);
570 nir_load_const_instr
*lc
=
571 nir_load_const_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
573 blob_copy_bytes(ctx
->blob
, lc
->value
, sizeof(*lc
->value
) * lc
->def
.num_components
);
574 read_add_object(ctx
, &lc
->def
);
579 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
581 uint32_t val
= undef
->def
.num_components
;
582 val
|= undef
->def
.bit_size
<< 3;
583 blob_write_uint32(ctx
->blob
, val
);
584 write_add_object(ctx
, &undef
->def
);
587 static nir_ssa_undef_instr
*
588 read_ssa_undef(read_ctx
*ctx
)
590 uint32_t val
= blob_read_uint32(ctx
->blob
);
592 nir_ssa_undef_instr
*undef
=
593 nir_ssa_undef_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
595 read_add_object(ctx
, &undef
->def
);
599 union packed_tex_data
{
602 enum glsl_sampler_dim sampler_dim
:4;
603 nir_alu_type dest_type
:8;
604 unsigned coord_components
:3;
606 unsigned is_shadow
:1;
607 unsigned is_new_style_shadow
:1;
608 unsigned component
:2;
609 unsigned unused
:10; /* Mark unused for valgrind. */
614 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
616 blob_write_uint32(ctx
->blob
, tex
->num_srcs
);
617 blob_write_uint32(ctx
->blob
, tex
->op
);
618 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
619 blob_write_uint32(ctx
->blob
, tex
->texture_array_size
);
620 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
621 blob_write_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
623 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
624 union packed_tex_data packed
= {
625 .u
.sampler_dim
= tex
->sampler_dim
,
626 .u
.dest_type
= tex
->dest_type
,
627 .u
.coord_components
= tex
->coord_components
,
628 .u
.is_array
= tex
->is_array
,
629 .u
.is_shadow
= tex
->is_shadow
,
630 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
631 .u
.component
= tex
->component
,
633 blob_write_uint32(ctx
->blob
, packed
.u32
);
635 write_dest(ctx
, &tex
->dest
);
636 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
637 blob_write_uint32(ctx
->blob
, tex
->src
[i
].src_type
);
638 write_src(ctx
, &tex
->src
[i
].src
);
642 static nir_tex_instr
*
643 read_tex(read_ctx
*ctx
)
645 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
646 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, num_srcs
);
648 tex
->op
= blob_read_uint32(ctx
->blob
);
649 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
650 tex
->texture_array_size
= blob_read_uint32(ctx
->blob
);
651 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
652 blob_copy_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
654 union packed_tex_data packed
;
655 packed
.u32
= blob_read_uint32(ctx
->blob
);
656 tex
->sampler_dim
= packed
.u
.sampler_dim
;
657 tex
->dest_type
= packed
.u
.dest_type
;
658 tex
->coord_components
= packed
.u
.coord_components
;
659 tex
->is_array
= packed
.u
.is_array
;
660 tex
->is_shadow
= packed
.u
.is_shadow
;
661 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
662 tex
->component
= packed
.u
.component
;
664 read_dest(ctx
, &tex
->dest
, &tex
->instr
);
665 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
666 tex
->src
[i
].src_type
= blob_read_uint32(ctx
->blob
);
667 read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
674 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
676 /* Phi nodes are special, since they may reference SSA definitions and
677 * basic blocks that don't exist yet. We leave two empty uint32_t's here,
678 * and then store enough information so that a later fixup pass can fill
681 write_dest(ctx
, &phi
->dest
);
683 blob_write_uint32(ctx
->blob
, exec_list_length(&phi
->srcs
));
685 nir_foreach_phi_src(src
, phi
) {
686 assert(src
->src
.is_ssa
);
687 size_t blob_offset
= blob_reserve_uint32(ctx
->blob
);
688 ASSERTED
size_t blob_offset2
= blob_reserve_uint32(ctx
->blob
);
689 assert(blob_offset
+ sizeof(uint32_t) == blob_offset2
);
690 write_phi_fixup fixup
= {
691 .blob_offset
= blob_offset
,
695 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
700 write_fixup_phis(write_ctx
*ctx
)
702 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
703 uint32_t *blob_ptr
= (uint32_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
704 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
705 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
708 util_dynarray_clear(&ctx
->phi_fixups
);
711 static nir_phi_instr
*
712 read_phi(read_ctx
*ctx
, nir_block
*blk
)
714 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
716 read_dest(ctx
, &phi
->dest
, &phi
->instr
);
718 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
720 /* For similar reasons as before, we just store the index directly into the
721 * pointer, and let a later pass resolve the phi sources.
723 * In order to ensure that the copied sources (which are just the indices
724 * from the blob for now) don't get inserted into the old shader's use-def
725 * lists, we have to add the phi instruction *before* we set up its
728 nir_instr_insert_after_block(blk
, &phi
->instr
);
730 for (unsigned i
= 0; i
< num_srcs
; i
++) {
731 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
733 src
->src
.is_ssa
= true;
734 src
->src
.ssa
= (nir_ssa_def
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
735 src
->pred
= (nir_block
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
737 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
738 * we have to set the parent_instr manually. It doesn't really matter
739 * when we do it, so we might as well do it here.
741 src
->src
.parent_instr
= &phi
->instr
;
743 /* Stash it in the list of phi sources. We'll walk this list and fix up
744 * sources at the very end of read_function_impl.
746 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
748 exec_list_push_tail(&phi
->srcs
, &src
->node
);
755 read_fixup_phis(read_ctx
*ctx
)
757 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
758 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
759 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
761 /* Remove from this list */
762 list_del(&src
->src
.use_link
);
764 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
766 assert(list_is_empty(&ctx
->phi_srcs
));
770 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
772 blob_write_uint32(ctx
->blob
, jmp
->type
);
775 static nir_jump_instr
*
776 read_jump(read_ctx
*ctx
)
778 nir_jump_type type
= blob_read_uint32(ctx
->blob
);
779 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, type
);
784 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
786 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
788 for (unsigned i
= 0; i
< call
->num_params
; i
++)
789 write_src(ctx
, &call
->params
[i
]);
792 static nir_call_instr
*
793 read_call(read_ctx
*ctx
)
795 nir_function
*callee
= read_object(ctx
);
796 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
798 for (unsigned i
= 0; i
< call
->num_params
; i
++)
799 read_src(ctx
, &call
->params
[i
], call
);
805 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
807 blob_write_uint32(ctx
->blob
, instr
->type
);
808 switch (instr
->type
) {
809 case nir_instr_type_alu
:
810 write_alu(ctx
, nir_instr_as_alu(instr
));
812 case nir_instr_type_deref
:
813 write_deref(ctx
, nir_instr_as_deref(instr
));
815 case nir_instr_type_intrinsic
:
816 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
818 case nir_instr_type_load_const
:
819 write_load_const(ctx
, nir_instr_as_load_const(instr
));
821 case nir_instr_type_ssa_undef
:
822 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
824 case nir_instr_type_tex
:
825 write_tex(ctx
, nir_instr_as_tex(instr
));
827 case nir_instr_type_phi
:
828 write_phi(ctx
, nir_instr_as_phi(instr
));
830 case nir_instr_type_jump
:
831 write_jump(ctx
, nir_instr_as_jump(instr
));
833 case nir_instr_type_call
:
834 write_call(ctx
, nir_instr_as_call(instr
));
836 case nir_instr_type_parallel_copy
:
837 unreachable("Cannot write parallel copies");
839 unreachable("bad instr type");
844 read_instr(read_ctx
*ctx
, nir_block
*block
)
846 nir_instr_type type
= blob_read_uint32(ctx
->blob
);
849 case nir_instr_type_alu
:
850 instr
= &read_alu(ctx
)->instr
;
852 case nir_instr_type_deref
:
853 instr
= &read_deref(ctx
)->instr
;
855 case nir_instr_type_intrinsic
:
856 instr
= &read_intrinsic(ctx
)->instr
;
858 case nir_instr_type_load_const
:
859 instr
= &read_load_const(ctx
)->instr
;
861 case nir_instr_type_ssa_undef
:
862 instr
= &read_ssa_undef(ctx
)->instr
;
864 case nir_instr_type_tex
:
865 instr
= &read_tex(ctx
)->instr
;
867 case nir_instr_type_phi
:
868 /* Phi instructions are a bit of a special case when reading because we
869 * don't want inserting the instruction to automatically handle use/defs
870 * for us. Instead, we need to wait until all the blocks/instructions
871 * are read so that we can set their sources up.
873 read_phi(ctx
, block
);
875 case nir_instr_type_jump
:
876 instr
= &read_jump(ctx
)->instr
;
878 case nir_instr_type_call
:
879 instr
= &read_call(ctx
)->instr
;
881 case nir_instr_type_parallel_copy
:
882 unreachable("Cannot read parallel copies");
884 unreachable("bad instr type");
887 nir_instr_insert_after_block(block
, instr
);
891 write_block(write_ctx
*ctx
, const nir_block
*block
)
893 write_add_object(ctx
, block
);
894 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
895 nir_foreach_instr(instr
, block
)
896 write_instr(ctx
, instr
);
900 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
902 /* Don't actually create a new block. Just use the one from the tail of
903 * the list. NIR guarantees that the tail of the list is a block and that
904 * no two blocks are side-by-side in the IR; It should be empty.
907 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
909 read_add_object(ctx
, block
);
910 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
911 for (unsigned i
= 0; i
< num_instrs
; i
++) {
912 read_instr(ctx
, block
);
917 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
920 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
923 write_if(write_ctx
*ctx
, nir_if
*nif
)
925 write_src(ctx
, &nif
->condition
);
927 write_cf_list(ctx
, &nif
->then_list
);
928 write_cf_list(ctx
, &nif
->else_list
);
932 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
934 nir_if
*nif
= nir_if_create(ctx
->nir
);
936 read_src(ctx
, &nif
->condition
, nif
);
938 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
940 read_cf_list(ctx
, &nif
->then_list
);
941 read_cf_list(ctx
, &nif
->else_list
);
945 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
947 write_cf_list(ctx
, &loop
->body
);
951 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
953 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
955 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
957 read_cf_list(ctx
, &loop
->body
);
961 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
963 blob_write_uint32(ctx
->blob
, cf
->type
);
966 case nir_cf_node_block
:
967 write_block(ctx
, nir_cf_node_as_block(cf
));
970 write_if(ctx
, nir_cf_node_as_if(cf
));
972 case nir_cf_node_loop
:
973 write_loop(ctx
, nir_cf_node_as_loop(cf
));
976 unreachable("bad cf type");
981 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
983 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
986 case nir_cf_node_block
:
987 read_block(ctx
, list
);
992 case nir_cf_node_loop
:
993 read_loop(ctx
, list
);
996 unreachable("bad cf type");
1001 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
1003 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
1004 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
1005 write_cf_node(ctx
, cf
);
1010 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1012 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1013 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1014 read_cf_node(ctx
, cf_list
);
1018 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1020 write_var_list(ctx
, &fi
->locals
);
1021 write_reg_list(ctx
, &fi
->registers
);
1022 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1024 write_cf_list(ctx
, &fi
->body
);
1025 write_fixup_phis(ctx
);
1028 static nir_function_impl
*
1029 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1031 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1034 read_var_list(ctx
, &fi
->locals
);
1035 read_reg_list(ctx
, &fi
->registers
);
1036 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1038 read_cf_list(ctx
, &fi
->body
);
1039 read_fixup_phis(ctx
);
1041 fi
->valid_metadata
= 0;
1047 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1049 blob_write_uint32(ctx
->blob
, !!(fxn
->name
));
1051 blob_write_string(ctx
->blob
, fxn
->name
);
1053 write_add_object(ctx
, fxn
);
1055 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1056 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1058 ((uint32_t)fxn
->params
[i
].num_components
) |
1059 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1060 blob_write_uint32(ctx
->blob
, val
);
1063 blob_write_uint32(ctx
->blob
, fxn
->is_entrypoint
);
1065 /* At first glance, it looks like we should write the function_impl here.
1066 * However, call instructions need to be able to reference at least the
1067 * function and those will get processed as we write the function_impls.
1068 * We stop here and write function_impls as a second pass.
1073 read_function(read_ctx
*ctx
)
1075 bool has_name
= blob_read_uint32(ctx
->blob
);
1076 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1078 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1080 read_add_object(ctx
, fxn
);
1082 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1083 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1084 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1085 uint32_t val
= blob_read_uint32(ctx
->blob
);
1086 fxn
->params
[i
].num_components
= val
& 0xff;
1087 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1090 fxn
->is_entrypoint
= blob_read_uint32(ctx
->blob
);
1094 nir_serialize(struct blob
*blob
, const nir_shader
*nir
, bool strip
)
1096 nir_shader
*stripped
= NULL
;
1099 /* Drop unnecessary information (like variable names), so the serialized
1100 * NIR is smaller, and also to let us detect more isomorphic shaders
1101 * when hashing, increasing cache hits.
1103 stripped
= nir_shader_clone(NULL
, nir
);
1104 nir_strip(stripped
);
1109 ctx
.remap_table
= _mesa_pointer_hash_table_create(NULL
);
1113 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1115 size_t idx_size_offset
= blob_reserve_uint32(blob
);
1117 struct shader_info info
= nir
->info
;
1118 uint32_t strings
= 0;
1123 blob_write_uint32(blob
, strings
);
1125 blob_write_string(blob
, info
.name
);
1127 blob_write_string(blob
, info
.label
);
1128 info
.name
= info
.label
= NULL
;
1129 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1131 write_var_list(&ctx
, &nir
->uniforms
);
1132 write_var_list(&ctx
, &nir
->inputs
);
1133 write_var_list(&ctx
, &nir
->outputs
);
1134 write_var_list(&ctx
, &nir
->shared
);
1135 write_var_list(&ctx
, &nir
->globals
);
1136 write_var_list(&ctx
, &nir
->system_values
);
1138 blob_write_uint32(blob
, nir
->num_inputs
);
1139 blob_write_uint32(blob
, nir
->num_uniforms
);
1140 blob_write_uint32(blob
, nir
->num_outputs
);
1141 blob_write_uint32(blob
, nir
->num_shared
);
1142 blob_write_uint32(blob
, nir
->scratch_size
);
1144 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1145 nir_foreach_function(fxn
, nir
) {
1146 write_function(&ctx
, fxn
);
1149 nir_foreach_function(fxn
, nir
) {
1150 write_function_impl(&ctx
, fxn
->impl
);
1153 blob_write_uint32(blob
, nir
->constant_data_size
);
1154 if (nir
->constant_data_size
> 0)
1155 blob_write_bytes(blob
, nir
->constant_data
, nir
->constant_data_size
);
1157 *(uint32_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1159 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1160 util_dynarray_fini(&ctx
.phi_fixups
);
1163 ralloc_free(stripped
);
1167 nir_deserialize(void *mem_ctx
,
1168 const struct nir_shader_compiler_options
*options
,
1169 struct blob_reader
*blob
)
1173 list_inithead(&ctx
.phi_srcs
);
1174 ctx
.idx_table_len
= blob_read_uint32(blob
);
1175 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1178 uint32_t strings
= blob_read_uint32(blob
);
1179 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1180 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1182 struct shader_info info
;
1183 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1185 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1187 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1188 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1190 ctx
.nir
->info
= info
;
1192 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1193 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1194 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1195 read_var_list(&ctx
, &ctx
.nir
->shared
);
1196 read_var_list(&ctx
, &ctx
.nir
->globals
);
1197 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1199 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1200 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1201 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1202 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1203 ctx
.nir
->scratch_size
= blob_read_uint32(blob
);
1205 unsigned num_functions
= blob_read_uint32(blob
);
1206 for (unsigned i
= 0; i
< num_functions
; i
++)
1207 read_function(&ctx
);
1209 nir_foreach_function(fxn
, ctx
.nir
)
1210 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1212 ctx
.nir
->constant_data_size
= blob_read_uint32(blob
);
1213 if (ctx
.nir
->constant_data_size
> 0) {
1214 ctx
.nir
->constant_data
=
1215 ralloc_size(ctx
.nir
, ctx
.nir
->constant_data_size
);
1216 blob_copy_bytes(blob
, ctx
.nir
->constant_data
,
1217 ctx
.nir
->constant_data_size
);
1220 free(ctx
.idx_table
);
1226 nir_shader_serialize_deserialize(nir_shader
*shader
)
1228 const struct nir_shader_compiler_options
*options
= shader
->options
;
1232 nir_serialize(&writer
, shader
, false);
1234 /* Delete all of dest's ralloc children but leave dest alone */
1235 void *dead_ctx
= ralloc_context(NULL
);
1236 ralloc_adopt(dead_ctx
, shader
);
1237 ralloc_free(dead_ctx
);
1239 dead_ctx
= ralloc_context(NULL
);
1241 struct blob_reader reader
;
1242 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1243 nir_shader
*copy
= nir_deserialize(dead_ctx
, options
, &reader
);
1245 blob_finish(&writer
);
1247 nir_shader_replace(shader
, copy
);
1248 ralloc_free(dead_ctx
);