MegDNN param structures are not packed and we need to initialize the structure paddings to zero or it would break MegBrain hash system. We do memset(0) in default ctor and use a trick, wrapping non-static members in a anonymous union which would copy the object repre
(self, p)
| 709 | ) |
| 710 | |
| 711 | def _on_param_end(self, p): |
| 712 | """ |
| 713 | MegDNN param structures are not packed and we need to initialize the structure |
| 714 | paddings to zero or it would break MegBrain hash system. We do memset(0) in default |
| 715 | ctor and use a trick, wrapping non-static members in a anonymous union which would |
| 716 | copy the object representation in its default copy/move ctor, for copy/move ctor. |
| 717 | > The implicitly-defined copy/move constructor for a non-union class X performs |
| 718 | > a memberwise copy/move of its bases and members. [class.copy.ctor 14] |
| 719 | > The implicitly-defined copy/move constructor for a union X copies the object |
| 720 | > representation (6.9) of X. [class.copy.ctor 15] |
| 721 | """ |
| 722 | if self._non_static_members: |
| 723 | self._write("union { struct {") |
| 724 | for i in self._non_static_members: |
| 725 | if isinstance(i, member_defs.Field): |
| 726 | self._write_doc(i.name) |
| 727 | self._write("%s%s %s;", i.dtype.cname_attr, i.dtype.cname, i.name) |
| 728 | else: |
| 729 | assert isinstance(i, (member_defs.Enum, member_defs.EnumAlias)) |
| 730 | self._write("%s %s;", i.name, i.name_field) |
| 731 | self._write("}; };") |
| 732 | if self._ctor_args: |
| 733 | pdefs, varnames = zip(*self._ctor_args) |
| 734 | self._write("%s(%s) {", p.name, ", ".join(pdefs), indent=1) |
| 735 | self._write("memset(this, 0, sizeof(*this));") |
| 736 | for var in varnames: |
| 737 | self._write("this->%s = %s_;", var, var) |
| 738 | self._write("}", indent=-1) |
| 739 | self._write("};\n", indent=-1) |
| 740 | |
| 741 | def _on_member_enum(self, e): |
| 742 | self._write_doc(e.name) |
nothing calls this directly
no test coverage detected