Loading...
Searching...
No Matches
ComputeGenerator Struct Reference

Visitor object which will generate llvm IR for a syntax tree. This provides the majority of the code generation functionality except for attribute access. This design allows for custom geometry to define their IR implementations for these accesses by deriving and extending this generator with ast::Attribute handling (see PointComputeGenerator.h and VolumeComputeGenerator.h for examples). More...

`#include <openvdb_ax/codegen/ComputeGenerator.h>`

Inheritance diagram for ComputeGenerator:

## Public Types

using | NodeType
| Templated conditional which resolves to a const NodeT if ConstVisit is true, or a non-const NodeT if ConstVisit is false.

## Public Member Functions

| ComputeGenerator (llvm::Module &module, const FunctionOptions &options, FunctionRegistry &functionRegistry, Logger &logger)
virtual | ~ComputeGenerator ()=default
bool | generate (const ast::Tree &)
SymbolTable & | globals ()
const SymbolTable & | globals () const
bool | postOrderNodes () const
| Code generation always runs post order.
bool | traverse (const ast::Block *block)
| Custom traversal of scoped blocks.
bool | traverse (const ast::CommaOperator *comma)
| Custom traversal of comma expression.
bool | traverse (const ast::ConditionalStatement *cond)
| Custom traversal of conditional statements.
bool | traverse (const ast::BinaryOperator *bin)
| Custom traversal of binary operators.
bool | traverse (const ast::TernaryOperator *tern)
| Custom traversal of ternary operators.
bool | traverse (const ast::Loop *loop)
| Custom traversal of loops.
bool | traverse (const ast::DeclareLocal *decl)
| Custom traversal of declarations.
ComputeGenerator & | derived ()
| Accesses the derived class by static casting the current object. Assumes use of the Curiously Recursive Template Pattern (CRTP).
virtual bool | visit (const ast::CommaOperator *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::AssignExpression *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Crement *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::FunctionCall *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Attribute *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Tree *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Block *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::ConditionalStatement *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Loop *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Keyword *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::UnaryOperator *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::BinaryOperator *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::TernaryOperator *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Cast *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::DeclareLocal *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Local *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::ExternalVariable *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::ArrayUnpack *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::ArrayPack *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< bool > *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< int16_t > *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< int32_t > *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< int64_t > *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< float > *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< double > *)
| Visitor methods for all AST nodes which implement IR generation.
virtual bool | visit (const ast::Value< std::string > *)
| Visitor methods for all AST nodes which implement IR generation.
template<typename ValueType>
std::enable_if< std::is_integral< ValueType >::value, bool >::type | visit (const ast::Value< ValueType > *node)
| Visitor methods for all AST nodes which implement IR generation.
template<typename ValueType>
std::enable_if< std::is_floating_point< ValueType >::value, bool >::type | visit (const ast::Value< ValueType > *node)
| Visitor methods for all AST nodes which implement IR generation.
Options
bool | reverseChildVisits () const
| Default behavior option. Reverses the traversal order of child nodes. If true, child nodes are accessed from last to first index .i.e. Node::children() -> 0. If false, child nodes are accessed from first to last .i.e. 0 -> Node::children().
bool | visitNodeHierarchies () const
| Default behavior option. Controls whether nodes visit themselves at each stage of their class hierarchy. If true, nodes perform multiple visits on their potentially abstract base classes. If false, only the concrete derived types are visited.
bool | reverseHierarchyVisits () const

Default behavior option. Reverses the traversal order of node hierarchies. If true, hierarchical visits start at the very top of their inheritance structure (always a Node AST node) and visit downwards until the lowest derived concrete node is reached. If false, hierarchical visits start at the lowest derived concrete node and visit upwards until the very top of their inheritance structure (always a Node AST node) is reached.

Traversals
bool | traverse (NodeType< ast::Tree > *tree)
| Default traversals for a given concrete AST node type.
Visits
bool | visit (NodeType< ast::Node > *)
| Visits for abstract (pure-virtual) Node types.

## Protected Member Functions

const FunctionGroup * | getFunction (const std::string &identifier, const bool allowInternal=false)
bool | binaryExpression (llvm::Value *&result, llvm::Value *lhs, llvm::Value *rhs, const ast::tokens::OperatorToken op, const ast::Node *node)
bool | assignExpression (llvm::Value *lhs, llvm::Value *&rhs, const ast::Node *node)
void | createFreeSymbolStrings (llvm::IRBuilder<> &)
| Clear any strings which were allocated in a given function. This method accepts an IRBuilder which is expected to be attached to a valid block/function. For each block in the function with a return instruction, this function calls the appropriate memory methods to deallocate any strings (which are alloced in the function prologue).

## Protected Attributes

