From 19ce37524763b67f4a282eac264f1700bf561bf6 Mon Sep 17 00:00:00 2001
From: auphelia <jakobapk@web.de>
Date: Tue, 7 Jan 2020 17:14:41 +0000
Subject: [PATCH] [Trafo-IP generation] Added first draft of code generation
 transformation for ip generation

---
 .../fpgadataflow/codegen_ipgen.py             | 43 +++++++++++++++++++
 1 file changed, 43 insertions(+)
 create mode 100644 src/finn/transformation/fpgadataflow/codegen_ipgen.py

diff --git a/src/finn/transformation/fpgadataflow/codegen_ipgen.py b/src/finn/transformation/fpgadataflow/codegen_ipgen.py
new file mode 100644
index 000000000..683c7f64c
--- /dev/null
+++ b/src/finn/transformation/fpgadataflow/codegen_ipgen.py
@@ -0,0 +1,43 @@
+import os
+import tempfile as tmp
+
+import finn.custom_op.registry as registry
+from finn.core.utils import get_by_name
+from finn.transformation import Transformation
+
+
+def _codegen_single_node(node, model):
+    """Call custom implementation to generate code for single custom node
+    and create folder that contains all the generated files"""
+    op_type = node.op_type
+    try:
+        # lookup op_type in registry of CustomOps
+        inst = registry.custom_op[op_type](node)
+        # get the path of the code generation directory
+        code_gen_dir = inst.get_nodeattr("code_gen_dir_ipgen")
+        # ensure that there is a directory
+        if code_gen_dir == "" or not os.path.isdir(code_gen_dir):
+            code_gen_dir = tmp.mkdtemp(
+                prefix="code_gen_ipgen" + str(node.op_type) + "_"
+            )
+            inst.set_nodeattr("code_gen_dir_ipgen", code_gen_dir)
+        # ensure that there is generated code inside the dir
+        inst.code_generation_ipgen(model)
+    except KeyError:
+        # exception if op_type is not supported
+        raise Exception("Custom op_type %s is currently not supported." % op_type)
+
+
+class CodeGen_ipgen(Transformation):
+    """Code generation for all nodes in model"""
+
+    def apply(self, model):
+        for node in model.graph.node:
+            if node.domain == "finn":
+                backend_attribute = get_by_name(node.attribute, "backend")
+                if backend_attribute is None:
+                    continue
+                backend_value = backend_attribute.s.decode("UTF-8")
+                if backend_value == "fpgadataflow":
+                    _codegen_single_node(node, model)
+        return (model, False)
-- 
GitLab