nir,spirv: Rework function calls
[mesa.git] / src / compiler / nir / nir_serialize.c
1 /*
2 * Copyright © 2017 Connor Abbott
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir_serialize.h"
25 #include "nir_control_flow.h"
26 #include "util/u_dynarray.h"
27
28 typedef struct {
29 size_t blob_offset;
30 nir_ssa_def *src;
31 nir_block *block;
32 } write_phi_fixup;
33
34 typedef struct {
35 const nir_shader *nir;
36
37 struct blob *blob;
38
39 /* maps pointer to index */
40 struct hash_table *remap_table;
41
42 /* the next index to assign to a NIR in-memory object */
43 uintptr_t next_idx;
44
45 /* Array of write_phi_fixup structs representing phi sources that need to
46 * be resolved in the second pass.
47 */
48 struct util_dynarray phi_fixups;
49 } write_ctx;
50
51 typedef struct {
52 nir_shader *nir;
53
54 struct blob_reader *blob;
55
56 /* the next index to assign to a NIR in-memory object */
57 uintptr_t next_idx;
58
59 /* The length of the index -> object table */
60 uintptr_t idx_table_len;
61
62 /* map from index to deserialized pointer */
63 void **idx_table;
64
65 /* List of phi sources. */
66 struct list_head phi_srcs;
67
68 } read_ctx;
69
70 static void
71 write_add_object(write_ctx *ctx, const void *obj)
72 {
73 uintptr_t index = ctx->next_idx++;
74 _mesa_hash_table_insert(ctx->remap_table, obj, (void *) index);
75 }
76
77 static uintptr_t
78 write_lookup_object(write_ctx *ctx, const void *obj)
79 {
80 struct hash_entry *entry = _mesa_hash_table_search(ctx->remap_table, obj);
81 assert(entry);
82 return (uintptr_t) entry->data;
83 }
84
85 static void
86 write_object(write_ctx *ctx, const void *obj)
87 {
88 blob_write_intptr(ctx->blob, write_lookup_object(ctx, obj));
89 }
90
91 static void
92 read_add_object(read_ctx *ctx, void *obj)
93 {
94 assert(ctx->next_idx < ctx->idx_table_len);
95 ctx->idx_table[ctx->next_idx++] = obj;
96 }
97
98 static void *
99 read_lookup_object(read_ctx *ctx, uintptr_t idx)
100 {
101 assert(idx < ctx->idx_table_len);
102 return ctx->idx_table[idx];
103 }
104
105 static void *
106 read_object(read_ctx *ctx)
107 {
108 return read_lookup_object(ctx, blob_read_intptr(ctx->blob));
109 }
110
111 static void
112 write_constant(write_ctx *ctx, const nir_constant *c)
113 {
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]);
118 }
119
120 static nir_constant *
121 read_constant(read_ctx *ctx, nir_variable *nvar)
122 {
123 nir_constant *c = ralloc(nvar, nir_constant);
124
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);
130
131 return c;
132 }
133
134 static void
135 write_variable(write_ctx *ctx, const nir_variable *var)
136 {
137 write_add_object(ctx, var);
138 encode_type_to_blob(ctx->blob, var->type);
139 blob_write_uint32(ctx->blob, !!(var->name));
140 if (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));
156 }
157 }
158
159 static nir_variable *
160 read_variable(read_ctx *ctx)
161 {
162 nir_variable *var = rzalloc(ctx->nir, nir_variable);
163 read_add_object(ctx, var);
164
165 var->type = decode_type_from_blob(ctx->blob);
166 bool has_name = blob_read_uint32(ctx->blob);
167 if (has_name) {
168 const char *name = blob_read_string(ctx->blob);
169 var->name = ralloc_strdup(var, name);
170 } else {
171 var->name = NULL;
172 }
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);
181 else
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);
186 else
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,
191 var->num_members);
192 blob_copy_bytes(ctx->blob, (uint8_t *) var->members,
193 var->num_members * sizeof(*var->members));
194 }
195
196 return var;
197 }
198
199 static void
200 write_var_list(write_ctx *ctx, const struct exec_list *src)
201 {
202 blob_write_uint32(ctx->blob, exec_list_length(src));
203 foreach_list_typed(nir_variable, var, node, src) {
204 write_variable(ctx, var);
205 }
206 }
207
208 static void
209 read_var_list(read_ctx *ctx, struct exec_list *dst)
210 {
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);
216 }
217 }
218
219 static void
220 write_register(write_ctx *ctx, const nir_register *reg)
221 {
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));
228 if (reg->name)
229 blob_write_string(ctx->blob, reg->name);
230 blob_write_uint32(ctx->blob, reg->is_global << 1 | reg->is_packed);
231 }
232
233 static nir_register *
234 read_register(read_ctx *ctx)
235 {
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);
243 if (has_name) {
244 const char *name = blob_read_string(ctx->blob);
245 reg->name = ralloc_strdup(reg, name);
246 } else {
247 reg->name = NULL;
248 }
249 unsigned flags = blob_read_uint32(ctx->blob);
250 reg->is_global = flags & 0x2;
251 reg->is_packed = flags & 0x1;
252
253 list_inithead(&reg->uses);
254 list_inithead(&reg->defs);
255 list_inithead(&reg->if_uses);
256
257 return reg;
258 }
259
260 static void
261 write_reg_list(write_ctx *ctx, const struct exec_list *src)
262 {
263 blob_write_uint32(ctx->blob, exec_list_length(src));
264 foreach_list_typed(nir_register, reg, node, src)
265 write_register(ctx, reg);
266 }
267
268 static void
269 read_reg_list(read_ctx *ctx, struct exec_list *dst)
270 {
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, &reg->node);
276 }
277 }
278
279 static void
280 write_src(write_ctx *ctx, const nir_src *src)
281 {
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!
287 */
288 if (src->is_ssa) {
289 uintptr_t idx = write_lookup_object(ctx, src->ssa) << 2;
290 idx |= 1;
291 blob_write_intptr(ctx->blob, idx);
292 } else {
293 uintptr_t idx = write_lookup_object(ctx, src->reg.reg) << 2;
294 if (src->reg.indirect)
295 idx |= 2;
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);
300 }
301 }
302 }
303
304 static void
305 read_src(read_ctx *ctx, nir_src *src, void *mem_ctx)
306 {
307 uintptr_t val = blob_read_intptr(ctx->blob);
308 uintptr_t idx = val >> 2;
309 src->is_ssa = val & 0x1;
310 if (src->is_ssa) {
311 src->ssa = read_lookup_object(ctx, idx);
312 } else {
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);
316 if (is_indirect) {
317 src->reg.indirect = ralloc(mem_ctx, nir_src);
318 read_src(ctx, src->reg.indirect, mem_ctx);
319 } else {
320 src->reg.indirect = NULL;
321 }
322 }
323 }
324
325 static void
326 write_dest(write_ctx *ctx, const nir_dest *dst)
327 {
328 uint32_t val = dst->is_ssa;
329 if (dst->is_ssa) {
330 val |= !!(dst->ssa.name) << 1;
331 val |= dst->ssa.num_components << 2;
332 val |= dst->ssa.bit_size << 5;
333 } else {
334 val |= !!(dst->reg.indirect) << 1;
335 }
336 blob_write_uint32(ctx->blob, val);
337 if (dst->is_ssa) {
338 write_add_object(ctx, &dst->ssa);
339 if (dst->ssa.name)
340 blob_write_string(ctx->blob, dst->ssa.name);
341 } else {
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);
346 }
347 }
348
349 static void
350 read_dest(read_ctx *ctx, nir_dest *dst, nir_instr *instr)
351 {
352 uint32_t val = blob_read_uint32(ctx->blob);
353 bool is_ssa = val & 0x1;
354 if (is_ssa) {
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);
361 } else {
362 bool is_indirect = val & 0x2;
363 dst->reg.reg = read_object(ctx);
364 dst->reg.base_offset = blob_read_uint32(ctx->blob);
365 if (is_indirect) {
366 dst->reg.indirect = ralloc(instr, nir_src);
367 read_src(ctx, dst->reg.indirect, instr);
368 }
369 }
370 }
371
372 static void
373 write_deref_chain(write_ctx *ctx, const nir_deref_var *deref_var)
374 {
375 write_object(ctx, deref_var->var);
376
377 uint32_t len = 0;
378 for (const nir_deref *d = deref_var->deref.child; d; d = d->child)
379 len++;
380 blob_write_uint32(ctx->blob, len);
381
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);
391 break;
392 }
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);
396 break;
397 }
398 case nir_deref_type_var:
399 unreachable("Invalid deref type");
400 }
401
402 encode_type_to_blob(ctx->blob, d->type);
403 }
404 }
405
406 static nir_deref_var *
407 read_deref_chain(read_ctx *ctx, void *mem_ctx)
408 {
409 nir_variable *var = read_object(ctx);
410 nir_deref_var *deref_var = nir_deref_var_create(mem_ctx, var);
411
412 uint32_t len = blob_read_uint32(ctx->blob);
413
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;
426 break;
427 }
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;
432 break;
433 }
434 case nir_deref_type_var:
435 unreachable("Invalid deref type");
436 }
437
438 deref->type = decode_type_from_blob(ctx->blob);
439
440 tail->child = deref;
441 tail = deref;
442 }
443
444 return deref_var;
445 }
446
447 static void
448 write_alu(write_ctx *ctx, const nir_alu_instr *alu)
449 {
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);
455
456 write_dest(ctx, &alu->dest.dest);
457
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);
465 }
466 }
467
468 static nir_alu_instr *
469 read_alu(read_ctx *ctx)
470 {
471 nir_op op = blob_read_uint32(ctx->blob);
472 nir_alu_instr *alu = nir_alu_instr_create(ctx->nir, op);
473
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;
478
479 read_dest(ctx, &alu->dest.dest, &alu->instr);
480
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;
488 }
489
490 return alu;
491 }
492
493 static void
494 write_deref(write_ctx *ctx, const nir_deref_instr *deref)
495 {
496 blob_write_uint32(ctx->blob, deref->deref_type);
497
498 blob_write_uint32(ctx->blob, deref->mode);
499 encode_type_to_blob(ctx->blob, deref->type);
500
501 write_dest(ctx, &deref->dest);
502
503 if (deref->deref_type == nir_deref_type_var) {
504 write_object(ctx, deref->var);
505 return;
506 }
507
508 write_src(ctx, &deref->parent);
509
510 switch (deref->deref_type) {
511 case nir_deref_type_struct:
512 blob_write_uint32(ctx->blob, deref->strct.index);
513 break;
514
515 case nir_deref_type_array:
516 write_src(ctx, &deref->arr.index);
517 break;
518
519 case nir_deref_type_array_wildcard:
520 case nir_deref_type_cast:
521 /* Nothing to do */
522 break;
523
524 default:
525 unreachable("Invalid deref type");
526 }
527 }
528
529 static nir_deref_instr *
530 read_deref(read_ctx *ctx)
531 {
532 nir_deref_type deref_type = blob_read_uint32(ctx->blob);
533 nir_deref_instr *deref = nir_deref_instr_create(ctx->nir, deref_type);
534
535 deref->mode = blob_read_uint32(ctx->blob);
536 deref->type = decode_type_from_blob(ctx->blob);
537
538 read_dest(ctx, &deref->dest, &deref->instr);
539
540 if (deref_type == nir_deref_type_var) {
541 deref->var = read_object(ctx);
542 return deref;
543 }
544
545 read_src(ctx, &deref->parent, &deref->instr);
546
547 switch (deref->deref_type) {
548 case nir_deref_type_struct:
549 deref->strct.index = blob_read_uint32(ctx->blob);
550 break;
551
552 case nir_deref_type_array:
553 read_src(ctx, &deref->arr.index, &deref->instr);
554 break;
555
556 case nir_deref_type_array_wildcard:
557 case nir_deref_type_cast:
558 /* Nothing to do */
559 break;
560
561 default:
562 unreachable("Invalid deref type");
563 }
564
565 return deref;
566 }
567
568 static void
569 write_intrinsic(write_ctx *ctx, const nir_intrinsic_instr *intrin)
570 {
571 blob_write_uint32(ctx->blob, intrin->intrinsic);
572
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;
576
577 blob_write_uint32(ctx->blob, intrin->num_components);
578
579 if (nir_intrinsic_infos[intrin->intrinsic].has_dest)
580 write_dest(ctx, &intrin->dest);
581
582 for (unsigned i = 0; i < num_variables; i++)
583 write_deref_chain(ctx, intrin->variables[i]);
584
585 for (unsigned i = 0; i < num_srcs; i++)
586 write_src(ctx, &intrin->src[i]);
587
588 for (unsigned i = 0; i < num_indices; i++)
589 blob_write_uint32(ctx->blob, intrin->const_index[i]);
590 }
591
592 static nir_intrinsic_instr *
593 read_intrinsic(read_ctx *ctx)
594 {
595 nir_intrinsic_op op = blob_read_uint32(ctx->blob);
596
597 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(ctx->nir, op);
598
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;
602
603 intrin->num_components = blob_read_uint32(ctx->blob);
604
605 if (nir_intrinsic_infos[op].has_dest)
606 read_dest(ctx, &intrin->dest, &intrin->instr);
607
608 for (unsigned i = 0; i < num_variables; i++)
609 intrin->variables[i] = read_deref_chain(ctx, &intrin->instr);
610
611 for (unsigned i = 0; i < num_srcs; i++)
612 read_src(ctx, &intrin->src[i], &intrin->instr);
613
614 for (unsigned i = 0; i < num_indices; i++)
615 intrin->const_index[i] = blob_read_uint32(ctx->blob);
616
617 return intrin;
618 }
619
620 static void
621 write_load_const(write_ctx *ctx, const nir_load_const_instr *lc)
622 {
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);
628 }
629
630 static nir_load_const_instr *
631 read_load_const(read_ctx *ctx)
632 {
633 uint32_t val = blob_read_uint32(ctx->blob);
634
635 nir_load_const_instr *lc =
636 nir_load_const_instr_create(ctx->nir, val & 0x7, val >> 3);
637
638 blob_copy_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));
639 read_add_object(ctx, &lc->def);
640 return lc;
641 }
642
643 static void
644 write_ssa_undef(write_ctx *ctx, const nir_ssa_undef_instr *undef)
645 {
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);
650 }
651
652 static nir_ssa_undef_instr *
653 read_ssa_undef(read_ctx *ctx)
654 {
655 uint32_t val = blob_read_uint32(ctx->blob);
656
657 nir_ssa_undef_instr *undef =
658 nir_ssa_undef_instr_create(ctx->nir, val & 0x7, val >> 3);
659
660 read_add_object(ctx, &undef->def);
661 return undef;
662 }
663
664 union packed_tex_data {
665 uint32_t u32;
666 struct {
667 enum glsl_sampler_dim sampler_dim:4;
668 nir_alu_type dest_type:8;
669 unsigned coord_components:3;
670 unsigned is_array:1;
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. */
677 } u;
678 };
679
680 static void
681 write_tex(write_ctx *ctx, const nir_tex_instr *tex)
682 {
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);
688
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,
700 };
701 blob_write_uint32(ctx->blob, packed.u32);
702
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);
707 }
708
709 if (tex->texture)
710 write_deref_chain(ctx, tex->texture);
711 if (tex->sampler)
712 write_deref_chain(ctx, tex->sampler);
713 }
714
715 static nir_tex_instr *
716 read_tex(read_ctx *ctx)
717 {
718 unsigned num_srcs = blob_read_uint32(ctx->blob);
719 nir_tex_instr *tex = nir_tex_instr_create(ctx->nir, num_srcs);
720
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);
725
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;
735
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);
740 }
741
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;
746
747 return tex;
748 }
749
750 static void
751 write_phi(write_ctx *ctx, const nir_phi_instr *phi)
752 {
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
756 * them in correctly.
757 */
758 write_dest(ctx, &phi->dest);
759
760 blob_write_uint32(ctx->blob, exec_list_length(&phi->srcs));
761
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,
769 .src = src->src.ssa,
770 .block = src->pred,
771 };
772 util_dynarray_append(&ctx->phi_fixups, write_phi_fixup, fixup);
773 }
774 }
775
776 static void
777 write_fixup_phis(write_ctx *ctx)
778 {
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);
783 }
784
785 util_dynarray_clear(&ctx->phi_fixups);
786 }
787
788 static nir_phi_instr *
789 read_phi(read_ctx *ctx, nir_block *blk)
790 {
791 nir_phi_instr *phi = nir_phi_instr_create(ctx->nir);
792
793 read_dest(ctx, &phi->dest, &phi->instr);
794
795 unsigned num_srcs = blob_read_uint32(ctx->blob);
796
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.
799 *
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
803 * sources.
804 */
805 nir_instr_insert_after_block(blk, &phi->instr);
806
807 for (unsigned i = 0; i < num_srcs; i++) {
808 nir_phi_src *src = ralloc(phi, nir_phi_src);
809
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);
813
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.
817 */
818 src->src.parent_instr = &phi->instr;
819
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.
822 */
823 list_add(&src->src.use_link, &ctx->phi_srcs);
824
825 exec_list_push_tail(&phi->srcs, &src->node);
826 }
827
828 return phi;
829 }
830
831 static void
832 read_fixup_phis(read_ctx *ctx)
833 {
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);
837
838 /* Remove from this list */
839 list_del(&src->src.use_link);
840
841 list_addtail(&src->src.use_link, &src->src.ssa->uses);
842 }
843 assert(list_empty(&ctx->phi_srcs));
844 }
845
846 static void
847 write_jump(write_ctx *ctx, const nir_jump_instr *jmp)
848 {
849 blob_write_uint32(ctx->blob, jmp->type);
850 }
851
852 static nir_jump_instr *
853 read_jump(read_ctx *ctx)
854 {
855 nir_jump_type type = blob_read_uint32(ctx->blob);
856 nir_jump_instr *jmp = nir_jump_instr_create(ctx->nir, type);
857 return jmp;
858 }
859
860 static void
861 write_call(write_ctx *ctx, const nir_call_instr *call)
862 {
863 blob_write_intptr(ctx->blob, write_lookup_object(ctx, call->callee));
864
865 for (unsigned i = 0; i < call->num_params; i++)
866 write_src(ctx, &call->params[i]);
867 }
868
869 static nir_call_instr *
870 read_call(read_ctx *ctx)
871 {
872 nir_function *callee = read_object(ctx);
873 nir_call_instr *call = nir_call_instr_create(ctx->nir, callee);
874
875 for (unsigned i = 0; i < call->num_params; i++)
876 read_src(ctx, &call->params[i], call);
877
878 return call;
879 }
880
881 static void
882 write_instr(write_ctx *ctx, const nir_instr *instr)
883 {
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));
888 break;
889 case nir_instr_type_deref:
890 write_deref(ctx, nir_instr_as_deref(instr));
891 break;
892 case nir_instr_type_intrinsic:
893 write_intrinsic(ctx, nir_instr_as_intrinsic(instr));
894 break;
895 case nir_instr_type_load_const:
896 write_load_const(ctx, nir_instr_as_load_const(instr));
897 break;
898 case nir_instr_type_ssa_undef:
899 write_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
900 break;
901 case nir_instr_type_tex:
902 write_tex(ctx, nir_instr_as_tex(instr));
903 break;
904 case nir_instr_type_phi:
905 write_phi(ctx, nir_instr_as_phi(instr));
906 break;
907 case nir_instr_type_jump:
908 write_jump(ctx, nir_instr_as_jump(instr));
909 break;
910 case nir_instr_type_call:
911 write_call(ctx, nir_instr_as_call(instr));
912 break;
913 case nir_instr_type_parallel_copy:
914 unreachable("Cannot write parallel copies");
915 default:
916 unreachable("bad instr type");
917 }
918 }
919
920 static void
921 read_instr(read_ctx *ctx, nir_block *block)
922 {
923 nir_instr_type type = blob_read_uint32(ctx->blob);
924 nir_instr *instr;
925 switch (type) {
926 case nir_instr_type_alu:
927 instr = &read_alu(ctx)->instr;
928 break;
929 case nir_instr_type_deref:
930 instr = &read_deref(ctx)->instr;
931 break;
932 case nir_instr_type_intrinsic:
933 instr = &read_intrinsic(ctx)->instr;
934 break;
935 case nir_instr_type_load_const:
936 instr = &read_load_const(ctx)->instr;
937 break;
938 case nir_instr_type_ssa_undef:
939 instr = &read_ssa_undef(ctx)->instr;
940 break;
941 case nir_instr_type_tex:
942 instr = &read_tex(ctx)->instr;
943 break;
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.
949 */
950 read_phi(ctx, block);
951 return;
952 case nir_instr_type_jump:
953 instr = &read_jump(ctx)->instr;
954 break;
955 case nir_instr_type_call:
956 instr = &read_call(ctx)->instr;
957 break;
958 case nir_instr_type_parallel_copy:
959 unreachable("Cannot read parallel copies");
960 default:
961 unreachable("bad instr type");
962 }
963
964 nir_instr_insert_after_block(block, instr);
965 }
966
967 static void
968 write_block(write_ctx *ctx, const nir_block *block)
969 {
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);
974 }
975
976 static void
977 read_block(read_ctx *ctx, struct exec_list *cf_list)
978 {
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.
982 */
983 nir_block *block =
984 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
985
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);
990 }
991 }
992
993 static void
994 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list);
995
996 static void
997 read_cf_list(read_ctx *ctx, struct exec_list *cf_list);
998
999 static void
1000 write_if(write_ctx *ctx, nir_if *nif)
1001 {
1002 write_src(ctx, &nif->condition);
1003
1004 write_cf_list(ctx, &nif->then_list);
1005 write_cf_list(ctx, &nif->else_list);
1006 }
1007
1008 static void
1009 read_if(read_ctx *ctx, struct exec_list *cf_list)
1010 {
1011 nir_if *nif = nir_if_create(ctx->nir);
1012
1013 read_src(ctx, &nif->condition, nif);
1014
1015 nir_cf_node_insert_end(cf_list, &nif->cf_node);
1016
1017 read_cf_list(ctx, &nif->then_list);
1018 read_cf_list(ctx, &nif->else_list);
1019 }
1020
1021 static void
1022 write_loop(write_ctx *ctx, nir_loop *loop)
1023 {
1024 write_cf_list(ctx, &loop->body);
1025 }
1026
1027 static void
1028 read_loop(read_ctx *ctx, struct exec_list *cf_list)
1029 {
1030 nir_loop *loop = nir_loop_create(ctx->nir);
1031
1032 nir_cf_node_insert_end(cf_list, &loop->cf_node);
1033
1034 read_cf_list(ctx, &loop->body);
1035 }
1036
1037 static void
1038 write_cf_node(write_ctx *ctx, nir_cf_node *cf)
1039 {
1040 blob_write_uint32(ctx->blob, cf->type);
1041
1042 switch (cf->type) {
1043 case nir_cf_node_block:
1044 write_block(ctx, nir_cf_node_as_block(cf));
1045 break;
1046 case nir_cf_node_if:
1047 write_if(ctx, nir_cf_node_as_if(cf));
1048 break;
1049 case nir_cf_node_loop:
1050 write_loop(ctx, nir_cf_node_as_loop(cf));
1051 break;
1052 default:
1053 unreachable("bad cf type");
1054 }
1055 }
1056
1057 static void
1058 read_cf_node(read_ctx *ctx, struct exec_list *list)
1059 {
1060 nir_cf_node_type type = blob_read_uint32(ctx->blob);
1061
1062 switch (type) {
1063 case nir_cf_node_block:
1064 read_block(ctx, list);
1065 break;
1066 case nir_cf_node_if:
1067 read_if(ctx, list);
1068 break;
1069 case nir_cf_node_loop:
1070 read_loop(ctx, list);
1071 break;
1072 default:
1073 unreachable("bad cf type");
1074 }
1075 }
1076
1077 static void
1078 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list)
1079 {
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);
1083 }
1084 }
1085
1086 static void
1087 read_cf_list(read_ctx *ctx, struct exec_list *cf_list)
1088 {
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);
1092 }
1093
1094 static void
1095 write_function_impl(write_ctx *ctx, const nir_function_impl *fi)
1096 {
1097 write_var_list(ctx, &fi->locals);
1098 write_reg_list(ctx, &fi->registers);
1099 blob_write_uint32(ctx->blob, fi->reg_alloc);
1100
1101 write_cf_list(ctx, &fi->body);
1102 write_fixup_phis(ctx);
1103 }
1104
1105 static nir_function_impl *
1106 read_function_impl(read_ctx *ctx, nir_function *fxn)
1107 {
1108 nir_function_impl *fi = nir_function_impl_create_bare(ctx->nir);
1109 fi->function = fxn;
1110
1111 read_var_list(ctx, &fi->locals);
1112 read_reg_list(ctx, &fi->registers);
1113 fi->reg_alloc = blob_read_uint32(ctx->blob);
1114
1115 read_cf_list(ctx, &fi->body);
1116 read_fixup_phis(ctx);
1117
1118 fi->valid_metadata = 0;
1119
1120 return fi;
1121 }
1122
1123 static void
1124 write_function(write_ctx *ctx, const nir_function *fxn)
1125 {
1126 blob_write_uint32(ctx->blob, !!(fxn->name));
1127 if (fxn->name)
1128 blob_write_string(ctx->blob, fxn->name);
1129
1130 write_add_object(ctx, fxn);
1131
1132 blob_write_uint32(ctx->blob, fxn->num_params);
1133 for (unsigned i = 0; i < fxn->num_params; i++) {
1134 uint32_t val =
1135 ((uint32_t)fxn->params[i].num_components) |
1136 ((uint32_t)fxn->params[i].bit_size) << 8;
1137 blob_write_uint32(ctx->blob, val);
1138 }
1139
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.
1144 */
1145 }
1146
1147 static void
1148 read_function(read_ctx *ctx)
1149 {
1150 bool has_name = blob_read_uint32(ctx->blob);
1151 char *name = has_name ? blob_read_string(ctx->blob) : NULL;
1152
1153 nir_function *fxn = nir_function_create(ctx->nir, name);
1154
1155 read_add_object(ctx, fxn);
1156
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;
1163 }
1164 }
1165
1166 void
1167 nir_serialize(struct blob *blob, const nir_shader *nir)
1168 {
1169 write_ctx ctx;
1170 ctx.remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1171 _mesa_key_pointer_equal);
1172 ctx.next_idx = 0;
1173 ctx.blob = blob;
1174 ctx.nir = nir;
1175 util_dynarray_init(&ctx.phi_fixups, NULL);
1176
1177 size_t idx_size_offset = blob_reserve_intptr(blob);
1178
1179 struct shader_info info = nir->info;
1180 uint32_t strings = 0;
1181 if (info.name)
1182 strings |= 0x1;
1183 if (info.label)
1184 strings |= 0x2;
1185 blob_write_uint32(blob, strings);
1186 if (info.name)
1187 blob_write_string(blob, info.name);
1188 if (info.label)
1189 blob_write_string(blob, info.label);
1190 info.name = info.label = NULL;
1191 blob_write_bytes(blob, (uint8_t *) &info, sizeof(info));
1192
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);
1199
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);
1207
1208 blob_write_uint32(blob, exec_list_length(&nir->functions));
1209 nir_foreach_function(fxn, nir) {
1210 write_function(&ctx, fxn);
1211 }
1212
1213 nir_foreach_function(fxn, nir) {
1214 write_function_impl(&ctx, fxn->impl);
1215 }
1216
1217 *(uintptr_t *)(blob->data + idx_size_offset) = ctx.next_idx;
1218
1219 _mesa_hash_table_destroy(ctx.remap_table, NULL);
1220 util_dynarray_fini(&ctx.phi_fixups);
1221 }
1222
1223 nir_shader *
1224 nir_deserialize(void *mem_ctx,
1225 const struct nir_shader_compiler_options *options,
1226 struct blob_reader *blob)
1227 {
1228 read_ctx ctx;
1229 ctx.blob = 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));
1233 ctx.next_idx = 0;
1234
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;
1238
1239 struct shader_info info;
1240 blob_copy_bytes(blob, (uint8_t *) &info, sizeof(info));
1241
1242 ctx.nir = nir_shader_create(mem_ctx, info.stage, options, NULL);
1243
1244 info.name = name ? ralloc_strdup(ctx.nir, name) : NULL;
1245 info.label = label ? ralloc_strdup(ctx.nir, label) : NULL;
1246
1247 ctx.nir->info = info;
1248
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);
1255
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);
1263
1264 unsigned num_functions = blob_read_uint32(blob);
1265 for (unsigned i = 0; i < num_functions; i++)
1266 read_function(&ctx);
1267
1268 nir_foreach_function(fxn, ctx.nir)
1269 fxn->impl = read_function_impl(&ctx, fxn);
1270
1271 free(ctx.idx_table);
1272
1273 return ctx.nir;
1274 }
1275
1276 nir_shader *
1277 nir_shader_serialize_deserialize(void *mem_ctx, nir_shader *s)
1278 {
1279 const struct nir_shader_compiler_options *options = s->options;
1280
1281 struct blob writer;
1282 blob_init(&writer);
1283 nir_serialize(&writer, s);
1284 ralloc_free(s);
1285
1286 struct blob_reader reader;
1287 blob_reader_init(&reader, writer.data, writer.size);
1288 nir_shader *ns = nir_deserialize(mem_ctx, options, &reader);
1289
1290 blob_finish(&writer);
1291
1292 return ns;
1293 }