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(ctx
->nir
, 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
));
140 blob_write_string(ctx
->blob
, var
->name
);
141 blob_write_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
142 blob_write_uint32(ctx
->blob
, var
->num_state_slots
);
143 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->state_slots
,
144 var
->num_state_slots
* sizeof(nir_state_slot
));
145 blob_write_uint32(ctx
->blob
, !!(var
->constant_initializer
));
146 if (var
->constant_initializer
)
147 write_constant(ctx
, var
->constant_initializer
);
148 blob_write_uint32(ctx
->blob
, !!(var
->interface_type
));
149 if (var
->interface_type
)
150 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
153 static nir_variable
*
154 read_variable(read_ctx
*ctx
)
156 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
157 read_add_object(ctx
, var
);
159 var
->type
= decode_type_from_blob(ctx
->blob
);
160 bool has_name
= blob_read_uint32(ctx
->blob
);
162 const char *name
= blob_read_string(ctx
->blob
);
163 var
->name
= ralloc_strdup(var
, name
);
167 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
168 var
->num_state_slots
= blob_read_uint32(ctx
->blob
);
169 var
->state_slots
= ralloc_array(var
, nir_state_slot
, var
->num_state_slots
);
170 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->state_slots
,
171 var
->num_state_slots
* sizeof(nir_state_slot
));
172 bool has_const_initializer
= blob_read_uint32(ctx
->blob
);
173 if (has_const_initializer
)
174 var
->constant_initializer
= read_constant(ctx
, var
);
176 var
->constant_initializer
= NULL
;
177 bool has_interface_type
= blob_read_uint32(ctx
->blob
);
178 if (has_interface_type
)
179 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
181 var
->interface_type
= NULL
;
187 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
189 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
190 foreach_list_typed(nir_variable
, var
, node
, src
) {
191 write_variable(ctx
, var
);
196 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
198 exec_list_make_empty(dst
);
199 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
200 for (unsigned i
= 0; i
< num_vars
; i
++) {
201 nir_variable
*var
= read_variable(ctx
);
202 exec_list_push_tail(dst
, &var
->node
);
207 write_register(write_ctx
*ctx
, const nir_register
*reg
)
209 write_add_object(ctx
, reg
);
210 blob_write_uint32(ctx
->blob
, reg
->num_components
);
211 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
212 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
213 blob_write_uint32(ctx
->blob
, reg
->index
);
214 blob_write_uint32(ctx
->blob
, !!(reg
->name
));
216 blob_write_string(ctx
->blob
, reg
->name
);
217 blob_write_uint32(ctx
->blob
, reg
->is_global
<< 1 | reg
->is_packed
);
220 static nir_register
*
221 read_register(read_ctx
*ctx
)
223 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
224 read_add_object(ctx
, reg
);
225 reg
->num_components
= blob_read_uint32(ctx
->blob
);
226 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
227 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
228 reg
->index
= blob_read_uint32(ctx
->blob
);
229 bool has_name
= blob_read_uint32(ctx
->blob
);
231 const char *name
= blob_read_string(ctx
->blob
);
232 reg
->name
= ralloc_strdup(reg
, name
);
236 unsigned flags
= blob_read_uint32(ctx
->blob
);
237 reg
->is_global
= flags
& 0x2;
238 reg
->is_packed
= flags
& 0x1;
240 list_inithead(®
->uses
);
241 list_inithead(®
->defs
);
242 list_inithead(®
->if_uses
);
248 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
250 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
251 foreach_list_typed(nir_register
, reg
, node
, src
)
252 write_register(ctx
, reg
);
256 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
258 exec_list_make_empty(dst
);
259 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
260 for (unsigned i
= 0; i
< num_regs
; i
++) {
261 nir_register
*reg
= read_register(ctx
);
262 exec_list_push_tail(dst
, ®
->node
);
267 write_src(write_ctx
*ctx
, const nir_src
*src
)
269 /* Since sources are very frequent, we try to save some space when storing
270 * them. In particular, we store whether the source is a register and
271 * whether the register has an indirect index in the low two bits. We can
272 * assume that the high two bits of the index are zero, since otherwise our
273 * address space would've been exhausted allocating the remap table!
276 uintptr_t idx
= write_lookup_object(ctx
, src
->ssa
) << 2;
278 blob_write_intptr(ctx
->blob
, idx
);
280 uintptr_t idx
= write_lookup_object(ctx
, src
->reg
.reg
) << 2;
281 if (src
->reg
.indirect
)
283 blob_write_intptr(ctx
->blob
, idx
);
284 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
285 if (src
->reg
.indirect
) {
286 write_src(ctx
, src
->reg
.indirect
);
292 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
294 uintptr_t val
= blob_read_intptr(ctx
->blob
);
295 uintptr_t idx
= val
>> 2;
296 src
->is_ssa
= val
& 0x1;
298 src
->ssa
= read_lookup_object(ctx
, idx
);
300 bool is_indirect
= val
& 0x2;
301 src
->reg
.reg
= read_lookup_object(ctx
, idx
);
302 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
304 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
305 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
307 src
->reg
.indirect
= NULL
;
313 write_dest(write_ctx
*ctx
, const nir_dest
*dst
)
315 uint32_t val
= dst
->is_ssa
;
317 val
|= !!(dst
->ssa
.name
) << 1;
318 val
|= dst
->ssa
.num_components
<< 2;
319 val
|= dst
->ssa
.bit_size
<< 5;
321 val
|= !!(dst
->reg
.indirect
) << 1;
323 blob_write_uint32(ctx
->blob
, val
);
325 write_add_object(ctx
, &dst
->ssa
);
327 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
329 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
330 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
331 if (dst
->reg
.indirect
)
332 write_src(ctx
, dst
->reg
.indirect
);
337 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
)
339 uint32_t val
= blob_read_uint32(ctx
->blob
);
340 bool is_ssa
= val
& 0x1;
342 bool has_name
= val
& 0x2;
343 unsigned num_components
= (val
>> 2) & 0x7;
344 unsigned bit_size
= val
>> 5;
345 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
346 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
347 read_add_object(ctx
, &dst
->ssa
);
349 bool is_indirect
= val
& 0x2;
350 dst
->reg
.reg
= read_object(ctx
);
351 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
353 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
354 read_src(ctx
, dst
->reg
.indirect
, instr
);
360 write_deref_chain(write_ctx
*ctx
, const nir_deref_var
*deref_var
)
362 write_object(ctx
, deref_var
->var
);
365 for (const nir_deref
*d
= deref_var
->deref
.child
; d
; d
= d
->child
)
367 blob_write_uint32(ctx
->blob
, len
);
369 for (const nir_deref
*d
= deref_var
->deref
.child
; d
; d
= d
->child
) {
370 blob_write_uint32(ctx
->blob
, d
->deref_type
);
371 switch (d
->deref_type
) {
372 case nir_deref_type_array
: {
373 const nir_deref_array
*deref_array
= nir_deref_as_array(d
);
374 blob_write_uint32(ctx
->blob
, deref_array
->deref_array_type
);
375 blob_write_uint32(ctx
->blob
, deref_array
->base_offset
);
376 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
)
377 write_src(ctx
, &deref_array
->indirect
);
380 case nir_deref_type_struct
: {
381 const nir_deref_struct
*deref_struct
= nir_deref_as_struct(d
);
382 blob_write_uint32(ctx
->blob
, deref_struct
->index
);
385 case nir_deref_type_var
:
386 unreachable("Invalid deref type");
389 encode_type_to_blob(ctx
->blob
, d
->type
);
393 static nir_deref_var
*
394 read_deref_chain(read_ctx
*ctx
, void *mem_ctx
)
396 nir_variable
*var
= read_object(ctx
);
397 nir_deref_var
*deref_var
= nir_deref_var_create(mem_ctx
, var
);
399 uint32_t len
= blob_read_uint32(ctx
->blob
);
401 nir_deref
*tail
= &deref_var
->deref
;
402 for (uint32_t i
= 0; i
< len
; i
++) {
403 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
404 nir_deref
*deref
= NULL
;
405 switch (deref_type
) {
406 case nir_deref_type_array
: {
407 nir_deref_array
*deref_array
= nir_deref_array_create(tail
);
408 deref_array
->deref_array_type
= blob_read_uint32(ctx
->blob
);
409 deref_array
->base_offset
= blob_read_uint32(ctx
->blob
);
410 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
)
411 read_src(ctx
, &deref_array
->indirect
, mem_ctx
);
412 deref
= &deref_array
->deref
;
415 case nir_deref_type_struct
: {
416 uint32_t index
= blob_read_uint32(ctx
->blob
);
417 nir_deref_struct
*deref_struct
= nir_deref_struct_create(tail
, index
);
418 deref
= &deref_struct
->deref
;
421 case nir_deref_type_var
:
422 unreachable("Invalid deref type");
425 deref
->type
= decode_type_from_blob(ctx
->blob
);
435 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
437 blob_write_uint32(ctx
->blob
, alu
->op
);
438 uint32_t flags
= alu
->exact
;
439 flags
|= alu
->dest
.saturate
<< 1;
440 flags
|= alu
->dest
.write_mask
<< 2;
441 blob_write_uint32(ctx
->blob
, flags
);
443 write_dest(ctx
, &alu
->dest
.dest
);
445 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
446 write_src(ctx
, &alu
->src
[i
].src
);
447 flags
= alu
->src
[i
].negate
;
448 flags
|= alu
->src
[i
].abs
<< 1;
449 for (unsigned j
= 0; j
< 4; j
++)
450 flags
|= alu
->src
[i
].swizzle
[j
] << (2 + 2 * j
);
451 blob_write_uint32(ctx
->blob
, flags
);
455 static nir_alu_instr
*
456 read_alu(read_ctx
*ctx
)
458 nir_op op
= blob_read_uint32(ctx
->blob
);
459 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, op
);
461 uint32_t flags
= blob_read_uint32(ctx
->blob
);
462 alu
->exact
= flags
& 1;
463 alu
->dest
.saturate
= flags
& 2;
464 alu
->dest
.write_mask
= flags
>> 2;
466 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
);
468 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
469 read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
470 flags
= blob_read_uint32(ctx
->blob
);
471 alu
->src
[i
].negate
= flags
& 1;
472 alu
->src
[i
].abs
= flags
& 2;
473 for (unsigned j
= 0; j
< 4; j
++)
474 alu
->src
[i
].swizzle
[j
] = (flags
>> (2 * j
+ 2)) & 3;
481 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
483 blob_write_uint32(ctx
->blob
, intrin
->intrinsic
);
485 unsigned num_variables
= nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
;
486 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
487 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
489 blob_write_uint32(ctx
->blob
, intrin
->num_components
);
491 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
492 write_dest(ctx
, &intrin
->dest
);
494 for (unsigned i
= 0; i
< num_variables
; i
++)
495 write_deref_chain(ctx
, intrin
->variables
[i
]);
497 for (unsigned i
= 0; i
< num_srcs
; i
++)
498 write_src(ctx
, &intrin
->src
[i
]);
500 for (unsigned i
= 0; i
< num_indices
; i
++)
501 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
504 static nir_intrinsic_instr
*
505 read_intrinsic(read_ctx
*ctx
)
507 nir_intrinsic_op op
= blob_read_uint32(ctx
->blob
);
509 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
511 unsigned num_variables
= nir_intrinsic_infos
[op
].num_variables
;
512 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
513 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
515 intrin
->num_components
= blob_read_uint32(ctx
->blob
);
517 if (nir_intrinsic_infos
[op
].has_dest
)
518 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
);
520 for (unsigned i
= 0; i
< num_variables
; i
++)
521 intrin
->variables
[i
] = read_deref_chain(ctx
, &intrin
->instr
);
523 for (unsigned i
= 0; i
< num_srcs
; i
++)
524 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
526 for (unsigned i
= 0; i
< num_indices
; i
++)
527 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
533 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
535 uint32_t val
= lc
->def
.num_components
;
536 val
|= lc
->def
.bit_size
<< 3;
537 blob_write_uint32(ctx
->blob
, val
);
538 blob_write_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
539 write_add_object(ctx
, &lc
->def
);
542 static nir_load_const_instr
*
543 read_load_const(read_ctx
*ctx
)
545 uint32_t val
= blob_read_uint32(ctx
->blob
);
547 nir_load_const_instr
*lc
=
548 nir_load_const_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
550 blob_copy_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
551 read_add_object(ctx
, &lc
->def
);
556 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
558 uint32_t val
= undef
->def
.num_components
;
559 val
|= undef
->def
.bit_size
<< 3;
560 blob_write_uint32(ctx
->blob
, val
);
561 write_add_object(ctx
, &undef
->def
);
564 static nir_ssa_undef_instr
*
565 read_ssa_undef(read_ctx
*ctx
)
567 uint32_t val
= blob_read_uint32(ctx
->blob
);
569 nir_ssa_undef_instr
*undef
=
570 nir_ssa_undef_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
572 read_add_object(ctx
, &undef
->def
);
576 union packed_tex_data
{
579 enum glsl_sampler_dim sampler_dim
:4;
580 nir_alu_type dest_type
:8;
581 unsigned coord_components
:3;
583 unsigned is_shadow
:1;
584 unsigned is_new_style_shadow
:1;
585 unsigned component
:2;
586 unsigned has_texture_deref
:1;
587 unsigned has_sampler_deref
:1;
592 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
594 blob_write_uint32(ctx
->blob
, tex
->num_srcs
);
595 blob_write_uint32(ctx
->blob
, tex
->op
);
596 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
597 blob_write_uint32(ctx
->blob
, tex
->texture_array_size
);
598 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
600 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
601 union packed_tex_data packed
= {
602 .u
.sampler_dim
= tex
->sampler_dim
,
603 .u
.dest_type
= tex
->dest_type
,
604 .u
.coord_components
= tex
->coord_components
,
605 .u
.is_array
= tex
->is_array
,
606 .u
.is_shadow
= tex
->is_shadow
,
607 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
608 .u
.component
= tex
->component
,
609 .u
.has_texture_deref
= tex
->texture
!= NULL
,
610 .u
.has_sampler_deref
= tex
->sampler
!= NULL
,
612 blob_write_uint32(ctx
->blob
, packed
.u32
);
614 write_dest(ctx
, &tex
->dest
);
615 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
616 blob_write_uint32(ctx
->blob
, tex
->src
[i
].src_type
);
617 write_src(ctx
, &tex
->src
[i
].src
);
621 write_deref_chain(ctx
, tex
->texture
);
623 write_deref_chain(ctx
, tex
->sampler
);
626 static nir_tex_instr
*
627 read_tex(read_ctx
*ctx
)
629 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
630 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, num_srcs
);
632 tex
->op
= blob_read_uint32(ctx
->blob
);
633 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
634 tex
->texture_array_size
= blob_read_uint32(ctx
->blob
);
635 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
637 union packed_tex_data packed
;
638 packed
.u32
= blob_read_uint32(ctx
->blob
);
639 tex
->sampler_dim
= packed
.u
.sampler_dim
;
640 tex
->dest_type
= packed
.u
.dest_type
;
641 tex
->coord_components
= packed
.u
.coord_components
;
642 tex
->is_array
= packed
.u
.is_array
;
643 tex
->is_shadow
= packed
.u
.is_shadow
;
644 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
645 tex
->component
= packed
.u
.component
;
647 read_dest(ctx
, &tex
->dest
, &tex
->instr
);
648 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
649 tex
->src
[i
].src_type
= blob_read_uint32(ctx
->blob
);
650 read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
653 tex
->texture
= packed
.u
.has_texture_deref
?
654 read_deref_chain(ctx
, &tex
->instr
) : NULL
;
655 tex
->sampler
= packed
.u
.has_sampler_deref
?
656 read_deref_chain(ctx
, &tex
->instr
) : NULL
;
662 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
664 /* Phi nodes are special, since they may reference SSA definitions and
665 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
666 * and then store enough information so that a later fixup pass can fill
669 write_dest(ctx
, &phi
->dest
);
671 blob_write_uint32(ctx
->blob
, exec_list_length(&phi
->srcs
));
673 nir_foreach_phi_src(src
, phi
) {
674 assert(src
->src
.is_ssa
);
675 size_t blob_offset
= blob_reserve_intptr(ctx
->blob
);
676 MAYBE_UNUSED
size_t blob_offset2
= blob_reserve_intptr(ctx
->blob
);
677 assert(blob_offset
+ sizeof(uintptr_t) == blob_offset2
);
678 write_phi_fixup fixup
= {
679 .blob_offset
= blob_offset
,
683 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
688 write_fixup_phis(write_ctx
*ctx
)
690 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
691 uintptr_t *blob_ptr
= (uintptr_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
692 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
693 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
696 util_dynarray_clear(&ctx
->phi_fixups
);
699 static nir_phi_instr
*
700 read_phi(read_ctx
*ctx
, nir_block
*blk
)
702 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
704 read_dest(ctx
, &phi
->dest
, &phi
->instr
);
706 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
708 /* For similar reasons as before, we just store the index directly into the
709 * pointer, and let a later pass resolve the phi sources.
711 * In order to ensure that the copied sources (which are just the indices
712 * from the blob for now) don't get inserted into the old shader's use-def
713 * lists, we have to add the phi instruction *before* we set up its
716 nir_instr_insert_after_block(blk
, &phi
->instr
);
718 for (unsigned i
= 0; i
< num_srcs
; i
++) {
719 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
721 src
->src
.is_ssa
= true;
722 src
->src
.ssa
= (nir_ssa_def
*) blob_read_intptr(ctx
->blob
);
723 src
->pred
= (nir_block
*) blob_read_intptr(ctx
->blob
);
725 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
726 * we have to set the parent_instr manually. It doesn't really matter
727 * when we do it, so we might as well do it here.
729 src
->src
.parent_instr
= &phi
->instr
;
731 /* Stash it in the list of phi sources. We'll walk this list and fix up
732 * sources at the very end of read_function_impl.
734 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
736 exec_list_push_tail(&phi
->srcs
, &src
->node
);
743 read_fixup_phis(read_ctx
*ctx
)
745 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
746 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
747 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
749 /* Remove from this list */
750 list_del(&src
->src
.use_link
);
752 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
754 assert(list_empty(&ctx
->phi_srcs
));
758 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
760 blob_write_uint32(ctx
->blob
, jmp
->type
);
763 static nir_jump_instr
*
764 read_jump(read_ctx
*ctx
)
766 nir_jump_type type
= blob_read_uint32(ctx
->blob
);
767 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, type
);
772 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
774 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
776 for (unsigned i
= 0; i
< call
->num_params
; i
++)
777 write_deref_chain(ctx
, call
->params
[i
]);
779 write_deref_chain(ctx
, call
->return_deref
);
782 static nir_call_instr
*
783 read_call(read_ctx
*ctx
)
785 nir_function
*callee
= read_object(ctx
);
786 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
788 for (unsigned i
= 0; i
< call
->num_params
; i
++)
789 call
->params
[i
] = read_deref_chain(ctx
, &call
->instr
);
791 call
->return_deref
= read_deref_chain(ctx
, &call
->instr
);
797 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
799 blob_write_uint32(ctx
->blob
, instr
->type
);
800 switch (instr
->type
) {
801 case nir_instr_type_alu
:
802 write_alu(ctx
, nir_instr_as_alu(instr
));
804 case nir_instr_type_intrinsic
:
805 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
807 case nir_instr_type_load_const
:
808 write_load_const(ctx
, nir_instr_as_load_const(instr
));
810 case nir_instr_type_ssa_undef
:
811 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
813 case nir_instr_type_tex
:
814 write_tex(ctx
, nir_instr_as_tex(instr
));
816 case nir_instr_type_phi
:
817 write_phi(ctx
, nir_instr_as_phi(instr
));
819 case nir_instr_type_jump
:
820 write_jump(ctx
, nir_instr_as_jump(instr
));
822 case nir_instr_type_call
:
823 write_call(ctx
, nir_instr_as_call(instr
));
825 case nir_instr_type_parallel_copy
:
826 unreachable("Cannot write parallel copies");
828 unreachable("bad instr type");
833 read_instr(read_ctx
*ctx
, nir_block
*block
)
835 nir_instr_type type
= blob_read_uint32(ctx
->blob
);
838 case nir_instr_type_alu
:
839 instr
= &read_alu(ctx
)->instr
;
841 case nir_instr_type_intrinsic
:
842 instr
= &read_intrinsic(ctx
)->instr
;
844 case nir_instr_type_load_const
:
845 instr
= &read_load_const(ctx
)->instr
;
847 case nir_instr_type_ssa_undef
:
848 instr
= &read_ssa_undef(ctx
)->instr
;
850 case nir_instr_type_tex
:
851 instr
= &read_tex(ctx
)->instr
;
853 case nir_instr_type_phi
:
854 /* Phi instructions are a bit of a special case when reading because we
855 * don't want inserting the instruction to automatically handle use/defs
856 * for us. Instead, we need to wait until all the blocks/instructions
857 * are read so that we can set their sources up.
859 read_phi(ctx
, block
);
861 case nir_instr_type_jump
:
862 instr
= &read_jump(ctx
)->instr
;
864 case nir_instr_type_call
:
865 instr
= &read_call(ctx
)->instr
;
867 case nir_instr_type_parallel_copy
:
868 unreachable("Cannot read parallel copies");
870 unreachable("bad instr type");
873 nir_instr_insert_after_block(block
, instr
);
877 write_block(write_ctx
*ctx
, const nir_block
*block
)
879 write_add_object(ctx
, block
);
880 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
881 nir_foreach_instr(instr
, block
)
882 write_instr(ctx
, instr
);
886 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
888 /* Don't actually create a new block. Just use the one from the tail of
889 * the list. NIR guarantees that the tail of the list is a block and that
890 * no two blocks are side-by-side in the IR; It should be empty.
893 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
895 read_add_object(ctx
, block
);
896 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
897 for (unsigned i
= 0; i
< num_instrs
; i
++) {
898 read_instr(ctx
, block
);
903 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
906 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
909 write_if(write_ctx
*ctx
, nir_if
*nif
)
911 write_src(ctx
, &nif
->condition
);
913 write_cf_list(ctx
, &nif
->then_list
);
914 write_cf_list(ctx
, &nif
->else_list
);
918 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
920 nir_if
*nif
= nir_if_create(ctx
->nir
);
922 read_src(ctx
, &nif
->condition
, nif
);
924 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
926 read_cf_list(ctx
, &nif
->then_list
);
927 read_cf_list(ctx
, &nif
->else_list
);
931 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
933 write_cf_list(ctx
, &loop
->body
);
937 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
939 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
941 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
943 read_cf_list(ctx
, &loop
->body
);
947 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
949 blob_write_uint32(ctx
->blob
, cf
->type
);
952 case nir_cf_node_block
:
953 write_block(ctx
, nir_cf_node_as_block(cf
));
956 write_if(ctx
, nir_cf_node_as_if(cf
));
958 case nir_cf_node_loop
:
959 write_loop(ctx
, nir_cf_node_as_loop(cf
));
962 unreachable("bad cf type");
967 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
969 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
972 case nir_cf_node_block
:
973 read_block(ctx
, list
);
978 case nir_cf_node_loop
:
979 read_loop(ctx
, list
);
982 unreachable("bad cf type");
987 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
989 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
990 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
991 write_cf_node(ctx
, cf
);
996 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
998 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
999 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1000 read_cf_node(ctx
, cf_list
);
1004 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1006 write_var_list(ctx
, &fi
->locals
);
1007 write_reg_list(ctx
, &fi
->registers
);
1008 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1010 blob_write_uint32(ctx
->blob
, fi
->num_params
);
1011 for (unsigned i
= 0; i
< fi
->num_params
; i
++) {
1012 write_variable(ctx
, fi
->params
[i
]);
1015 blob_write_uint32(ctx
->blob
, !!(fi
->return_var
));
1017 write_variable(ctx
, fi
->return_var
);
1019 write_cf_list(ctx
, &fi
->body
);
1020 write_fixup_phis(ctx
);
1023 static nir_function_impl
*
1024 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1026 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1029 read_var_list(ctx
, &fi
->locals
);
1030 read_reg_list(ctx
, &fi
->registers
);
1031 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1033 fi
->num_params
= blob_read_uint32(ctx
->blob
);
1034 for (unsigned i
= 0; i
< fi
->num_params
; i
++) {
1035 fi
->params
[i
] = read_variable(ctx
);
1038 bool has_return
= blob_read_uint32(ctx
->blob
);
1040 fi
->return_var
= read_variable(ctx
);
1042 fi
->return_var
= NULL
;
1044 read_cf_list(ctx
, &fi
->body
);
1045 read_fixup_phis(ctx
);
1047 fi
->valid_metadata
= 0;
1053 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1055 blob_write_uint32(ctx
->blob
, !!(fxn
->name
));
1057 blob_write_string(ctx
->blob
, fxn
->name
);
1059 write_add_object(ctx
, fxn
);
1061 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1062 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1063 blob_write_uint32(ctx
->blob
, fxn
->params
[i
].param_type
);
1064 encode_type_to_blob(ctx
->blob
, fxn
->params
[i
].type
);
1067 encode_type_to_blob(ctx
->blob
, fxn
->return_type
);
1069 /* At first glance, it looks like we should write the function_impl here.
1070 * However, call instructions need to be able to reference at least the
1071 * function and those will get processed as we write the function_impls.
1072 * We stop here and write function_impls as a second pass.
1077 read_function(read_ctx
*ctx
)
1079 bool has_name
= blob_read_uint32(ctx
->blob
);
1080 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1082 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1084 read_add_object(ctx
, fxn
);
1086 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1087 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1088 fxn
->params
[i
].param_type
= blob_read_uint32(ctx
->blob
);
1089 fxn
->params
[i
].type
= decode_type_from_blob(ctx
->blob
);
1092 fxn
->return_type
= decode_type_from_blob(ctx
->blob
);
1096 nir_serialize(struct blob
*blob
, const nir_shader
*nir
)
1099 ctx
.remap_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1100 _mesa_key_pointer_equal
);
1104 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1106 size_t idx_size_offset
= blob_reserve_intptr(blob
);
1108 struct shader_info info
= nir
->info
;
1109 uint32_t strings
= 0;
1114 blob_write_uint32(blob
, strings
);
1116 blob_write_string(blob
, info
.name
);
1118 blob_write_string(blob
, info
.label
);
1119 info
.name
= info
.label
= NULL
;
1120 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1122 write_var_list(&ctx
, &nir
->uniforms
);
1123 write_var_list(&ctx
, &nir
->inputs
);
1124 write_var_list(&ctx
, &nir
->outputs
);
1125 write_var_list(&ctx
, &nir
->shared
);
1126 write_var_list(&ctx
, &nir
->globals
);
1127 write_var_list(&ctx
, &nir
->system_values
);
1129 write_reg_list(&ctx
, &nir
->registers
);
1130 blob_write_uint32(blob
, nir
->reg_alloc
);
1131 blob_write_uint32(blob
, nir
->num_inputs
);
1132 blob_write_uint32(blob
, nir
->num_uniforms
);
1133 blob_write_uint32(blob
, nir
->num_outputs
);
1134 blob_write_uint32(blob
, nir
->num_shared
);
1136 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1137 nir_foreach_function(fxn
, nir
) {
1138 write_function(&ctx
, fxn
);
1141 nir_foreach_function(fxn
, nir
) {
1142 write_function_impl(&ctx
, fxn
->impl
);
1145 *(uintptr_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1147 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1148 util_dynarray_fini(&ctx
.phi_fixups
);
1152 nir_deserialize(void *mem_ctx
,
1153 const struct nir_shader_compiler_options
*options
,
1154 struct blob_reader
*blob
)
1158 list_inithead(&ctx
.phi_srcs
);
1159 ctx
.idx_table_len
= blob_read_intptr(blob
);
1160 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1163 uint32_t strings
= blob_read_uint32(blob
);
1164 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1165 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1167 struct shader_info info
;
1168 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1170 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1172 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1173 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1175 ctx
.nir
->info
= info
;
1177 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1178 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1179 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1180 read_var_list(&ctx
, &ctx
.nir
->shared
);
1181 read_var_list(&ctx
, &ctx
.nir
->globals
);
1182 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1184 read_reg_list(&ctx
, &ctx
.nir
->registers
);
1185 ctx
.nir
->reg_alloc
= blob_read_uint32(blob
);
1186 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1187 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1188 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1189 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1191 unsigned num_functions
= blob_read_uint32(blob
);
1192 for (unsigned i
= 0; i
< num_functions
; i
++)
1193 read_function(&ctx
);
1195 nir_foreach_function(fxn
, ctx
.nir
)
1196 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1198 free(ctx
.idx_table
);
1204 nir_shader_serialize_deserialize(void *mem_ctx
, nir_shader
*s
)
1206 const struct nir_shader_compiler_options
*options
= s
->options
;
1210 nir_serialize(&writer
, s
);
1213 struct blob_reader reader
;
1214 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1215 nir_shader
*ns
= nir_deserialize(mem_ctx
, options
, &reader
);
1217 blob_finish(&writer
);