[Mesa-dev] [wip 2/9] glsl: serialize methods for IR instructions

Tapani Pälli tapani.palli at intel.com
Thu Jan 2 03:58:14 PST 2014


Patch adds a new virtual function for ir_instruction base class which
has to be implemented by each instruction. This data will be used by the
shader binary cache implementation.

Signed-off-by: Tapani Pälli <tapani.palli at intel.com>
---
 src/glsl/Makefile.sources |   1 +
 src/glsl/ir.h             |  45 ++++++
 src/glsl/ir_serialize.cpp | 392 ++++++++++++++++++++++++++++++++++++++++++++++
 src/glsl/ir_serialize.h   |  35 +++++
 4 files changed, 473 insertions(+)
 create mode 100644 src/glsl/ir_serialize.cpp
 create mode 100644 src/glsl/ir_serialize.h

diff --git a/src/glsl/Makefile.sources b/src/glsl/Makefile.sources
index 2e81ded..5573d00 100644
--- a/src/glsl/Makefile.sources
+++ b/src/glsl/Makefile.sources
@@ -45,6 +45,7 @@ LIBGLSL_FILES = \
 	$(GLSL_SRCDIR)/ir_reader.cpp \
 	$(GLSL_SRCDIR)/ir_rvalue_visitor.cpp \
 	$(GLSL_SRCDIR)/ir_set_program_inouts.cpp \
+	$(GLSL_SRCDIR)/ir_serialize.cpp \
 	$(GLSL_SRCDIR)/ir_validate.cpp \
 	$(GLSL_SRCDIR)/ir_variable_refcount.cpp \
 	$(GLSL_SRCDIR)/linker.cpp \
diff --git a/src/glsl/ir.h b/src/glsl/ir.h
index 780959b..71ddf87 100644
--- a/src/glsl/ir.h
+++ b/src/glsl/ir.h
@@ -35,6 +35,7 @@
 #include "ir_visitor.h"
 #include "ir_hierarchical_visitor.h"
 #include "main/mtypes.h"
+#include "memory_writer.h"
 
 #ifdef __cplusplus
 
@@ -107,6 +108,10 @@ public:
    /** ir_print_visitor helper for debugging. */
    void print(void) const;
 
+   /* serialization */
+   void serialize(memory_writer &mem);
+   virtual void serialize_data(memory_writer &mem) = 0;
+
    virtual void accept(ir_visitor *) = 0;
    virtual ir_visitor_status accept(ir_hierarchical_visitor *) = 0;
    virtual ir_instruction *clone(void *mem_ctx,
@@ -167,6 +172,8 @@ public:
 
    virtual ir_rvalue *clone(void *mem_ctx, struct hash_table *) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -379,6 +386,8 @@ public:
       return this;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -769,6 +778,8 @@ public:
    ir_function_signature *clone_prototype(void *mem_ctx,
 					  struct hash_table *ht) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -900,6 +911,8 @@ public:
       return this;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -982,6 +995,8 @@ public:
       return this;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1006,6 +1021,8 @@ public:
 
    virtual ir_loop *clone(void *mem_ctx, struct hash_table *ht) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1049,6 +1066,8 @@ public:
 
    virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1475,6 +1494,8 @@ public:
     */
    static ir_expression_operation get_operator(const char *);
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1513,6 +1534,8 @@ public:
       return this;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1607,6 +1630,8 @@ public:
       return value;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1641,6 +1666,8 @@ public:
 
    virtual ir_loop_jump *clone(void *mem_ctx, struct hash_table *) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1681,6 +1708,8 @@ public:
 
    virtual ir_discard *clone(void *mem_ctx, struct hash_table *ht) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1753,6 +1782,8 @@ public:
 
    virtual ir_constant *constant_expression_value(struct hash_table *variable_context = NULL);
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1864,6 +1895,8 @@ public:
     */
    static ir_swizzle *create(ir_rvalue *, const char *, unsigned vector_length);
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -1967,6 +2000,8 @@ public:
       return this->var;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -2016,6 +2051,8 @@ public:
     */
    virtual void constant_referenced(struct hash_table *variable_context, ir_constant *&store, int &offset) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -2064,6 +2101,8 @@ public:
     */
    virtual void constant_referenced(struct hash_table *variable_context, ir_constant *&store, int &offset) const;
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -2126,6 +2165,8 @@ public:
       return this;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -2225,6 +2266,8 @@ public:
       ir_type = ir_type_emit_vertex;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
@@ -2249,6 +2292,8 @@ public:
       ir_type = ir_type_end_primitive;
    }
 
