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 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
);
152 blob_write_uint32(ctx
->blob
, var
->num_members
);
153 if (var
->num_members
> 0) {
154 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->members
,
155 var
->num_members
* sizeof(*var
->members
));
159 static nir_variable
*
160 read_variable(read_ctx
*ctx
)
162 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
163 read_add_object(ctx
, var
);
165 var
->type
= decode_type_from_blob(ctx
->blob
);
166 bool has_name
= blob_read_uint32(ctx
->blob
);
168 const char *name
= blob_read_string(ctx
->blob
);
169 var
->name
= ralloc_strdup(var
, name
);
173 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
174 var
->num_state_slots
= blob_read_uint32(ctx
->blob
);
175 var
->state_slots
= ralloc_array(var
, nir_state_slot
, var
->num_state_slots
);
176 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->state_slots
,
177 var
->num_state_slots
* sizeof(nir_state_slot
));
178 bool has_const_initializer
= blob_read_uint32(ctx
->blob
);
179 if (has_const_initializer
)
180 var
->constant_initializer
= read_constant(ctx
, var
);
182 var
->constant_initializer
= NULL
;
183 bool has_interface_type
= blob_read_uint32(ctx
->blob
);
184 if (has_interface_type
)
185 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
187 var
->interface_type
= NULL
;
188 var
->num_members
= blob_read_uint32(ctx
->blob
);
189 if (var
->num_members
> 0) {
190 var
->members
= ralloc_array(var
, struct nir_variable_data
,
192 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->members
,
193 var
->num_members
* sizeof(*var
->members
));
200 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
202 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
203 foreach_list_typed(nir_variable
, var
, node
, src
) {
204 write_variable(ctx
, var
);
209 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
211 exec_list_make_empty(dst
);
212 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
213 for (unsigned i
= 0; i
< num_vars
; i
++) {
214 nir_variable
*var
= read_variable(ctx
);
215 exec_list_push_tail(dst
, &var
->node
);
220 write_register(write_ctx
*ctx
, const nir_register
*reg
)
222 write_add_object(ctx
, reg
);
223 blob_write_uint32(ctx
->blob
, reg
->num_components
);
224 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
225 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
226 blob_write_uint32(ctx
->blob
, reg
->index
);
227 blob_write_uint32(ctx
->blob
, !!(reg
->name
));
229 blob_write_string(ctx
->blob
, reg
->name
);
230 blob_write_uint32(ctx
->blob
, reg
->is_global
<< 1 | reg
->is_packed
);
233 static nir_register
*
234 read_register(read_ctx
*ctx
)
236 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
237 read_add_object(ctx
, reg
);
238 reg
->num_components
= blob_read_uint32(ctx
->blob
);
239 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
240 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
241 reg
->index
= blob_read_uint32(ctx
->blob
);
242 bool has_name
= blob_read_uint32(ctx
->blob
);
244 const char *name
= blob_read_string(ctx
->blob
);
245 reg
->name
= ralloc_strdup(reg
, name
);
249 unsigned flags
= blob_read_uint32(ctx
->blob
);
250 reg
->is_global
= flags
& 0x2;
251 reg
->is_packed
= flags
& 0x1;
253 list_inithead(®
->uses
);
254 list_inithead(®
->defs
);
255 list_inithead(®
->if_uses
);
261 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
263 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
264 foreach_list_typed(nir_register
, reg
, node
, src
)
265 write_register(ctx
, reg
);
269 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
271 exec_list_make_empty(dst
);
272 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
273 for (unsigned i
= 0; i
< num_regs
; i
++) {
274 nir_register
*reg
= read_register(ctx
);
275 exec_list_push_tail(dst
, ®
->node
);
280 write_src(write_ctx
*ctx
, const nir_src
*src
)
282 /* Since sources are very frequent, we try to save some space when storing
283 * them. In particular, we store whether the source is a register and
284 * whether the register has an indirect index in the low two bits. We can
285 * assume that the high two bits of the index are zero, since otherwise our
286 * address space would've been exhausted allocating the remap table!
289 uintptr_t idx
= write_lookup_object(ctx
, src
->ssa
) << 2;
291 blob_write_intptr(ctx
->blob
, idx
);
293 uintptr_t idx
= write_lookup_object(ctx
, src
->reg
.reg
) << 2;
294 if (src
->reg
.indirect
)
296 blob_write_intptr(ctx
->blob
, idx
);
297 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
298 if (src
->reg
.indirect
) {
299 write_src(ctx
, src
->reg
.indirect
);
305 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
307 uintptr_t val
= blob_read_intptr(ctx
->blob
);
308 uintptr_t idx
= val
>> 2;
309 src
->is_ssa
= val
& 0x1;
311 src
->ssa
= read_lookup_object(ctx
, idx
);
313 bool is_indirect
= val
& 0x2;
314 src
->reg
.reg
= read_lookup_object(ctx
, idx
);
315 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
317 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
318 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
320 src
->reg
.indirect
= NULL
;
326 write_dest(write_ctx
*ctx
, const nir_dest
*dst
)
328 uint32_t val
= dst
->is_ssa
;
330 val
|= !!(dst
->ssa
.name
) << 1;
331 val
|= dst
->ssa
.num_components
<< 2;
332 val
|= dst
->ssa
.bit_size
<< 5;
334 val
|= !!(dst
->reg
.indirect
) << 1;
336 blob_write_uint32(ctx
->blob
, val
);
338 write_add_object(ctx
, &dst
->ssa
);
340 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
342 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
343 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
344 if (dst
->reg
.indirect
)
345 write_src(ctx
, dst
->reg
.indirect
);
350 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
)
352 uint32_t val
= blob_read_uint32(ctx
->blob
);
353 bool is_ssa
= val
& 0x1;
355 bool has_name
= val
& 0x2;
356 unsigned num_components
= (val
>> 2) & 0x7;
357 unsigned bit_size
= val
>> 5;
358 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
359 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
360 read_add_object(ctx
, &dst
->ssa
);
362 bool is_indirect
= val
& 0x2;
363 dst
->reg
.reg
= read_object(ctx
);
364 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
366 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
367 read_src(ctx
, dst
->reg
.indirect
, instr
);
373 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
375 blob_write_uint32(ctx
->blob
, alu
->op
);
376 uint32_t flags
= alu
->exact
;
377 flags
|= alu
->dest
.saturate
<< 1;
378 flags
|= alu
->dest
.write_mask
<< 2;
379 blob_write_uint32(ctx
->blob
, flags
);
381 write_dest(ctx
, &alu
->dest
.dest
);
383 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
384 write_src(ctx
, &alu
->src
[i
].src
);
385 flags
= alu
->src
[i
].negate
;
386 flags
|= alu
->src
[i
].abs
<< 1;
387 for (unsigned j
= 0; j
< 4; j
++)
388 flags
|= alu
->src
[i
].swizzle
[j
] << (2 + 2 * j
);
389 blob_write_uint32(ctx
->blob
, flags
);
393 static nir_alu_instr
*
394 read_alu(read_ctx
*ctx
)
396 nir_op op
= blob_read_uint32(ctx
->blob
);
397 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, op
);
399 uint32_t flags
= blob_read_uint32(ctx
->blob
);
400 alu
->exact
= flags
& 1;
401 alu
->dest
.saturate
= flags
& 2;
402 alu
->dest
.write_mask
= flags
>> 2;
404 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
);
406 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
407 read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
408 flags
= blob_read_uint32(ctx
->blob
);
409 alu
->src
[i
].negate
= flags
& 1;
410 alu
->src
[i
].abs
= flags
& 2;
411 for (unsigned j
= 0; j
< 4; j
++)
412 alu
->src
[i
].swizzle
[j
] = (flags
>> (2 * j
+ 2)) & 3;
419 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
421 blob_write_uint32(ctx
->blob
, deref
->deref_type
);
423 blob_write_uint32(ctx
->blob
, deref
->mode
);
424 encode_type_to_blob(ctx
->blob
, deref
->type
);
426 write_dest(ctx
, &deref
->dest
);
428 if (deref
->deref_type
== nir_deref_type_var
) {
429 write_object(ctx
, deref
->var
);
433 write_src(ctx
, &deref
->parent
);
435 switch (deref
->deref_type
) {
436 case nir_deref_type_struct
:
437 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
440 case nir_deref_type_array
:
441 write_src(ctx
, &deref
->arr
.index
);
444 case nir_deref_type_array_wildcard
:
445 case nir_deref_type_cast
:
450 unreachable("Invalid deref type");
454 static nir_deref_instr
*
455 read_deref(read_ctx
*ctx
)
457 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
458 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
460 deref
->mode
= blob_read_uint32(ctx
->blob
);
461 deref
->type
= decode_type_from_blob(ctx
->blob
);
463 read_dest(ctx
, &deref
->dest
, &deref
->instr
);
465 if (deref_type
== nir_deref_type_var
) {
466 deref
->var
= read_object(ctx
);
470 read_src(ctx
, &deref
->parent
, &deref
->instr
);
472 switch (deref
->deref_type
) {
473 case nir_deref_type_struct
:
474 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
477 case nir_deref_type_array
:
478 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
481 case nir_deref_type_array_wildcard
:
482 case nir_deref_type_cast
:
487 unreachable("Invalid deref type");
494 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
496 blob_write_uint32(ctx
->blob
, intrin
->intrinsic
);
498 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
499 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
501 blob_write_uint32(ctx
->blob
, intrin
->num_components
);
503 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
504 write_dest(ctx
, &intrin
->dest
);
506 for (unsigned i
= 0; i
< num_srcs
; i
++)
507 write_src(ctx
, &intrin
->src
[i
]);
509 for (unsigned i
= 0; i
< num_indices
; i
++)
510 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
513 static nir_intrinsic_instr
*
514 read_intrinsic(read_ctx
*ctx
)
516 nir_intrinsic_op op
= blob_read_uint32(ctx
->blob
);
518 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
520 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
521 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
523 intrin
->num_components
= blob_read_uint32(ctx
->blob
);
525 if (nir_intrinsic_infos
[op
].has_dest
)
526 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
);
528 for (unsigned i
= 0; i
< num_srcs
; i
++)
529 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
531 for (unsigned i
= 0; i
< num_indices
; i
++)
532 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
538 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
540 uint32_t val
= lc
->def
.num_components
;
541 val
|= lc
->def
.bit_size
<< 3;
542 blob_write_uint32(ctx
->blob
, val
);
543 blob_write_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
544 write_add_object(ctx
, &lc
->def
);
547 static nir_load_const_instr
*
548 read_load_const(read_ctx
*ctx
)
550 uint32_t val
= blob_read_uint32(ctx
->blob
);
552 nir_load_const_instr
*lc
=
553 nir_load_const_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
555 blob_copy_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
556 read_add_object(ctx
, &lc
->def
);
561 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
563 uint32_t val
= undef
->def
.num_components
;
564 val
|= undef
->def
.bit_size
<< 3;
565 blob_write_uint32(ctx
->blob
, val
);
566 write_add_object(ctx
, &undef
->def
);
569 static nir_ssa_undef_instr
*
570 read_ssa_undef(read_ctx
*ctx
)
572 uint32_t val
= blob_read_uint32(ctx
->blob
);
574 nir_ssa_undef_instr
*undef
=
575 nir_ssa_undef_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
577 read_add_object(ctx
, &undef
->def
);
581 union packed_tex_data
{
584 enum glsl_sampler_dim sampler_dim
:4;
585 nir_alu_type dest_type
:8;
586 unsigned coord_components
:3;
588 unsigned is_shadow
:1;
589 unsigned is_new_style_shadow
:1;
590 unsigned component
:2;
591 unsigned unused
:10; /* Mark unused for valgrind. */
596 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
598 blob_write_uint32(ctx
->blob
, tex
->num_srcs
);
599 blob_write_uint32(ctx
->blob
, tex
->op
);
600 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
601 blob_write_uint32(ctx
->blob
, tex
->texture_array_size
);
602 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
604 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
605 union packed_tex_data packed
= {
606 .u
.sampler_dim
= tex
->sampler_dim
,
607 .u
.dest_type
= tex
->dest_type
,
608 .u
.coord_components
= tex
->coord_components
,
609 .u
.is_array
= tex
->is_array
,
610 .u
.is_shadow
= tex
->is_shadow
,
611 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
612 .u
.component
= tex
->component
,
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 static nir_tex_instr
*
624 read_tex(read_ctx
*ctx
)
626 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
627 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, num_srcs
);
629 tex
->op
= blob_read_uint32(ctx
->blob
);
630 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
631 tex
->texture_array_size
= blob_read_uint32(ctx
->blob
);
632 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
634 union packed_tex_data packed
;
635 packed
.u32
= blob_read_uint32(ctx
->blob
);
636 tex
->sampler_dim
= packed
.u
.sampler_dim
;
637 tex
->dest_type
= packed
.u
.dest_type
;
638 tex
->coord_components
= packed
.u
.coord_components
;
639 tex
->is_array
= packed
.u
.is_array
;
640 tex
->is_shadow
= packed
.u
.is_shadow
;
641 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
642 tex
->component
= packed
.u
.component
;
644 read_dest(ctx
, &tex
->dest
, &tex
->instr
);
645 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
646 tex
->src
[i
].src_type
= blob_read_uint32(ctx
->blob
);
647 read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
654 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
656 /* Phi nodes are special, since they may reference SSA definitions and
657 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
658 * and then store enough information so that a later fixup pass can fill
661 write_dest(ctx
, &phi
->dest
);
663 blob_write_uint32(ctx
->blob
, exec_list_length(&phi
->srcs
));
665 nir_foreach_phi_src(src
, phi
) {
666 assert(src
->src
.is_ssa
);
667 size_t blob_offset
= blob_reserve_intptr(ctx
->blob
);
668 MAYBE_UNUSED
size_t blob_offset2
= blob_reserve_intptr(ctx
->blob
);
669 assert(blob_offset
+ sizeof(uintptr_t) == blob_offset2
);
670 write_phi_fixup fixup
= {
671 .blob_offset
= blob_offset
,
675 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
680 write_fixup_phis(write_ctx
*ctx
)
682 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
683 uintptr_t *blob_ptr
= (uintptr_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
684 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
685 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
688 util_dynarray_clear(&ctx
->phi_fixups
);
691 static nir_phi_instr
*
692 read_phi(read_ctx
*ctx
, nir_block
*blk
)
694 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
696 read_dest(ctx
, &phi
->dest
, &phi
->instr
);
698 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
700 /* For similar reasons as before, we just store the index directly into the
701 * pointer, and let a later pass resolve the phi sources.
703 * In order to ensure that the copied sources (which are just the indices
704 * from the blob for now) don't get inserted into the old shader's use-def
705 * lists, we have to add the phi instruction *before* we set up its
708 nir_instr_insert_after_block(blk
, &phi
->instr
);
710 for (unsigned i
= 0; i
< num_srcs
; i
++) {
711 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
713 src
->src
.is_ssa
= true;
714 src
->src
.ssa
= (nir_ssa_def
*) blob_read_intptr(ctx
->blob
);
715 src
->pred
= (nir_block
*) blob_read_intptr(ctx
->blob
);
717 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
718 * we have to set the parent_instr manually. It doesn't really matter
719 * when we do it, so we might as well do it here.
721 src
->src
.parent_instr
= &phi
->instr
;
723 /* Stash it in the list of phi sources. We'll walk this list and fix up
724 * sources at the very end of read_function_impl.
726 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
728 exec_list_push_tail(&phi
->srcs
, &src
->node
);
735 read_fixup_phis(read_ctx
*ctx
)
737 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
738 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
739 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
741 /* Remove from this list */
742 list_del(&src
->src
.use_link
);
744 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
746 assert(list_empty(&ctx
->phi_srcs
));
750 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
752 blob_write_uint32(ctx
->blob
, jmp
->type
);
755 static nir_jump_instr
*
756 read_jump(read_ctx
*ctx
)
758 nir_jump_type type
= blob_read_uint32(ctx
->blob
);
759 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, type
);
764 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
766 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
768 for (unsigned i
= 0; i
< call
->num_params
; i
++)
769 write_src(ctx
, &call
->params
[i
]);
772 static nir_call_instr
*
773 read_call(read_ctx
*ctx
)
775 nir_function
*callee
= read_object(ctx
);
776 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
778 for (unsigned i
= 0; i
< call
->num_params
; i
++)
779 read_src(ctx
, &call
->params
[i
], call
);
785 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
787 blob_write_uint32(ctx
->blob
, instr
->type
);
788 switch (instr
->type
) {
789 case nir_instr_type_alu
:
790 write_alu(ctx
, nir_instr_as_alu(instr
));
792 case nir_instr_type_deref
:
793 write_deref(ctx
, nir_instr_as_deref(instr
));
795 case nir_instr_type_intrinsic
:
796 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
798 case nir_instr_type_load_const
:
799 write_load_const(ctx
, nir_instr_as_load_const(instr
));
801 case nir_instr_type_ssa_undef
:
802 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
804 case nir_instr_type_tex
:
805 write_tex(ctx
, nir_instr_as_tex(instr
));
807 case nir_instr_type_phi
:
808 write_phi(ctx
, nir_instr_as_phi(instr
));
810 case nir_instr_type_jump
:
811 write_jump(ctx
, nir_instr_as_jump(instr
));
813 case nir_instr_type_call
:
814 write_call(ctx
, nir_instr_as_call(instr
));
816 case nir_instr_type_parallel_copy
:
817 unreachable("Cannot write parallel copies");
819 unreachable("bad instr type");
824 read_instr(read_ctx
*ctx
, nir_block
*block
)
826 nir_instr_type type
= blob_read_uint32(ctx
->blob
);
829 case nir_instr_type_alu
:
830 instr
= &read_alu(ctx
)->instr
;
832 case nir_instr_type_deref
:
833 instr
= &read_deref(ctx
)->instr
;
835 case nir_instr_type_intrinsic
:
836 instr
= &read_intrinsic(ctx
)->instr
;
838 case nir_instr_type_load_const
:
839 instr
= &read_load_const(ctx
)->instr
;
841 case nir_instr_type_ssa_undef
:
842 instr
= &read_ssa_undef(ctx
)->instr
;
844 case nir_instr_type_tex
:
845 instr
= &read_tex(ctx
)->instr
;
847 case nir_instr_type_phi
:
848 /* Phi instructions are a bit of a special case when reading because we
849 * don't want inserting the instruction to automatically handle use/defs
850 * for us. Instead, we need to wait until all the blocks/instructions
851 * are read so that we can set their sources up.
853 read_phi(ctx
, block
);
855 case nir_instr_type_jump
:
856 instr
= &read_jump(ctx
)->instr
;
858 case nir_instr_type_call
:
859 instr
= &read_call(ctx
)->instr
;
861 case nir_instr_type_parallel_copy
:
862 unreachable("Cannot read parallel copies");
864 unreachable("bad instr type");
867 nir_instr_insert_after_block(block
, instr
);
871 write_block(write_ctx
*ctx
, const nir_block
*block
)
873 write_add_object(ctx
, block
);
874 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
875 nir_foreach_instr(instr
, block
)
876 write_instr(ctx
, instr
);
880 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
882 /* Don't actually create a new block. Just use the one from the tail of
883 * the list. NIR guarantees that the tail of the list is a block and that
884 * no two blocks are side-by-side in the IR; It should be empty.
887 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
889 read_add_object(ctx
, block
);
890 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
891 for (unsigned i
= 0; i
< num_instrs
; i
++) {
892 read_instr(ctx
, block
);
897 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
900 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
903 write_if(write_ctx
*ctx
, nir_if
*nif
)
905 write_src(ctx
, &nif
->condition
);
907 write_cf_list(ctx
, &nif
->then_list
);
908 write_cf_list(ctx
, &nif
->else_list
);
912 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
914 nir_if
*nif
= nir_if_create(ctx
->nir
);
916 read_src(ctx
, &nif
->condition
, nif
);
918 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
920 read_cf_list(ctx
, &nif
->then_list
);
921 read_cf_list(ctx
, &nif
->else_list
);
925 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
927 write_cf_list(ctx
, &loop
->body
);
931 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
933 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
935 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
937 read_cf_list(ctx
, &loop
->body
);
941 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
943 blob_write_uint32(ctx
->blob
, cf
->type
);
946 case nir_cf_node_block
:
947 write_block(ctx
, nir_cf_node_as_block(cf
));
950 write_if(ctx
, nir_cf_node_as_if(cf
));
952 case nir_cf_node_loop
:
953 write_loop(ctx
, nir_cf_node_as_loop(cf
));
956 unreachable("bad cf type");
961 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
963 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
966 case nir_cf_node_block
:
967 read_block(ctx
, list
);
972 case nir_cf_node_loop
:
973 read_loop(ctx
, list
);
976 unreachable("bad cf type");
981 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
983 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
984 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
985 write_cf_node(ctx
, cf
);
990 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
992 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
993 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
994 read_cf_node(ctx
, cf_list
);
998 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1000 write_var_list(ctx
, &fi
->locals
);
1001 write_reg_list(ctx
, &fi
->registers
);
1002 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1004 write_cf_list(ctx
, &fi
->body
);
1005 write_fixup_phis(ctx
);
1008 static nir_function_impl
*
1009 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1011 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1014 read_var_list(ctx
, &fi
->locals
);
1015 read_reg_list(ctx
, &fi
->registers
);
1016 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1018 read_cf_list(ctx
, &fi
->body
);
1019 read_fixup_phis(ctx
);
1021 fi
->valid_metadata
= 0;
1027 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1029 blob_write_uint32(ctx
->blob
, !!(fxn
->name
));
1031 blob_write_string(ctx
->blob
, fxn
->name
);
1033 write_add_object(ctx
, fxn
);
1035 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1036 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1038 ((uint32_t)fxn
->params
[i
].num_components
) |
1039 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1040 blob_write_uint32(ctx
->blob
, val
);
1043 /* At first glance, it looks like we should write the function_impl here.
1044 * However, call instructions need to be able to reference at least the
1045 * function and those will get processed as we write the function_impls.
1046 * We stop here and write function_impls as a second pass.
1051 read_function(read_ctx
*ctx
)
1053 bool has_name
= blob_read_uint32(ctx
->blob
);
1054 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1056 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1058 read_add_object(ctx
, fxn
);
1060 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1061 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1062 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1063 uint32_t val
= blob_read_uint32(ctx
->blob
);
1064 fxn
->params
[i
].num_components
= val
& 0xff;
1065 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1070 nir_serialize(struct blob
*blob
, const nir_shader
*nir
)
1073 ctx
.remap_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1074 _mesa_key_pointer_equal
);
1078 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1080 size_t idx_size_offset
= blob_reserve_intptr(blob
);
1082 struct shader_info info
= nir
->info
;
1083 uint32_t strings
= 0;
1088 blob_write_uint32(blob
, strings
);
1090 blob_write_string(blob
, info
.name
);
1092 blob_write_string(blob
, info
.label
);
1093 info
.name
= info
.label
= NULL
;
1094 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1096 write_var_list(&ctx
, &nir
->uniforms
);
1097 write_var_list(&ctx
, &nir
->inputs
);
1098 write_var_list(&ctx
, &nir
->outputs
);
1099 write_var_list(&ctx
, &nir
->shared
);
1100 write_var_list(&ctx
, &nir
->globals
);
1101 write_var_list(&ctx
, &nir
->system_values
);
1103 write_reg_list(&ctx
, &nir
->registers
);
1104 blob_write_uint32(blob
, nir
->reg_alloc
);
1105 blob_write_uint32(blob
, nir
->num_inputs
);
1106 blob_write_uint32(blob
, nir
->num_uniforms
);
1107 blob_write_uint32(blob
, nir
->num_outputs
);
1108 blob_write_uint32(blob
, nir
->num_shared
);
1110 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1111 nir_foreach_function(fxn
, nir
) {
1112 write_function(&ctx
, fxn
);
1115 nir_foreach_function(fxn
, nir
) {
1116 write_function_impl(&ctx
, fxn
->impl
);
1119 blob_write_uint32(blob
, nir
->constant_data_size
);
1120 if (nir
->constant_data_size
> 0)
1121 blob_write_bytes(blob
, nir
->constant_data
, nir
->constant_data_size
);
1123 *(uintptr_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1125 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1126 util_dynarray_fini(&ctx
.phi_fixups
);
1130 nir_deserialize(void *mem_ctx
,
1131 const struct nir_shader_compiler_options
*options
,
1132 struct blob_reader
*blob
)
1136 list_inithead(&ctx
.phi_srcs
);
1137 ctx
.idx_table_len
= blob_read_intptr(blob
);
1138 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1141 uint32_t strings
= blob_read_uint32(blob
);
1142 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1143 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1145 struct shader_info info
;
1146 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1148 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1150 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1151 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1153 ctx
.nir
->info
= info
;
1155 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1156 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1157 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1158 read_var_list(&ctx
, &ctx
.nir
->shared
);
1159 read_var_list(&ctx
, &ctx
.nir
->globals
);
1160 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1162 read_reg_list(&ctx
, &ctx
.nir
->registers
);
1163 ctx
.nir
->reg_alloc
= blob_read_uint32(blob
);
1164 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1165 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1166 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1167 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1169 unsigned num_functions
= blob_read_uint32(blob
);
1170 for (unsigned i
= 0; i
< num_functions
; i
++)
1171 read_function(&ctx
);
1173 nir_foreach_function(fxn
, ctx
.nir
)
1174 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1176 ctx
.nir
->constant_data_size
= blob_read_uint32(blob
);
1177 if (ctx
.nir
->constant_data_size
> 0) {
1178 ctx
.nir
->constant_data
=
1179 ralloc_size(ctx
.nir
, ctx
.nir
->constant_data_size
);
1180 blob_copy_bytes(blob
, ctx
.nir
->constant_data
,
1181 ctx
.nir
->constant_data_size
);
1184 free(ctx
.idx_table
);
1190 nir_shader_serialize_deserialize(void *mem_ctx
, nir_shader
*s
)
1192 const struct nir_shader_compiler_options
*options
= s
->options
;
1196 nir_serialize(&writer
, s
);
1199 struct blob_reader reader
;
1200 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1201 nir_shader
*ns
= nir_deserialize(mem_ctx
, options
, &reader
);
1203 blob_finish(&writer
);