diff --git a/src/lava/magma/compiler/compiler.py b/src/lava/magma/compiler/compiler.py index c6011c562..a0add53de 100644 --- a/src/lava/magma/compiler/compiler.py +++ b/src/lava/magma/compiler/compiler.py @@ -299,13 +299,42 @@ def _create_subcompilers( initialized with the Processes they will compile. """ subcompilers = [] + c_idx = [] + nc_idx = [] # Go through all required subcompiler classes... - for subcompiler_class, procs in compiler_type_to_procs.items(): + for idx, (subcompiler_class, procs) in \ + enumerate(compiler_type_to_procs.items()): # ...create the subcompiler instance... compiler = subcompiler_class(procs, self._compile_config) # ...and add it to the list. subcompilers.append(compiler) - + # Remember the index for C and Nc subcompilers: + if isinstance(compiler, CProcCompiler): + c_idx.append(idx) + if isinstance(compiler, NcProcCompiler): + nc_idx.append(idx) + + # Implement the heuristic "C-first Nc-second" whenever C and Nc + # compilers are in the list. The subcompiler.compile is called in + # their order of appearance in this list. + # ToDo: In the implementation below, we assume that there is only one + # instance of each subcompiler (Py/C/Nc) per ProcGroup. This should + # be true as long as the `compiler_type_to_procs` mapping comes from + # `self._map_subcompiler_type_to_procs`. + # 1. Confirm that there is only one instance of C and Nc subcompilers + if len(c_idx) > 1 or len(nc_idx) > 1: + raise AssertionError("More than one instance of C or Nc " + "subcompiler detected.") + # 2. If the index of C subcompiler is larger (appears later in the + # list), then swap it with Nc subcompiler. + if len(c_idx) > 0 and len(nc_idx) > 0: + if c_idx[0] > nc_idx[0]: + subcompilers[c_idx[0]], subcompilers[nc_idx[0]] = subcompilers[ + nc_idx[0]], subcompilers[c_idx[0]] + # We have ensured that C subcompiler appears before Nc subcompiler in + # the list we return. As compile() is called serially on each + # subcompiler, C Processes will be compiled before Nc Processes + # within a ProcGroup. return subcompilers @staticmethod