+   virtual void serialize_data(memory_writer &mem);
+
    virtual void accept(ir_visitor *v)
    {
       v->visit(this);
diff --git a/src/glsl/ir_serialize.cpp b/src/glsl/ir_serialize.cpp
new file mode 100644
index 0000000..30ca018
--- /dev/null
+++ b/src/glsl/ir_serialize.cpp
@@ -0,0 +1,392 @@
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include "ir_serialize.h"
+
+
+/**
+ * Wraps serialization of an ir instruction, writes ir_type
+ * and length of each instruction package as a header for it
+ */
+void
+ir_instruction::serialize(memory_writer &mem)
+{
+   uint32_t data_len = 0;
+   uint8_t ir_type = this->ir_type;
+   mem.write_uint8_t(ir_type);
+
+   int32_t start_pos = mem.position();
+   mem.write_uint32_t(data_len);
+
+   this->serialize_data(mem);
+
+   data_len = mem.position() - start_pos - sizeof(data_len);
+   mem.overwrite(&data_len, sizeof(data_len), start_pos);
+}
+
+
+/**
+ * Wraps rvalue serialization, rvalue has its type or
+ * ir_type_unset written before it to indicate if value is NULL
+ */
+static void
+serialize_rvalue(ir_rvalue *val, memory_writer &mem)
+{
+   uint8_t ir_type = val ? val->ir_type : ir_type_unset;
+   mem.write_uint8_t(ir_type);
+
+   if (val)
+      val->serialize(mem);
+}
+
+
+/**
+ * Serialization of exec_list, writes length of the list +
+ * calls serialize_data for each instruction
+ */
+void
+serialize_list(exec_list &list, memory_writer &mem)
+{
+   uint32_t list_len = 0;
+   foreach_iter(exec_list_iterator, iter, list)
+      list_len++;
+
+   mem.write_uint32_t(list_len);
+
+   foreach_iter(exec_list_iterator, iter, list)
+      ((ir_instruction *)iter.get())->serialize(mem);
+}
+
+
+static void
+serialize_glsl_type(const glsl_type *type, memory_writer &mem)
+{
+   uint32_t data_len = 0;
+
+   mem.write_string(type->name);
+
+   int32_t start_pos = mem.position();
+   mem.write_uint32_t(data_len);
+
+   uint8_t base_type, vector_elms, matrix_cols, interface_packing;
+   uint32_t length;
+
+   /* skip builtin types */
+   if (type->base_type == GLSL_TYPE_SAMPLER)
+      goto glsl_type_serilization_epilogue;
+
+   /* actual glsl_type data */
+   base_type = type->base_type;
+   length = type->length;
+   vector_elms = type->vector_elements;
+   matrix_cols = type->matrix_columns;
+   interface_packing = type->interface_packing;
+
+   mem.write_uint8_t(base_type);
+   mem.write_uint32_t(length);
+   mem.write_uint8_t(vector_elms);
+   mem.write_uint8_t(matrix_cols);
+   mem.write_uint8_t(interface_packing);
+
+   if (type->base_type == GLSL_TYPE_ARRAY)
+      serialize_glsl_type(type->element_type(), mem);
+   else if (type->base_type == GLSL_TYPE_STRUCT ||
+      type->base_type == GLSL_TYPE_INTERFACE) {
+      glsl_struct_field *field = type->fields.structure;
+      for (unsigned k = 0; k < length; k++, field++) {
+         mem.write_string(field->name);
+         serialize_glsl_type(field->type, mem);
+         uint8_t row_major = field->row_major;
+         uint8_t interpolation = field->interpolation;
+         uint8_t centroid = field->centroid;
+         mem.write_uint8_t(row_major);
+         mem.write_int32_t(field->location);
+         mem.write_uint8_t(interpolation);
+         mem.write_uint8_t(centroid);
+      }
+   }
+
+glsl_type_serilization_epilogue:
+
+   data_len = mem.position() - start_pos - sizeof(data_len);
+   mem.overwrite(&data_len, sizeof(data_len), start_pos);
+}
+
+
+void
+ir_variable::serialize_data(memory_writer &mem)
+{
+   /* name can be NULL, see ir_print_visitor for explanation */
+   const char *non_null_name = name ? name : "parameter";
+   int64_t unique_id = (int64_t) (intptr_t) this;
+   uint8_t mode = data.mode;
+   uint8_t has_constant_value = constant_value ? 1 : 0;
+   uint8_t has_constant_initializer = constant_initializer ? 1 : 0;
+
+   serialize_glsl_type(type, mem);
+
+   mem.write_string(non_null_name);
+   mem.write_int64_t(unique_id);
+   mem.write_uint8_t(mode);
+
+   mem.write(&data, sizeof(data));
+
+   mem.write_uint32_t(num_state_slots);
+   mem.write_uint8_t(has_constant_value);
+   mem.write_uint8_t(has_constant_initializer);
+
+   for (unsigned i = 0; i < num_state_slots; i++) {
+      mem.write_int32_t(state_slots[i].swizzle);
+      for (unsigned j = 0; j < 5; j++) {
+         mem.write_int32_t(state_slots[i].tokens[j]);
+      }
+   }
+
+   if (constant_value)
+      constant_value->serialize(mem);
+
+   if (constant_initializer)
+      constant_initializer->serialize(mem);
+
+   uint8_t has_interface_type = get_interface_type() ? 1 : 0;
+
+   mem.write_uint8_t(has_interface_type);
+   if (has_interface_type)
+      serialize_glsl_type(get_interface_type(), mem);
+}
+
+
+void
+ir_assignment::serialize_data(memory_writer &mem)
+{
+   uint8_t assignment_mask = write_mask;
+   mem.write_uint8_t(assignment_mask);
+
+   serialize_rvalue(lhs, mem);
+   serialize_rvalue(condition, mem);
+   serialize_rvalue(rhs, mem);
+}
+
+
+void
+ir_call::serialize_data(memory_writer &mem)
+{
+   mem.write_string(callee_name());
+
+   uint8_t list_len = 0;
+   uint8_t uses_builtin = use_builtin;
+
+   serialize_rvalue(return_deref, mem);
+
+   /* call parameter list */
+   foreach_iter(exec_list_iterator, iter, *this)
+      list_len++;
+
+   mem.write_uint8_t(list_len);
+
+   foreach_iter(exec_list_iterator, iter, *this)
+      serialize_rvalue((ir_rvalue*)iter.get(), mem);
+
+   mem.write_uint8_t(uses_builtin);
+}
+
+
+void
+ir_constant::serialize_data(memory_writer &mem)
+{
+   serialize_glsl_type(type, mem);
+
+   mem.write(&value, sizeof(ir_constant_data));
+
+   if (array_elements) {
+      for (unsigned i = 0; i < type->length; i++)
+         array_elements[i]->serialize(mem);
+   }
+
+   /* struct constant, dump components exec_list */
+   if (!components.is_empty())
+      serialize_list(components, mem);
+}
+
+
+void
+ir_dereference_array::serialize_data(memory_writer &mem)
+{
+   serialize_rvalue(array, mem);
+   serialize_rvalue(array_index, mem);
+}
+
+
+void
+ir_dereference_record::serialize_data(memory_writer &mem)
+{
+   mem.write_string(field);
+   serialize_rvalue(record, mem);
+}
+
+
+
+/**
+ * address of the variable is used as unique identifier for it
+ */
+void
+ir_dereference_variable::serialize_data(memory_writer &mem)
+{
+   mem.write_int64_t((int64_t) (intptr_t) var);
+}
+
+
+void
+ir_discard::serialize_data(memory_writer &mem)
+{
+   serialize_rvalue(condition, mem);
+}
+
+
+void
+ir_expression::serialize_data(memory_writer &mem)
+{
+   uint32_t num_operands = get_num_operands();
+   uint32_t exp_operation = operation;
+
+   serialize_glsl_type(type, mem);
+
+   mem.write_uint32_t(exp_operation);
+   mem.write_uint32_t(num_operands);
+
+   for (unsigned i = 0; i < num_operands; i++)
+      serialize_rvalue(operands[i], mem);
+}
+
+
+void
+ir_function::serialize_data(memory_writer &mem)
+{
+   mem.write_string(name);
+   serialize_list(signatures, mem);
+}
+
+
+void
+ir_function_signature::serialize_data(memory_writer &mem)
+{
+   uint8_t builtin_func = is_builtin();
+   mem.write_uint8_t(builtin_func);
+
+   /* dump the return type of function */
+   serialize_glsl_type(return_type, mem);
+
+   /* function parameters */
+   serialize_list(parameters, mem);
+
+   /* function body */
+   serialize_list(body, mem);
+}
+
+
+void
+ir_if::serialize_data(memory_writer &mem)
+{
+   serialize_rvalue(condition, mem);
+   serialize_list(then_instructions, mem);
+   serialize_list(else_instructions, mem);
+}
+
+
+void
+ir_loop::serialize_data(memory_writer &mem)
+{
+   serialize_list(body_instructions, mem);
+}
+
+
+void
+ir_loop_jump::serialize_data(memory_writer &mem)
+{
+   uint8_t jump_mode = mode;
+   mem.write_uint8_t(jump_mode);
+}
+
+
+void
+ir_return::serialize_data(memory_writer &mem)
+{
+   serialize_rvalue(value, mem);
+}
+
+
+void
+ir_swizzle::serialize_data(memory_writer &mem)
+{
+   mem.write(&mask, sizeof(ir_swizzle_mask));
+   serialize_rvalue(val, mem);
+}
+
+
+void
+ir_texture::serialize_data(memory_writer &mem)
+{
+   mem.write_int32_t((int32_t)op);
+
+   serialize_glsl_type(type, mem);
+
+   /* sampler */
+   mem.write_uint8_t((uint8_t)sampler->ir_type);
+
+   sampler->serialize(mem);
+
+   serialize_rvalue(coordinate, mem);
+   serialize_rvalue(projector, mem);
+   serialize_rvalue(shadow_comparitor, mem);
+   serialize_rvalue(offset, mem);
+
+   /* lod_info structure */
+   serialize_rvalue(lod_info.lod, mem);
+   serialize_rvalue(lod_info.bias, mem);
+   serialize_rvalue(lod_info.sample_index, mem);
+   serialize_rvalue(lod_info.component, mem);
+   serialize_rvalue(lod_info.grad.dPdx, mem);
+   serialize_rvalue(lod_info.grad.dPdy, mem);
+}
+
+
+void
+ir_emit_vertex::serialize_data(memory_writer &mem)
+{
+   /* no data */
+}
+
+
+void
+ir_end_primitive::serialize_data(memory_writer &mem)
+{
+   /* no data */
+}
+
+
+void
+ir_rvalue::serialize_data(memory_writer &mem)
+{
+   assert(0 && "unreachable");
+}
diff --git a/src/glsl/ir_serialize.h b/src/glsl/ir_serialize.h
new file mode 100644
index 0000000..dbfdbc9
--- /dev/null
+++ b/src/glsl/ir_serialize.h
@@ -0,0 +1,35 @@
+/* -*- c++ -*- */
+/*
+ * Copyright © 2013 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#pragma once
+#ifndef IR_SERIALIZE_H
+#define IR_SERIALIZE_H
+
+#include "ir.h"
+#include "memory_writer.h"
+
+void
+serialize_list(exec_list &list, memory_writer &mem);
+
+#endif /* IR_SERIALIZE_H */
-- 
1.8.3.1



More information about the mesa-dev mailing list