Deeploy.CommonExtensions.CodeTransformationPasses.Closure.ClosureGeneration

class Deeploy.CommonExtensions.CodeTransformationPasses.Closure.ClosureGeneration(closureCallTemplate: NodeTemplate = <Deeploy.DeeployTypes.NodeTemplate object>, closureSuffix='_closure', writeback: bool = True, generateStruct: bool = True)

Bases: CodeTransformationPass, IntrospectiveCodeTransformationMixIn

Code transformation pass for generating function closures.

This class transforms execution blocks into closure-based code patterns where functions are wrapped with argument structures. It generates the necessary struct definitions, closure functions, and call sites to enable closure-based execution patterns in generated code.

Notes

The closure generation process involves: 1. Analyzing the execution block to identify dynamic references 2. Creating a struct type to hold closure arguments 3. Generating the closure function definition 4. Replacing the original call with a closure call 5. Optionally generating argument writeback code

Methods

__init__(closureCallTemplate: NodeTemplate = <Deeploy.DeeployTypes.NodeTemplate object>, closureSuffix='_closure', writeback: bool = True, generateStruct: bool = True)

Initialize the ClosureGeneration transformation pass.

Parameters:
  • closureCallTemplate (NodeTemplate, optional) – Template for generating closure function calls. Default is the global _closureCallTemplate.

  • closureSuffix (str, optional) – Suffix to append to closure function names. Default is “_closure”.

  • writeback (bool, optional) – Whether to generate writeback code for closure arguments. Default is True.

  • generateStruct (bool, optional) – Whether to generate argument structure definitions. Default is True.

__init__(closureCallTemplate[, closureSuffix])

Initialize the ClosureGeneration transformation pass.

apply(ctxt, executionBlock, name[, verbose])

Apply the closure generation transformation.

dereferenceVars(template, varNames)

Dereference the specified variables in the given template.

extractDynamicReferences(ctxt[, ...])

Extract all dynamic references from the given execution block.

indexVars(template, varNames, index)

Index the specified variables in the given template.

Attributes

parseTreeDict

closureStructArgType

closureStructArgs

apply(ctxt: NetworkContext, executionBlock: ExecutionBlock, name: str, verbose: CodeGenVerbosity = CodeGenVerbosity(tilingProfiling=None, untiledProfiling=None)) Tuple[NetworkContext, ExecutionBlock]

Apply the closure generation transformation.

Transforms the given execution block into a closure-based pattern by generating the necessary struct, closure function, and call site. This is the main entry point for the closure transformation.

Parameters:
  • ctxt (NetworkContext) – The network context containing buffer and type information.

  • executionBlock (ExecutionBlock) – The execution block to transform into a closure pattern.

  • name (str) – The base name for generating closure-related identifiers.

  • verbose (CodeGenVerbosity, optional) – The verbosity level for code generation. Default is _NoVerbosity.

Returns:

A tuple containing: - The modified network context with closure definitions - The new ClosureExecutionBlock with closure call patterns

Return type:

Tuple[NetworkContext, ExecutionBlock]

Notes

The transformation process includes: 1. Generating a unique closure name with the specified suffix 2. Capturing the original function call code 3. Creating the closure argument struct 4. Generating the closure function definition in global scope 5. Replacing the original call with a closure call pattern

static dereferenceVars(template: Template, varNames: List[str]) None

Dereference the specified variables in the given template.

This function modifies the provided template in place by dereferencing the variables listed in varNames. The template is modified in place.

Parameters:
  • template (Template) – The template object to be modified.

  • varNames (list of str) – List of variable names to dereference within the template.

extractDynamicReferences(ctxt: NetworkContext, executionBlock: ExecutionBlock = None, unrollStructs=False, includeGlobalReferences=False)

Extract all dynamic references from the given execution block.

Parameters:
  • ctxt (NetworkContext) – The network context.

  • executionBlock (ExecutionBlock, optional) – The execution block.

  • unrollStructs (bool, optional) – Whether to unroll structs.

  • includeGlobalReferences (bool, optional) – Whether to include global references.

Returns:

A list of dynamic references.

Return type:

List[str]

static indexVars(template: Template, varNames: List[str], index: str) None

Index the specified variables in the given template.

Modifies the template in place by indexing the specified variable names.

Parameters:
  • template (Template) – The template to modify.

  • varNames (List[str]) – The variable names to index.

  • index (str) – The index to use.