OpenVDB  7.2.0
ComputeGenerator.h
Go to the documentation of this file.
1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
10 
11 #ifndef OPENVDB_AX_COMPUTE_GENERATOR_HAS_BEEN_INCLUDED
12 #define OPENVDB_AX_COMPUTE_GENERATOR_HAS_BEEN_INCLUDED
13 
14 #include "FunctionRegistry.h"
15 #include "FunctionTypes.h"
16 #include "SymbolTable.h"
17 
18 #include "../ast/AST.h"
19 #include "../ast/Visitor.h"
20 #include "../compiler/CompilerOptions.h"
21 #include "../compiler/Logger.h"
22 
23 #include <openvdb/version.h>
24 
25 #include <llvm/Analysis/TargetLibraryInfo.h>
26 #include <llvm/IR/BasicBlock.h>
27 #include <llvm/IR/Function.h>
28 #include <llvm/IR/IRBuilder.h>
29 #include <llvm/IR/LLVMContext.h>
30 #include <llvm/IR/Module.h>
31 
32 #include <stack>
33 
34 namespace openvdb {
36 namespace OPENVDB_VERSION_NAME {
37 
38 namespace ax {
39 namespace codegen {
40 
49 {
51  static const std::string Name;
52 
54  using Signature = void(const void* const);
56  static const size_t N_ARGS = FunctionTraitsT::N_ARGS;
57 
59  static const std::array<std::string, N_ARGS>& getArgumentKeys();
60  static std::string getDefaultName();
61 };
62 
63 
66 
67 namespace codegen_internal {
68 
83 struct ComputeGenerator : public ast::Visitor<ComputeGenerator>
84 {
85  ComputeGenerator(llvm::Module& module,
86  const FunctionOptions& options,
87  FunctionRegistry& functionRegistry,
88  Logger& logger);
89 
90  virtual ~ComputeGenerator() = default;
91 
92  bool generate(const ast::Tree&);
93 
94  inline SymbolTable& globals() { return mSymbolTables.globals(); }
95  inline const SymbolTable& globals() const { return mSymbolTables.globals(); }
96 
97  // Visitor pattern
98 
101 
103  inline bool postOrderNodes() const { return true; }
104 
108  bool traverse(const ast::Block* block)
109  {
110  if (!block) return true;
111  if (!this->visit(block)) return false;
112  return true;
113  }
114 
119  bool traverse(const ast::CommaOperator* comma)
120  {
121  if (!comma) return true;
122  if (!this->visit(comma)) return false;
123  return true;
124  }
125 
126 
130  bool traverse(const ast::ConditionalStatement* cond)
131  {
132  if (!cond) return true;
133  if (!this->visit(cond)) return false;
134  return true;
135  }
136 
140  bool traverse(const ast::BinaryOperator* bin)
141  {
142  if (!bin) return true;
143  if (!this->visit(bin)) return false;
144  return true;
145  }
146 
150  bool traverse(const ast::TernaryOperator* tern)
151  {
152  if (!tern) return true;
153  if (!this->visit(tern)) return false;
154  return true;
155  }
156 
161  bool traverse(const ast::Loop* loop)
162  {
163  if (!loop) return true;
164  if (!this->visit(loop)) return false;
165  return true;
166  }
167 
172  bool traverse(const ast::DeclareLocal* decl)
173  {
174  if (!decl) return true;
175  if (!this->visit(decl)) return false;
176  return true;
177  }
178 
179  virtual bool visit(const ast::CommaOperator*);
180  virtual bool visit(const ast::AssignExpression*);
181  virtual bool visit(const ast::Crement*);
182  virtual bool visit(const ast::FunctionCall*);
183  virtual bool visit(const ast::Attribute*);
184  virtual bool visit(const ast::Tree*);
185  virtual bool visit(const ast::Block*);
186  virtual bool visit(const ast::ConditionalStatement*);
187  virtual bool visit(const ast::Loop*);
188  virtual bool visit(const ast::Keyword*);
189  virtual bool visit(const ast::UnaryOperator*);
190  virtual bool visit(const ast::BinaryOperator*);
191  virtual bool visit(const ast::TernaryOperator*);
192  virtual bool visit(const ast::Cast*);
193  virtual bool visit(const ast::DeclareLocal*);
194  virtual bool visit(const ast::Local*);
195  virtual bool visit(const ast::ExternalVariable*);
196  virtual bool visit(const ast::ArrayUnpack*);
197  virtual bool visit(const ast::ArrayPack*);
198  virtual bool visit(const ast::Value<bool>*);
199  virtual bool visit(const ast::Value<int16_t>*);
200  virtual bool visit(const ast::Value<int32_t>*);
201  virtual bool visit(const ast::Value<int64_t>*);
202  virtual bool visit(const ast::Value<float>*);
203  virtual bool visit(const ast::Value<double>*);
204  virtual bool visit(const ast::Value<std::string>*);
205 
206  template <typename ValueType>
207  typename std::enable_if<std::is_integral<ValueType>::value, bool>::type
208  visit(const ast::Value<ValueType>* node);
209 
210  template <typename ValueType>
211  typename std::enable_if<std::is_floating_point<ValueType>::value, bool>::type
212  visit(const ast::Value<ValueType>* node);
213 
214 protected:
215 
216  const FunctionGroup* getFunction(const std::string& identifier,
217  const bool allowInternal = false);
218 
219  bool binaryExpression(llvm::Value*& result, llvm::Value* lhs, llvm::Value* rhs,
220  const ast::tokens::OperatorToken op, const ast::Node* node);
221  bool assignExpression(llvm::Value* lhs, llvm::Value*& rhs, const ast::Node* node);
222 
223  llvm::Module& mModule;
224  llvm::LLVMContext& mContext;
225  llvm::IRBuilder<> mBuilder;
226 
227  // The stack of accessed values
228  std::stack<llvm::Value*> mValues;
229 
230  // The stack of blocks for keyword branching
231  std::stack<std::pair<llvm::BasicBlock*, llvm::BasicBlock*>> mBreakContinueStack;
232 
233  // The current scope number used to track scoped declarations
234  size_t mScopeIndex;
235 
236  // The map of scope number to local variable names to values
238 
239  // The function used as the base code block
240  llvm::Function* mFunction;
241 
243 
245 
246 private:
247  FunctionRegistry& mFunctionRegistry;
248 };
249 
250 } // codegen_internal
251 
252 } // namespace codegen
253 } // namespace ax
254 } // namespace OPENVDB_VERSION_NAME
255 } // namespace openvdb
256 
257 #endif // OPENVDB_AX_COMPUTE_GENERATOR_HAS_BEEN_INCLUDED
258 
openvdb::v7_2::ax::ast::Tree
A Tree is the highest concrete (non-abstract) node in the entire AX AST hierarchy....
Definition: AST.h:562
openvdb::v7_2::ax::Logger
Logger for collecting errors and warnings that occur during AX compilation.
Definition: Logger.h:55
FunctionRegistry.h
Contains the global function registration definition which described all available user front end fun...
openvdb::v7_2::ax::codegen::SymbolTable
A symbol table which can be used to represent a single scoped set of a programs variables....
Definition: SymbolTable.h:35
openvdb::v7_2::ax::codegen::FunctionTraits
Templated function traits which provides compile-time index access to the types of the function signa...
Definition: ax/openvdb_ax/codegen/Types.h:279
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::generate
bool generate(const ast::Tree &)
openvdb::v7_2::ax::codegen::ComputeKernel::Signature
void(const void *const) Signature
The signature of the generated function.
Definition: ComputeGenerator.h:54
openvdb::v7_2::ax::ast::Block
A Block node represents a scoped list of statements. It may comprise of 0 or more statements,...
Definition: AST.h:476
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mSymbolTables
SymbolTableBlocks mSymbolTables
Definition: ComputeGenerator.h:237
openvdb::v7_2::ax::ast::Crement
A Crement node represents a single increment '++' and decrement '–' operation. As well as it's cremen...
Definition: AST.h:1294
openvdb::v7_2::ax::ast::Cast
Cast nodes represent the conversion of an underlying expression to a target type. Cast nodes are typi...
Definition: AST.h:1464
openvdb::v7_2::ax::codegen::FunctionRegistry
The function registry which is used for function code generation. Each time a function is visited wit...
Definition: FunctionRegistry.h:37
openvdb::v7_2::ax::ast::Value
A Value (literal) AST node holds either literal text or absolute value information on all numerical,...
Definition: AST.h:2253
openvdb::v7_2::ax::ast::Keyword
Keywords represent keyword statements defining changes in execution. These include those that define ...
Definition: AST.h:1641
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mLog
Logger & mLog
Definition: ComputeGenerator.h:244
openvdb::v7_2::ax::ast::ArrayUnpack
ArrayUnpack represent indexing operations into AX container types, primarily vectors and matrices ind...
Definition: AST.h:1686
openvdb::v7_2::ax::FunctionOptions
Options that control how functions behave.
Definition: CompilerOptions.h:25
openvdb::v7_2::ax::ast::TernaryOperator
A TernaryOperator represents a ternary (conditional) expression 'a ? b : c' which evaluates to 'b' if...
Definition: AST.h:1092
version.h
Library and file format version numbers.
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mOptions
const FunctionOptions mOptions
Definition: ComputeGenerator.h:242
openvdb::v7_2::ax::ast::UnaryOperator
A UnaryOperator represents a single unary operation on an expression. The operation type is stored as...
Definition: AST.h:1389
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::binaryExpression
bool binaryExpression(llvm::Value *&result, llvm::Value *lhs, llvm::Value *rhs, const ast::tokens::OperatorToken op, const ast::Node *node)
openvdb::v7_2::ax::ast::Local
Local AST nodes represent a single accesses to a local variable. The only store the name of the varia...
Definition: AST.h:2112
openvdb::v7_2::ax::ast::Node
The base abstract node which determines the interface and required methods for all derived concrete n...
Definition: AST.h:102
openvdb::v7_2::ax::ast::Value< std::string >
Specialization of Values for strings.
Definition: AST.h:2335
openvdb::v7_2::ax::codegen::ComputeKernel::getArgumentKeys
static const std::array< std::string, N_ARGS > & getArgumentKeys()
The argument key names available during code generation.
openvdb::v7_2::ax::ast::DeclareLocal
DeclareLocal AST nodes symbolize a single type declaration of a local variable. These store the local...
Definition: AST.h:2139
openvdb::v7_2::ax::ast::ExternalVariable
ExternalVariable represent any access to external (custom) data, typically associated with the '$' sy...
Definition: AST.h:2002
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::~ComputeGenerator
virtual ~ComputeGenerator()=default
openvdb::v7_2::ax::ast::Visitor
The Visitor class uses the Curiously Recursive Template Pattern (CRTP) to provide a customizable inte...
Definition: Visitor.h:96
openvdb::v7_2::ax::ast::ConditionalStatement
ConditionalStatements represents all combinations of 'if', 'else' and 'else if' syntax and semantics....
Definition: AST.h:864
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mBreakContinueStack
std::stack< std::pair< llvm::BasicBlock *, llvm::BasicBlock * > > mBreakContinueStack
Definition: ComputeGenerator.h:231
openvdb::v7_2::ax::codegen::ComputeKernel::Name
static const std::string Name
The name of the generated function.
Definition: ComputeGenerator.h:51
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator
Visitor object which will generate llvm IR for a syntax tree. This provides the majority of the code ...
Definition: ComputeGenerator.h:84
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mModule
llvm::Module & mModule
Definition: ComputeGenerator.h:223
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::ComputeGenerator
ComputeGenerator(llvm::Module &module, const FunctionOptions &options, FunctionRegistry &functionRegistry, Logger &logger)
SymbolTable.h
Contains the symbol table which holds mappings of variables names to llvm::Values.
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::getFunction
const FunctionGroup * getFunction(const std::string &identifier, const bool allowInternal=false)
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mValues
std::stack< llvm::Value * > mValues
Definition: ComputeGenerator.h:228
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::globals
const SymbolTable & globals() const
Definition: ComputeGenerator.h:95
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mScopeIndex
size_t mScopeIndex
Definition: ComputeGenerator.h:234
OPENVDB_USE_VERSION_NAMESPACE
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:147
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::postOrderNodes
bool postOrderNodes() const
Code generation always runs post order.
Definition: ComputeGenerator.h:103
openvdb::v7_2::ax::ast::tokens::OperatorToken
OperatorToken
Definition: Tokens.h:151
openvdb::v7_2::ax::ast::Loop
Loops represent for, while and do-while loop constructs. These all consist of a condition - evaluated...
Definition: AST.h:708
openvdb::v7_2::ax::codegen::SymbolTableBlocks
A map of unique ids to symbol tables which can be used to represent local variables within a program....
Definition: SymbolTable.h:112
FunctionTypes.h
Contains frameworks for creating custom AX functions which can be registered within the FunctionRegis...
openvdb::v7_2::ax::ast::BinaryOperator
A BinaryOperator represents a single binary operation between a left hand side (LHS) and right hand s...
Definition: AST.h:988
openvdb::v7_2::ax::ast::CommaOperator
Definition: AST.h:607
openvdb::v7_2::ax::codegen::ComputeKernel
The function definition and signature which is built by the ComputeGenerator.
Definition: ComputeGenerator.h:49
openvdb::v7_2::ax::ast::Attribute
Attributes represent any access to a primitive value, typically associated with the '@' symbol syntax...
Definition: AST.h:1874
openvdb::v7_2::ax::ast::AssignExpression
AssignExpressions represents a similar object construction to a BinaryOperator. AssignExpressions can...
Definition: AST.h:1198
OPENVDB_VERSION_NAME
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:95
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mBuilder
llvm::IRBuilder mBuilder
Definition: ComputeGenerator.h:225
openvdb::v7_2::ax::ast::FunctionCall
FunctionCalls represent a single call to a function and any provided arguments. The argument list can...
Definition: AST.h:1541
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::assignExpression
bool assignExpression(llvm::Value *lhs, llvm::Value *&rhs, const ast::Node *node)
openvdb::v7_2::ax::codegen::ComputeKernel::getDefaultName
static std::string getDefaultName()
openvdb
Definition: openvdb/Exceptions.h:13
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mFunction
llvm::Function * mFunction
Definition: ComputeGenerator.h:240
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::globals
SymbolTable & globals()
Definition: ComputeGenerator.h:94
openvdb::v7_2::ax::ast::ArrayPack
ArrayPacks represent temporary container creations of arbitrary sizes, typically generated through th...
Definition: AST.h:1785
openvdb::v7_2::ax::codegen::FunctionGroup
todo
Definition: FunctionTypes.h:779
openvdb::v7_2::ax::codegen::codegen_internal::ComputeGenerator::mContext
llvm::LLVMContext & mContext
Definition: ComputeGenerator.h:224