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"
35 const nir_shader
*nir
;
39 /* maps pointer to index */
40 struct hash_table
*remap_table
;
42 /* the next index to assign to a NIR in-memory object */
45 /* Array of write_phi_fixup structs representing phi sources that need to
46 * be resolved in the second pass.
48 struct util_dynarray phi_fixups
;
54 struct blob_reader
*blob
;
56 /* the next index to assign to a NIR in-memory object */
59 /* The length of the index -> object table */
60 uintptr_t idx_table_len
;
62 /* map from index to deserialized pointer */
65 /* List of phi sources. */
66 struct list_head phi_srcs
;
71 write_add_object(write_ctx
*ctx
, const void *obj
)
73 uintptr_t index
= ctx
->next_idx
++;
74 _mesa_hash_table_insert(ctx
->remap_table
, obj
, (void *) index
);
78 write_lookup_object(write_ctx
*ctx
, const void *obj
)
80 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->remap_table
, obj
);
82 return (uintptr_t) entry
->data
;
86 write_object(write_ctx
*ctx
, const void *obj
)
88 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, obj
));
92 read_add_object(read_ctx
*ctx
, void *obj
)
94 assert(ctx
->next_idx
< ctx
->idx_table_len
);
95 ctx
->idx_table
[ctx
->next_idx
++] = obj
;
99 read_lookup_object(read_ctx
*ctx
, uintptr_t idx
)
101 assert(idx
< ctx
->idx_table_len
);
102 return ctx
->idx_table
[idx
];
106 read_object(read_ctx
*ctx
)
108 return read_lookup_object(ctx
, blob_read_intptr(ctx
->blob
));
112 write_constant(write_ctx
*ctx
, const nir_constant
*c
)
114 blob_write_bytes(ctx
->blob
, c
->values
, sizeof(c
->values
));
115 blob_write_uint32(ctx
->blob
, c
->num_elements
);
116 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
117 write_constant(ctx
, c
->elements
[i
]);
120 static nir_constant
*
121 read_constant(read_ctx
*ctx
, nir_variable
*nvar
)
123 nir_constant
*c
= ralloc(nvar
, nir_constant
);
125 blob_copy_bytes(ctx
->blob
, (uint8_t *)c
->values
, sizeof(c
->values
));
126 c
->num_elements
= blob_read_uint32(ctx
->blob
);
127 c
->elements
= ralloc_array(nvar
, nir_constant
*, c
->num_elements
);
128 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
129 c
->elements
[i
] = read_constant(ctx
, nvar
);
135 write_variable(write_ctx
*ctx
, const nir_variable
*var
)
137 write_add_object(ctx
, var
);
138 encode_type_to_blob(ctx
->blob
, var
->type
);
139 blob_write_uint32(ctx
->blob
, !!(var
->name
));
141 blob_write_string(ctx
->blob
, var
->name
);
142 blob_write_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
143 blob_write_uint32(ctx
->blob
, var
->num_state_slots
);
144 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
145 for (unsigned j
= 0; j
< STATE_LENGTH
; j
++)
146 blob_write_uint32(ctx
->blob
, var
->state_slots
[i
].tokens
[j
]);
147 blob_write_uint32(ctx
->blob
, var
->state_slots
[i
].swizzle
);
149 blob_write_uint32(ctx
->blob
, !!(var
->constant_initializer
));
150 if (var
->constant_initializer
)
151 write_constant(ctx
, var
->constant_initializer
);
152 blob_write_uint32(ctx
->blob
, !!(var
->interface_type
));
153 if (var
->interface_type
)
154 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
155 blob_write_uint32(ctx
->blob
, var
->num_members
);
156 if (var
->num_members
> 0) {
157 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->members
,
158 var
->num_members
* sizeof(*var
->members
));
162 static nir_variable
*
163 read_variable(read_ctx
*ctx
)
165 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
166 read_add_object(ctx
, var
);
168 var
->type
= decode_type_from_blob(ctx
->blob
);
169 bool has_name
= blob_read_uint32(ctx
->blob
);
171 const char *name
= blob_read_string(ctx
->blob
);
172 var
->name
= ralloc_strdup(var
, name
);
176 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
177 var
->num_state_slots
= blob_read_uint32(ctx
->blob
);
178 if (var
->num_state_slots
!= 0) {
179 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
180 var
->num_state_slots
);
181 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
182 for (unsigned j
= 0; j
< STATE_LENGTH
; j
++)
183 var
->state_slots
[i
].tokens
[j
] = blob_read_uint32(ctx
->blob
);
184 var
->state_slots
[i
].swizzle
= blob_read_uint32(ctx
->blob
);
187 bool has_const_initializer
= blob_read_uint32(ctx
->blob
);
188 if (has_const_initializer
)
189 var
->constant_initializer
= read_constant(ctx
, var
);
191 var
->constant_initializer
= NULL
;
192 bool has_interface_type
= blob_read_uint32(ctx
->blob
);
193 if (has_interface_type
)
194 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
196 var
->interface_type
= NULL
;
197 var
->num_members
= blob_read_uint32(ctx
->blob
);
198 if (var
->num_members
> 0) {
199 var
->members
= ralloc_array(var
, struct nir_variable_data
,
201 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->members
,
202 var
->num_members
* sizeof(*var
->members
));
209 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
211 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
212 foreach_list_typed(nir_variable
, var
, node
, src
) {
213 write_variable(ctx
, var
);
218 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
220 exec_list_make_empty(dst
);
221 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
222 for (unsigned i
= 0; i
< num_vars
; i
++) {
223 nir_variable
*var
= read_variable(ctx
);
224 exec_list_push_tail(dst
, &var
->node
);
229 write_register(write_ctx
*ctx
, const nir_register
*reg
)
231 write_add_object(ctx
, reg
);
232 blob_write_uint32(ctx
->blob
, reg
->num_components
);
233 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
234 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
235 blob_write_uint32(ctx
->blob
, reg
->index
);
236 blob_write_uint32(ctx
->blob
, !!(reg
->name
));
238 blob_write_string(ctx
->blob
, reg
->name
);
239 blob_write_uint32(ctx
->blob
, reg
->is_global
<< 1 | reg
->is_packed
);
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
);
258 unsigned flags
= blob_read_uint32(ctx
->blob
);
259 reg
->is_global
= flags
& 0x2;
260 reg
->is_packed
= flags
& 0x1;
262 list_inithead(®
->uses
);
263 list_inithead(®
->defs
);
264 list_inithead(®
->if_uses
);
270 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
272 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
273 foreach_list_typed(nir_register
, reg
, node
, src
)
274 write_register(ctx
, reg
);
278 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
280 exec_list_make_empty(dst
);
281 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
282 for (unsigned i
= 0; i
< num_regs
; i
++) {
283 nir_register
*reg
= read_register(ctx
);
284 exec_list_push_tail(dst
, ®
->node
);
289 write_src(write_ctx
*ctx
, const nir_src
*src
)
291 /* Since sources are very frequent, we try to save some space when storing
292 * them. In particular, we store whether the source is a register and
293 * whether the register has an indirect index in the low two bits. We can
294 * assume that the high two bits of the index are zero, since otherwise our
295 * address space would've been exhausted allocating the remap table!
298 uintptr_t idx
= write_lookup_object(ctx
, src
->ssa
) << 2;
300 blob_write_intptr(ctx
->blob
, idx
);
302 uintptr_t idx
= write_lookup_object(ctx
, src
->reg
.reg
) << 2;
303 if (src
->reg
.indirect
)
305 blob_write_intptr(ctx
->blob
, idx
);
306 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
307 if (src
->reg
.indirect
) {
308 write_src(ctx
, src
->reg
.indirect
);
314 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
316 uintptr_t val
= blob_read_intptr(ctx
->blob
);
317 uintptr_t idx
= val
>> 2;
318 src
->is_ssa
= val
& 0x1;
320 src
->ssa
= read_lookup_object(ctx
, idx
);
322 bool is_indirect
= val
& 0x2;
323 src
->reg
.reg
= read_lookup_object(ctx
, idx
);
324 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
326 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
327 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
329 src
->reg
.indirect
= NULL
;
335 write_dest(write_ctx
*ctx
, const nir_dest
*dst
)
337 uint32_t val
= dst
->is_ssa
;
339 val
|= !!(dst
->ssa
.name
) << 1;
340 val
|= dst
->ssa
.num_components
<< 2;
341 val
|= dst
->ssa
.bit_size
<< 5;
343 val
|= !!(dst
->reg
.indirect
) << 1;
345 blob_write_uint32(ctx
->blob
, val
);
347 write_add_object(ctx
, &dst
->ssa
);
349 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
351 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
352 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
353 if (dst
->reg
.indirect
)
354 write_src(ctx
, dst
->reg
.indirect
);
359 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
)
361 uint32_t val
= blob_read_uint32(ctx
->blob
);
362 bool is_ssa
= val
& 0x1;
364 bool has_name
= val
& 0x2;
365 unsigned num_components
= (val
>> 2) & 0x7;
366 unsigned bit_size
= val
>> 5;
367 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
368 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
369 read_add_object(ctx
, &dst
->ssa
);
371 bool is_indirect
= val
& 0x2;
372 dst
->reg
.reg
= read_object(ctx
);
373 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
375 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
376 read_src(ctx
, dst
->reg
.indirect
, instr
);
382 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
384 blob_write_uint32(ctx
->blob
, alu
->op
);
385 uint32_t flags
= alu
->exact
;
386 flags
|= alu
->dest
.saturate
<< 1;
387 flags
|= alu
->dest
.write_mask
<< 2;
388 blob_write_uint32(ctx
->blob
, flags
);
390 write_dest(ctx
, &alu
->dest
.dest
);
392 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
393 write_src(ctx
, &alu
->src
[i
].src
);
394 flags
= alu
->src
[i
].negate
;
395 flags
|= alu
->src
[i
].abs
<< 1;
396 for (unsigned j
= 0; j
< 4; j
++)
397 flags
|= alu
->src
[i
].swizzle
[j
] << (2 + 2 * j
);
398 blob_write_uint32(ctx
->blob
, flags
);
402 static nir_alu_instr
*
403 read_alu(read_ctx
*ctx
)
405 nir_op op
= blob_read_uint32(ctx
->blob
);
406 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, op
);
408 uint32_t flags
= blob_read_uint32(ctx
->blob
);
409 alu
->exact
= flags
& 1;
410 alu
->dest
.saturate
= flags
& 2;
411 alu
->dest
.write_mask
= flags
>> 2;
413 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
);
415 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
416 read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
417 flags
= blob_read_uint32(ctx
->blob
);
418 alu
->src
[i
].negate
= flags
& 1;
419 alu
->src
[i
].abs
= flags
& 2;
420 for (unsigned j
= 0; j
< 4; j
++)
421 alu
->src
[i
].swizzle
[j
] = (flags
>> (2 * j
+ 2)) & 3;
428 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
430 blob_write_uint32(ctx
->blob
, deref
->deref_type
);
432 blob_write_uint32(ctx
->blob
, deref
->mode
);
433 encode_type_to_blob(ctx
->blob
, deref
->type
);
435 write_dest(ctx
, &deref
->dest
);
437 if (deref
->deref_type
== nir_deref_type_var
) {
438 write_object(ctx
, deref
->var
);
442 write_src(ctx
, &deref
->parent
);
444 switch (deref
->deref_type
) {
445 case nir_deref_type_struct
:
446 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
449 case nir_deref_type_array
:
450 case nir_deref_type_ptr_as_array
:
451 write_src(ctx
, &deref
->arr
.index
);
454 case nir_deref_type_cast
:
455 blob_write_uint32(ctx
->blob
, deref
->cast
.ptr_stride
);
458 case nir_deref_type_array_wildcard
:
463 unreachable("Invalid deref type");
467 static nir_deref_instr
*
468 read_deref(read_ctx
*ctx
)
470 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
471 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
473 deref
->mode
= blob_read_uint32(ctx
->blob
);
474 deref
->type
= decode_type_from_blob(ctx
->blob
);
476 read_dest(ctx
, &deref
->dest
, &deref
->instr
);
478 if (deref_type
== nir_deref_type_var
) {
479 deref
->var
= read_object(ctx
);
483 read_src(ctx
, &deref
->parent
, &deref
->instr
);
485 switch (deref
->deref_type
) {
486 case nir_deref_type_struct
:
487 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
490 case nir_deref_type_array
:
491 case nir_deref_type_ptr_as_array
:
492 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
495 case nir_deref_type_cast
:
496 deref
->cast
.ptr_stride
= blob_read_uint32(ctx
->blob
);
499 case nir_deref_type_array_wildcard
:
504 unreachable("Invalid deref type");
511 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
513 blob_write_uint32(ctx
->blob
, intrin
->intrinsic
);
515 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
516 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
518 blob_write_uint32(ctx
->blob
, intrin
->num_components
);
520 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
521 write_dest(ctx
, &intrin
->dest
);
523 for (unsigned i
= 0; i
< num_srcs
; i
++)
524 write_src(ctx
, &intrin
->src
[i
]);
526 for (unsigned i
= 0; i
< num_indices
; i
++)
527 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
530 static nir_intrinsic_instr
*
531 read_intrinsic(read_ctx
*ctx
)
533 nir_intrinsic_op op
= blob_read_uint32(ctx
->blob
);
535 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
537 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
538 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
540 intrin
->num_components
= blob_read_uint32(ctx
->blob
);
542 if (nir_intrinsic_infos
[op
].has_dest
)
543 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
);
545 for (unsigned i
= 0; i
< num_srcs
; i
++)
546 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
548 for (unsigned i
= 0; i
< num_indices
; i
++)
549 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
555 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
557 uint32_t val
= lc
->def
.num_components
;
558 val
|= lc
->def
.bit_size
<< 3;
559 blob_write_uint32(ctx
->blob
, val
);
560 blob_write_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
561 write_add_object(ctx
, &lc
->def
);
564 static nir_load_const_instr
*
565 read_load_const(read_ctx
*ctx
)
567 uint32_t val
= blob_read_uint32(ctx
->blob
);
569 nir_load_const_instr
*lc
=
570 nir_load_const_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
572 blob_copy_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
573 read_add_object(ctx
, &lc
->def
);
578 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
580 uint32_t val
= undef
->def
.num_components
;
581 val
|= undef
->def
.bit_size
<< 3;
582 blob_write_uint32(ctx
->blob
, val
);
583 write_add_object(ctx
, &undef
->def
);
586 static nir_ssa_undef_instr
*
587 read_ssa_undef(read_ctx
*ctx
)
589 uint32_t val
= blob_read_uint32(ctx
->blob
);
591 nir_ssa_undef_instr
*undef
=
592 nir_ssa_undef_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
594 read_add_object(ctx
, &undef
->def
);
598 union packed_tex_data
{
601 enum glsl_sampler_dim sampler_dim
:4;
602 nir_alu_type dest_type
:8;
603 unsigned coord_components
:3;
605 unsigned is_shadow
:1;
606 unsigned is_new_style_shadow
:1;
607 unsigned component
:2;
608 unsigned unused
:10; /* Mark unused for valgrind. */
613 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
615 blob_write_uint32(ctx
->blob
, tex
->num_srcs
);
616 blob_write_uint32(ctx
->blob
, tex
->op
);
617 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
618 blob_write_uint32(ctx
->blob
, tex
->texture_array_size
);
619 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
621 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
622 union packed_tex_data packed
= {
623 .u
.sampler_dim
= tex
->sampler_dim
,
624 .u
.dest_type
= tex
->dest_type
,
625 .u
.coord_components
= tex
->coord_components
,
626 .u
.is_array
= tex
->is_array
,
627 .u
.is_shadow
= tex
->is_shadow
,
628 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
629 .u
.component
= tex
->component
,
631 blob_write_uint32(ctx
->blob
, packed
.u32
);
633 write_dest(ctx
, &tex
->dest
);
634 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
635 blob_write_uint32(ctx
->blob
, tex
->src
[i
].src_type
);
636 write_src(ctx
, &tex
->src
[i
].src
);
640 static nir_tex_instr
*
641 read_tex(read_ctx
*ctx
)
643 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
644 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, num_srcs
);
646 tex
->op
= blob_read_uint32(ctx
->blob
);
647 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
648 tex
->texture_array_size
= blob_read_uint32(ctx
->blob
);
649 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
651 union packed_tex_data packed
;
652 packed
.u32
= blob_read_uint32(ctx
->blob
);
653 tex
->sampler_dim
= packed
.u
.sampler_dim
;
654 tex
->dest_type
= packed
.u
.dest_type
;
655 tex
->coord_components
= packed
.u
.coord_components
;
656 tex
->is_array
= packed
.u
.is_array
;
657 tex
->is_shadow
= packed
.u
.is_shadow
;
658 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
659 tex
->component
= packed
.u
.component
;
661 read_dest(ctx
, &tex
->dest
, &tex
->instr
);
662 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
663 tex
->src
[i
].src_type
= blob_read_uint32(ctx
->blob
);
664 read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
671 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
673 /* Phi nodes are special, since they may reference SSA definitions and
674 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
675 * and then store enough information so that a later fixup pass can fill
678 write_dest(ctx
, &phi
->dest
);
680 blob_write_uint32(ctx
->blob
, exec_list_length(&phi
->srcs
));
682 nir_foreach_phi_src(src
, phi
) {
683 assert(src
->src
.is_ssa
);
684 size_t blob_offset
= blob_reserve_intptr(ctx
->blob
);
685 MAYBE_UNUSED
size_t blob_offset2
= blob_reserve_intptr(ctx
->blob
);
686 assert(blob_offset
+ sizeof(uintptr_t) == blob_offset2
);
687 write_phi_fixup fixup
= {
688 .blob_offset
= blob_offset
,
692 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
697 write_fixup_phis(write_ctx
*ctx
)
699 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
700 uintptr_t *blob_ptr
= (uintptr_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
701 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
702 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
705 util_dynarray_clear(&ctx
->phi_fixups
);
708 static nir_phi_instr
*
709 read_phi(read_ctx
*ctx
, nir_block
*blk
)
711 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
713 read_dest(ctx
, &phi
->dest
, &phi
->instr
);
715 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
717 /* For similar reasons as before, we just store the index directly into the
718 * pointer, and let a later pass resolve the phi sources.
720 * In order to ensure that the copied sources (which are just the indices
721 * from the blob for now) don't get inserted into the old shader's use-def
722 * lists, we have to add the phi instruction *before* we set up its
725 nir_instr_insert_after_block(blk
, &phi
->instr
);
727 for (unsigned i
= 0; i
< num_srcs
; i
++) {
728 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
730 src
->src
.is_ssa
= true;
731 src
->src
.ssa
= (nir_ssa_def
*) blob_read_intptr(ctx
->blob
);
732 src
->pred
= (nir_block
*) blob_read_intptr(ctx
->blob
);
734 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
735 * we have to set the parent_instr manually. It doesn't really matter
736 * when we do it, so we might as well do it here.
738 src
->src
.parent_instr
= &phi
->instr
;
740 /* Stash it in the list of phi sources. We'll walk this list and fix up
741 * sources at the very end of read_function_impl.
743 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
745 exec_list_push_tail(&phi
->srcs
, &src
->node
);
752 read_fixup_phis(read_ctx
*ctx
)
754 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
755 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
756 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
758 /* Remove from this list */
759 list_del(&src
->src
.use_link
);
761 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
763 assert(list_empty(&ctx
->phi_srcs
));
767 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
769 blob_write_uint32(ctx
->blob
, jmp
->type
);
772 static nir_jump_instr
*
773 read_jump(read_ctx
*ctx
)
775 nir_jump_type type
= blob_read_uint32(ctx
->blob
);
776 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, type
);
781 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
783 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
785 for (unsigned i
= 0; i
< call
->num_params
; i
++)
786 write_src(ctx
, &call
->params
[i
]);
789 static nir_call_instr
*
790 read_call(read_ctx
*ctx
)
792 nir_function
*callee
= read_object(ctx
);
793 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
795 for (unsigned i
= 0; i
< call
->num_params
; i
++)
796 read_src(ctx
, &call
->params
[i
], call
);
802 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
804 blob_write_uint32(ctx
->blob
, instr
->type
);
805 switch (instr
->type
) {
806 case nir_instr_type_alu
:
807 write_alu(ctx
, nir_instr_as_alu(instr
));
809 case nir_instr_type_deref
:
810 write_deref(ctx
, nir_instr_as_deref(instr
));
812 case nir_instr_type_intrinsic
:
813 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
815 case nir_instr_type_load_const
:
816 write_load_const(ctx
, nir_instr_as_load_const(instr
));
818 case nir_instr_type_ssa_undef
:
819 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
821 case nir_instr_type_tex
:
822 write_tex(ctx
, nir_instr_as_tex(instr
));
824 case nir_instr_type_phi
:
825 write_phi(ctx
, nir_instr_as_phi(instr
));
827 case nir_instr_type_jump
:
828 write_jump(ctx
, nir_instr_as_jump(instr
));
830 case nir_instr_type_call
:
831 write_call(ctx
, nir_instr_as_call(instr
));
833 case nir_instr_type_parallel_copy
:
834 unreachable("Cannot write parallel copies");
836 unreachable("bad instr type");
841 read_instr(read_ctx
*ctx
, nir_block
*block
)
843 nir_instr_type type
= blob_read_uint32(ctx
->blob
);
846 case nir_instr_type_alu
:
847 instr
= &read_alu(ctx
)->instr
;
849 case nir_instr_type_deref
:
850 instr
= &read_deref(ctx
)->instr
;
852 case nir_instr_type_intrinsic
:
853 instr
= &read_intrinsic(ctx
)->instr
;
855 case nir_instr_type_load_const
:
856 instr
= &read_load_const(ctx
)->instr
;
858 case nir_instr_type_ssa_undef
:
859 instr
= &read_ssa_undef(ctx
)->instr
;
861 case nir_instr_type_tex
:
862 instr
= &read_tex(ctx
)->instr
;
864 case nir_instr_type_phi
:
865 /* Phi instructions are a bit of a special case when reading because we
866 * don't want inserting the instruction to automatically handle use/defs
867 * for us. Instead, we need to wait until all the blocks/instructions
868 * are read so that we can set their sources up.
870 read_phi(ctx
, block
);
872 case nir_instr_type_jump
:
873 instr
= &read_jump(ctx
)->instr
;
875 case nir_instr_type_call
:
876 instr
= &read_call(ctx
)->instr
;
878 case nir_instr_type_parallel_copy
:
879 unreachable("Cannot read parallel copies");
881 unreachable("bad instr type");
884 nir_instr_insert_after_block(block
, instr
);
888 write_block(write_ctx
*ctx
, const nir_block
*block
)
890 write_add_object(ctx
, block
);
891 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
892 nir_foreach_instr(instr
, block
)
893 write_instr(ctx
, instr
);
897 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
899 /* Don't actually create a new block. Just use the one from the tail of
900 * the list. NIR guarantees that the tail of the list is a block and that
901 * no two blocks are side-by-side in the IR; It should be empty.
904 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
906 read_add_object(ctx
, block
);
907 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
908 for (unsigned i
= 0; i
< num_instrs
; i
++) {
909 read_instr(ctx
, block
);
914 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
917 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
920 write_if(write_ctx
*ctx
, nir_if
*nif
)
922 write_src(ctx
, &nif
->condition
);
924 write_cf_list(ctx
, &nif
->then_list
);
925 write_cf_list(ctx
, &nif
->else_list
);
929 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
931 nir_if
*nif
= nir_if_create(ctx
->nir
);
933 read_src(ctx
, &nif
->condition
, nif
);
935 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
937 read_cf_list(ctx
, &nif
->then_list
);
938 read_cf_list(ctx
, &nif
->else_list
);
942 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
944 write_cf_list(ctx
, &loop
->body
);
948 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
950 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
952 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
954 read_cf_list(ctx
, &loop
->body
);
958 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
960 blob_write_uint32(ctx
->blob
, cf
->type
);
963 case nir_cf_node_block
:
964 write_block(ctx
, nir_cf_node_as_block(cf
));
967 write_if(ctx
, nir_cf_node_as_if(cf
));
969 case nir_cf_node_loop
:
970 write_loop(ctx
, nir_cf_node_as_loop(cf
));
973 unreachable("bad cf type");
978 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
980 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
983 case nir_cf_node_block
:
984 read_block(ctx
, list
);
989 case nir_cf_node_loop
:
990 read_loop(ctx
, list
);
993 unreachable("bad cf type");
998 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
1000 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
1001 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
1002 write_cf_node(ctx
, cf
);
1007 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1009 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1010 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1011 read_cf_node(ctx
, cf_list
);
1015 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1017 write_var_list(ctx
, &fi
->locals
);
1018 write_reg_list(ctx
, &fi
->registers
);
1019 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1021 write_cf_list(ctx
, &fi
->body
);
1022 write_fixup_phis(ctx
);
1025 static nir_function_impl
*
1026 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1028 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1031 read_var_list(ctx
, &fi
->locals
);
1032 read_reg_list(ctx
, &fi
->registers
);
1033 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1035 read_cf_list(ctx
, &fi
->body
);
1036 read_fixup_phis(ctx
);
1038 fi
->valid_metadata
= 0;
1044 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1046 blob_write_uint32(ctx
->blob
, !!(fxn
->name
));
1048 blob_write_string(ctx
->blob
, fxn
->name
);
1050 write_add_object(ctx
, fxn
);
1052 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1053 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1055 ((uint32_t)fxn
->params
[i
].num_components
) |
1056 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1057 blob_write_uint32(ctx
->blob
, val
);
1060 blob_write_uint32(ctx
->blob
, fxn
->is_entrypoint
);
1062 /* At first glance, it looks like we should write the function_impl here.
1063 * However, call instructions need to be able to reference at least the
1064 * function and those will get processed as we write the function_impls.
1065 * We stop here and write function_impls as a second pass.
1070 read_function(read_ctx
*ctx
)
1072 bool has_name
= blob_read_uint32(ctx
->blob
);
1073 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1075 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1077 read_add_object(ctx
, fxn
);
1079 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1080 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1081 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1082 uint32_t val
= blob_read_uint32(ctx
->blob
);
1083 fxn
->params
[i
].num_components
= val
& 0xff;
1084 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1087 fxn
->is_entrypoint
= blob_read_uint32(ctx
->blob
);
1091 nir_serialize(struct blob
*blob
, const nir_shader
*nir
)
1094 ctx
.remap_table
= _mesa_pointer_hash_table_create(NULL
);
1098 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1100 size_t idx_size_offset
= blob_reserve_intptr(blob
);
1102 struct shader_info info
= nir
->info
;
1103 uint32_t strings
= 0;
1108 blob_write_uint32(blob
, strings
);
1110 blob_write_string(blob
, info
.name
);
1112 blob_write_string(blob
, info
.label
);
1113 info
.name
= info
.label
= NULL
;
1114 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1116 write_var_list(&ctx
, &nir
->uniforms
);
1117 write_var_list(&ctx
, &nir
->inputs
);
1118 write_var_list(&ctx
, &nir
->outputs
);
1119 write_var_list(&ctx
, &nir
->shared
);
1120 write_var_list(&ctx
, &nir
->globals
);
1121 write_var_list(&ctx
, &nir
->system_values
);
1123 write_reg_list(&ctx
, &nir
->registers
);
1124 blob_write_uint32(blob
, nir
->reg_alloc
);
1125 blob_write_uint32(blob
, nir
->num_inputs
);
1126 blob_write_uint32(blob
, nir
->num_uniforms
);
1127 blob_write_uint32(blob
, nir
->num_outputs
);
1128 blob_write_uint32(blob
, nir
->num_shared
);
1130 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1131 nir_foreach_function(fxn
, nir
) {
1132 write_function(&ctx
, fxn
);
1135 nir_foreach_function(fxn
, nir
) {
1136 write_function_impl(&ctx
, fxn
->impl
);
1139 blob_write_uint32(blob
, nir
->constant_data_size
);
1140 if (nir
->constant_data_size
> 0)
1141 blob_write_bytes(blob
, nir
->constant_data
, nir
->constant_data_size
);
1143 *(uintptr_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1145 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1146 util_dynarray_fini(&ctx
.phi_fixups
);
1150 nir_deserialize(void *mem_ctx
,
1151 const struct nir_shader_compiler_options
*options
,
1152 struct blob_reader
*blob
)
1156 list_inithead(&ctx
.phi_srcs
);
1157 ctx
.idx_table_len
= blob_read_intptr(blob
);
1158 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1161 uint32_t strings
= blob_read_uint32(blob
);
1162 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1163 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1165 struct shader_info info
;
1166 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1168 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1170 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1171 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1173 ctx
.nir
->info
= info
;
1175 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1176 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1177 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1178 read_var_list(&ctx
, &ctx
.nir
->shared
);
1179 read_var_list(&ctx
, &ctx
.nir
->globals
);
1180 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1182 read_reg_list(&ctx
, &ctx
.nir
->registers
);
1183 ctx
.nir
->reg_alloc
= blob_read_uint32(blob
);
1184 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1185 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1186 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1187 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1189 unsigned num_functions
= blob_read_uint32(blob
);
1190 for (unsigned i
= 0; i
< num_functions
; i
++)
1191 read_function(&ctx
);
1193 nir_foreach_function(fxn
, ctx
.nir
)
1194 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1196 ctx
.nir
->constant_data_size
= blob_read_uint32(blob
);
1197 if (ctx
.nir
->constant_data_size
> 0) {
1198 ctx
.nir
->constant_data
=
1199 ralloc_size(ctx
.nir
, ctx
.nir
->constant_data_size
);
1200 blob_copy_bytes(blob
, ctx
.nir
->constant_data
,
1201 ctx
.nir
->constant_data_size
);
1204 free(ctx
.idx_table
);
1210 nir_shader_serialize_deserialize(void *mem_ctx
, nir_shader
*s
)
1212 const struct nir_shader_compiler_options
*options
= s
->options
;
1216 nir_serialize(&writer
, s
);
1219 struct blob_reader reader
;
1220 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1221 nir_shader
*ns
= nir_deserialize(mem_ctx
, options
, &reader
);
1223 blob_finish(&writer
);