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"
27 #include "util/u_math.h"
29 #define NIR_SERIALIZE_FUNC_HAS_IMPL ((void *)(intptr_t)1)
30 #define MAX_OBJECT_IDS (1 << 20)
39 const nir_shader
*nir
;
43 /* maps pointer to index */
44 struct hash_table
*remap_table
;
46 /* the next index to assign to a NIR in-memory object */
49 /* Array of write_phi_fixup structs representing phi sources that need to
50 * be resolved in the second pass.
52 struct util_dynarray phi_fixups
;
54 /* The last serialized type. */
55 const struct glsl_type
*last_type
;
56 const struct glsl_type
*last_interface_type
;
57 struct nir_variable_data last_var_data
;
59 /* For skipping equal ALU headers (typical after scalarization). */
60 nir_instr_type last_instr_type
;
61 uintptr_t last_alu_header_offset
;
63 /* Don't write optional data such as variable names. */
70 struct blob_reader
*blob
;
72 /* the next index to assign to a NIR in-memory object */
75 /* The length of the index -> object table */
76 uint32_t idx_table_len
;
78 /* map from index to deserialized pointer */
81 /* List of phi sources. */
82 struct list_head phi_srcs
;
84 /* The last deserialized type. */
85 const struct glsl_type
*last_type
;
86 const struct glsl_type
*last_interface_type
;
87 struct nir_variable_data last_var_data
;
91 write_add_object(write_ctx
*ctx
, const void *obj
)
93 uint32_t index
= ctx
->next_idx
++;
94 assert(index
!= MAX_OBJECT_IDS
);
95 _mesa_hash_table_insert(ctx
->remap_table
, obj
, (void *)(uintptr_t) index
);
99 write_lookup_object(write_ctx
*ctx
, const void *obj
)
101 struct hash_entry
*entry
= _mesa_hash_table_search(ctx
->remap_table
, obj
);
103 return (uint32_t)(uintptr_t) entry
->data
;
107 read_add_object(read_ctx
*ctx
, void *obj
)
109 assert(ctx
->next_idx
< ctx
->idx_table_len
);
110 ctx
->idx_table
[ctx
->next_idx
++] = obj
;
114 read_lookup_object(read_ctx
*ctx
, uint32_t idx
)
116 assert(idx
< ctx
->idx_table_len
);
117 return ctx
->idx_table
[idx
];
121 read_object(read_ctx
*ctx
)
123 return read_lookup_object(ctx
, blob_read_uint32(ctx
->blob
));
127 encode_bit_size_3bits(uint8_t bit_size
)
129 /* Encode values of 0, 1, 2, 4, 8, 16, 32, 64 in 3 bits. */
130 assert(bit_size
<= 64 && util_is_power_of_two_or_zero(bit_size
));
132 return util_logbase2(bit_size
) + 1;
137 decode_bit_size_3bits(uint8_t bit_size
)
140 return 1 << (bit_size
- 1);
145 encode_num_components_in_3bits(uint8_t num_components
)
147 if (num_components
<= 4)
148 return num_components
;
149 if (num_components
== 8)
151 if (num_components
== 16)
154 unreachable("invalid number in num_components");
159 decode_num_components_in_3bits(uint8_t value
)
168 unreachable("invalid num_components encoding");
173 write_constant(write_ctx
*ctx
, const nir_constant
*c
)
175 blob_write_bytes(ctx
->blob
, c
->values
, sizeof(c
->values
));
176 blob_write_uint32(ctx
->blob
, c
->num_elements
);
177 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
178 write_constant(ctx
, c
->elements
[i
]);
181 static nir_constant
*
182 read_constant(read_ctx
*ctx
, nir_variable
*nvar
)
184 nir_constant
*c
= ralloc(nvar
, nir_constant
);
186 blob_copy_bytes(ctx
->blob
, (uint8_t *)c
->values
, sizeof(c
->values
));
187 c
->num_elements
= blob_read_uint32(ctx
->blob
);
188 c
->elements
= ralloc_array(nvar
, nir_constant
*, c
->num_elements
);
189 for (unsigned i
= 0; i
< c
->num_elements
; i
++)
190 c
->elements
[i
] = read_constant(ctx
, nvar
);
195 enum var_data_encoding
{
197 var_encode_shader_temp
,
198 var_encode_function_temp
,
199 var_encode_location_diff
,
206 unsigned has_constant_initializer
:1;
207 unsigned has_interface_type
:1;
208 unsigned num_state_slots
:7;
209 unsigned data_encoding
:2;
210 unsigned type_same_as_last
:1;
211 unsigned interface_type_same_as_last
:1;
213 unsigned num_members
:16;
217 union packed_var_data_diff
{
222 int driver_location
:16;
227 write_variable(write_ctx
*ctx
, const nir_variable
*var
)
229 write_add_object(ctx
, var
);
231 assert(var
->num_state_slots
< (1 << 7));
232 assert(var
->num_members
< (1 << 16));
234 STATIC_ASSERT(sizeof(union packed_var
) == 4);
235 union packed_var flags
;
238 flags
.u
.has_name
= !ctx
->strip
&& var
->name
;
239 flags
.u
.has_constant_initializer
= !!(var
->constant_initializer
);
240 flags
.u
.has_interface_type
= !!(var
->interface_type
);
241 flags
.u
.type_same_as_last
= var
->type
== ctx
->last_type
;
242 flags
.u
.interface_type_same_as_last
=
243 var
->interface_type
&& var
->interface_type
== ctx
->last_interface_type
;
244 flags
.u
.num_state_slots
= var
->num_state_slots
;
245 flags
.u
.num_members
= var
->num_members
;
247 struct nir_variable_data data
= var
->data
;
249 /* When stripping, we expect that the location is no longer needed,
250 * which is typically after shaders are linked.
253 data
.mode
!= nir_var_shader_in
&&
254 data
.mode
!= nir_var_shader_out
)
257 /* Temporary variables don't serialize var->data. */
258 if (data
.mode
== nir_var_shader_temp
)
259 flags
.u
.data_encoding
= var_encode_shader_temp
;
260 else if (data
.mode
== nir_var_function_temp
)
261 flags
.u
.data_encoding
= var_encode_function_temp
;
263 struct nir_variable_data tmp
= data
;
265 tmp
.location
= ctx
->last_var_data
.location
;
266 tmp
.location_frac
= ctx
->last_var_data
.location_frac
;
267 tmp
.driver_location
= ctx
->last_var_data
.driver_location
;
269 /* See if we can encode only the difference in locations from the last
272 if (memcmp(&ctx
->last_var_data
, &tmp
, sizeof(tmp
)) == 0 &&
273 abs((int)data
.location
-
274 (int)ctx
->last_var_data
.location
) < (1 << 12) &&
275 abs((int)data
.driver_location
-
276 (int)ctx
->last_var_data
.driver_location
) < (1 << 15))
277 flags
.u
.data_encoding
= var_encode_location_diff
;
279 flags
.u
.data_encoding
= var_encode_full
;
282 blob_write_uint32(ctx
->blob
, flags
.u32
);
284 if (!flags
.u
.type_same_as_last
) {
285 encode_type_to_blob(ctx
->blob
, var
->type
);
286 ctx
->last_type
= var
->type
;
289 if (var
->interface_type
&& !flags
.u
.interface_type_same_as_last
) {
290 encode_type_to_blob(ctx
->blob
, var
->interface_type
);
291 ctx
->last_interface_type
= var
->interface_type
;
294 if (flags
.u
.has_name
)
295 blob_write_string(ctx
->blob
, var
->name
);
297 if (flags
.u
.data_encoding
== var_encode_full
||
298 flags
.u
.data_encoding
== var_encode_location_diff
) {
299 if (flags
.u
.data_encoding
== var_encode_full
) {
300 blob_write_bytes(ctx
->blob
, &data
, sizeof(data
));
302 /* Serialize only the difference in locations from the last variable.
304 union packed_var_data_diff diff
;
306 diff
.u
.location
= data
.location
- ctx
->last_var_data
.location
;
307 diff
.u
.location_frac
= data
.location_frac
-
308 ctx
->last_var_data
.location_frac
;
309 diff
.u
.driver_location
= data
.driver_location
-
310 ctx
->last_var_data
.driver_location
;
312 blob_write_uint32(ctx
->blob
, diff
.u32
);
315 ctx
->last_var_data
= data
;
318 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
319 blob_write_bytes(ctx
->blob
, &var
->state_slots
[i
],
320 sizeof(var
->state_slots
[i
]));
322 if (var
->constant_initializer
)
323 write_constant(ctx
, var
->constant_initializer
);
324 if (var
->num_members
> 0) {
325 blob_write_bytes(ctx
->blob
, (uint8_t *) var
->members
,
326 var
->num_members
* sizeof(*var
->members
));
330 static nir_variable
*
331 read_variable(read_ctx
*ctx
)
333 nir_variable
*var
= rzalloc(ctx
->nir
, nir_variable
);
334 read_add_object(ctx
, var
);
336 union packed_var flags
;
337 flags
.u32
= blob_read_uint32(ctx
->blob
);
339 if (flags
.u
.type_same_as_last
) {
340 var
->type
= ctx
->last_type
;
342 var
->type
= decode_type_from_blob(ctx
->blob
);
343 ctx
->last_type
= var
->type
;
346 if (flags
.u
.has_interface_type
) {
347 if (flags
.u
.interface_type_same_as_last
) {
348 var
->interface_type
= ctx
->last_interface_type
;
350 var
->interface_type
= decode_type_from_blob(ctx
->blob
);
351 ctx
->last_interface_type
= var
->interface_type
;
355 if (flags
.u
.has_name
) {
356 const char *name
= blob_read_string(ctx
->blob
);
357 var
->name
= ralloc_strdup(var
, name
);
362 if (flags
.u
.data_encoding
== var_encode_shader_temp
)
363 var
->data
.mode
= nir_var_shader_temp
;
364 else if (flags
.u
.data_encoding
== var_encode_function_temp
)
365 var
->data
.mode
= nir_var_function_temp
;
366 else if (flags
.u
.data_encoding
== var_encode_full
) {
367 blob_copy_bytes(ctx
->blob
, (uint8_t *) &var
->data
, sizeof(var
->data
));
368 ctx
->last_var_data
= var
->data
;
369 } else { /* var_encode_location_diff */
370 union packed_var_data_diff diff
;
371 diff
.u32
= blob_read_uint32(ctx
->blob
);
373 var
->data
= ctx
->last_var_data
;
374 var
->data
.location
+= diff
.u
.location
;
375 var
->data
.location_frac
+= diff
.u
.location_frac
;
376 var
->data
.driver_location
+= diff
.u
.driver_location
;
378 ctx
->last_var_data
= var
->data
;
381 var
->num_state_slots
= flags
.u
.num_state_slots
;
382 if (var
->num_state_slots
!= 0) {
383 var
->state_slots
= ralloc_array(var
, nir_state_slot
,
384 var
->num_state_slots
);
385 for (unsigned i
= 0; i
< var
->num_state_slots
; i
++) {
386 blob_copy_bytes(ctx
->blob
, &var
->state_slots
[i
],
387 sizeof(var
->state_slots
[i
]));
390 if (flags
.u
.has_constant_initializer
)
391 var
->constant_initializer
= read_constant(ctx
, var
);
393 var
->constant_initializer
= NULL
;
394 var
->num_members
= flags
.u
.num_members
;
395 if (var
->num_members
> 0) {
396 var
->members
= ralloc_array(var
, struct nir_variable_data
,
398 blob_copy_bytes(ctx
->blob
, (uint8_t *) var
->members
,
399 var
->num_members
* sizeof(*var
->members
));
406 write_var_list(write_ctx
*ctx
, const struct exec_list
*src
)
408 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
409 foreach_list_typed(nir_variable
, var
, node
, src
) {
410 write_variable(ctx
, var
);
415 read_var_list(read_ctx
*ctx
, struct exec_list
*dst
)
417 exec_list_make_empty(dst
);
418 unsigned num_vars
= blob_read_uint32(ctx
->blob
);
419 for (unsigned i
= 0; i
< num_vars
; i
++) {
420 nir_variable
*var
= read_variable(ctx
);
421 exec_list_push_tail(dst
, &var
->node
);
426 write_register(write_ctx
*ctx
, const nir_register
*reg
)
428 write_add_object(ctx
, reg
);
429 blob_write_uint32(ctx
->blob
, reg
->num_components
);
430 blob_write_uint32(ctx
->blob
, reg
->bit_size
);
431 blob_write_uint32(ctx
->blob
, reg
->num_array_elems
);
432 blob_write_uint32(ctx
->blob
, reg
->index
);
433 blob_write_uint32(ctx
->blob
, !ctx
->strip
&& reg
->name
);
434 if (!ctx
->strip
&& reg
->name
)
435 blob_write_string(ctx
->blob
, reg
->name
);
438 static nir_register
*
439 read_register(read_ctx
*ctx
)
441 nir_register
*reg
= ralloc(ctx
->nir
, nir_register
);
442 read_add_object(ctx
, reg
);
443 reg
->num_components
= blob_read_uint32(ctx
->blob
);
444 reg
->bit_size
= blob_read_uint32(ctx
->blob
);
445 reg
->num_array_elems
= blob_read_uint32(ctx
->blob
);
446 reg
->index
= blob_read_uint32(ctx
->blob
);
447 bool has_name
= blob_read_uint32(ctx
->blob
);
449 const char *name
= blob_read_string(ctx
->blob
);
450 reg
->name
= ralloc_strdup(reg
, name
);
455 list_inithead(®
->uses
);
456 list_inithead(®
->defs
);
457 list_inithead(®
->if_uses
);
463 write_reg_list(write_ctx
*ctx
, const struct exec_list
*src
)
465 blob_write_uint32(ctx
->blob
, exec_list_length(src
));
466 foreach_list_typed(nir_register
, reg
, node
, src
)
467 write_register(ctx
, reg
);
471 read_reg_list(read_ctx
*ctx
, struct exec_list
*dst
)
473 exec_list_make_empty(dst
);
474 unsigned num_regs
= blob_read_uint32(ctx
->blob
);
475 for (unsigned i
= 0; i
< num_regs
; i
++) {
476 nir_register
*reg
= read_register(ctx
);
477 exec_list_push_tail(dst
, ®
->node
);
484 unsigned is_ssa
:1; /* <-- Header */
485 unsigned is_indirect
:1;
486 unsigned object_idx
:20;
487 unsigned _footer
:10; /* <-- Footer */
490 unsigned _header
:22; /* <-- Header */
491 unsigned negate
:1; /* <-- Footer */
493 unsigned swizzle_x
:2;
494 unsigned swizzle_y
:2;
495 unsigned swizzle_z
:2;
496 unsigned swizzle_w
:2;
499 unsigned _header
:22; /* <-- Header */
500 unsigned src_type
:5; /* <-- Footer */
506 write_src_full(write_ctx
*ctx
, const nir_src
*src
, union packed_src header
)
508 /* Since sources are very frequent, we try to save some space when storing
509 * them. In particular, we store whether the source is a register and
510 * whether the register has an indirect index in the low two bits. We can
511 * assume that the high two bits of the index are zero, since otherwise our
512 * address space would've been exhausted allocating the remap table!
514 header
.any
.is_ssa
= src
->is_ssa
;
516 header
.any
.object_idx
= write_lookup_object(ctx
, src
->ssa
);
517 blob_write_uint32(ctx
->blob
, header
.u32
);
519 header
.any
.object_idx
= write_lookup_object(ctx
, src
->reg
.reg
);
520 header
.any
.is_indirect
= !!src
->reg
.indirect
;
521 blob_write_uint32(ctx
->blob
, header
.u32
);
522 blob_write_uint32(ctx
->blob
, src
->reg
.base_offset
);
523 if (src
->reg
.indirect
) {
524 union packed_src header
= {0};
525 write_src_full(ctx
, src
->reg
.indirect
, header
);
531 write_src(write_ctx
*ctx
, const nir_src
*src
)
533 union packed_src header
= {0};
534 write_src_full(ctx
, src
, header
);
537 static union packed_src
538 read_src(read_ctx
*ctx
, nir_src
*src
, void *mem_ctx
)
540 STATIC_ASSERT(sizeof(union packed_src
) == 4);
541 union packed_src header
;
542 header
.u32
= blob_read_uint32(ctx
->blob
);
544 src
->is_ssa
= header
.any
.is_ssa
;
546 src
->ssa
= read_lookup_object(ctx
, header
.any
.object_idx
);
548 src
->reg
.reg
= read_lookup_object(ctx
, header
.any
.object_idx
);
549 src
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
550 if (header
.any
.is_indirect
) {
551 src
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
552 read_src(ctx
, src
->reg
.indirect
, mem_ctx
);
554 src
->reg
.indirect
= NULL
;
565 uint8_t num_components
:3;
570 uint8_t is_indirect
:1;
575 enum intrinsic_const_indices_encoding
{
576 /* Use the 6 bits of packed_const_indices to store 1-6 indices.
577 * 1 6-bit index, or 2 3-bit indices, or 3 2-bit indices, or
580 * The common case for load_ubo is 0, 0, 0, which is trivially represented.
581 * The common cases for load_interpolated_input also fit here, e.g.: 7, 3
583 const_indices_6bit_all_combined
,
585 const_indices_8bit
, /* 8 bits per element */
586 const_indices_16bit
, /* 16 bits per element */
587 const_indices_32bit
, /* 32 bits per element */
590 enum load_const_packing
{
591 /* Constants are not packed and are stored in following dwords. */
594 /* packed_value contains high 19 bits, low bits are 0,
595 * good for floating-point decimals
597 load_const_scalar_hi_19bits
,
599 /* packed_value contains low 19 bits, high bits are sign-extended */
600 load_const_scalar_lo_19bits_sext
,
606 unsigned instr_type
:4; /* always present */
608 unsigned dest
:8; /* always last */
611 unsigned instr_type
:4;
613 unsigned no_signed_wrap
:1;
614 unsigned no_unsigned_wrap
:1;
616 unsigned writemask
:4;
618 unsigned packed_src_ssa_16bit
:1;
619 /* Scalarized ALUs always have the same header. */
620 unsigned num_followup_alu_sharing_header
:2;
624 unsigned instr_type
:4;
625 unsigned deref_type
:3;
626 unsigned cast_type_same_as_last
:1;
627 unsigned mode
:10; /* deref_var redefines this */
628 unsigned packed_src_ssa_16bit
:1; /* deref_var redefines this */
629 unsigned _pad
:5; /* deref_var redefines this */
633 unsigned instr_type
:4;
634 unsigned deref_type
:3;
636 unsigned object_idx
:16; /* if 0, the object ID is a separate uint32 */
640 unsigned instr_type
:4;
641 unsigned intrinsic
:9;
642 unsigned num_components
:3;
643 unsigned const_indices_encoding
:2;
644 unsigned packed_const_indices
:6;
648 unsigned instr_type
:4;
649 unsigned last_component
:4;
651 unsigned packing
:2; /* enum load_const_packing */
652 unsigned packed_value
:19; /* meaning determined by packing */
655 unsigned instr_type
:4;
656 unsigned last_component
:4;
661 unsigned instr_type
:4;
664 unsigned texture_array_size
:12;
668 unsigned instr_type
:4;
669 unsigned num_srcs
:20;
673 unsigned instr_type
:4;
679 /* Write "lo24" as low 24 bits in the first uint32. */
681 write_dest(write_ctx
*ctx
, const nir_dest
*dst
, union packed_instr header
,
682 nir_instr_type instr_type
)
684 STATIC_ASSERT(sizeof(union packed_dest
) == 1);
685 union packed_dest dest
;
688 dest
.ssa
.is_ssa
= dst
->is_ssa
;
690 dest
.ssa
.has_name
= !ctx
->strip
&& dst
->ssa
.name
;
691 dest
.ssa
.num_components
=
692 encode_num_components_in_3bits(dst
->ssa
.num_components
);
693 dest
.ssa
.bit_size
= encode_bit_size_3bits(dst
->ssa
.bit_size
);
695 dest
.reg
.is_indirect
= !!(dst
->reg
.indirect
);
697 header
.any
.dest
= dest
.u8
;
699 /* Check if the current ALU instruction has the same header as the previous
700 * instruction that is also ALU. If it is, we don't have to write
701 * the current header. This is a typical occurence after scalarization.
703 if (instr_type
== nir_instr_type_alu
) {
704 bool equal_header
= false;
706 if (ctx
->last_instr_type
== nir_instr_type_alu
) {
707 assert(ctx
->last_alu_header_offset
);
708 union packed_instr
*last_header
=
709 (union packed_instr
*)(ctx
->blob
->data
+
710 ctx
->last_alu_header_offset
);
712 /* Clear the field that counts ALUs with equal headers. */
713 union packed_instr clean_header
;
714 clean_header
.u32
= last_header
->u32
;
715 clean_header
.alu
.num_followup_alu_sharing_header
= 0;
717 /* There can be at most 4 consecutive ALU instructions
718 * sharing the same header.
720 if (last_header
->alu
.num_followup_alu_sharing_header
< 3 &&
721 header
.u32
== clean_header
.u32
) {
722 last_header
->alu
.num_followup_alu_sharing_header
++;
728 ctx
->last_alu_header_offset
= ctx
->blob
->size
;
729 blob_write_uint32(ctx
->blob
, header
.u32
);
732 blob_write_uint32(ctx
->blob
, header
.u32
);
736 write_add_object(ctx
, &dst
->ssa
);
737 if (dest
.ssa
.has_name
)
738 blob_write_string(ctx
->blob
, dst
->ssa
.name
);
740 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, dst
->reg
.reg
));
741 blob_write_uint32(ctx
->blob
, dst
->reg
.base_offset
);
742 if (dst
->reg
.indirect
)
743 write_src(ctx
, dst
->reg
.indirect
);
748 read_dest(read_ctx
*ctx
, nir_dest
*dst
, nir_instr
*instr
,
749 union packed_instr header
)
751 union packed_dest dest
;
752 dest
.u8
= header
.any
.dest
;
754 if (dest
.ssa
.is_ssa
) {
755 unsigned bit_size
= decode_bit_size_3bits(dest
.ssa
.bit_size
);
756 unsigned num_components
=
757 decode_num_components_in_3bits(dest
.ssa
.num_components
);
758 char *name
= dest
.ssa
.has_name
? blob_read_string(ctx
->blob
) : NULL
;
759 nir_ssa_dest_init(instr
, dst
, num_components
, bit_size
, name
);
760 read_add_object(ctx
, &dst
->ssa
);
762 dst
->reg
.reg
= read_object(ctx
);
763 dst
->reg
.base_offset
= blob_read_uint32(ctx
->blob
);
764 if (dest
.reg
.is_indirect
) {
765 dst
->reg
.indirect
= ralloc(instr
, nir_src
);
766 read_src(ctx
, dst
->reg
.indirect
, instr
);
772 are_object_ids_16bit(write_ctx
*ctx
)
774 /* Check the highest object ID, because they are monotonic. */
775 return ctx
->next_idx
< (1 << 16);
779 is_alu_src_ssa_16bit(write_ctx
*ctx
, const nir_alu_instr
*alu
)
781 unsigned num_srcs
= nir_op_infos
[alu
->op
].num_inputs
;
783 for (unsigned i
= 0; i
< num_srcs
; i
++) {
784 if (!alu
->src
[i
].src
.is_ssa
|| alu
->src
[i
].abs
|| alu
->src
[i
].negate
)
787 unsigned src_components
= nir_ssa_alu_instr_src_components(alu
, i
);
789 for (unsigned chan
= 0; chan
< src_components
; chan
++) {
790 if (alu
->src
[i
].swizzle
[chan
] != chan
)
795 return are_object_ids_16bit(ctx
);
799 write_alu(write_ctx
*ctx
, const nir_alu_instr
*alu
)
801 unsigned num_srcs
= nir_op_infos
[alu
->op
].num_inputs
;
802 /* 9 bits for nir_op */
803 STATIC_ASSERT(nir_num_opcodes
<= 512);
804 union packed_instr header
;
807 header
.alu
.instr_type
= alu
->instr
.type
;
808 header
.alu
.exact
= alu
->exact
;
809 header
.alu
.no_signed_wrap
= alu
->no_signed_wrap
;
810 header
.alu
.no_unsigned_wrap
= alu
->no_unsigned_wrap
;
811 header
.alu
.saturate
= alu
->dest
.saturate
;
812 header
.alu
.writemask
= alu
->dest
.write_mask
;
813 header
.alu
.op
= alu
->op
;
814 header
.alu
.packed_src_ssa_16bit
= is_alu_src_ssa_16bit(ctx
, alu
);
816 write_dest(ctx
, &alu
->dest
.dest
, header
, alu
->instr
.type
);
818 if (header
.alu
.packed_src_ssa_16bit
) {
819 for (unsigned i
= 0; i
< num_srcs
; i
++) {
820 assert(alu
->src
[i
].src
.is_ssa
);
821 unsigned idx
= write_lookup_object(ctx
, alu
->src
[i
].src
.ssa
);
822 assert(idx
< (1 << 16));
823 blob_write_uint16(ctx
->blob
, idx
);
826 for (unsigned i
= 0; i
< num_srcs
; i
++) {
827 union packed_src src
;
830 src
.alu
.negate
= alu
->src
[i
].negate
;
831 src
.alu
.abs
= alu
->src
[i
].abs
;
832 src
.alu
.swizzle_x
= alu
->src
[i
].swizzle
[0];
833 src
.alu
.swizzle_y
= alu
->src
[i
].swizzle
[1];
834 src
.alu
.swizzle_z
= alu
->src
[i
].swizzle
[2];
835 src
.alu
.swizzle_w
= alu
->src
[i
].swizzle
[3];
837 write_src_full(ctx
, &alu
->src
[i
].src
, src
);
842 static nir_alu_instr
*
843 read_alu(read_ctx
*ctx
, union packed_instr header
)
845 unsigned num_srcs
= nir_op_infos
[header
.alu
.op
].num_inputs
;
846 nir_alu_instr
*alu
= nir_alu_instr_create(ctx
->nir
, header
.alu
.op
);
848 alu
->exact
= header
.alu
.exact
;
849 alu
->no_signed_wrap
= header
.alu
.no_signed_wrap
;
850 alu
->no_unsigned_wrap
= header
.alu
.no_unsigned_wrap
;
851 alu
->dest
.saturate
= header
.alu
.saturate
;
852 alu
->dest
.write_mask
= header
.alu
.writemask
;
854 read_dest(ctx
, &alu
->dest
.dest
, &alu
->instr
, header
);
856 if (header
.alu
.packed_src_ssa_16bit
) {
857 for (unsigned i
= 0; i
< num_srcs
; i
++) {
858 nir_alu_src
*src
= &alu
->src
[i
];
859 src
->src
.is_ssa
= true;
860 src
->src
.ssa
= read_lookup_object(ctx
, blob_read_uint16(ctx
->blob
));
862 memset(&src
->swizzle
, 0, sizeof(src
->swizzle
));
864 unsigned src_components
= nir_ssa_alu_instr_src_components(alu
, i
);
866 for (unsigned chan
= 0; chan
< src_components
; chan
++)
867 src
->swizzle
[chan
] = chan
;
870 for (unsigned i
= 0; i
< num_srcs
; i
++) {
871 union packed_src src
= read_src(ctx
, &alu
->src
[i
].src
, &alu
->instr
);
873 alu
->src
[i
].negate
= src
.alu
.negate
;
874 alu
->src
[i
].abs
= src
.alu
.abs
;
875 alu
->src
[i
].swizzle
[0] = src
.alu
.swizzle_x
;
876 alu
->src
[i
].swizzle
[1] = src
.alu
.swizzle_y
;
877 alu
->src
[i
].swizzle
[2] = src
.alu
.swizzle_z
;
878 alu
->src
[i
].swizzle
[3] = src
.alu
.swizzle_w
;
886 write_deref(write_ctx
*ctx
, const nir_deref_instr
*deref
)
888 assert(deref
->deref_type
< 8);
889 assert(deref
->mode
< (1 << 10));
891 union packed_instr header
;
894 header
.deref
.instr_type
= deref
->instr
.type
;
895 header
.deref
.deref_type
= deref
->deref_type
;
897 if (deref
->deref_type
== nir_deref_type_cast
) {
898 header
.deref
.mode
= deref
->mode
;
899 header
.deref
.cast_type_same_as_last
= deref
->type
== ctx
->last_type
;
902 unsigned var_idx
= 0;
903 if (deref
->deref_type
== nir_deref_type_var
) {
904 var_idx
= write_lookup_object(ctx
, deref
->var
);
905 if (var_idx
&& var_idx
< (1 << 16))
906 header
.deref_var
.object_idx
= var_idx
;
909 if (deref
->deref_type
== nir_deref_type_array
||
910 deref
->deref_type
== nir_deref_type_ptr_as_array
) {
911 header
.deref
.packed_src_ssa_16bit
=
912 deref
->parent
.is_ssa
&& deref
->arr
.index
.is_ssa
&&
913 are_object_ids_16bit(ctx
);
916 write_dest(ctx
, &deref
->dest
, header
, deref
->instr
.type
);
918 switch (deref
->deref_type
) {
919 case nir_deref_type_var
:
920 if (!header
.deref_var
.object_idx
)
921 blob_write_uint32(ctx
->blob
, var_idx
);
924 case nir_deref_type_struct
:
925 write_src(ctx
, &deref
->parent
);
926 blob_write_uint32(ctx
->blob
, deref
->strct
.index
);
929 case nir_deref_type_array
:
930 case nir_deref_type_ptr_as_array
:
931 if (header
.deref
.packed_src_ssa_16bit
) {
932 blob_write_uint16(ctx
->blob
,
933 write_lookup_object(ctx
, deref
->parent
.ssa
));
934 blob_write_uint16(ctx
->blob
,
935 write_lookup_object(ctx
, deref
->arr
.index
.ssa
));
937 write_src(ctx
, &deref
->parent
);
938 write_src(ctx
, &deref
->arr
.index
);
942 case nir_deref_type_cast
:
943 write_src(ctx
, &deref
->parent
);
944 blob_write_uint32(ctx
->blob
, deref
->cast
.ptr_stride
);
945 if (!header
.deref
.cast_type_same_as_last
) {
946 encode_type_to_blob(ctx
->blob
, deref
->type
);
947 ctx
->last_type
= deref
->type
;
951 case nir_deref_type_array_wildcard
:
952 write_src(ctx
, &deref
->parent
);
956 unreachable("Invalid deref type");
960 static nir_deref_instr
*
961 read_deref(read_ctx
*ctx
, union packed_instr header
)
963 nir_deref_type deref_type
= header
.deref
.deref_type
;
964 nir_deref_instr
*deref
= nir_deref_instr_create(ctx
->nir
, deref_type
);
966 read_dest(ctx
, &deref
->dest
, &deref
->instr
, header
);
968 nir_deref_instr
*parent
;
970 switch (deref
->deref_type
) {
971 case nir_deref_type_var
:
972 if (header
.deref_var
.object_idx
)
973 deref
->var
= read_lookup_object(ctx
, header
.deref_var
.object_idx
);
975 deref
->var
= read_object(ctx
);
977 deref
->type
= deref
->var
->type
;
980 case nir_deref_type_struct
:
981 read_src(ctx
, &deref
->parent
, &deref
->instr
);
982 parent
= nir_src_as_deref(deref
->parent
);
983 deref
->strct
.index
= blob_read_uint32(ctx
->blob
);
984 deref
->type
= glsl_get_struct_field(parent
->type
, deref
->strct
.index
);
987 case nir_deref_type_array
:
988 case nir_deref_type_ptr_as_array
:
989 if (header
.deref
.packed_src_ssa_16bit
) {
990 deref
->parent
.is_ssa
= true;
991 deref
->parent
.ssa
= read_lookup_object(ctx
, blob_read_uint16(ctx
->blob
));
992 deref
->arr
.index
.is_ssa
= true;
993 deref
->arr
.index
.ssa
= read_lookup_object(ctx
, blob_read_uint16(ctx
->blob
));
995 read_src(ctx
, &deref
->parent
, &deref
->instr
);
996 read_src(ctx
, &deref
->arr
.index
, &deref
->instr
);
999 parent
= nir_src_as_deref(deref
->parent
);
1000 if (deref
->deref_type
== nir_deref_type_array
)
1001 deref
->type
= glsl_get_array_element(parent
->type
);
1003 deref
->type
= parent
->type
;
1006 case nir_deref_type_cast
:
1007 read_src(ctx
, &deref
->parent
, &deref
->instr
);
1008 deref
->cast
.ptr_stride
= blob_read_uint32(ctx
->blob
);
1009 if (header
.deref
.cast_type_same_as_last
) {
1010 deref
->type
= ctx
->last_type
;
1012 deref
->type
= decode_type_from_blob(ctx
->blob
);
1013 ctx
->last_type
= deref
->type
;
1017 case nir_deref_type_array_wildcard
:
1018 read_src(ctx
, &deref
->parent
, &deref
->instr
);
1019 parent
= nir_src_as_deref(deref
->parent
);
1020 deref
->type
= glsl_get_array_element(parent
->type
);
1024 unreachable("Invalid deref type");
1027 if (deref_type
== nir_deref_type_var
) {
1028 deref
->mode
= deref
->var
->data
.mode
;
1029 } else if (deref
->deref_type
== nir_deref_type_cast
) {
1030 deref
->mode
= header
.deref
.mode
;
1032 assert(deref
->parent
.is_ssa
);
1033 deref
->mode
= nir_instr_as_deref(deref
->parent
.ssa
->parent_instr
)->mode
;
1040 write_intrinsic(write_ctx
*ctx
, const nir_intrinsic_instr
*intrin
)
1042 /* 9 bits for nir_intrinsic_op */
1043 STATIC_ASSERT(nir_num_intrinsics
<= 512);
1044 unsigned num_srcs
= nir_intrinsic_infos
[intrin
->intrinsic
].num_srcs
;
1045 unsigned num_indices
= nir_intrinsic_infos
[intrin
->intrinsic
].num_indices
;
1046 assert(intrin
->intrinsic
< 512);
1048 union packed_instr header
;
1051 header
.intrinsic
.instr_type
= intrin
->instr
.type
;
1052 header
.intrinsic
.intrinsic
= intrin
->intrinsic
;
1053 header
.intrinsic
.num_components
=
1054 encode_num_components_in_3bits(intrin
->num_components
);
1056 /* Analyze constant indices to decide how to encode them. */
1058 unsigned max_bits
= 0;
1059 for (unsigned i
= 0; i
< num_indices
; i
++) {
1060 unsigned max
= util_last_bit(intrin
->const_index
[i
]);
1061 max_bits
= MAX2(max_bits
, max
);
1064 if (max_bits
* num_indices
<= 6) {
1065 header
.intrinsic
.const_indices_encoding
= const_indices_6bit_all_combined
;
1067 /* Pack all const indices into 6 bits. */
1068 unsigned bit_size
= 6 / num_indices
;
1069 for (unsigned i
= 0; i
< num_indices
; i
++) {
1070 header
.intrinsic
.packed_const_indices
|=
1071 intrin
->const_index
[i
] << (i
* bit_size
);
1073 } else if (max_bits
<= 8)
1074 header
.intrinsic
.const_indices_encoding
= const_indices_8bit
;
1075 else if (max_bits
<= 16)
1076 header
.intrinsic
.const_indices_encoding
= const_indices_16bit
;
1078 header
.intrinsic
.const_indices_encoding
= const_indices_32bit
;
1081 if (nir_intrinsic_infos
[intrin
->intrinsic
].has_dest
)
1082 write_dest(ctx
, &intrin
->dest
, header
, intrin
->instr
.type
);
1084 blob_write_uint32(ctx
->blob
, header
.u32
);
1086 for (unsigned i
= 0; i
< num_srcs
; i
++)
1087 write_src(ctx
, &intrin
->src
[i
]);
1090 switch (header
.intrinsic
.const_indices_encoding
) {
1091 case const_indices_8bit
:
1092 for (unsigned i
= 0; i
< num_indices
; i
++)
1093 blob_write_uint8(ctx
->blob
, intrin
->const_index
[i
]);
1095 case const_indices_16bit
:
1096 for (unsigned i
= 0; i
< num_indices
; i
++)
1097 blob_write_uint16(ctx
->blob
, intrin
->const_index
[i
]);
1099 case const_indices_32bit
:
1100 for (unsigned i
= 0; i
< num_indices
; i
++)
1101 blob_write_uint32(ctx
->blob
, intrin
->const_index
[i
]);
1107 static nir_intrinsic_instr
*
1108 read_intrinsic(read_ctx
*ctx
, union packed_instr header
)
1110 nir_intrinsic_op op
= header
.intrinsic
.intrinsic
;
1111 nir_intrinsic_instr
*intrin
= nir_intrinsic_instr_create(ctx
->nir
, op
);
1113 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
1114 unsigned num_indices
= nir_intrinsic_infos
[op
].num_indices
;
1116 intrin
->num_components
=
1117 decode_num_components_in_3bits(header
.intrinsic
.num_components
);
1119 if (nir_intrinsic_infos
[op
].has_dest
)
1120 read_dest(ctx
, &intrin
->dest
, &intrin
->instr
, header
);
1122 for (unsigned i
= 0; i
< num_srcs
; i
++)
1123 read_src(ctx
, &intrin
->src
[i
], &intrin
->instr
);
1126 switch (header
.intrinsic
.const_indices_encoding
) {
1127 case const_indices_6bit_all_combined
: {
1128 unsigned bit_size
= 6 / num_indices
;
1129 unsigned bit_mask
= u_bit_consecutive(0, bit_size
);
1130 for (unsigned i
= 0; i
< num_indices
; i
++) {
1131 intrin
->const_index
[i
] =
1132 (header
.intrinsic
.packed_const_indices
>> (i
* bit_size
)) &
1137 case const_indices_8bit
:
1138 for (unsigned i
= 0; i
< num_indices
; i
++)
1139 intrin
->const_index
[i
] = blob_read_uint8(ctx
->blob
);
1141 case const_indices_16bit
:
1142 for (unsigned i
= 0; i
< num_indices
; i
++)
1143 intrin
->const_index
[i
] = blob_read_uint16(ctx
->blob
);
1145 case const_indices_32bit
:
1146 for (unsigned i
= 0; i
< num_indices
; i
++)
1147 intrin
->const_index
[i
] = blob_read_uint32(ctx
->blob
);
1156 write_load_const(write_ctx
*ctx
, const nir_load_const_instr
*lc
)
1158 assert(lc
->def
.num_components
>= 1 && lc
->def
.num_components
<= 16);
1159 union packed_instr header
;
1162 header
.load_const
.instr_type
= lc
->instr
.type
;
1163 header
.load_const
.last_component
= lc
->def
.num_components
- 1;
1164 header
.load_const
.bit_size
= encode_bit_size_3bits(lc
->def
.bit_size
);
1165 header
.load_const
.packing
= load_const_full
;
1167 /* Try to pack 1-component constants into the 19 free bits in the header. */
1168 if (lc
->def
.num_components
== 1) {
1169 switch (lc
->def
.bit_size
) {
1171 if ((lc
->value
[0].u64
& 0x1fffffffffffull
) == 0) {
1172 /* packed_value contains high 19 bits, low bits are 0 */
1173 header
.load_const
.packing
= load_const_scalar_hi_19bits
;
1174 header
.load_const
.packed_value
= lc
->value
[0].u64
>> 45;
1175 } else if (((lc
->value
[0].i64
<< 45) >> 45) == lc
->value
[0].i64
) {
1176 /* packed_value contains low 19 bits, high bits are sign-extended */
1177 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1178 header
.load_const
.packed_value
= lc
->value
[0].u64
;
1183 if ((lc
->value
[0].u32
& 0x1fff) == 0) {
1184 header
.load_const
.packing
= load_const_scalar_hi_19bits
;
1185 header
.load_const
.packed_value
= lc
->value
[0].u32
>> 13;
1186 } else if (((lc
->value
[0].i32
<< 13) >> 13) == lc
->value
[0].i32
) {
1187 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1188 header
.load_const
.packed_value
= lc
->value
[0].u32
;
1193 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1194 header
.load_const
.packed_value
= lc
->value
[0].u16
;
1197 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1198 header
.load_const
.packed_value
= lc
->value
[0].u8
;
1201 header
.load_const
.packing
= load_const_scalar_lo_19bits_sext
;
1202 header
.load_const
.packed_value
= lc
->value
[0].b
;
1205 unreachable("invalid bit_size");
1209 blob_write_uint32(ctx
->blob
, header
.u32
);
1211 if (header
.load_const
.packing
== load_const_full
) {
1212 switch (lc
->def
.bit_size
) {
1214 blob_write_bytes(ctx
->blob
, lc
->value
,
1215 sizeof(*lc
->value
) * lc
->def
.num_components
);
1219 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1220 blob_write_uint32(ctx
->blob
, lc
->value
[i
].u32
);
1224 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1225 blob_write_uint16(ctx
->blob
, lc
->value
[i
].u16
);
1229 assert(lc
->def
.bit_size
<= 8);
1230 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1231 blob_write_uint8(ctx
->blob
, lc
->value
[i
].u8
);
1236 write_add_object(ctx
, &lc
->def
);
1239 static nir_load_const_instr
*
1240 read_load_const(read_ctx
*ctx
, union packed_instr header
)
1242 nir_load_const_instr
*lc
=
1243 nir_load_const_instr_create(ctx
->nir
, header
.load_const
.last_component
+ 1,
1244 decode_bit_size_3bits(header
.load_const
.bit_size
));
1246 switch (header
.load_const
.packing
) {
1247 case load_const_scalar_hi_19bits
:
1248 switch (lc
->def
.bit_size
) {
1250 lc
->value
[0].u64
= (uint64_t)header
.load_const
.packed_value
<< 45;
1253 lc
->value
[0].u32
= (uint64_t)header
.load_const
.packed_value
<< 13;
1256 unreachable("invalid bit_size");
1260 case load_const_scalar_lo_19bits_sext
:
1261 switch (lc
->def
.bit_size
) {
1263 lc
->value
[0].i64
= ((int64_t)header
.load_const
.packed_value
<< 45) >> 45;
1266 lc
->value
[0].i32
= ((int32_t)header
.load_const
.packed_value
<< 13) >> 13;
1269 lc
->value
[0].u16
= header
.load_const
.packed_value
;
1272 lc
->value
[0].u8
= header
.load_const
.packed_value
;
1275 lc
->value
[0].b
= header
.load_const
.packed_value
;
1278 unreachable("invalid bit_size");
1282 case load_const_full
:
1283 switch (lc
->def
.bit_size
) {
1285 blob_copy_bytes(ctx
->blob
, lc
->value
, sizeof(*lc
->value
) * lc
->def
.num_components
);
1289 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1290 lc
->value
[i
].u32
= blob_read_uint32(ctx
->blob
);
1294 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1295 lc
->value
[i
].u16
= blob_read_uint16(ctx
->blob
);
1299 assert(lc
->def
.bit_size
<= 8);
1300 for (unsigned i
= 0; i
< lc
->def
.num_components
; i
++)
1301 lc
->value
[i
].u8
= blob_read_uint8(ctx
->blob
);
1307 read_add_object(ctx
, &lc
->def
);
1312 write_ssa_undef(write_ctx
*ctx
, const nir_ssa_undef_instr
*undef
)
1314 assert(undef
->def
.num_components
>= 1 && undef
->def
.num_components
<= 16);
1316 union packed_instr header
;
1319 header
.undef
.instr_type
= undef
->instr
.type
;
1320 header
.undef
.last_component
= undef
->def
.num_components
- 1;
1321 header
.undef
.bit_size
= encode_bit_size_3bits(undef
->def
.bit_size
);
1323 blob_write_uint32(ctx
->blob
, header
.u32
);
1324 write_add_object(ctx
, &undef
->def
);
1327 static nir_ssa_undef_instr
*
1328 read_ssa_undef(read_ctx
*ctx
, union packed_instr header
)
1330 nir_ssa_undef_instr
*undef
=
1331 nir_ssa_undef_instr_create(ctx
->nir
, header
.undef
.last_component
+ 1,
1332 decode_bit_size_3bits(header
.undef
.bit_size
));
1334 read_add_object(ctx
, &undef
->def
);
1338 union packed_tex_data
{
1341 enum glsl_sampler_dim sampler_dim
:4;
1342 nir_alu_type dest_type
:8;
1343 unsigned coord_components
:3;
1344 unsigned is_array
:1;
1345 unsigned is_shadow
:1;
1346 unsigned is_new_style_shadow
:1;
1347 unsigned component
:2;
1348 unsigned unused
:10; /* Mark unused for valgrind. */
1353 write_tex(write_ctx
*ctx
, const nir_tex_instr
*tex
)
1355 assert(tex
->num_srcs
< 16);
1356 assert(tex
->op
< 16);
1357 assert(tex
->texture_array_size
< 1024);
1359 union packed_instr header
;
1362 header
.tex
.instr_type
= tex
->instr
.type
;
1363 header
.tex
.num_srcs
= tex
->num_srcs
;
1364 header
.tex
.op
= tex
->op
;
1365 header
.tex
.texture_array_size
= tex
->texture_array_size
;
1367 write_dest(ctx
, &tex
->dest
, header
, tex
->instr
.type
);
1369 blob_write_uint32(ctx
->blob
, tex
->texture_index
);
1370 blob_write_uint32(ctx
->blob
, tex
->sampler_index
);
1371 if (tex
->op
== nir_texop_tg4
)
1372 blob_write_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
1374 STATIC_ASSERT(sizeof(union packed_tex_data
) == sizeof(uint32_t));
1375 union packed_tex_data packed
= {
1376 .u
.sampler_dim
= tex
->sampler_dim
,
1377 .u
.dest_type
= tex
->dest_type
,
1378 .u
.coord_components
= tex
->coord_components
,
1379 .u
.is_array
= tex
->is_array
,
1380 .u
.is_shadow
= tex
->is_shadow
,
1381 .u
.is_new_style_shadow
= tex
->is_new_style_shadow
,
1382 .u
.component
= tex
->component
,
1384 blob_write_uint32(ctx
->blob
, packed
.u32
);
1386 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1387 union packed_src src
;
1389 src
.tex
.src_type
= tex
->src
[i
].src_type
;
1390 write_src_full(ctx
, &tex
->src
[i
].src
, src
);
1394 static nir_tex_instr
*
1395 read_tex(read_ctx
*ctx
, union packed_instr header
)
1397 nir_tex_instr
*tex
= nir_tex_instr_create(ctx
->nir
, header
.tex
.num_srcs
);
1399 read_dest(ctx
, &tex
->dest
, &tex
->instr
, header
);
1401 tex
->op
= header
.tex
.op
;
1402 tex
->texture_index
= blob_read_uint32(ctx
->blob
);
1403 tex
->texture_array_size
= header
.tex
.texture_array_size
;
1404 tex
->sampler_index
= blob_read_uint32(ctx
->blob
);
1405 if (tex
->op
== nir_texop_tg4
)
1406 blob_copy_bytes(ctx
->blob
, tex
->tg4_offsets
, sizeof(tex
->tg4_offsets
));
1408 union packed_tex_data packed
;
1409 packed
.u32
= blob_read_uint32(ctx
->blob
);
1410 tex
->sampler_dim
= packed
.u
.sampler_dim
;
1411 tex
->dest_type
= packed
.u
.dest_type
;
1412 tex
->coord_components
= packed
.u
.coord_components
;
1413 tex
->is_array
= packed
.u
.is_array
;
1414 tex
->is_shadow
= packed
.u
.is_shadow
;
1415 tex
->is_new_style_shadow
= packed
.u
.is_new_style_shadow
;
1416 tex
->component
= packed
.u
.component
;
1418 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
1419 union packed_src src
= read_src(ctx
, &tex
->src
[i
].src
, &tex
->instr
);
1420 tex
->src
[i
].src_type
= src
.tex
.src_type
;
1427 write_phi(write_ctx
*ctx
, const nir_phi_instr
*phi
)
1429 union packed_instr header
;
1432 header
.phi
.instr_type
= phi
->instr
.type
;
1433 header
.phi
.num_srcs
= exec_list_length(&phi
->srcs
);
1435 /* Phi nodes are special, since they may reference SSA definitions and
1436 * basic blocks that don't exist yet. We leave two empty uint32_t's here,
1437 * and then store enough information so that a later fixup pass can fill
1438 * them in correctly.
1440 write_dest(ctx
, &phi
->dest
, header
, phi
->instr
.type
);
1442 nir_foreach_phi_src(src
, phi
) {
1443 assert(src
->src
.is_ssa
);
1444 size_t blob_offset
= blob_reserve_uint32(ctx
->blob
);
1445 ASSERTED
size_t blob_offset2
= blob_reserve_uint32(ctx
->blob
);
1446 assert(blob_offset
+ sizeof(uint32_t) == blob_offset2
);
1447 write_phi_fixup fixup
= {
1448 .blob_offset
= blob_offset
,
1449 .src
= src
->src
.ssa
,
1452 util_dynarray_append(&ctx
->phi_fixups
, write_phi_fixup
, fixup
);
1457 write_fixup_phis(write_ctx
*ctx
)
1459 util_dynarray_foreach(&ctx
->phi_fixups
, write_phi_fixup
, fixup
) {
1460 uint32_t *blob_ptr
= (uint32_t *)(ctx
->blob
->data
+ fixup
->blob_offset
);
1461 blob_ptr
[0] = write_lookup_object(ctx
, fixup
->src
);
1462 blob_ptr
[1] = write_lookup_object(ctx
, fixup
->block
);
1465 util_dynarray_clear(&ctx
->phi_fixups
);
1468 static nir_phi_instr
*
1469 read_phi(read_ctx
*ctx
, nir_block
*blk
, union packed_instr header
)
1471 nir_phi_instr
*phi
= nir_phi_instr_create(ctx
->nir
);
1473 read_dest(ctx
, &phi
->dest
, &phi
->instr
, header
);
1475 /* For similar reasons as before, we just store the index directly into the
1476 * pointer, and let a later pass resolve the phi sources.
1478 * In order to ensure that the copied sources (which are just the indices
1479 * from the blob for now) don't get inserted into the old shader's use-def
1480 * lists, we have to add the phi instruction *before* we set up its
1483 nir_instr_insert_after_block(blk
, &phi
->instr
);
1485 for (unsigned i
= 0; i
< header
.phi
.num_srcs
; i
++) {
1486 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
1488 src
->src
.is_ssa
= true;
1489 src
->src
.ssa
= (nir_ssa_def
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
1490 src
->pred
= (nir_block
*)(uintptr_t) blob_read_uint32(ctx
->blob
);
1492 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
1493 * we have to set the parent_instr manually. It doesn't really matter
1494 * when we do it, so we might as well do it here.
1496 src
->src
.parent_instr
= &phi
->instr
;
1498 /* Stash it in the list of phi sources. We'll walk this list and fix up
1499 * sources at the very end of read_function_impl.
1501 list_add(&src
->src
.use_link
, &ctx
->phi_srcs
);
1503 exec_list_push_tail(&phi
->srcs
, &src
->node
);
1510 read_fixup_phis(read_ctx
*ctx
)
1512 list_for_each_entry_safe(nir_phi_src
, src
, &ctx
->phi_srcs
, src
.use_link
) {
1513 src
->pred
= read_lookup_object(ctx
, (uintptr_t)src
->pred
);
1514 src
->src
.ssa
= read_lookup_object(ctx
, (uintptr_t)src
->src
.ssa
);
1516 /* Remove from this list */
1517 list_del(&src
->src
.use_link
);
1519 list_addtail(&src
->src
.use_link
, &src
->src
.ssa
->uses
);
1521 assert(list_is_empty(&ctx
->phi_srcs
));
1525 write_jump(write_ctx
*ctx
, const nir_jump_instr
*jmp
)
1527 assert(jmp
->type
< 4);
1529 union packed_instr header
;
1532 header
.jump
.instr_type
= jmp
->instr
.type
;
1533 header
.jump
.type
= jmp
->type
;
1535 blob_write_uint32(ctx
->blob
, header
.u32
);
1538 static nir_jump_instr
*
1539 read_jump(read_ctx
*ctx
, union packed_instr header
)
1541 nir_jump_instr
*jmp
= nir_jump_instr_create(ctx
->nir
, header
.jump
.type
);
1546 write_call(write_ctx
*ctx
, const nir_call_instr
*call
)
1548 blob_write_uint32(ctx
->blob
, write_lookup_object(ctx
, call
->callee
));
1550 for (unsigned i
= 0; i
< call
->num_params
; i
++)
1551 write_src(ctx
, &call
->params
[i
]);
1554 static nir_call_instr
*
1555 read_call(read_ctx
*ctx
)
1557 nir_function
*callee
= read_object(ctx
);
1558 nir_call_instr
*call
= nir_call_instr_create(ctx
->nir
, callee
);
1560 for (unsigned i
= 0; i
< call
->num_params
; i
++)
1561 read_src(ctx
, &call
->params
[i
], call
);
1567 write_instr(write_ctx
*ctx
, const nir_instr
*instr
)
1569 /* We have only 4 bits for the instruction type. */
1570 assert(instr
->type
< 16);
1572 switch (instr
->type
) {
1573 case nir_instr_type_alu
:
1574 write_alu(ctx
, nir_instr_as_alu(instr
));
1576 case nir_instr_type_deref
:
1577 write_deref(ctx
, nir_instr_as_deref(instr
));
1579 case nir_instr_type_intrinsic
:
1580 write_intrinsic(ctx
, nir_instr_as_intrinsic(instr
));
1582 case nir_instr_type_load_const
:
1583 write_load_const(ctx
, nir_instr_as_load_const(instr
));
1585 case nir_instr_type_ssa_undef
:
1586 write_ssa_undef(ctx
, nir_instr_as_ssa_undef(instr
));
1588 case nir_instr_type_tex
:
1589 write_tex(ctx
, nir_instr_as_tex(instr
));
1591 case nir_instr_type_phi
:
1592 write_phi(ctx
, nir_instr_as_phi(instr
));
1594 case nir_instr_type_jump
:
1595 write_jump(ctx
, nir_instr_as_jump(instr
));
1597 case nir_instr_type_call
:
1598 blob_write_uint32(ctx
->blob
, instr
->type
);
1599 write_call(ctx
, nir_instr_as_call(instr
));
1601 case nir_instr_type_parallel_copy
:
1602 unreachable("Cannot write parallel copies");
1604 unreachable("bad instr type");
1608 /* Return the number of instructions read. */
1610 read_instr(read_ctx
*ctx
, nir_block
*block
)
1612 STATIC_ASSERT(sizeof(union packed_instr
) == 4);
1613 union packed_instr header
;
1614 header
.u32
= blob_read_uint32(ctx
->blob
);
1617 switch (header
.any
.instr_type
) {
1618 case nir_instr_type_alu
:
1619 for (unsigned i
= 0; i
<= header
.alu
.num_followup_alu_sharing_header
; i
++)
1620 nir_instr_insert_after_block(block
, &read_alu(ctx
, header
)->instr
);
1621 return header
.alu
.num_followup_alu_sharing_header
+ 1;
1622 case nir_instr_type_deref
:
1623 instr
= &read_deref(ctx
, header
)->instr
;
1625 case nir_instr_type_intrinsic
:
1626 instr
= &read_intrinsic(ctx
, header
)->instr
;
1628 case nir_instr_type_load_const
:
1629 instr
= &read_load_const(ctx
, header
)->instr
;
1631 case nir_instr_type_ssa_undef
:
1632 instr
= &read_ssa_undef(ctx
, header
)->instr
;
1634 case nir_instr_type_tex
:
1635 instr
= &read_tex(ctx
, header
)->instr
;
1637 case nir_instr_type_phi
:
1638 /* Phi instructions are a bit of a special case when reading because we
1639 * don't want inserting the instruction to automatically handle use/defs
1640 * for us. Instead, we need to wait until all the blocks/instructions
1641 * are read so that we can set their sources up.
1643 read_phi(ctx
, block
, header
);
1645 case nir_instr_type_jump
:
1646 instr
= &read_jump(ctx
, header
)->instr
;
1648 case nir_instr_type_call
:
1649 instr
= &read_call(ctx
)->instr
;
1651 case nir_instr_type_parallel_copy
:
1652 unreachable("Cannot read parallel copies");
1654 unreachable("bad instr type");
1657 nir_instr_insert_after_block(block
, instr
);
1662 write_block(write_ctx
*ctx
, const nir_block
*block
)
1664 write_add_object(ctx
, block
);
1665 blob_write_uint32(ctx
->blob
, exec_list_length(&block
->instr_list
));
1667 ctx
->last_instr_type
= ~0;
1668 ctx
->last_alu_header_offset
= 0;
1670 nir_foreach_instr(instr
, block
) {
1671 write_instr(ctx
, instr
);
1672 ctx
->last_instr_type
= instr
->type
;
1677 read_block(read_ctx
*ctx
, struct exec_list
*cf_list
)
1679 /* Don't actually create a new block. Just use the one from the tail of
1680 * the list. NIR guarantees that the tail of the list is a block and that
1681 * no two blocks are side-by-side in the IR; It should be empty.
1684 exec_node_data(nir_block
, exec_list_get_tail(cf_list
), cf_node
.node
);
1686 read_add_object(ctx
, block
);
1687 unsigned num_instrs
= blob_read_uint32(ctx
->blob
);
1688 for (unsigned i
= 0; i
< num_instrs
;) {
1689 i
+= read_instr(ctx
, block
);
1694 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
);
1697 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
);
1700 write_if(write_ctx
*ctx
, nir_if
*nif
)
1702 write_src(ctx
, &nif
->condition
);
1704 write_cf_list(ctx
, &nif
->then_list
);
1705 write_cf_list(ctx
, &nif
->else_list
);
1709 read_if(read_ctx
*ctx
, struct exec_list
*cf_list
)
1711 nir_if
*nif
= nir_if_create(ctx
->nir
);
1713 read_src(ctx
, &nif
->condition
, nif
);
1715 nir_cf_node_insert_end(cf_list
, &nif
->cf_node
);
1717 read_cf_list(ctx
, &nif
->then_list
);
1718 read_cf_list(ctx
, &nif
->else_list
);
1722 write_loop(write_ctx
*ctx
, nir_loop
*loop
)
1724 write_cf_list(ctx
, &loop
->body
);
1728 read_loop(read_ctx
*ctx
, struct exec_list
*cf_list
)
1730 nir_loop
*loop
= nir_loop_create(ctx
->nir
);
1732 nir_cf_node_insert_end(cf_list
, &loop
->cf_node
);
1734 read_cf_list(ctx
, &loop
->body
);
1738 write_cf_node(write_ctx
*ctx
, nir_cf_node
*cf
)
1740 blob_write_uint32(ctx
->blob
, cf
->type
);
1743 case nir_cf_node_block
:
1744 write_block(ctx
, nir_cf_node_as_block(cf
));
1746 case nir_cf_node_if
:
1747 write_if(ctx
, nir_cf_node_as_if(cf
));
1749 case nir_cf_node_loop
:
1750 write_loop(ctx
, nir_cf_node_as_loop(cf
));
1753 unreachable("bad cf type");
1758 read_cf_node(read_ctx
*ctx
, struct exec_list
*list
)
1760 nir_cf_node_type type
= blob_read_uint32(ctx
->blob
);
1763 case nir_cf_node_block
:
1764 read_block(ctx
, list
);
1766 case nir_cf_node_if
:
1769 case nir_cf_node_loop
:
1770 read_loop(ctx
, list
);
1773 unreachable("bad cf type");
1778 write_cf_list(write_ctx
*ctx
, const struct exec_list
*cf_list
)
1780 blob_write_uint32(ctx
->blob
, exec_list_length(cf_list
));
1781 foreach_list_typed(nir_cf_node
, cf
, node
, cf_list
) {
1782 write_cf_node(ctx
, cf
);
1787 read_cf_list(read_ctx
*ctx
, struct exec_list
*cf_list
)
1789 uint32_t num_cf_nodes
= blob_read_uint32(ctx
->blob
);
1790 for (unsigned i
= 0; i
< num_cf_nodes
; i
++)
1791 read_cf_node(ctx
, cf_list
);
1795 write_function_impl(write_ctx
*ctx
, const nir_function_impl
*fi
)
1797 write_var_list(ctx
, &fi
->locals
);
1798 write_reg_list(ctx
, &fi
->registers
);
1799 blob_write_uint32(ctx
->blob
, fi
->reg_alloc
);
1801 write_cf_list(ctx
, &fi
->body
);
1802 write_fixup_phis(ctx
);
1805 static nir_function_impl
*
1806 read_function_impl(read_ctx
*ctx
, nir_function
*fxn
)
1808 nir_function_impl
*fi
= nir_function_impl_create_bare(ctx
->nir
);
1811 read_var_list(ctx
, &fi
->locals
);
1812 read_reg_list(ctx
, &fi
->registers
);
1813 fi
->reg_alloc
= blob_read_uint32(ctx
->blob
);
1815 read_cf_list(ctx
, &fi
->body
);
1816 read_fixup_phis(ctx
);
1818 fi
->valid_metadata
= 0;
1824 write_function(write_ctx
*ctx
, const nir_function
*fxn
)
1826 uint32_t flags
= fxn
->is_entrypoint
;
1831 blob_write_uint32(ctx
->blob
, flags
);
1833 blob_write_string(ctx
->blob
, fxn
->name
);
1835 write_add_object(ctx
, fxn
);
1837 blob_write_uint32(ctx
->blob
, fxn
->num_params
);
1838 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1840 ((uint32_t)fxn
->params
[i
].num_components
) |
1841 ((uint32_t)fxn
->params
[i
].bit_size
) << 8;
1842 blob_write_uint32(ctx
->blob
, val
);
1845 /* At first glance, it looks like we should write the function_impl here.
1846 * However, call instructions need to be able to reference at least the
1847 * function and those will get processed as we write the function_impls.
1848 * We stop here and write function_impls as a second pass.
1853 read_function(read_ctx
*ctx
)
1855 uint32_t flags
= blob_read_uint32(ctx
->blob
);
1856 bool has_name
= flags
& 0x2;
1857 char *name
= has_name
? blob_read_string(ctx
->blob
) : NULL
;
1859 nir_function
*fxn
= nir_function_create(ctx
->nir
, name
);
1861 read_add_object(ctx
, fxn
);
1863 fxn
->num_params
= blob_read_uint32(ctx
->blob
);
1864 fxn
->params
= ralloc_array(fxn
, nir_parameter
, fxn
->num_params
);
1865 for (unsigned i
= 0; i
< fxn
->num_params
; i
++) {
1866 uint32_t val
= blob_read_uint32(ctx
->blob
);
1867 fxn
->params
[i
].num_components
= val
& 0xff;
1868 fxn
->params
[i
].bit_size
= (val
>> 8) & 0xff;
1871 fxn
->is_entrypoint
= flags
& 0x1;
1873 fxn
->impl
= NIR_SERIALIZE_FUNC_HAS_IMPL
;
1877 * Serialize NIR into a binary blob.
1879 * \param strip Don't serialize information only useful for debugging,
1880 * such as variable names, making cache hits from similar
1881 * shaders more likely.
1884 nir_serialize(struct blob
*blob
, const nir_shader
*nir
, bool strip
)
1886 write_ctx ctx
= {0};
1887 ctx
.remap_table
= _mesa_pointer_hash_table_create(NULL
);
1891 util_dynarray_init(&ctx
.phi_fixups
, NULL
);
1893 size_t idx_size_offset
= blob_reserve_uint32(blob
);
1895 struct shader_info info
= nir
->info
;
1896 uint32_t strings
= 0;
1897 if (!strip
&& info
.name
)
1899 if (!strip
&& info
.label
)
1901 blob_write_uint32(blob
, strings
);
1902 if (!strip
&& info
.name
)
1903 blob_write_string(blob
, info
.name
);
1904 if (!strip
&& info
.label
)
1905 blob_write_string(blob
, info
.label
);
1906 info
.name
= info
.label
= NULL
;
1907 blob_write_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1909 write_var_list(&ctx
, &nir
->uniforms
);
1910 write_var_list(&ctx
, &nir
->inputs
);
1911 write_var_list(&ctx
, &nir
->outputs
);
1912 write_var_list(&ctx
, &nir
->shared
);
1913 write_var_list(&ctx
, &nir
->globals
);
1914 write_var_list(&ctx
, &nir
->system_values
);
1916 blob_write_uint32(blob
, nir
->num_inputs
);
1917 blob_write_uint32(blob
, nir
->num_uniforms
);
1918 blob_write_uint32(blob
, nir
->num_outputs
);
1919 blob_write_uint32(blob
, nir
->num_shared
);
1920 blob_write_uint32(blob
, nir
->scratch_size
);
1922 blob_write_uint32(blob
, exec_list_length(&nir
->functions
));
1923 nir_foreach_function(fxn
, nir
) {
1924 write_function(&ctx
, fxn
);
1927 nir_foreach_function(fxn
, nir
) {
1929 write_function_impl(&ctx
, fxn
->impl
);
1932 blob_write_uint32(blob
, nir
->constant_data_size
);
1933 if (nir
->constant_data_size
> 0)
1934 blob_write_bytes(blob
, nir
->constant_data
, nir
->constant_data_size
);
1936 *(uint32_t *)(blob
->data
+ idx_size_offset
) = ctx
.next_idx
;
1938 _mesa_hash_table_destroy(ctx
.remap_table
, NULL
);
1939 util_dynarray_fini(&ctx
.phi_fixups
);
1943 nir_deserialize(void *mem_ctx
,
1944 const struct nir_shader_compiler_options
*options
,
1945 struct blob_reader
*blob
)
1949 list_inithead(&ctx
.phi_srcs
);
1950 ctx
.idx_table_len
= blob_read_uint32(blob
);
1951 ctx
.idx_table
= calloc(ctx
.idx_table_len
, sizeof(uintptr_t));
1953 uint32_t strings
= blob_read_uint32(blob
);
1954 char *name
= (strings
& 0x1) ? blob_read_string(blob
) : NULL
;
1955 char *label
= (strings
& 0x2) ? blob_read_string(blob
) : NULL
;
1957 struct shader_info info
;
1958 blob_copy_bytes(blob
, (uint8_t *) &info
, sizeof(info
));
1960 ctx
.nir
= nir_shader_create(mem_ctx
, info
.stage
, options
, NULL
);
1962 info
.name
= name
? ralloc_strdup(ctx
.nir
, name
) : NULL
;
1963 info
.label
= label
? ralloc_strdup(ctx
.nir
, label
) : NULL
;
1965 ctx
.nir
->info
= info
;
1967 read_var_list(&ctx
, &ctx
.nir
->uniforms
);
1968 read_var_list(&ctx
, &ctx
.nir
->inputs
);
1969 read_var_list(&ctx
, &ctx
.nir
->outputs
);
1970 read_var_list(&ctx
, &ctx
.nir
->shared
);
1971 read_var_list(&ctx
, &ctx
.nir
->globals
);
1972 read_var_list(&ctx
, &ctx
.nir
->system_values
);
1974 ctx
.nir
->num_inputs
= blob_read_uint32(blob
);
1975 ctx
.nir
->num_uniforms
= blob_read_uint32(blob
);
1976 ctx
.nir
->num_outputs
= blob_read_uint32(blob
);
1977 ctx
.nir
->num_shared
= blob_read_uint32(blob
);
1978 ctx
.nir
->scratch_size
= blob_read_uint32(blob
);
1980 unsigned num_functions
= blob_read_uint32(blob
);
1981 for (unsigned i
= 0; i
< num_functions
; i
++)
1982 read_function(&ctx
);
1984 nir_foreach_function(fxn
, ctx
.nir
) {
1985 if (fxn
->impl
== NIR_SERIALIZE_FUNC_HAS_IMPL
)
1986 fxn
->impl
= read_function_impl(&ctx
, fxn
);
1989 ctx
.nir
->constant_data_size
= blob_read_uint32(blob
);
1990 if (ctx
.nir
->constant_data_size
> 0) {
1991 ctx
.nir
->constant_data
=
1992 ralloc_size(ctx
.nir
, ctx
.nir
->constant_data_size
);
1993 blob_copy_bytes(blob
, ctx
.nir
->constant_data
,
1994 ctx
.nir
->constant_data_size
);
1997 free(ctx
.idx_table
);
2003 nir_shader_serialize_deserialize(nir_shader
*shader
)
2005 const struct nir_shader_compiler_options
*options
= shader
->options
;
2009 nir_serialize(&writer
, shader
, false);
2011 /* Delete all of dest's ralloc children but leave dest alone */
2012 void *dead_ctx
= ralloc_context(NULL
);
2013 ralloc_adopt(dead_ctx
, shader
);
2014 ralloc_free(dead_ctx
);
2016 dead_ctx
= ralloc_context(NULL
);
2018 struct blob_reader reader
;
2019 blob_reader_init(&reader
, writer
.data
, writer
.size
);
2020 nir_shader
*copy
= nir_deserialize(dead_ctx
, options
, &reader
);
2022 blob_finish(&writer
);
2024 nir_shader_replace(shader
, copy
);
2025 ralloc_free(dead_ctx
);