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
));
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 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->state_slots
,
145 var
->num_state_slots
* sizeof(nir_state_slot
));
146 blob_write_uint32(ctx
->blob
, !!(var
->constant_initializer
));
147 if (var
->constant_initializer
)
148 write_constant(ctx
, var
->constant_initializer
);
149 blob_write_uint32(ctx
->blob
, !!(var
->interface_type
));
150 if (var
->interface_type
)
151 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
154 static nir_variable
*
155 read_variable(read_ctx
*ctx
)
157 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
158 read_add_object(ctx
, var
);
160 var
->type
= decode_type_from_blob(ctx
->blob
);
161 bool has_name
= blob_read_uint32(ctx
->blob
);
163 const char *name
= blob_read_string(ctx
->blob
);
164 var
->name
= ralloc_strdup(var
, name
);
168 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
169 var
->num_state_slots
= blob_read_uint32(ctx
->blob
);
170 var
->state_slots
= ralloc_array(var
, nir_state_slot
, var
->num_state_slots
);
171 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->state_slots
,
172 var
->num_state_slots
* sizeof(nir_state_slot
));
173 bool has_const_initializer
= blob_read_uint32(ctx
->blob
);
174 if (has_const_initializer
)
175 var
->constant_initializer
= read_constant(ctx
, var
);
177 var
->constant_initializer
= NULL
;
178 bool has_interface_type
= blob_read_uint32(ctx
->blob
);
179 if (has_interface_type
)
180 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
182 var
->interface_type
= NULL
;
188 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
190 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
191 foreach_list_typed(nir_variable
, var
, node
, src
) {
192 write_variable(ctx
, var
);
197 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
199 exec_list_make_empty(dst
);
200 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
201 for (unsigned i
= 0; i
< num_vars
; i
++) {
202 nir_variable
*var
= read_variable(ctx
);
203 exec_list_push_tail(dst
, &var
->node
);
208 write_register(write_ctx
*ctx
, const nir_register
*reg
)
210 write_add_object(ctx
, reg
);
211 blob_write_uint32(ctx
->blob
, reg
->num_components
);
212 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
213 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
214 blob_write_uint32(ctx
->blob
, reg
->index
);
215 blob_write_uint32(ctx
->blob
, !!(reg
->name
));
217 blob_write_string(ctx
->blob
, reg
->name
);
218 blob_write_uint32(ctx
->blob
, reg
->is_global
<< 1 | reg
->is_packed
);
221 static nir_register
*
222 read_register(read_ctx
*ctx
)
224 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
225 read_add_object(ctx
, reg
);
226 reg
->num_components
= blob_read_uint32(ctx
->blob
);
227 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
228 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
229 reg
->index
= blob_read_uint32(ctx
->blob
);
230 bool has_name
= blob_read_uint32(ctx
->blob
);
232 const char *name
= blob_read_string(ctx
->blob
);
233 reg
->name
= ralloc_strdup(reg
, name
);
237 unsigned flags
= blob_read_uint32(ctx
->blob
);
238 reg
->is_global
= flags
& 0x2;
239 reg
->is_packed
= flags
& 0x1;
241 list_inithead(®
->uses
);
242 list_inithead(®
->defs
);
243 list_inithead(®
->if_uses
);
249 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
251 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
252 foreach_list_typed(nir_register
, reg
, node
, src
)
253 write_register(ctx
, reg
);
257 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
259 exec_list_make_empty(dst
);
260 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
261 for (unsigned i
= 0; i
< num_regs
; i
++) {
262 nir_register
*reg
= read_register(ctx
);
263 exec_list_push_tail(dst
, ®
->node
);
268 write_src(write_ctx
*ctx
, const nir_src
*src
)
270 /* Since sources are very frequent, we try to save some space when storing
271 * them. In particular, we store whether the source is a register and
272 * whether the register has an indirect index in the low two bits. We can
273 * assume that the high two bits of the index are zero, since otherwise our
274 * address space would've been exhausted allocating the remap table!
277 uintptr_t idx
= write_lookup_object(ctx
, src
->ssa
) << 2;
279 blob_write_intptr(ctx
->blob
, idx
);
281 uintptr_t idx
= write_lookup_object(ctx
, src
->reg
.reg
) << 2;
282 if (src
->reg
.indirect
)
284 blob_write_intptr(ctx
->blob
, idx
);
285 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
286 if (src
->reg
.indirect
) {
287 write_src(ctx
, src
->reg
.indirect
);
293 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
295 uintptr_t val
= blob_read_intptr(ctx
->blob
);
296 uintptr_t idx
= val
>> 2;
297 src
->is_ssa
= val
& 0x1;
299 src
->ssa
= read_lookup_object(ctx
, idx
);
301 bool is_indirect
= val
& 0x2;
302 src
->reg
.reg
= read_lookup_object(ctx
, idx
);
303 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
305 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
306 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
308 src
->reg
.indirect
= NULL
;
314 write_dest(write_ctx
*ctx
, const nir_dest
*dst
)
316 uint32_t val
= dst
->is_ssa
;
318 val
|= !!(dst
->ssa
.name
) << 1;
319 val
|= dst
->ssa
.num_components
<< 2;
320 val
|= dst
->ssa
.bit_size
<< 5;
322 val
|= !!(dst
->reg
.indirect
) << 1;
324 blob_write_uint32(ctx
->blob
, val
);
326 write_add_object(ctx
, &dst
->ssa
);
328 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
330 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
331 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
332 if (dst
->reg
.indirect
)
333 write_src(ctx
, dst
->reg
.indirect
);
338 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
)
340 uint32_t val
= blob_read_uint32(ctx
->blob
);
341 bool is_ssa
= val
& 0x1;
343 bool has_name
= val
& 0x2;
344 unsigned num_components
= (val
>> 2) & 0x7;
345 unsigned bit_size
= val
>> 5;
346 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
347 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
348 read_add_object(ctx
, &dst
->ssa
);
350 bool is_indirect
= val
& 0x2;
351 dst
->reg
.reg
= read_object(ctx
);
352 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
354 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
355 read_src(ctx
, dst
->reg
.indirect
, instr
);
361 write_deref_chain(write_ctx
*ctx
, const nir_deref_var
*deref_var
)
363 write_object(ctx
, deref_var
->var
);
366 for (const nir_deref
*d
= deref_var
->deref
.child
; d
; d
= d
->child
)
368 blob_write_uint32(ctx
->blob
, len
);
370 for (const nir_deref
*d
= deref_var
->deref
.child
; d
; d
= d
->child
) {
371 blob_write_uint32(ctx
->blob
, d
->deref_type
);
372 switch (d
->deref_type
) {
373 case nir_deref_type_array
: {
374 const nir_deref_array
*deref_array
= nir_deref_as_array(d
);
375 blob_write_uint32(ctx
->blob
, deref_array
->deref_array_type
);
376 blob_write_uint32(ctx
->blob
, deref_array
->base_offset
);
377 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
)
378 write_src(ctx
, &deref_array
->indirect
);
381 case nir_deref_type_struct
: {
382 const nir_deref_struct
*deref_struct
= nir_deref_as_struct(d
);
383 blob_write_uint32(ctx
->blob
, deref_struct
->index
);
386 case nir_deref_type_var
:
387 unreachable("Invalid deref type");
390 encode_type_to_blob(ctx
->blob
, d
->type
);
394 static nir_deref_var
*
395 read_deref_chain(read_ctx
*ctx
, void *mem_ctx
)
397 nir_variable
*var
= read_object(ctx
);
398 nir_deref_var
*deref_var
= nir_deref_var_create(mem_ctx
, var
);
400 uint32_t len
= blob_read_uint32(ctx
->blob
);
402 nir_deref
*tail
= &deref_var
->deref
;
403 for (uint32_t i
= 0; i
< len
; i
++) {
404 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
405 nir_deref
*deref
= NULL
;
406 switch (deref_type
) {
407 case nir_deref_type_array
: {
408 nir_deref_array
*deref_array
= nir_deref_array_create(tail
);
409 deref_array
->deref_array_type
= blob_read_uint32(ctx
->blob
);
410 deref_array
->base_offset
= blob_read_uint32(ctx
->blob
);
411 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
)
412 read_src(ctx
, &deref_array
->indirect
, mem_ctx
);
413 deref
= &deref_array
->deref
;
416 case nir_deref_type_struct
: {
417 uint32_t index
= blob_read_uint32(ctx
->blob
);
418 nir_deref_struct
*deref_struct
= nir_deref_struct_create(tail
, index
);
419 deref
= &deref_struct
->deref
;
422 case nir_deref_type_var
:
423 unreachable("Invalid deref type");
426 deref
->type
= decode_type_from_blob(ctx
->blob
);
436 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
438 blob_write_uint32(ctx
->blob
, alu
->op
);
439 uint32_t flags
= alu
->exact
;
440 flags
|= alu
->dest
.saturate
<< 1;
441 flags
|= alu
->dest
.write_mask
<< 2;
442 blob_write_uint32(ctx
->blob
, flags
);
444 write_dest(ctx
, &alu
->dest
.dest
);
446 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
447 write_src(ctx
, &alu
->src
[i
].src
);
448 flags
= alu
->src
[i
].negate
;
449 flags
|= alu
->src
[i
].abs
<< 1;
450 for (unsigned j
= 0; j
< 4; j
++)
451 flags
|= alu
->src
[i
].swizzle
[j
] << (2 + 2 * j
);
452 blob_write_uint32(ctx
->blob
, flags
);
456 static nir_alu_instr
*
457 read_alu(read_ctx
*ctx
)
459 nir_op op
= blob_read_uint32(ctx
->blob
);
460 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, op
);
462 uint32_t flags
= blob_read_uint32(ctx
->blob
);
463 alu
->exact
= flags
& 1;
464 alu
->dest
.saturate
= flags
& 2;
465 alu
->dest
.write_mask
= flags
>> 2;
467 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
);
469 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
470 read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
471 flags
= blob_read_uint32(ctx
->blob
);
472 alu
->src
[i
].negate
= flags
& 1;
473 alu
->src
[i
].abs
= flags
& 2;
474 for (unsigned j
= 0; j
< 4; j
++)
475 alu
->src
[i
].swizzle
[j
] = (flags
>> (2 * j
+ 2)) & 3;
482 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
484 blob_write_uint32(ctx
->blob
, intrin
->intrinsic
);
486 unsigned num_variables
= nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
;
487 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
488 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
490 blob_write_uint32(ctx
->blob
, intrin
->num_components
);
492 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
493 write_dest(ctx
, &intrin
->dest
);
495 for (unsigned i
= 0; i
< num_variables
; i
++)
496 write_deref_chain(ctx
, intrin
->variables
[i
]);
498 for (unsigned i
= 0; i
< num_srcs
; i
++)
499 write_src(ctx
, &intrin
->src
[i
]);
501 for (unsigned i
= 0; i
< num_indices
; i
++)
502 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
505 static nir_intrinsic_instr
*
506 read_intrinsic(read_ctx
*ctx
)
508 nir_intrinsic_op op
= blob_read_uint32(ctx
->blob
);
510 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
512 unsigned num_variables
= nir_intrinsic_infos
[op
].num_variables
;
513 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
514 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
516 intrin
->num_components
= blob_read_uint32(ctx
->blob
);
518 if (nir_intrinsic_infos
[op
].has_dest
)
519 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
);
521 for (unsigned i
= 0; i
< num_variables
; i
++)
522 intrin
->variables
[i
] = read_deref_chain(ctx
, &intrin
->instr
);
524 for (unsigned i
= 0; i
< num_srcs
; i
++)
525 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
527 for (unsigned i
= 0; i
< num_indices
; i
++)
528 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
534 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
536 uint32_t val
= lc
->def
.num_components
;
537 val
|= lc
->def
.bit_size
<< 3;
538 blob_write_uint32(ctx
->blob
, val
);
539 blob_write_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
540 write_add_object(ctx
, &lc
->def
);
543 static nir_load_const_instr
*
544 read_load_const(read_ctx
*ctx
)
546 uint32_t val
= blob_read_uint32(ctx
->blob
);
548 nir_load_const_instr
*lc
=
549 nir_load_const_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
551 blob_copy_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
552 read_add_object(ctx
, &lc
->def
);
557 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
559 uint32_t val
= undef
->def
.num_components
;
560 val
|= undef
->def
.bit_size
<< 3;
561 blob_write_uint32(ctx
->blob
, val
);
562 write_add_object(ctx
, &undef
->def
);
565 static nir_ssa_undef_instr
*
566 read_ssa_undef(read_ctx
*ctx
)
568 uint32_t val
= blob_read_uint32(ctx
->blob
);
570 nir_ssa_undef_instr
*undef
=
571 nir_ssa_undef_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
573 read_add_object(ctx
, &undef
->def
);
577 union packed_tex_data
{
580 enum glsl_sampler_dim sampler_dim
:4;
581 nir_alu_type dest_type
:8;
582 unsigned coord_components
:3;
584 unsigned is_shadow
:1;
585 unsigned is_new_style_shadow
:1;
586 unsigned component
:2;
587 unsigned has_texture_deref
:1;
588 unsigned has_sampler_deref
:1;
589 unsigned unused
:10; /* Mark unused for valgrind. */
594 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
596 blob_write_uint32(ctx
->blob
, tex
->num_srcs
);
597 blob_write_uint32(ctx
->blob
, tex
->op
);
598 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
599 blob_write_uint32(ctx
->blob
, tex
->texture_array_size
);
600 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
602 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
603 union packed_tex_data packed
= {
604 .u
.sampler_dim
= tex
->sampler_dim
,
605 .u
.dest_type
= tex
->dest_type
,
606 .u
.coord_components
= tex
->coord_components
,
607 .u
.is_array
= tex
->is_array
,
608 .u
.is_shadow
= tex
->is_shadow
,
609 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
610 .u
.component
= tex
->component
,
611 .u
.has_texture_deref
= tex
->texture
!= NULL
,
612 .u
.has_sampler_deref
= tex
->sampler
!= NULL
,
614 blob_write_uint32(ctx
->blob
, packed
.u32
);
616 write_dest(ctx
, &tex
->dest
);
617 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
618 blob_write_uint32(ctx
->blob
, tex
->src
[i
].src_type
);
619 write_src(ctx
, &tex
->src
[i
].src
);
623 write_deref_chain(ctx
, tex
->texture
);
625 write_deref_chain(ctx
, tex
->sampler
);
628 static nir_tex_instr
*
629 read_tex(read_ctx
*ctx
)
631 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
632 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, num_srcs
);
634 tex
->op
= blob_read_uint32(ctx
->blob
);
635 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
636 tex
->texture_array_size
= blob_read_uint32(ctx
->blob
);
637 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
639 union packed_tex_data packed
;
640 packed
.u32
= blob_read_uint32(ctx
->blob
);
641 tex
->sampler_dim
= packed
.u
.sampler_dim
;
642 tex
->dest_type
= packed
.u
.dest_type
;
643 tex
->coord_components
= packed
.u
.coord_components
;
644 tex
->is_array
= packed
.u
.is_array
;
645 tex
->is_shadow
= packed
.u
.is_shadow
;
646 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
647 tex
->component
= packed
.u
.component
;
649 read_dest(ctx
, &tex
->dest
, &tex
->instr
);
650 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
651 tex
->src
[i
].src_type
= blob_read_uint32(ctx
->blob
);
652 read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
655 tex
->texture
= packed
.u
.has_texture_deref
?
656 read_deref_chain(ctx
, &tex
->instr
) : NULL
;
657 tex
->sampler
= packed
.u
.has_sampler_deref
?
658 read_deref_chain(ctx
, &tex
->instr
) : NULL
;
664 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
666 /* Phi nodes are special, since they may reference SSA definitions and
667 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
668 * and then store enough information so that a later fixup pass can fill
671 write_dest(ctx
, &phi
->dest
);
673 blob_write_uint32(ctx
->blob
, exec_list_length(&phi
->srcs
));
675 nir_foreach_phi_src(src
, phi
) {
676 assert(src
->src
.is_ssa
);
677 size_t blob_offset
= blob_reserve_intptr(ctx
->blob
);
678 MAYBE_UNUSED
size_t blob_offset2
= blob_reserve_intptr(ctx
->blob
);
679 assert(blob_offset
+ sizeof(uintptr_t) == blob_offset2
);
680 write_phi_fixup fixup
= {
681 .blob_offset
= blob_offset
,
685 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
690 write_fixup_phis(write_ctx
*ctx
)
692 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
693 uintptr_t *blob_ptr
= (uintptr_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
694 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
695 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
698 util_dynarray_clear(&ctx
->phi_fixups
);
701 static nir_phi_instr
*
702 read_phi(read_ctx
*ctx
, nir_block
*blk
)
704 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
706 read_dest(ctx
, &phi
->dest
, &phi
->instr
);
708 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
710 /* For similar reasons as before, we just store the index directly into the
711 * pointer, and let a later pass resolve the phi sources.
713 * In order to ensure that the copied sources (which are just the indices
714 * from the blob for now) don't get inserted into the old shader's use-def
715 * lists, we have to add the phi instruction *before* we set up its
718 nir_instr_insert_after_block(blk
, &phi
->instr
);
720 for (unsigned i
= 0; i
< num_srcs
; i
++) {
721 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
723 src
->src
.is_ssa
= true;
724 src
->src
.ssa
= (nir_ssa_def
*) blob_read_intptr(ctx
->blob
);
725 src
->pred
= (nir_block
*) blob_read_intptr(ctx
->blob
);
727 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
728 * we have to set the parent_instr manually. It doesn't really matter
729 * when we do it, so we might as well do it here.
731 src
->src
.parent_instr
= &phi
->instr
;
733 /* Stash it in the list of phi sources. We'll walk this list and fix up
734 * sources at the very end of read_function_impl.
736 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
738 exec_list_push_tail(&phi
->srcs
, &src
->node
);
745 read_fixup_phis(read_ctx
*ctx
)
747 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
748 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
749 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
751 /* Remove from this list */
752 list_del(&src
->src
.use_link
);
754 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
756 assert(list_empty(&ctx
->phi_srcs
));
760 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
762 blob_write_uint32(ctx
->blob
, jmp
->type
);
765 static nir_jump_instr
*
766 read_jump(read_ctx
*ctx
)
768 nir_jump_type type
= blob_read_uint32(ctx
->blob
);
769 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, type
);
774 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
776 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
778 for (unsigned i
= 0; i
< call
->num_params
; i
++)
779 write_deref_chain(ctx
, call
->params
[i
]);
781 write_deref_chain(ctx
, call
->return_deref
);
784 static nir_call_instr
*
785 read_call(read_ctx
*ctx
)
787 nir_function
*callee
= read_object(ctx
);
788 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
790 for (unsigned i
= 0; i
< call
->num_params
; i
++)
791 call
->params
[i
] = read_deref_chain(ctx
, &call
->instr
);
793 call
->return_deref
= read_deref_chain(ctx
, &call
->instr
);
799 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
801 blob_write_uint32(ctx
->blob
, instr
->type
);
802 switch (instr
->type
) {
803 case nir_instr_type_alu
:
804 write_alu(ctx
, nir_instr_as_alu(instr
));
806 case nir_instr_type_intrinsic
:
807 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
809 case nir_instr_type_load_const
:
810 write_load_const(ctx
, nir_instr_as_load_const(instr
));
812 case nir_instr_type_ssa_undef
:
813 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
815 case nir_instr_type_tex
:
816 write_tex(ctx
, nir_instr_as_tex(instr
));
818 case nir_instr_type_phi
:
819 write_phi(ctx
, nir_instr_as_phi(instr
));
821 case nir_instr_type_jump
:
822 write_jump(ctx
, nir_instr_as_jump(instr
));
824 case nir_instr_type_call
:
825 write_call(ctx
, nir_instr_as_call(instr
));
827 case nir_instr_type_parallel_copy
:
828 unreachable("Cannot write parallel copies");
830 unreachable("bad instr type");
835 read_instr(read_ctx
*ctx
, nir_block
*block
)
837 nir_instr_type type
= blob_read_uint32(ctx
->blob
);
840 case nir_instr_type_alu
:
841 instr
= &read_alu(ctx
)->instr
;
843 case nir_instr_type_intrinsic
:
844 instr
= &read_intrinsic(ctx
)->instr
;
846 case nir_instr_type_load_const
:
847 instr
= &read_load_const(ctx
)->instr
;
849 case nir_instr_type_ssa_undef
:
850 instr
= &read_ssa_undef(ctx
)->instr
;
852 case nir_instr_type_tex
:
853 instr
= &read_tex(ctx
)->instr
;
855 case nir_instr_type_phi
:
856 /* Phi instructions are a bit of a special case when reading because we
857 * don't want inserting the instruction to automatically handle use/defs
858 * for us. Instead, we need to wait until all the blocks/instructions
859 * are read so that we can set their sources up.
861 read_phi(ctx
, block
);
863 case nir_instr_type_jump
:
864 instr
= &read_jump(ctx
)->instr
;
866 case nir_instr_type_call
:
867 instr
= &read_call(ctx
)->instr
;
869 case nir_instr_type_parallel_copy
:
870 unreachable("Cannot read parallel copies");
872 unreachable("bad instr type");
875 nir_instr_insert_after_block(block
, instr
);
879 write_block(write_ctx
*ctx
, const nir_block
*block
)
881 write_add_object(ctx
, block
);
882 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
883 nir_foreach_instr(instr
, block
)
884 write_instr(ctx
, instr
);
888 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
890 /* Don't actually create a new block. Just use the one from the tail of
891 * the list. NIR guarantees that the tail of the list is a block and that
892 * no two blocks are side-by-side in the IR; It should be empty.
895 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
897 read_add_object(ctx
, block
);
898 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
899 for (unsigned i
= 0; i
< num_instrs
; i
++) {
900 read_instr(ctx
, block
);
905 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
908 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
911 write_if(write_ctx
*ctx
, nir_if
*nif
)
913 write_src(ctx
, &nif
->condition
);
915 write_cf_list(ctx
, &nif
->then_list
);
916 write_cf_list(ctx
, &nif
->else_list
);
920 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
922 nir_if
*nif
= nir_if_create(ctx
->nir
);
924 read_src(ctx
, &nif
->condition
, nif
);
926 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
928 read_cf_list(ctx
, &nif
->then_list
);
929 read_cf_list(ctx
, &nif
->else_list
);
933 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
935 write_cf_list(ctx
, &loop
->body
);
939 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
941 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
943 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
945 read_cf_list(ctx
, &loop
->body
);
949 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
951 blob_write_uint32(ctx
->blob
, cf
->type
);
954 case nir_cf_node_block
:
955 write_block(ctx
, nir_cf_node_as_block(cf
));
958 write_if(ctx
, nir_cf_node_as_if(cf
));
960 case nir_cf_node_loop
:
961 write_loop(ctx
, nir_cf_node_as_loop(cf
));
964 unreachable("bad cf type");
969 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
971 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
974 case nir_cf_node_block
:
975 read_block(ctx
, list
);
980 case nir_cf_node_loop
:
981 read_loop(ctx
, list
);
984 unreachable("bad cf type");
989 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
991 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
992 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
993 write_cf_node(ctx
, cf
);
998 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1000 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1001 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1002 read_cf_node(ctx
, cf_list
);
1006 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1008 write_var_list(ctx
, &fi
->locals
);
1009 write_reg_list(ctx
, &fi
->registers
);
1010 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1012 blob_write_uint32(ctx
->blob
, fi
->num_params
);
1013 for (unsigned i
= 0; i
< fi
->num_params
; i
++) {
1014 write_variable(ctx
, fi
->params
[i
]);
1017 blob_write_uint32(ctx
->blob
, !!(fi
->return_var
));
1019 write_variable(ctx
, fi
->return_var
);
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 fi
->num_params
= blob_read_uint32(ctx
->blob
);
1036 for (unsigned i
= 0; i
< fi
->num_params
; i
++) {
1037 fi
->params
[i
] = read_variable(ctx
);
1040 bool has_return
= blob_read_uint32(ctx
->blob
);
1042 fi
->return_var
= read_variable(ctx
);
1044 fi
->return_var
= NULL
;
1046 read_cf_list(ctx
, &fi
->body
);
1047 read_fixup_phis(ctx
);
1049 fi
->valid_metadata
= 0;
1055 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1057 blob_write_uint32(ctx
->blob
, !!(fxn
->name
));
1059 blob_write_string(ctx
->blob
, fxn
->name
);
1061 write_add_object(ctx
, fxn
);
1063 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1064 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1065 blob_write_uint32(ctx
->blob
, fxn
->params
[i
].param_type
);
1066 encode_type_to_blob(ctx
->blob
, fxn
->params
[i
].type
);
1069 encode_type_to_blob(ctx
->blob
, fxn
->return_type
);
1071 /* At first glance, it looks like we should write the function_impl here.
1072 * However, call instructions need to be able to reference at least the
1073 * function and those will get processed as we write the function_impls.
1074 * We stop here and write function_impls as a second pass.
1079 read_function(read_ctx
*ctx
)
1081 bool has_name
= blob_read_uint32(ctx
->blob
);
1082 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1084 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1086 read_add_object(ctx
, fxn
);
1088 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1089 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1090 fxn
->params
[i
].param_type
= blob_read_uint32(ctx
->blob
);
1091 fxn
->params
[i
].type
= decode_type_from_blob(ctx
->blob
);
1094 fxn
->return_type
= decode_type_from_blob(ctx
->blob
);
1098 nir_serialize(struct blob
*blob
, const nir_shader
*nir
)
1101 ctx
.remap_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1102 _mesa_key_pointer_equal
);
1106 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1108 size_t idx_size_offset
= blob_reserve_intptr(blob
);
1110 struct shader_info info
= nir
->info
;
1111 uint32_t strings
= 0;
1116 blob_write_uint32(blob
, strings
);
1118 blob_write_string(blob
, info
.name
);
1120 blob_write_string(blob
, info
.label
);
1121 info
.name
= info
.label
= NULL
;
1122 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1124 write_var_list(&ctx
, &nir
->uniforms
);
1125 write_var_list(&ctx
, &nir
->inputs
);
1126 write_var_list(&ctx
, &nir
->outputs
);
1127 write_var_list(&ctx
, &nir
->shared
);
1128 write_var_list(&ctx
, &nir
->globals
);
1129 write_var_list(&ctx
, &nir
->system_values
);
1131 write_reg_list(&ctx
, &nir
->registers
);
1132 blob_write_uint32(blob
, nir
->reg_alloc
);
1133 blob_write_uint32(blob
, nir
->num_inputs
);
1134 blob_write_uint32(blob
, nir
->num_uniforms
);
1135 blob_write_uint32(blob
, nir
->num_outputs
);
1136 blob_write_uint32(blob
, nir
->num_shared
);
1138 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1139 nir_foreach_function(fxn
, nir
) {
1140 write_function(&ctx
, fxn
);
1143 nir_foreach_function(fxn
, nir
) {
1144 write_function_impl(&ctx
, fxn
->impl
);
1147 *(uintptr_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1149 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1150 util_dynarray_fini(&ctx
.phi_fixups
);
1154 nir_deserialize(void *mem_ctx
,
1155 const struct nir_shader_compiler_options
*options
,
1156 struct blob_reader
*blob
)
1160 list_inithead(&ctx
.phi_srcs
);
1161 ctx
.idx_table_len
= blob_read_intptr(blob
);
1162 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1165 uint32_t strings
= blob_read_uint32(blob
);
1166 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1167 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1169 struct shader_info info
;
1170 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1172 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1174 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1175 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1177 ctx
.nir
->info
= info
;
1179 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1180 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1181 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1182 read_var_list(&ctx
, &ctx
.nir
->shared
);
1183 read_var_list(&ctx
, &ctx
.nir
->globals
);
1184 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1186 read_reg_list(&ctx
, &ctx
.nir
->registers
);
1187 ctx
.nir
->reg_alloc
= blob_read_uint32(blob
);
1188 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1189 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1190 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1191 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1193 unsigned num_functions
= blob_read_uint32(blob
);
1194 for (unsigned i
= 0; i
< num_functions
; i
++)
1195 read_function(&ctx
);
1197 nir_foreach_function(fxn
, ctx
.nir
)
1198 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1200 free(ctx
.idx_table
);
1206 nir_shader_serialize_deserialize(void *mem_ctx
, nir_shader
*s
)
1208 const struct nir_shader_compiler_options
*options
= s
->options
;
1212 nir_serialize(&writer
, s
);
1215 struct blob_reader reader
;
1216 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1217 nir_shader
*ns
= nir_deserialize(mem_ctx
, options
, &reader
);
1219 blob_finish(&writer
);