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
);
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_deref_chain(write_ctx
*ctx
, const nir_deref_var
*deref_var
)
375 write_object(ctx
, deref_var
->var
);
378 for (const nir_deref
*d
= deref_var
->deref
.child
; d
; d
= d
->child
)
380 blob_write_uint32(ctx
->blob
, len
);
382 for (const nir_deref
*d
= deref_var
->deref
.child
; d
; d
= d
->child
) {
383 blob_write_uint32(ctx
->blob
, d
->deref_type
);
384 switch (d
->deref_type
) {
385 case nir_deref_type_array
: {
386 const nir_deref_array
*deref_array
= nir_deref_as_array(d
);
387 blob_write_uint32(ctx
->blob
, deref_array
->deref_array_type
);
388 blob_write_uint32(ctx
->blob
, deref_array
->base_offset
);
389 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
)
390 write_src(ctx
, &deref_array
->indirect
);
393 case nir_deref_type_struct
: {
394 const nir_deref_struct
*deref_struct
= nir_deref_as_struct(d
);
395 blob_write_uint32(ctx
->blob
, deref_struct
->index
);
398 case nir_deref_type_var
:
399 unreachable("Invalid deref type");
402 encode_type_to_blob(ctx
->blob
, d
->type
);
406 static nir_deref_var
*
407 read_deref_chain(read_ctx
*ctx
, void *mem_ctx
)
409 nir_variable
*var
= read_object(ctx
);
410 nir_deref_var
*deref_var
= nir_deref_var_create(mem_ctx
, var
);
412 uint32_t len
= blob_read_uint32(ctx
->blob
);
414 nir_deref
*tail
= &deref_var
->deref
;
415 for (uint32_t i
= 0; i
< len
; i
++) {
416 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
417 nir_deref
*deref
= NULL
;
418 switch (deref_type
) {
419 case nir_deref_type_array
: {
420 nir_deref_array
*deref_array
= nir_deref_array_create(tail
);
421 deref_array
->deref_array_type
= blob_read_uint32(ctx
->blob
);
422 deref_array
->base_offset
= blob_read_uint32(ctx
->blob
);
423 if (deref_array
->deref_array_type
== nir_deref_array_type_indirect
)
424 read_src(ctx
, &deref_array
->indirect
, mem_ctx
);
425 deref
= &deref_array
->deref
;
428 case nir_deref_type_struct
: {
429 uint32_t index
= blob_read_uint32(ctx
->blob
);
430 nir_deref_struct
*deref_struct
= nir_deref_struct_create(tail
, index
);
431 deref
= &deref_struct
->deref
;
434 case nir_deref_type_var
:
435 unreachable("Invalid deref type");
438 deref
->type
= decode_type_from_blob(ctx
->blob
);
448 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
450 blob_write_uint32(ctx
->blob
, alu
->op
);
451 uint32_t flags
= alu
->exact
;
452 flags
|= alu
->dest
.saturate
<< 1;
453 flags
|= alu
->dest
.write_mask
<< 2;
454 blob_write_uint32(ctx
->blob
, flags
);
456 write_dest(ctx
, &alu
->dest
.dest
);
458 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
459 write_src(ctx
, &alu
->src
[i
].src
);
460 flags
= alu
->src
[i
].negate
;
461 flags
|= alu
->src
[i
].abs
<< 1;
462 for (unsigned j
= 0; j
< 4; j
++)
463 flags
|= alu
->src
[i
].swizzle
[j
] << (2 + 2 * j
);
464 blob_write_uint32(ctx
->blob
, flags
);
468 static nir_alu_instr
*
469 read_alu(read_ctx
*ctx
)
471 nir_op op
= blob_read_uint32(ctx
->blob
);
472 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, op
);
474 uint32_t flags
= blob_read_uint32(ctx
->blob
);
475 alu
->exact
= flags
& 1;
476 alu
->dest
.saturate
= flags
& 2;
477 alu
->dest
.write_mask
= flags
>> 2;
479 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
);
481 for (unsigned i
= 0; i
< nir_op_infos
[op
].num_inputs
; i
++) {
482 read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
483 flags
= blob_read_uint32(ctx
->blob
);
484 alu
->src
[i
].negate
= flags
& 1;
485 alu
->src
[i
].abs
= flags
& 2;
486 for (unsigned j
= 0; j
< 4; j
++)
487 alu
->src
[i
].swizzle
[j
] = (flags
>> (2 * j
+ 2)) & 3;
494 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
496 blob_write_uint32(ctx
->blob
, deref
->deref_type
);
498 blob_write_uint32(ctx
->blob
, deref
->mode
);
499 encode_type_to_blob(ctx
->blob
, deref
->type
);
501 write_dest(ctx
, &deref
->dest
);
503 if (deref
->deref_type
== nir_deref_type_var
) {
504 write_object(ctx
, deref
->var
);
508 write_src(ctx
, &deref
->parent
);
510 switch (deref
->deref_type
) {
511 case nir_deref_type_struct
:
512 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
515 case nir_deref_type_array
:
516 write_src(ctx
, &deref
->arr
.index
);
519 case nir_deref_type_array_wildcard
:
520 case nir_deref_type_cast
:
525 unreachable("Invalid deref type");
529 static nir_deref_instr
*
530 read_deref(read_ctx
*ctx
)
532 nir_deref_type deref_type
= blob_read_uint32(ctx
->blob
);
533 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
535 deref
->mode
= blob_read_uint32(ctx
->blob
);
536 deref
->type
= decode_type_from_blob(ctx
->blob
);
538 read_dest(ctx
, &deref
->dest
, &deref
->instr
);
540 if (deref_type
== nir_deref_type_var
) {
541 deref
->var
= read_object(ctx
);
545 read_src(ctx
, &deref
->parent
, &deref
->instr
);
547 switch (deref
->deref_type
) {
548 case nir_deref_type_struct
:
549 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
552 case nir_deref_type_array
:
553 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
556 case nir_deref_type_array_wildcard
:
557 case nir_deref_type_cast
:
562 unreachable("Invalid deref type");
569 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
571 blob_write_uint32(ctx
->blob
, intrin
->intrinsic
);
573 unsigned num_variables
= nir_intrinsic_infos
[intrin
->intrinsic
].num_variables
;
574 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
575 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
577 blob_write_uint32(ctx
->blob
, intrin
->num_components
);
579 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
580 write_dest(ctx
, &intrin
->dest
);
582 for (unsigned i
= 0; i
< num_variables
; i
++)
583 write_deref_chain(ctx
, intrin
->variables
[i
]);
585 for (unsigned i
= 0; i
< num_srcs
; i
++)
586 write_src(ctx
, &intrin
->src
[i
]);
588 for (unsigned i
= 0; i
< num_indices
; i
++)
589 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
592 static nir_intrinsic_instr
*
593 read_intrinsic(read_ctx
*ctx
)
595 nir_intrinsic_op op
= blob_read_uint32(ctx
->blob
);
597 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
599 unsigned num_variables
= nir_intrinsic_infos
[op
].num_variables
;
600 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
601 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
603 intrin
->num_components
= blob_read_uint32(ctx
->blob
);
605 if (nir_intrinsic_infos
[op
].has_dest
)
606 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
);
608 for (unsigned i
= 0; i
< num_variables
; i
++)
609 intrin
->variables
[i
] = read_deref_chain(ctx
, &intrin
->instr
);
611 for (unsigned i
= 0; i
< num_srcs
; i
++)
612 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
614 for (unsigned i
= 0; i
< num_indices
; i
++)
615 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
621 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
623 uint32_t val
= lc
->def
.num_components
;
624 val
|= lc
->def
.bit_size
<< 3;
625 blob_write_uint32(ctx
->blob
, val
);
626 blob_write_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
627 write_add_object(ctx
, &lc
->def
);
630 static nir_load_const_instr
*
631 read_load_const(read_ctx
*ctx
)
633 uint32_t val
= blob_read_uint32(ctx
->blob
);
635 nir_load_const_instr
*lc
=
636 nir_load_const_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
638 blob_copy_bytes(ctx
->blob
, (uint8_t *) &lc
->value
, sizeof(lc
->value
));
639 read_add_object(ctx
, &lc
->def
);
644 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
646 uint32_t val
= undef
->def
.num_components
;
647 val
|= undef
->def
.bit_size
<< 3;
648 blob_write_uint32(ctx
->blob
, val
);
649 write_add_object(ctx
, &undef
->def
);
652 static nir_ssa_undef_instr
*
653 read_ssa_undef(read_ctx
*ctx
)
655 uint32_t val
= blob_read_uint32(ctx
->blob
);
657 nir_ssa_undef_instr
*undef
=
658 nir_ssa_undef_instr_create(ctx
->nir
, val
& 0x7, val
>> 3);
660 read_add_object(ctx
, &undef
->def
);
664 union packed_tex_data
{
667 enum glsl_sampler_dim sampler_dim
:4;
668 nir_alu_type dest_type
:8;
669 unsigned coord_components
:3;
671 unsigned is_shadow
:1;
672 unsigned is_new_style_shadow
:1;
673 unsigned component
:2;
674 unsigned has_texture_deref
:1;
675 unsigned has_sampler_deref
:1;
676 unsigned unused
:10; /* Mark unused for valgrind. */
681 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
683 blob_write_uint32(ctx
->blob
, tex
->num_srcs
);
684 blob_write_uint32(ctx
->blob
, tex
->op
);
685 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
686 blob_write_uint32(ctx
->blob
, tex
->texture_array_size
);
687 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
689 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
690 union packed_tex_data packed
= {
691 .u
.sampler_dim
= tex
->sampler_dim
,
692 .u
.dest_type
= tex
->dest_type
,
693 .u
.coord_components
= tex
->coord_components
,
694 .u
.is_array
= tex
->is_array
,
695 .u
.is_shadow
= tex
->is_shadow
,
696 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
697 .u
.component
= tex
->component
,
698 .u
.has_texture_deref
= tex
->texture
!= NULL
,
699 .u
.has_sampler_deref
= tex
->sampler
!= NULL
,
701 blob_write_uint32(ctx
->blob
, packed
.u32
);
703 write_dest(ctx
, &tex
->dest
);
704 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
705 blob_write_uint32(ctx
->blob
, tex
->src
[i
].src_type
);
706 write_src(ctx
, &tex
->src
[i
].src
);
710 write_deref_chain(ctx
, tex
->texture
);
712 write_deref_chain(ctx
, tex
->sampler
);
715 static nir_tex_instr
*
716 read_tex(read_ctx
*ctx
)
718 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
719 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, num_srcs
);
721 tex
->op
= blob_read_uint32(ctx
->blob
);
722 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
723 tex
->texture_array_size
= blob_read_uint32(ctx
->blob
);
724 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
726 union packed_tex_data packed
;
727 packed
.u32
= blob_read_uint32(ctx
->blob
);
728 tex
->sampler_dim
= packed
.u
.sampler_dim
;
729 tex
->dest_type
= packed
.u
.dest_type
;
730 tex
->coord_components
= packed
.u
.coord_components
;
731 tex
->is_array
= packed
.u
.is_array
;
732 tex
->is_shadow
= packed
.u
.is_shadow
;
733 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
734 tex
->component
= packed
.u
.component
;
736 read_dest(ctx
, &tex
->dest
, &tex
->instr
);
737 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
738 tex
->src
[i
].src_type
= blob_read_uint32(ctx
->blob
);
739 read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
742 tex
->texture
= packed
.u
.has_texture_deref
?
743 read_deref_chain(ctx
, &tex
->instr
) : NULL
;
744 tex
->sampler
= packed
.u
.has_sampler_deref
?
745 read_deref_chain(ctx
, &tex
->instr
) : NULL
;
751 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
753 /* Phi nodes are special, since they may reference SSA definitions and
754 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
755 * and then store enough information so that a later fixup pass can fill
758 write_dest(ctx
, &phi
->dest
);
760 blob_write_uint32(ctx
->blob
, exec_list_length(&phi
->srcs
));
762 nir_foreach_phi_src(src
, phi
) {
763 assert(src
->src
.is_ssa
);
764 size_t blob_offset
= blob_reserve_intptr(ctx
->blob
);
765 MAYBE_UNUSED
size_t blob_offset2
= blob_reserve_intptr(ctx
->blob
);
766 assert(blob_offset
+ sizeof(uintptr_t) == blob_offset2
);
767 write_phi_fixup fixup
= {
768 .blob_offset
= blob_offset
,
772 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
777 write_fixup_phis(write_ctx
*ctx
)
779 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
780 uintptr_t *blob_ptr
= (uintptr_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
781 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
782 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
785 util_dynarray_clear(&ctx
->phi_fixups
);
788 static nir_phi_instr
*
789 read_phi(read_ctx
*ctx
, nir_block
*blk
)
791 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
793 read_dest(ctx
, &phi
->dest
, &phi
->instr
);
795 unsigned num_srcs
= blob_read_uint32(ctx
->blob
);
797 /* For similar reasons as before, we just store the index directly into the
798 * pointer, and let a later pass resolve the phi sources.
800 * In order to ensure that the copied sources (which are just the indices
801 * from the blob for now) don't get inserted into the old shader's use-def
802 * lists, we have to add the phi instruction *before* we set up its
805 nir_instr_insert_after_block(blk
, &phi
->instr
);
807 for (unsigned i
= 0; i
< num_srcs
; i
++) {
808 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
810 src
->src
.is_ssa
= true;
811 src
->src
.ssa
= (nir_ssa_def
*) blob_read_intptr(ctx
->blob
);
812 src
->pred
= (nir_block
*) blob_read_intptr(ctx
->blob
);
814 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
815 * we have to set the parent_instr manually. It doesn't really matter
816 * when we do it, so we might as well do it here.
818 src
->src
.parent_instr
= &phi
->instr
;
820 /* Stash it in the list of phi sources. We'll walk this list and fix up
821 * sources at the very end of read_function_impl.
823 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
825 exec_list_push_tail(&phi
->srcs
, &src
->node
);
832 read_fixup_phis(read_ctx
*ctx
)
834 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
835 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
836 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
838 /* Remove from this list */
839 list_del(&src
->src
.use_link
);
841 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
843 assert(list_empty(&ctx
->phi_srcs
));
847 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
849 blob_write_uint32(ctx
->blob
, jmp
->type
);
852 static nir_jump_instr
*
853 read_jump(read_ctx
*ctx
)
855 nir_jump_type type
= blob_read_uint32(ctx
->blob
);
856 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, type
);
861 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
863 blob_write_intptr(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
865 for (unsigned i
= 0; i
< call
->num_params
; i
++)
866 write_src(ctx
, &call
->params
[i
]);
869 static nir_call_instr
*
870 read_call(read_ctx
*ctx
)
872 nir_function
*callee
= read_object(ctx
);
873 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
875 for (unsigned i
= 0; i
< call
->num_params
; i
++)
876 read_src(ctx
, &call
->params
[i
], call
);
882 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
884 blob_write_uint32(ctx
->blob
, instr
->type
);
885 switch (instr
->type
) {
886 case nir_instr_type_alu
:
887 write_alu(ctx
, nir_instr_as_alu(instr
));
889 case nir_instr_type_deref
:
890 write_deref(ctx
, nir_instr_as_deref(instr
));
892 case nir_instr_type_intrinsic
:
893 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
895 case nir_instr_type_load_const
:
896 write_load_const(ctx
, nir_instr_as_load_const(instr
));
898 case nir_instr_type_ssa_undef
:
899 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
901 case nir_instr_type_tex
:
902 write_tex(ctx
, nir_instr_as_tex(instr
));
904 case nir_instr_type_phi
:
905 write_phi(ctx
, nir_instr_as_phi(instr
));
907 case nir_instr_type_jump
:
908 write_jump(ctx
, nir_instr_as_jump(instr
));
910 case nir_instr_type_call
:
911 write_call(ctx
, nir_instr_as_call(instr
));
913 case nir_instr_type_parallel_copy
:
914 unreachable("Cannot write parallel copies");
916 unreachable("bad instr type");
921 read_instr(read_ctx
*ctx
, nir_block
*block
)
923 nir_instr_type type
= blob_read_uint32(ctx
->blob
);
926 case nir_instr_type_alu
:
927 instr
= &read_alu(ctx
)->instr
;
929 case nir_instr_type_deref
:
930 instr
= &read_deref(ctx
)->instr
;
932 case nir_instr_type_intrinsic
:
933 instr
= &read_intrinsic(ctx
)->instr
;
935 case nir_instr_type_load_const
:
936 instr
= &read_load_const(ctx
)->instr
;
938 case nir_instr_type_ssa_undef
:
939 instr
= &read_ssa_undef(ctx
)->instr
;
941 case nir_instr_type_tex
:
942 instr
= &read_tex(ctx
)->instr
;
944 case nir_instr_type_phi
:
945 /* Phi instructions are a bit of a special case when reading because we
946 * don't want inserting the instruction to automatically handle use/defs
947 * for us. Instead, we need to wait until all the blocks/instructions
948 * are read so that we can set their sources up.
950 read_phi(ctx
, block
);
952 case nir_instr_type_jump
:
953 instr
= &read_jump(ctx
)->instr
;
955 case nir_instr_type_call
:
956 instr
= &read_call(ctx
)->instr
;
958 case nir_instr_type_parallel_copy
:
959 unreachable("Cannot read parallel copies");
961 unreachable("bad instr type");
964 nir_instr_insert_after_block(block
, instr
);
968 write_block(write_ctx
*ctx
, const nir_block
*block
)
970 write_add_object(ctx
, block
);
971 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
972 nir_foreach_instr(instr
, block
)
973 write_instr(ctx
, instr
);
977 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
979 /* Don't actually create a new block. Just use the one from the tail of
980 * the list. NIR guarantees that the tail of the list is a block and that
981 * no two blocks are side-by-side in the IR; It should be empty.
984 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
986 read_add_object(ctx
, block
);
987 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
988 for (unsigned i
= 0; i
< num_instrs
; i
++) {
989 read_instr(ctx
, block
);
994 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
997 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
1000 write_if(write_ctx
*ctx
, nir_if
*nif
)
1002 write_src(ctx
, &nif
->condition
);
1004 write_cf_list(ctx
, &nif
->then_list
);
1005 write_cf_list(ctx
, &nif
->else_list
);
1009 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
1011 nir_if
*nif
= nir_if_create(ctx
->nir
);
1013 read_src(ctx
, &nif
->condition
, nif
);
1015 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
1017 read_cf_list(ctx
, &nif
->then_list
);
1018 read_cf_list(ctx
, &nif
->else_list
);
1022 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
1024 write_cf_list(ctx
, &loop
->body
);
1028 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
1030 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
1032 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
1034 read_cf_list(ctx
, &loop
->body
);
1038 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
1040 blob_write_uint32(ctx
->blob
, cf
->type
);
1043 case nir_cf_node_block
:
1044 write_block(ctx
, nir_cf_node_as_block(cf
));
1046 case nir_cf_node_if
:
1047 write_if(ctx
, nir_cf_node_as_if(cf
));
1049 case nir_cf_node_loop
:
1050 write_loop(ctx
, nir_cf_node_as_loop(cf
));
1053 unreachable("bad cf type");
1058 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
1060 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
1063 case nir_cf_node_block
:
1064 read_block(ctx
, list
);
1066 case nir_cf_node_if
:
1069 case nir_cf_node_loop
:
1070 read_loop(ctx
, list
);
1073 unreachable("bad cf type");
1078 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
1080 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
1081 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
1082 write_cf_node(ctx
, cf
);
1087 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1089 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1090 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1091 read_cf_node(ctx
, cf_list
);
1095 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1097 write_var_list(ctx
, &fi
->locals
);
1098 write_reg_list(ctx
, &fi
->registers
);
1099 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1101 write_cf_list(ctx
, &fi
->body
);
1102 write_fixup_phis(ctx
);
1105 static nir_function_impl
*
1106 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1108 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1111 read_var_list(ctx
, &fi
->locals
);
1112 read_reg_list(ctx
, &fi
->registers
);
1113 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1115 read_cf_list(ctx
, &fi
->body
);
1116 read_fixup_phis(ctx
);
1118 fi
->valid_metadata
= 0;
1124 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1126 blob_write_uint32(ctx
->blob
, !!(fxn
->name
));
1128 blob_write_string(ctx
->blob
, fxn
->name
);
1130 write_add_object(ctx
, fxn
);
1132 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1133 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1135 ((uint32_t)fxn
->params
[i
].num_components
) |
1136 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1137 blob_write_uint32(ctx
->blob
, val
);
1140 /* At first glance, it looks like we should write the function_impl here.
1141 * However, call instructions need to be able to reference at least the
1142 * function and those will get processed as we write the function_impls.
1143 * We stop here and write function_impls as a second pass.
1148 read_function(read_ctx
*ctx
)
1150 bool has_name
= blob_read_uint32(ctx
->blob
);
1151 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1153 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1155 read_add_object(ctx
, fxn
);
1157 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1158 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1159 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1160 uint32_t val
= blob_read_uint32(ctx
->blob
);
1161 fxn
->params
[i
].num_components
= val
& 0xff;
1162 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1167 nir_serialize(struct blob
*blob
, const nir_shader
*nir
)
1170 ctx
.remap_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
1171 _mesa_key_pointer_equal
);
1175 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1177 size_t idx_size_offset
= blob_reserve_intptr(blob
);
1179 struct shader_info info
= nir
->info
;
1180 uint32_t strings
= 0;
1185 blob_write_uint32(blob
, strings
);
1187 blob_write_string(blob
, info
.name
);
1189 blob_write_string(blob
, info
.label
);
1190 info
.name
= info
.label
= NULL
;
1191 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1193 write_var_list(&ctx
, &nir
->uniforms
);
1194 write_var_list(&ctx
, &nir
->inputs
);
1195 write_var_list(&ctx
, &nir
->outputs
);
1196 write_var_list(&ctx
, &nir
->shared
);
1197 write_var_list(&ctx
, &nir
->globals
);
1198 write_var_list(&ctx
, &nir
->system_values
);
1200 write_reg_list(&ctx
, &nir
->registers
);
1201 blob_write_uint32(blob
, nir
->reg_alloc
);
1202 blob_write_uint32(blob
, nir
->num_inputs
);
1203 blob_write_uint32(blob
, nir
->num_uniforms
);
1204 blob_write_uint32(blob
, nir
->num_outputs
);
1205 blob_write_uint32(blob
, nir
->num_shared
);
1206 blob_write_uint32(blob
, nir
->lowered_derefs
);
1208 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1209 nir_foreach_function(fxn
, nir
) {
1210 write_function(&ctx
, fxn
);
1213 nir_foreach_function(fxn
, nir
) {
1214 write_function_impl(&ctx
, fxn
->impl
);
1217 *(uintptr_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1219 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1220 util_dynarray_fini(&ctx
.phi_fixups
);
1224 nir_deserialize(void *mem_ctx
,
1225 const struct nir_shader_compiler_options
*options
,
1226 struct blob_reader
*blob
)
1230 list_inithead(&ctx
.phi_srcs
);
1231 ctx
.idx_table_len
= blob_read_intptr(blob
);
1232 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1235 uint32_t strings
= blob_read_uint32(blob
);
1236 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1237 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1239 struct shader_info info
;
1240 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1242 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1244 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1245 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1247 ctx
.nir
->info
= info
;
1249 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1250 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1251 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1252 read_var_list(&ctx
, &ctx
.nir
->shared
);
1253 read_var_list(&ctx
, &ctx
.nir
->globals
);
1254 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1256 read_reg_list(&ctx
, &ctx
.nir
->registers
);
1257 ctx
.nir
->reg_alloc
= blob_read_uint32(blob
);
1258 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1259 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1260 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1261 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1262 ctx
.nir
->lowered_derefs
= blob_read_uint32(blob
);
1264 unsigned num_functions
= blob_read_uint32(blob
);
1265 for (unsigned i
= 0; i
< num_functions
; i
++)
1266 read_function(&ctx
);
1268 nir_foreach_function(fxn
, ctx
.nir
)
1269 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1271 free(ctx
.idx_table
);
1277 nir_shader_serialize_deserialize(void *mem_ctx
, nir_shader
*s
)
1279 const struct nir_shader_compiler_options
*options
= s
->options
;
1283 nir_serialize(&writer
, s
);
1286 struct blob_reader reader
;
1287 blob_reader_init(&reader
, writer
.data
, writer
.size
);
1288 nir_shader
*ns
= nir_deserialize(mem_ctx
, options
, &reader
);
1290 blob_finish(&writer
);