llvm::Module & | mModule
llvm::LLVMContext & | mContext
llvm::IRBuilder | mBuilder
std::stack< llvm::Value * > | mValues
std::stack< std::pair< llvm::BasicBlock *, llvm::BasicBlock * > > | mBreakContinueStack
size_t | mScopeIndex
SymbolTableBlocks | mSymbolTables
llvm::Function * | mFunction
const FunctionOptions | mOptions
Logger & | mLog

## Detailed Description

Visitor object which will generate llvm IR for a syntax tree. This provides the majority of the code generation functionality except for attribute access. This design allows for custom geometry to define their IR implementations for these accesses by deriving and extending this generator with ast::Attribute handling (see PointComputeGenerator.h and VolumeComputeGenerator.h for examples).

Note
  The visit/traverse methods work slightly differently to the normal Visitor to allow proper handling of errors and visitation history. Nodes that inherit from ast::Expression can return false from visit() (and so traverse()), but this will not necessarily stop traversal altogether. Instead, any ast::Statements that are not also ast::Expressions i.e. Block, ConditionalStatement, Loop, DeclareLocal, etc override their visit and traverse methods to handle custom traversal order, and the catching of failed child Expression visit/traverse calls. This allows errors in independent Statements to not halt traversal for future Statements and so allow capturing of multiple errors in an ast::Tree in a single call to ComputeGenerator::generate().

## Member Typedef Documentation

## NodeType

using NodeType

inherited

Templated conditional which resolves to a const NodeT if ConstVisit is true, or a non-const NodeT if ConstVisit is false.

## Constructor & Destructor Documentation

## ComputeGenerator()

ComputeGenerator | ( | llvm::Module & | module,
|  | const FunctionOptions & | options,
|  | FunctionRegistry & | functionRegistry,
|  | Logger & | logger )

## ~ComputeGenerator()

virtual ~ComputeGenerator | ( |  | ) |

virtual default

## Member Function Documentation

## assignExpression()

bool assignExpression | ( | llvm::Value * | lhs,
|  | llvm::Value *& | rhs,
|  | const ast::Node * | node )

protected

## binaryExpression()

bool binaryExpression | ( | llvm::Value *& | result,
|  | llvm::Value * | lhs,
|  | llvm::Value * | rhs,
|  | const ast::tokens::OperatorToken | op,
|  | const ast::Node * | node )

protected

## createFreeSymbolStrings()

void createFreeSymbolStrings | ( | llvm::IRBuilder<> & |  | ) |

protected

Clear any strings which were allocated in a given function. This method accepts an IRBuilder which is expected to be attached to a valid block/function. For each block in the function with a return instruction, this function calls the appropriate memory methods to deallocate any strings (which are alloced in the function prologue).

## derived()

ComputeGenerator & derived | ( |  | ) |

inline inherited

Accesses the derived class by static casting the current object. Assumes use of the Curiously Recursive Template Pattern (CRTP).

## generate()

bool generate | ( | const ast::Tree & |  | ) |

## getFunction()

const FunctionGroup * getFunction | ( | const std::string & | identifier,
|  | const bool | allowInternal = false )

protected

## globals() [1/2]

SymbolTable & globals | ( |  | ) |

inline

## globals() [2/2]

const SymbolTable & globals | ( |  | ) | const

inline

## postOrderNodes()

bool postOrderNodes | ( |  | ) | const

inline

Code generation always runs post order.

## reverseChildVisits()

bool reverseChildVisits | ( |  | ) | const

inline inherited

Default behavior option. Reverses the traversal order of child nodes. If true, child nodes are accessed from last to first index .i.e. Node::children() -> 0. If false, child nodes are accessed from first to last .i.e. 0 -> Node::children().

## reverseHierarchyVisits()

bool reverseHierarchyVisits | ( |  | ) | const

inline inherited

Default behavior option. Reverses the traversal order of node hierarchies. If true, hierarchical visits start at the very top of their inheritance structure (always a Node AST node) and visit downwards until the lowest derived concrete node is reached. If false, hierarchical visits start at the lowest derived concrete node and visit upwards until the very top of their inheritance structure (always a Node AST node) is reached.

Note
  Has no effect if visitNodeHierarchies() is false

## traverse() [1/8]

bool traverse | ( | NodeType< ast::Tree > * | tree | ) |

inline inherited

Default traversals for a given concrete AST node type.

Returns
  True if traversal should continue, false to terminate

## traverse() [2/8]

bool traverse | ( | const ast::BinaryOperator * | bin | ) |

inline

Custom traversal of binary operators.

Note
  This overrides the default traversal to handle short-circuiting in logical AND and OR

## traverse() [3/8]

bool traverse | ( | const ast::Block * | block | ) |

inline

Custom traversal of scoped blocks.

Note
  This overrides the default traversal to incorporate the scoping of variables declared in this block

## traverse() [4/8]

bool traverse | ( | const ast::CommaOperator * | comma | ) |

inline

Custom traversal of comma expression.

Note
  This overrides the default traversal to handle errors without stopping generation of entire list

## traverse() [5/8]

bool traverse | ( | const ast::ConditionalStatement * | cond | ) |

