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
write_add_object(write_ctx * ctx,const void * obj)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
write_lookup_object(write_ctx * ctx,const void * obj)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
write_object(write_ctx * ctx,const void * obj)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
read_add_object(read_ctx * ctx,void * obj)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 *
read_lookup_object(read_ctx * ctx,uintptr_t idx)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 *
read_object(read_ctx * ctx)106 read_object(read_ctx *ctx)
107 {
108 return read_lookup_object(ctx, blob_read_intptr(ctx->blob));
109 }
110
111 static void
write_constant(write_ctx * ctx,const nir_constant * c)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 *
read_constant(read_ctx * ctx,nir_variable * nvar)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
write_variable(write_ctx * ctx,const nir_variable * var)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 blob_write_string(ctx->blob, var->name);
141 blob_write_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
142 blob_write_uint32(ctx->blob, var->num_state_slots);
143 blob_write_bytes(ctx->blob, (uint8_t *) var->state_slots,
144 var->num_state_slots * sizeof(nir_state_slot));
145 blob_write_uint32(ctx->blob, !!(var->constant_initializer));
146 if (var->constant_initializer)
147 write_constant(ctx, var->constant_initializer);
148 blob_write_uint32(ctx->blob, !!(var->interface_type));
149 if (var->interface_type)
150 encode_type_to_blob(ctx->blob, var->interface_type);
151 }
152
153 static nir_variable *
read_variable(read_ctx * ctx)154 read_variable(read_ctx *ctx)
155 {
156 nir_variable *var = rzalloc(ctx->nir, nir_variable);
157 read_add_object(ctx, var);
158
159 var->type = decode_type_from_blob(ctx->blob);
160 bool has_name = blob_read_uint32(ctx->blob);
161 if (has_name) {
162 const char *name = blob_read_string(ctx->blob);
163 var->name = ralloc_strdup(var, name);
164 } else {
165 var->name = NULL;
166 }
167 blob_copy_bytes(ctx->blob, (uint8_t *) &var->data, sizeof(var->data));
168 var->num_state_slots = blob_read_uint32(ctx->blob);
169 var->state_slots = ralloc_array(var, nir_state_slot, var->num_state_slots);
170 blob_copy_bytes(ctx->blob, (uint8_t *) var->state_slots,
171 var->num_state_slots * sizeof(nir_state_slot));
172 bool has_const_initializer = blob_read_uint32(ctx->blob);
173 if (has_const_initializer)
174 var->constant_initializer = read_constant(ctx, var);
175 else
176 var->constant_initializer = NULL;
177 bool has_interface_type = blob_read_uint32(ctx->blob);
178 if (has_interface_type)
179 var->interface_type = decode_type_from_blob(ctx->blob);
180 else
181 var->interface_type = NULL;
182
183 return var;
184 }
185
186 static void
write_var_list(write_ctx * ctx,const struct exec_list * src)187 write_var_list(write_ctx *ctx, const struct exec_list *src)
188 {
189 blob_write_uint32(ctx->blob, exec_list_length(src));
190 foreach_list_typed(nir_variable, var, node, src) {
191 write_variable(ctx, var);
192 }
193 }
194
195 static void
read_var_list(read_ctx * ctx,struct exec_list * dst)196 read_var_list(read_ctx *ctx, struct exec_list *dst)
197 {
198 exec_list_make_empty(dst);
199 unsigned num_vars = blob_read_uint32(ctx->blob);
200 for (unsigned i = 0; i < num_vars; i++) {
201 nir_variable *var = read_variable(ctx);
202 exec_list_push_tail(dst, &var->node);
203 }
204 }
205
206 static void
write_register(write_ctx * ctx,const nir_register * reg)207 write_register(write_ctx *ctx, const nir_register *reg)
208 {
209 write_add_object(ctx, reg);
210 blob_write_uint32(ctx->blob, reg->num_components);
211 blob_write_uint32(ctx->blob, reg->bit_size);
212 blob_write_uint32(ctx->blob, reg->num_array_elems);
213 blob_write_uint32(ctx->blob, reg->index);
214 blob_write_uint32(ctx->blob, !!(reg->name));
215 if (reg->name)
216 blob_write_string(ctx->blob, reg->name);
217 blob_write_uint32(ctx->blob, reg->is_global << 1 | reg->is_packed);
218 }
219
220 static nir_register *
read_register(read_ctx * ctx)221 read_register(read_ctx *ctx)
222 {
223 nir_register *reg = ralloc(ctx->nir, nir_register);
224 read_add_object(ctx, reg);
225 reg->num_components = blob_read_uint32(ctx->blob);
226 reg->bit_size = blob_read_uint32(ctx->blob);
227 reg->num_array_elems = blob_read_uint32(ctx->blob);
228 reg->index = blob_read_uint32(ctx->blob);
229 bool has_name = blob_read_uint32(ctx->blob);
230 if (has_name) {
231 const char *name = blob_read_string(ctx->blob);
232 reg->name = ralloc_strdup(reg, name);
233 } else {
234 reg->name = NULL;
235 }
236 unsigned flags = blob_read_uint32(ctx->blob);
237 reg->is_global = flags & 0x2;
238 reg->is_packed = flags & 0x1;
239
240 list_inithead(®->uses);
241 list_inithead(®->defs);
242 list_inithead(®->if_uses);
243
244 return reg;
245 }
246
247 static void
write_reg_list(write_ctx * ctx,const struct exec_list * src)248 write_reg_list(write_ctx *ctx, const struct exec_list *src)
249 {
250 blob_write_uint32(ctx->blob, exec_list_length(src));
251 foreach_list_typed(nir_register, reg, node, src)
252 write_register(ctx, reg);
253 }
254
255 static void
read_reg_list(read_ctx * ctx,struct exec_list * dst)256 read_reg_list(read_ctx *ctx, struct exec_list *dst)
257 {
258 exec_list_make_empty(dst);
259 unsigned num_regs = blob_read_uint32(ctx->blob);
260 for (unsigned i = 0; i < num_regs; i++) {
261 nir_register *reg = read_register(ctx);
262 exec_list_push_tail(dst, ®->node);
263 }
264 }
265
266 static void
write_src(write_ctx * ctx,const nir_src * src)267 write_src(write_ctx *ctx, const nir_src *src)
268 {
269 /* Since sources are very frequent, we try to save some space when storing
270 * them. In particular, we store whether the source is a register and
271 * whether the register has an indirect index in the low two bits. We can
272 * assume that the high two bits of the index are zero, since otherwise our
273 * address space would've been exhausted allocating the remap table!
274 */
275 if (src->is_ssa) {
276 uintptr_t idx = write_lookup_object(ctx, src->ssa) << 2;
277 idx |= 1;
278 blob_write_intptr(ctx->blob, idx);
279 } else {
280 uintptr_t idx = write_lookup_object(ctx, src->reg.reg) << 2;
281 if (src->reg.indirect)
282 idx |= 2;
283 blob_write_intptr(ctx->blob, idx);
284 blob_write_uint32(ctx->blob, src->reg.base_offset);
285 if (src->reg.indirect) {
286 write_src(ctx, src->reg.indirect);
287 }
288 }
289 }
290
291 static void
read_src(read_ctx * ctx,nir_src * src,void * mem_ctx)292 read_src(read_ctx *ctx, nir_src *src, void *mem_ctx)
293 {
294 uintptr_t val = blob_read_intptr(ctx->blob);
295 uintptr_t idx = val >> 2;
296 src->is_ssa = val & 0x1;
297 if (src->is_ssa) {
298 src->ssa = read_lookup_object(ctx, idx);
299 } else {
300 bool is_indirect = val & 0x2;
301 src->reg.reg = read_lookup_object(ctx, idx);
302 src->reg.base_offset = blob_read_uint32(ctx->blob);
303 if (is_indirect) {
304 src->reg.indirect = ralloc(mem_ctx, nir_src);
305 read_src(ctx, src->reg.indirect, mem_ctx);
306 } else {
307 src->reg.indirect = NULL;
308 }
309 }
310 }
311
312 static void
write_dest(write_ctx * ctx,const nir_dest * dst)313 write_dest(write_ctx *ctx, const nir_dest *dst)
314 {
315 uint32_t val = dst->is_ssa;
316 if (dst->is_ssa) {
317 val |= !!(dst->ssa.name) << 1;
318 val |= dst->ssa.num_components << 2;
319 val |= dst->ssa.bit_size << 5;
320 } else {
321 val |= !!(dst->reg.indirect) << 1;
322 }
323 blob_write_uint32(ctx->blob, val);
324 if (dst->is_ssa) {
325 write_add_object(ctx, &dst->ssa);
326 if (dst->ssa.name)
327 blob_write_string(ctx->blob, dst->ssa.name);
328 } else {
329 blob_write_intptr(ctx->blob, write_lookup_object(ctx, dst->reg.reg));
330 blob_write_uint32(ctx->blob, dst->reg.base_offset);
331 if (dst->reg.indirect)
332 write_src(ctx, dst->reg.indirect);
333 }
334 }
335
336 static void
read_dest(read_ctx * ctx,nir_dest * dst,nir_instr * instr)337 read_dest(read_ctx *ctx, nir_dest *dst, nir_instr *instr)
338 {
339 uint32_t val = blob_read_uint32(ctx->blob);
340 bool is_ssa = val & 0x1;
341 if (is_ssa) {
342 bool has_name = val & 0x2;
343 unsigned num_components = (val >> 2) & 0x7;
344 unsigned bit_size = val >> 5;
345 char *name = has_name ? blob_read_string(ctx->blob) : NULL;
346 nir_ssa_dest_init(instr, dst, num_components, bit_size, name);
347 read_add_object(ctx, &dst->ssa);
348 } else {
349 bool is_indirect = val & 0x2;
350 dst->reg.reg = read_object(ctx);
351 dst->reg.base_offset = blob_read_uint32(ctx->blob);
352 if (is_indirect) {
353 dst->reg.indirect = ralloc(instr, nir_src);
354 read_src(ctx, dst->reg.indirect, instr);
355 }
356 }
357 }
358
359 static void
write_deref_chain(write_ctx * ctx,const nir_deref_var * deref_var)360 write_deref_chain(write_ctx *ctx, const nir_deref_var *deref_var)
361 {
362 write_object(ctx, deref_var->var);
363
364 uint32_t len = 0;
365 for (const nir_deref *d = deref_var->deref.child; d; d = d->child)
366 len++;
367 blob_write_uint32(ctx->blob, len);
368
369 for (const nir_deref *d = deref_var->deref.child; d; d = d->child) {
370 blob_write_uint32(ctx->blob, d->deref_type);
371 switch (d->deref_type) {
372 case nir_deref_type_array: {
373 const nir_deref_array *deref_array = nir_deref_as_array(d);
374 blob_write_uint32(ctx->blob, deref_array->deref_array_type);
375 blob_write_uint32(ctx->blob, deref_array->base_offset);
376 if (deref_array->deref_array_type == nir_deref_array_type_indirect)
377 write_src(ctx, &deref_array->indirect);
378 break;
379 }
380 case nir_deref_type_struct: {
381 const nir_deref_struct *deref_struct = nir_deref_as_struct(d);
382 blob_write_uint32(ctx->blob, deref_struct->index);
383 break;
384 }
385 case nir_deref_type_var:
386 unreachable("Invalid deref type");
387 }
388
389 encode_type_to_blob(ctx->blob, d->type);
390 }
391 }
392
393 static nir_deref_var *
read_deref_chain(read_ctx * ctx,void * mem_ctx)394 read_deref_chain(read_ctx *ctx, void *mem_ctx)
395 {
396 nir_variable *var = read_object(ctx);
397 nir_deref_var *deref_var = nir_deref_var_create(mem_ctx, var);
398
399 uint32_t len = blob_read_uint32(ctx->blob);
400
401 nir_deref *tail = &deref_var->deref;
402 for (uint32_t i = 0; i < len; i++) {
403 nir_deref_type deref_type = blob_read_uint32(ctx->blob);
404 nir_deref *deref = NULL;
405 switch (deref_type) {
406 case nir_deref_type_array: {
407 nir_deref_array *deref_array = nir_deref_array_create(tail);
408 deref_array->deref_array_type = blob_read_uint32(ctx->blob);
409 deref_array->base_offset = blob_read_uint32(ctx->blob);
410 if (deref_array->deref_array_type == nir_deref_array_type_indirect)
411 read_src(ctx, &deref_array->indirect, mem_ctx);
412 deref = &deref_array->deref;
413 break;
414 }
415 case nir_deref_type_struct: {
416 uint32_t index = blob_read_uint32(ctx->blob);
417 nir_deref_struct *deref_struct = nir_deref_struct_create(tail, index);
418 deref = &deref_struct->deref;
419 break;
420 }
421 case nir_deref_type_var:
422 unreachable("Invalid deref type");
423 }
424
425 deref->type = decode_type_from_blob(ctx->blob);
426
427 tail->child = deref;
428 tail = deref;
429 }
430
431 return deref_var;
432 }
433
434 static void
write_alu(write_ctx * ctx,const nir_alu_instr * alu)435 write_alu(write_ctx *ctx, const nir_alu_instr *alu)
436 {
437 blob_write_uint32(ctx->blob, alu->op);
438 uint32_t flags = alu->exact;
439 flags |= alu->dest.saturate << 1;
440 flags |= alu->dest.write_mask << 2;
441 blob_write_uint32(ctx->blob, flags);
442
443 write_dest(ctx, &alu->dest.dest);
444
445 for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
446 write_src(ctx, &alu->src[i].src);
447 flags = alu->src[i].negate;
448 flags |= alu->src[i].abs << 1;
449 for (unsigned j = 0; j < 4; j++)
450 flags |= alu->src[i].swizzle[j] << (2 + 2 * j);
451 blob_write_uint32(ctx->blob, flags);
452 }
453 }
454
455 static nir_alu_instr *
read_alu(read_ctx * ctx)456 read_alu(read_ctx *ctx)
457 {
458 nir_op op = blob_read_uint32(ctx->blob);
459 nir_alu_instr *alu = nir_alu_instr_create(ctx->nir, op);
460
461 uint32_t flags = blob_read_uint32(ctx->blob);
462 alu->exact = flags & 1;
463 alu->dest.saturate = flags & 2;
464 alu->dest.write_mask = flags >> 2;
465
466 read_dest(ctx, &alu->dest.dest, &alu->instr);
467
468 for (unsigned i = 0; i < nir_op_infos[op].num_inputs; i++) {
469 read_src(ctx, &alu->src[i].src, &alu->instr);
470 flags = blob_read_uint32(ctx->blob);
471 alu->src[i].negate = flags & 1;
472 alu->src[i].abs = flags & 2;
473 for (unsigned j = 0; j < 4; j++)
474 alu->src[i].swizzle[j] = (flags >> (2 * j + 2)) & 3;
475 }
476
477 return alu;
478 }
479
480 static void
write_intrinsic(write_ctx * ctx,const nir_intrinsic_instr * intrin)481 write_intrinsic(write_ctx *ctx, const nir_intrinsic_instr *intrin)
482 {
483 blob_write_uint32(ctx->blob, intrin->intrinsic);
484
485 unsigned num_variables = nir_intrinsic_infos[intrin->intrinsic].num_variables;
486 unsigned num_srcs = nir_intrinsic_infos[intrin->intrinsic].num_srcs;
487 unsigned num_indices = nir_intrinsic_infos[intrin->intrinsic].num_indices;
488
489 blob_write_uint32(ctx->blob, intrin->num_components);
490
491 if (nir_intrinsic_infos[intrin->intrinsic].has_dest)
492 write_dest(ctx, &intrin->dest);
493
494 for (unsigned i = 0; i < num_variables; i++)
495 write_deref_chain(ctx, intrin->variables[i]);
496
497 for (unsigned i = 0; i < num_srcs; i++)
498 write_src(ctx, &intrin->src[i]);
499
500 for (unsigned i = 0; i < num_indices; i++)
501 blob_write_uint32(ctx->blob, intrin->const_index[i]);
502 }
503
504 static nir_intrinsic_instr *
read_intrinsic(read_ctx * ctx)505 read_intrinsic(read_ctx *ctx)
506 {
507 nir_intrinsic_op op = blob_read_uint32(ctx->blob);
508
509 nir_intrinsic_instr *intrin = nir_intrinsic_instr_create(ctx->nir, op);
510
511 unsigned num_variables = nir_intrinsic_infos[op].num_variables;
512 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
513 unsigned num_indices = nir_intrinsic_infos[op].num_indices;
514
515 intrin->num_components = blob_read_uint32(ctx->blob);
516
517 if (nir_intrinsic_infos[op].has_dest)
518 read_dest(ctx, &intrin->dest, &intrin->instr);
519
520 for (unsigned i = 0; i < num_variables; i++)
521 intrin->variables[i] = read_deref_chain(ctx, &intrin->instr);
522
523 for (unsigned i = 0; i < num_srcs; i++)
524 read_src(ctx, &intrin->src[i], &intrin->instr);
525
526 for (unsigned i = 0; i < num_indices; i++)
527 intrin->const_index[i] = blob_read_uint32(ctx->blob);
528
529 return intrin;
530 }
531
532 static void
write_load_const(write_ctx * ctx,const nir_load_const_instr * lc)533 write_load_const(write_ctx *ctx, const nir_load_const_instr *lc)
534 {
535 uint32_t val = lc->def.num_components;
536 val |= lc->def.bit_size << 3;
537 blob_write_uint32(ctx->blob, val);
538 blob_write_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));
539 write_add_object(ctx, &lc->def);
540 }
541
542 static nir_load_const_instr *
read_load_const(read_ctx * ctx)543 read_load_const(read_ctx *ctx)
544 {
545 uint32_t val = blob_read_uint32(ctx->blob);
546
547 nir_load_const_instr *lc =
548 nir_load_const_instr_create(ctx->nir, val & 0x7, val >> 3);
549
550 blob_copy_bytes(ctx->blob, (uint8_t *) &lc->value, sizeof(lc->value));
551 read_add_object(ctx, &lc->def);
552 return lc;
553 }
554
555 static void
write_ssa_undef(write_ctx * ctx,const nir_ssa_undef_instr * undef)556 write_ssa_undef(write_ctx *ctx, const nir_ssa_undef_instr *undef)
557 {
558 uint32_t val = undef->def.num_components;
559 val |= undef->def.bit_size << 3;
560 blob_write_uint32(ctx->blob, val);
561 write_add_object(ctx, &undef->def);
562 }
563
564 static nir_ssa_undef_instr *
read_ssa_undef(read_ctx * ctx)565 read_ssa_undef(read_ctx *ctx)
566 {
567 uint32_t val = blob_read_uint32(ctx->blob);
568
569 nir_ssa_undef_instr *undef =
570 nir_ssa_undef_instr_create(ctx->nir, val & 0x7, val >> 3);
571
572 read_add_object(ctx, &undef->def);
573 return undef;
574 }
575
576 union packed_tex_data {
577 uint32_t u32;
578 struct {
579 enum glsl_sampler_dim sampler_dim:4;
580 nir_alu_type dest_type:8;
581 unsigned coord_components:3;
582 unsigned is_array:1;
583 unsigned is_shadow:1;
584 unsigned is_new_style_shadow:1;
585 unsigned component:2;
586 unsigned has_texture_deref:1;
587 unsigned has_sampler_deref:1;
588 unsigned unused:10; /* Mark unused for valgrind. */
589 } u;
590 };
591
592 static void
write_tex(write_ctx * ctx,const nir_tex_instr * tex)593 write_tex(write_ctx *ctx, const nir_tex_instr *tex)
594 {
595 blob_write_uint32(ctx->blob, tex->num_srcs);
596 blob_write_uint32(ctx->blob, tex->op);
597 blob_write_uint32(ctx->blob, tex->texture_index);
598 blob_write_uint32(ctx->blob, tex->texture_array_size);
599 blob_write_uint32(ctx->blob, tex->sampler_index);
600
601 STATIC_ASSERT(sizeof(union packed_tex_data) == sizeof(uint32_t));
602 union packed_tex_data packed = {
603 .u.sampler_dim = tex->sampler_dim,
604 .u.dest_type = tex->dest_type,
605 .u.coord_components = tex->coord_components,
606 .u.is_array = tex->is_array,
607 .u.is_shadow = tex->is_shadow,
608 .u.is_new_style_shadow = tex->is_new_style_shadow,
609 .u.component = tex->component,
610 .u.has_texture_deref = tex->texture != NULL,
611 .u.has_sampler_deref = tex->sampler != NULL,
612 };
613 blob_write_uint32(ctx->blob, packed.u32);
614
615 write_dest(ctx, &tex->dest);
616 for (unsigned i = 0; i < tex->num_srcs; i++) {
617 blob_write_uint32(ctx->blob, tex->src[i].src_type);
618 write_src(ctx, &tex->src[i].src);
619 }
620
621 if (tex->texture)
622 write_deref_chain(ctx, tex->texture);
623 if (tex->sampler)
624 write_deref_chain(ctx, tex->sampler);
625 }
626
627 static nir_tex_instr *
read_tex(read_ctx * ctx)628 read_tex(read_ctx *ctx)
629 {
630 unsigned num_srcs = blob_read_uint32(ctx->blob);
631 nir_tex_instr *tex = nir_tex_instr_create(ctx->nir, num_srcs);
632
633 tex->op = blob_read_uint32(ctx->blob);
634 tex->texture_index = blob_read_uint32(ctx->blob);
635 tex->texture_array_size = blob_read_uint32(ctx->blob);
636 tex->sampler_index = blob_read_uint32(ctx->blob);
637
638 union packed_tex_data packed;
639 packed.u32 = blob_read_uint32(ctx->blob);
640 tex->sampler_dim = packed.u.sampler_dim;
641 tex->dest_type = packed.u.dest_type;
642 tex->coord_components = packed.u.coord_components;
643 tex->is_array = packed.u.is_array;
644 tex->is_shadow = packed.u.is_shadow;
645 tex->is_new_style_shadow = packed.u.is_new_style_shadow;
646 tex->component = packed.u.component;
647
648 read_dest(ctx, &tex->dest, &tex->instr);
649 for (unsigned i = 0; i < tex->num_srcs; i++) {
650 tex->src[i].src_type = blob_read_uint32(ctx->blob);
651 read_src(ctx, &tex->src[i].src, &tex->instr);
652 }
653
654 tex->texture = packed.u.has_texture_deref ?
655 read_deref_chain(ctx, &tex->instr) : NULL;
656 tex->sampler = packed.u.has_sampler_deref ?
657 read_deref_chain(ctx, &tex->instr) : NULL;
658
659 return tex;
660 }
661
662 static void
write_phi(write_ctx * ctx,const nir_phi_instr * phi)663 write_phi(write_ctx *ctx, const nir_phi_instr *phi)
664 {
665 /* Phi nodes are special, since they may reference SSA definitions and
666 * basic blocks that don't exist yet. We leave two empty uintptr_t's here,
667 * and then store enough information so that a later fixup pass can fill
668 * them in correctly.
669 */
670 write_dest(ctx, &phi->dest);
671
672 blob_write_uint32(ctx->blob, exec_list_length(&phi->srcs));
673
674 nir_foreach_phi_src(src, phi) {
675 assert(src->src.is_ssa);
676 size_t blob_offset = blob_reserve_intptr(ctx->blob);
677 MAYBE_UNUSED size_t blob_offset2 = blob_reserve_intptr(ctx->blob);
678 assert(blob_offset + sizeof(uintptr_t) == blob_offset2);
679 write_phi_fixup fixup = {
680 .blob_offset = blob_offset,
681 .src = src->src.ssa,
682 .block = src->pred,
683 };
684 util_dynarray_append(&ctx->phi_fixups, write_phi_fixup, fixup);
685 }
686 }
687
688 static void
write_fixup_phis(write_ctx * ctx)689 write_fixup_phis(write_ctx *ctx)
690 {
691 util_dynarray_foreach(&ctx->phi_fixups, write_phi_fixup, fixup) {
692 uintptr_t *blob_ptr = (uintptr_t *)(ctx->blob->data + fixup->blob_offset);
693 blob_ptr[0] = write_lookup_object(ctx, fixup->src);
694 blob_ptr[1] = write_lookup_object(ctx, fixup->block);
695 }
696
697 util_dynarray_clear(&ctx->phi_fixups);
698 }
699
700 static nir_phi_instr *
read_phi(read_ctx * ctx,nir_block * blk)701 read_phi(read_ctx *ctx, nir_block *blk)
702 {
703 nir_phi_instr *phi = nir_phi_instr_create(ctx->nir);
704
705 read_dest(ctx, &phi->dest, &phi->instr);
706
707 unsigned num_srcs = blob_read_uint32(ctx->blob);
708
709 /* For similar reasons as before, we just store the index directly into the
710 * pointer, and let a later pass resolve the phi sources.
711 *
712 * In order to ensure that the copied sources (which are just the indices
713 * from the blob for now) don't get inserted into the old shader's use-def
714 * lists, we have to add the phi instruction *before* we set up its
715 * sources.
716 */
717 nir_instr_insert_after_block(blk, &phi->instr);
718
719 for (unsigned i = 0; i < num_srcs; i++) {
720 nir_phi_src *src = ralloc(phi, nir_phi_src);
721
722 src->src.is_ssa = true;
723 src->src.ssa = (nir_ssa_def *) blob_read_intptr(ctx->blob);
724 src->pred = (nir_block *) blob_read_intptr(ctx->blob);
725
726 /* Since we're not letting nir_insert_instr handle use/def stuff for us,
727 * we have to set the parent_instr manually. It doesn't really matter
728 * when we do it, so we might as well do it here.
729 */
730 src->src.parent_instr = &phi->instr;
731
732 /* Stash it in the list of phi sources. We'll walk this list and fix up
733 * sources at the very end of read_function_impl.
734 */
735 list_add(&src->src.use_link, &ctx->phi_srcs);
736
737 exec_list_push_tail(&phi->srcs, &src->node);
738 }
739
740 return phi;
741 }
742
743 static void
read_fixup_phis(read_ctx * ctx)744 read_fixup_phis(read_ctx *ctx)
745 {
746 list_for_each_entry_safe(nir_phi_src, src, &ctx->phi_srcs, src.use_link) {
747 src->pred = read_lookup_object(ctx, (uintptr_t)src->pred);
748 src->src.ssa = read_lookup_object(ctx, (uintptr_t)src->src.ssa);
749
750 /* Remove from this list */
751 list_del(&src->src.use_link);
752
753 list_addtail(&src->src.use_link, &src->src.ssa->uses);
754 }
755 assert(list_empty(&ctx->phi_srcs));
756 }
757
758 static void
write_jump(write_ctx * ctx,const nir_jump_instr * jmp)759 write_jump(write_ctx *ctx, const nir_jump_instr *jmp)
760 {
761 blob_write_uint32(ctx->blob, jmp->type);
762 }
763
764 static nir_jump_instr *
read_jump(read_ctx * ctx)765 read_jump(read_ctx *ctx)
766 {
767 nir_jump_type type = blob_read_uint32(ctx->blob);
768 nir_jump_instr *jmp = nir_jump_instr_create(ctx->nir, type);
769 return jmp;
770 }
771
772 static void
write_call(write_ctx * ctx,const nir_call_instr * call)773 write_call(write_ctx *ctx, const nir_call_instr *call)
774 {
775 blob_write_intptr(ctx->blob, write_lookup_object(ctx, call->callee));
776
777 for (unsigned i = 0; i < call->num_params; i++)
778 write_deref_chain(ctx, call->params[i]);
779
780 write_deref_chain(ctx, call->return_deref);
781 }
782
783 static nir_call_instr *
read_call(read_ctx * ctx)784 read_call(read_ctx *ctx)
785 {
786 nir_function *callee = read_object(ctx);
787 nir_call_instr *call = nir_call_instr_create(ctx->nir, callee);
788
789 for (unsigned i = 0; i < call->num_params; i++)
790 call->params[i] = read_deref_chain(ctx, &call->instr);
791
792 call->return_deref = read_deref_chain(ctx, &call->instr);
793
794 return call;
795 }
796
797 static void
write_instr(write_ctx * ctx,const nir_instr * instr)798 write_instr(write_ctx *ctx, const nir_instr *instr)
799 {
800 blob_write_uint32(ctx->blob, instr->type);
801 switch (instr->type) {
802 case nir_instr_type_alu:
803 write_alu(ctx, nir_instr_as_alu(instr));
804 break;
805 case nir_instr_type_intrinsic:
806 write_intrinsic(ctx, nir_instr_as_intrinsic(instr));
807 break;
808 case nir_instr_type_load_const:
809 write_load_const(ctx, nir_instr_as_load_const(instr));
810 break;
811 case nir_instr_type_ssa_undef:
812 write_ssa_undef(ctx, nir_instr_as_ssa_undef(instr));
813 break;
814 case nir_instr_type_tex:
815 write_tex(ctx, nir_instr_as_tex(instr));
816 break;
817 case nir_instr_type_phi:
818 write_phi(ctx, nir_instr_as_phi(instr));
819 break;
820 case nir_instr_type_jump:
821 write_jump(ctx, nir_instr_as_jump(instr));
822 break;
823 case nir_instr_type_call:
824 write_call(ctx, nir_instr_as_call(instr));
825 break;
826 case nir_instr_type_parallel_copy:
827 unreachable("Cannot write parallel copies");
828 default:
829 unreachable("bad instr type");
830 }
831 }
832
833 static void
read_instr(read_ctx * ctx,nir_block * block)834 read_instr(read_ctx *ctx, nir_block *block)
835 {
836 nir_instr_type type = blob_read_uint32(ctx->blob);
837 nir_instr *instr;
838 switch (type) {
839 case nir_instr_type_alu:
840 instr = &read_alu(ctx)->instr;
841 break;
842 case nir_instr_type_intrinsic:
843 instr = &read_intrinsic(ctx)->instr;
844 break;
845 case nir_instr_type_load_const:
846 instr = &read_load_const(ctx)->instr;
847 break;
848 case nir_instr_type_ssa_undef:
849 instr = &read_ssa_undef(ctx)->instr;
850 break;
851 case nir_instr_type_tex:
852 instr = &read_tex(ctx)->instr;
853 break;
854 case nir_instr_type_phi:
855 /* Phi instructions are a bit of a special case when reading because we
856 * don't want inserting the instruction to automatically handle use/defs
857 * for us. Instead, we need to wait until all the blocks/instructions
858 * are read so that we can set their sources up.
859 */
860 read_phi(ctx, block);
861 return;
862 case nir_instr_type_jump:
863 instr = &read_jump(ctx)->instr;
864 break;
865 case nir_instr_type_call:
866 instr = &read_call(ctx)->instr;
867 break;
868 case nir_instr_type_parallel_copy:
869 unreachable("Cannot read parallel copies");
870 default:
871 unreachable("bad instr type");
872 }
873
874 nir_instr_insert_after_block(block, instr);
875 }
876
877 static void
write_block(write_ctx * ctx,const nir_block * block)878 write_block(write_ctx *ctx, const nir_block *block)
879 {
880 write_add_object(ctx, block);
881 blob_write_uint32(ctx->blob, exec_list_length(&block->instr_list));
882 nir_foreach_instr(instr, block)
883 write_instr(ctx, instr);
884 }
885
886 static void
read_block(read_ctx * ctx,struct exec_list * cf_list)887 read_block(read_ctx *ctx, struct exec_list *cf_list)
888 {
889 /* Don't actually create a new block. Just use the one from the tail of
890 * the list. NIR guarantees that the tail of the list is a block and that
891 * no two blocks are side-by-side in the IR; It should be empty.
892 */
893 nir_block *block =
894 exec_node_data(nir_block, exec_list_get_tail(cf_list), cf_node.node);
895
896 read_add_object(ctx, block);
897 unsigned num_instrs = blob_read_uint32(ctx->blob);
898 for (unsigned i = 0; i < num_instrs; i++) {
899 read_instr(ctx, block);
900 }
901 }
902
903 static void
904 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list);
905
906 static void
907 read_cf_list(read_ctx *ctx, struct exec_list *cf_list);
908
909 static void
write_if(write_ctx * ctx,nir_if * nif)910 write_if(write_ctx *ctx, nir_if *nif)
911 {
912 write_src(ctx, &nif->condition);
913
914 write_cf_list(ctx, &nif->then_list);
915 write_cf_list(ctx, &nif->else_list);
916 }
917
918 static void
read_if(read_ctx * ctx,struct exec_list * cf_list)919 read_if(read_ctx *ctx, struct exec_list *cf_list)
920 {
921 nir_if *nif = nir_if_create(ctx->nir);
922
923 read_src(ctx, &nif->condition, nif);
924
925 nir_cf_node_insert_end(cf_list, &nif->cf_node);
926
927 read_cf_list(ctx, &nif->then_list);
928 read_cf_list(ctx, &nif->else_list);
929 }
930
931 static void
write_loop(write_ctx * ctx,nir_loop * loop)932 write_loop(write_ctx *ctx, nir_loop *loop)
933 {
934 write_cf_list(ctx, &loop->body);
935 }
936
937 static void
read_loop(read_ctx * ctx,struct exec_list * cf_list)938 read_loop(read_ctx *ctx, struct exec_list *cf_list)
939 {
940 nir_loop *loop = nir_loop_create(ctx->nir);
941
942 nir_cf_node_insert_end(cf_list, &loop->cf_node);
943
944 read_cf_list(ctx, &loop->body);
945 }
946
947 static void
write_cf_node(write_ctx * ctx,nir_cf_node * cf)948 write_cf_node(write_ctx *ctx, nir_cf_node *cf)
949 {
950 blob_write_uint32(ctx->blob, cf->type);
951
952 switch (cf->type) {
953 case nir_cf_node_block:
954 write_block(ctx, nir_cf_node_as_block(cf));
955 break;
956 case nir_cf_node_if:
957 write_if(ctx, nir_cf_node_as_if(cf));
958 break;
959 case nir_cf_node_loop:
960 write_loop(ctx, nir_cf_node_as_loop(cf));
961 break;
962 default:
963 unreachable("bad cf type");
964 }
965 }
966
967 static void
read_cf_node(read_ctx * ctx,struct exec_list * list)968 read_cf_node(read_ctx *ctx, struct exec_list *list)
969 {
970 nir_cf_node_type type = blob_read_uint32(ctx->blob);
971
972 switch (type) {
973 case nir_cf_node_block:
974 read_block(ctx, list);
975 break;
976 case nir_cf_node_if:
977 read_if(ctx, list);
978 break;
979 case nir_cf_node_loop:
980 read_loop(ctx, list);
981 break;
982 default:
983 unreachable("bad cf type");
984 }
985 }
986
987 static void
write_cf_list(write_ctx * ctx,const struct exec_list * cf_list)988 write_cf_list(write_ctx *ctx, const struct exec_list *cf_list)
989 {
990 blob_write_uint32(ctx->blob, exec_list_length(cf_list));
991 foreach_list_typed(nir_cf_node, cf, node, cf_list) {
992 write_cf_node(ctx, cf);
993 }
994 }
995
996 static void
read_cf_list(read_ctx * ctx,struct exec_list * cf_list)997 read_cf_list(read_ctx *ctx, struct exec_list *cf_list)
998 {
999 uint32_t num_cf_nodes = blob_read_uint32(ctx->blob);
1000 for (unsigned i = 0; i < num_cf_nodes; i++)
1001 read_cf_node(ctx, cf_list);
1002 }
1003
1004 static void
write_function_impl(write_ctx * ctx,const nir_function_impl * fi)1005 write_function_impl(write_ctx *ctx, const nir_function_impl *fi)
1006 {
1007 write_var_list(ctx, &fi->locals);
1008 write_reg_list(ctx, &fi->registers);
1009 blob_write_uint32(ctx->blob, fi->reg_alloc);
1010
1011 blob_write_uint32(ctx->blob, fi->num_params);
1012 for (unsigned i = 0; i < fi->num_params; i++) {
1013 write_variable(ctx, fi->params[i]);
1014 }
1015
1016 blob_write_uint32(ctx->blob, !!(fi->return_var));
1017 if (fi->return_var)
1018 write_variable(ctx, fi->return_var);
1019
1020 write_cf_list(ctx, &fi->body);
1021 write_fixup_phis(ctx);
1022 }
1023
1024 static nir_function_impl *
read_function_impl(read_ctx * ctx,nir_function * fxn)1025 read_function_impl(read_ctx *ctx, nir_function *fxn)
1026 {
1027 nir_function_impl *fi = nir_function_impl_create_bare(ctx->nir);
1028 fi->function = fxn;
1029
1030 read_var_list(ctx, &fi->locals);
1031 read_reg_list(ctx, &fi->registers);
1032 fi->reg_alloc = blob_read_uint32(ctx->blob);
1033
1034 fi->num_params = blob_read_uint32(ctx->blob);
1035 for (unsigned i = 0; i < fi->num_params; i++) {
1036 fi->params[i] = read_variable(ctx);
1037 }
1038
1039 bool has_return = blob_read_uint32(ctx->blob);
1040 if (has_return)
1041 fi->return_var = read_variable(ctx);
1042 else
1043 fi->return_var = NULL;
1044
1045 read_cf_list(ctx, &fi->body);
1046 read_fixup_phis(ctx);
1047
1048 fi->valid_metadata = 0;
1049
1050 return fi;
1051 }
1052
1053 static void
write_function(write_ctx * ctx,const nir_function * fxn)1054 write_function(write_ctx *ctx, const nir_function *fxn)
1055 {
1056 blob_write_uint32(ctx->blob, !!(fxn->name));
1057 if (fxn->name)
1058 blob_write_string(ctx->blob, fxn->name);
1059
1060 write_add_object(ctx, fxn);
1061
1062 blob_write_uint32(ctx->blob, fxn->num_params);
1063 for (unsigned i = 0; i < fxn->num_params; i++) {
1064 blob_write_uint32(ctx->blob, fxn->params[i].param_type);
1065 encode_type_to_blob(ctx->blob, fxn->params[i].type);
1066 }
1067
1068 encode_type_to_blob(ctx->blob, fxn->return_type);
1069
1070 /* At first glance, it looks like we should write the function_impl here.
1071 * However, call instructions need to be able to reference at least the
1072 * function and those will get processed as we write the function_impls.
1073 * We stop here and write function_impls as a second pass.
1074 */
1075 }
1076
1077 static void
read_function(read_ctx * ctx)1078 read_function(read_ctx *ctx)
1079 {
1080 bool has_name = blob_read_uint32(ctx->blob);
1081 char *name = has_name ? blob_read_string(ctx->blob) : NULL;
1082
1083 nir_function *fxn = nir_function_create(ctx->nir, name);
1084
1085 read_add_object(ctx, fxn);
1086
1087 fxn->num_params = blob_read_uint32(ctx->blob);
1088 for (unsigned i = 0; i < fxn->num_params; i++) {
1089 fxn->params[i].param_type = blob_read_uint32(ctx->blob);
1090 fxn->params[i].type = decode_type_from_blob(ctx->blob);
1091 }
1092
1093 fxn->return_type = decode_type_from_blob(ctx->blob);
1094 }
1095
1096 void
nir_serialize(struct blob * blob,const nir_shader * nir)1097 nir_serialize(struct blob *blob, const nir_shader *nir)
1098 {
1099 write_ctx ctx;
1100 ctx.remap_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
1101 _mesa_key_pointer_equal);
1102 ctx.next_idx = 0;
1103 ctx.blob = blob;
1104 ctx.nir = nir;
1105 util_dynarray_init(&ctx.phi_fixups, NULL);
1106
1107 size_t idx_size_offset = blob_reserve_intptr(blob);
1108
1109 struct shader_info info = nir->info;
1110 uint32_t strings = 0;
1111 if (info.name)
1112 strings |= 0x1;
1113 if (info.label)
1114 strings |= 0x2;
1115 blob_write_uint32(blob, strings);
1116 if (info.name)
1117 blob_write_string(blob, info.name);
1118 if (info.label)
1119 blob_write_string(blob, info.label);
1120 info.name = info.label = NULL;
1121 blob_write_bytes(blob, (uint8_t *) &info, sizeof(info));
1122
1123 write_var_list(&ctx, &nir->uniforms);
1124 write_var_list(&ctx, &nir->inputs);
1125 write_var_list(&ctx, &nir->outputs);
1126 write_var_list(&ctx, &nir->shared);
1127 write_var_list(&ctx, &nir->globals);
1128 write_var_list(&ctx, &nir->system_values);
1129
1130 write_reg_list(&ctx, &nir->registers);
1131 blob_write_uint32(blob, nir->reg_alloc);
1132 blob_write_uint32(blob, nir->num_inputs);
1133 blob_write_uint32(blob, nir->num_uniforms);
1134 blob_write_uint32(blob, nir->num_outputs);
1135 blob_write_uint32(blob, nir->num_shared);
1136
1137 blob_write_uint32(blob, exec_list_length(&nir->functions));
1138 nir_foreach_function(fxn, nir) {
1139 write_function(&ctx, fxn);
1140 }
1141
1142 nir_foreach_function(fxn, nir) {
1143 write_function_impl(&ctx, fxn->impl);
1144 }
1145
1146 *(uintptr_t *)(blob->data + idx_size_offset) = ctx.next_idx;
1147
1148 _mesa_hash_table_destroy(ctx.remap_table, NULL);
1149 util_dynarray_fini(&ctx.phi_fixups);
1150 }
1151
1152 nir_shader *
nir_deserialize(void * mem_ctx,const struct nir_shader_compiler_options * options,struct blob_reader * blob)1153 nir_deserialize(void *mem_ctx,
1154 const struct nir_shader_compiler_options *options,
1155 struct blob_reader *blob)
1156 {
1157 read_ctx ctx;
1158 ctx.blob = blob;
1159 list_inithead(&ctx.phi_srcs);
1160 ctx.idx_table_len = blob_read_intptr(blob);
1161 ctx.idx_table = calloc(ctx.idx_table_len, sizeof(uintptr_t));
1162 ctx.next_idx = 0;
1163
1164 uint32_t strings = blob_read_uint32(blob);
1165 char *name = (strings & 0x1) ? blob_read_string(blob) : NULL;
1166 char *label = (strings & 0x2) ? blob_read_string(blob) : NULL;
1167
1168 struct shader_info info;
1169 blob_copy_bytes(blob, (uint8_t *) &info, sizeof(info));
1170
1171 ctx.nir = nir_shader_create(mem_ctx, info.stage, options, NULL);
1172
1173 info.name = name ? ralloc_strdup(ctx.nir, name) : NULL;
1174 info.label = label ? ralloc_strdup(ctx.nir, label) : NULL;
1175
1176 ctx.nir->info = info;
1177
1178 read_var_list(&ctx, &ctx.nir->uniforms);
1179 read_var_list(&ctx, &ctx.nir->inputs);
1180 read_var_list(&ctx, &ctx.nir->outputs);
1181 read_var_list(&ctx, &ctx.nir->shared);
1182 read_var_list(&ctx, &ctx.nir->globals);
1183 read_var_list(&ctx, &ctx.nir->system_values);
1184
1185 read_reg_list(&ctx, &ctx.nir->registers);
1186 ctx.nir->reg_alloc = blob_read_uint32(blob);
1187 ctx.nir->num_inputs = blob_read_uint32(blob);
1188 ctx.nir->num_uniforms = blob_read_uint32(blob);
1189 ctx.nir->num_outputs = blob_read_uint32(blob);
1190 ctx.nir->num_shared = blob_read_uint32(blob);
1191
1192 unsigned num_functions = blob_read_uint32(blob);
1193 for (unsigned i = 0; i < num_functions; i++)
1194 read_function(&ctx);
1195
1196 nir_foreach_function(fxn, ctx.nir)
1197 fxn->impl = read_function_impl(&ctx, fxn);
1198
1199 free(ctx.idx_table);
1200
1201 return ctx.nir;
1202 }
1203
1204 nir_shader *
nir_shader_serialize_deserialize(void * mem_ctx,nir_shader * s)1205 nir_shader_serialize_deserialize(void *mem_ctx, nir_shader *s)
1206 {
1207 const struct nir_shader_compiler_options *options = s->options;
1208
1209 struct blob writer;
1210 blob_init(&writer);
1211 nir_serialize(&writer, s);
1212 ralloc_free(s);
1213
1214 struct blob_reader reader;
1215 blob_reader_init(&reader, writer.data, writer.size);
1216 nir_shader *ns = nir_deserialize(mem_ctx, options, &reader);
1217
1218 blob_finish(&writer);
1219
1220 return ns;
1221 }
1222