inline

Custom traversal of conditional statements.

Note
  This overrides the default traversal to handle branching between different code paths

## traverse() [6/8]

bool traverse | ( | const ast::DeclareLocal * | decl | ) |

inline

Custom traversal of declarations.

Note
  This overrides the default traversal to handle traversal of the local and assignment of initialiser, if it exists

## traverse() [7/8]

bool traverse | ( | const ast::Loop * | loop | ) |

inline

Custom traversal of loops.

Note
  This overrides the default traversal to handle branching between different code paths and the scoping of variables in for-loop initialisation

## traverse() [8/8]

bool traverse | ( | const ast::TernaryOperator * | tern | ) |

inline

Custom traversal of ternary operators.

Note
  This overrides the default traversal to handle branching between different code paths

## visit() [1/29]

bool visit | ( | NodeType< ast::Node > * |  | ) |

inline inherited

Visits for abstract (pure-virtual) Node types.

Note
  These are only hit through the default behavior if Visitor::visitNodeHierarchies is enabled.

Returns
  True if traversal should continue, false to terminate

## visit() [2/29]

virtual bool visit | ( | const ast::ArrayPack * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [3/29]

virtual bool visit | ( | const ast::ArrayUnpack * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [4/29]

virtual bool visit | ( | const ast::AssignExpression * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [5/29]

virtual bool visit | ( | const ast::Attribute * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [6/29]

virtual bool visit | ( | const ast::BinaryOperator * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [7/29]

virtual bool visit | ( | const ast::Block * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [8/29]

virtual bool visit | ( | const ast::Cast * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [9/29]

virtual bool visit | ( | const ast::CommaOperator * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [10/29]

virtual bool visit | ( | const ast::ConditionalStatement * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [11/29]

virtual bool visit | ( | const ast::Crement * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [12/29]

virtual bool visit | ( | const ast::DeclareLocal * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [13/29]

virtual bool visit | ( | const ast::ExternalVariable * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [14/29]

virtual bool visit | ( | const ast::FunctionCall * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [15/29]

virtual bool visit | ( | const ast::Keyword * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [16/29]

virtual bool visit | ( | const ast::Local * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [17/29]

virtual bool visit | ( | const ast::Loop * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [18/29]

virtual bool visit | ( | const ast::TernaryOperator * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [19/29]

virtual bool visit | ( | const ast::Tree * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [20/29]

virtual bool visit | ( | const ast::UnaryOperator * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [21/29]

virtual bool visit | ( | const ast::Value< bool > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [22/29]

virtual bool visit | ( | const ast::Value< double > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [23/29]

virtual bool visit | ( | const ast::Value< float > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [24/29]

virtual bool visit | ( | const ast::Value< int16_t > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [25/29]

virtual bool visit | ( | const ast::Value< int32_t > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [26/29]

virtual bool visit | ( | const ast::Value< int64_t > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [27/29]

virtual bool visit | ( | const ast::Value< std::string > * |  | ) |

virtual

Visitor methods for all AST nodes which implement IR generation.

Reimplemented in PointComputeGenerator, and VolumeComputeGenerator.

## visit() [28/29]

template<typename ValueType>

std::enable_if< std::is_floating_point< ValueType >::value, bool >::type visit | ( | const ast::Value< ValueType > * | node | ) |

Visitor methods for all AST nodes which implement IR generation.

## visit() [29/29]

template<typename ValueType>

std::enable_if< std::is_integral< ValueType >::value, bool >::type visit | ( | const ast::Value< ValueType > * | node | ) |

Visitor methods for all AST nodes which implement IR generation.

## visitNodeHierarchies()

bool visitNodeHierarchies | ( |  | ) | const

inline inherited

Default behavior option. Controls whether nodes visit themselves at each stage of their class hierarchy. If true, nodes perform multiple visits on their potentially abstract base classes. If false, only the concrete derived types are visited.

When disabled, abstract node visitor methods are never accessed directly through the default Visitor implementation. These types include Node, Statement, Expression, etc AST nodes. If true, for each linearly inherited AST node, a visit is performed on the entire hierarchy. For example, for a Local AST node which derives from Variable -> Expression -> Statement -> Node, 5 visits will be performed at each level.

## Member Data Documentation

## mBreakContinueStack

std::stack<std::pair<llvm::BasicBlock*, llvm::BasicBlock*> > mBreakContinueStack

protected

## mBuilder

llvm::IRBuilder mBuilder

protected

## mContext

llvm::LLVMContext& mContext

protected

## mFunction

llvm::Function* mFunction

protected

## mLog

Logger& mLog

protected

## mModule

llvm::Module& mModule

protected

## mOptions

const FunctionOptions mOptions

protected

## mScopeIndex

size_t mScopeIndex

protected

## mSymbolTables

SymbolTableBlocks mSymbolTables

protected

## mValues

std::stack<llvm::Value*> mValues

protected