![]() |
User Guide
|
Helper visitor to gather AST information to help code generation. More...
Helper visitor to gather AST information to help code generation.
Code generation pass needs various information from AST and symbol table. Different code generation backends will need this information. This helper pass visit ast and collect all information into single class object of CodegenInfo.
vectorize
as part of the passthreadsafe
as part of the passDefinition at line 47 of file codegen_helper_visitor.hpp.
#include <codegen_helper_visitor.hpp>
Private Types | |
using | SymbolType = std::shared_ptr< symtab::Symbol > |
using | SymbolVectorType = std::vector< std::shared_ptr< symtab::Symbol > > |
Private Member Functions | |
void | find_ion_variables (const ast::Program &node) |
Find all ions used in mod file. More... | |
void | find_table_variables () |
void | find_range_variables () |
Find range variables i.e. More... | |
void | find_non_range_variables () |
Find non-range variables i.e. More... | |
void | find_neuron_global_variables () |
void | check_cvode_codegen (const ast::Program &node) |
Find whether or not we need to emit CVODE-related code for NEURON Notes: we generate CVODE-related code if and only if: More... | |
Static Private Member Functions | |
static void | sort_with_mod2c_symbol_order (std::vector< SymbolType > &symbols) |
How symbols are stored in NEURON? See notes written in markdown file. More... | |
Private Attributes | |
codegen::CodegenInfo | info |
holds all codegen related information More... | |
bool | enable_cvode |
Config variable for enabling/disabling CVODE, see emit_cvode . More... | |
bool | under_net_receive_block = false |
if visiting net receive block More... | |
bool | under_derivative_block = false |
if visiting derivative block More... | |
bool | under_breakpoint_block = false |
if visiting breakpoint block More... | |
bool | table_statement_used = false |
table statement found More... | |
symtab::SymbolTable * | psymtab = nullptr |
symbol table for the program More... | |
std::shared_ptr< ast::Expression > | assign_lhs |
lhs of assignment in derivative block More... | |
|
private |
Definition at line 48 of file codegen_helper_visitor.hpp.
|
private |
Definition at line 49 of file codegen_helper_visitor.hpp.
|
inline |
Definition at line 84 of file codegen_helper_visitor.hpp.
CodegenInfo nmodl::codegen::CodegenHelperVisitor::analyze | ( | const ast::Program & | node | ) |
run visitor and return information for code generation
Definition at line 821 of file codegen_helper_visitor.cpp.
|
private |
Find whether or not we need to emit CVODE-related code for NEURON Notes: we generate CVODE-related code if and only if:
after_cvode
, cvode_t
, or cvode_t_v
methods Definition at line 185 of file codegen_helper_visitor.cpp.
|
private |
Find all ions used in mod file.
Check if given variable belongs to given ion. For example, eca belongs to ca ion, nai belongs to na ion. We just check if we exclude first/last char, if that is ion name.
iterate over all ion types and construct the Ion objects
once ions are populated, we can find all currents
check if write_conc(...) will be needed
Definition at line 89 of file codegen_helper_visitor.cpp.
|
private |
Definition at line 547 of file codegen_helper_visitor.cpp.
|
private |
Find non-range variables i.e.
ones that are not belong to per instance allocation
Certain variables like pointers, global, parameters are not necessary to be per instance variables. NEURON apply certain rules to determine which variables become thread, static or global variables. Here we construct those variables.
Top local variables are local variables appear in global scope. All local variables in program symbol table are in global scope.
All global variables remain global if mod file is not marked thread safe. Otherwise, global variables written at least once gets promoted to thread variables.
If parameter is not a range and used only as read variable then it becomes global variable. To qualify it as thread variable it must be be written at least once and mod file must be marked as thread safe. To exclusively get parameters only, we exclude all other variables (in without) and then sort them with neuron/mod2c order.
If derivimplicit is used, then first three thread ids get assigned to: 1st thread is used for: deriv_advance 2nd thread is used for: dith 3rd thread is used for: newtonspace
slist and dlist represent the offsets for prime variables used. For euler or derivimplicit methods its always first number.
next thread id is allocated for top local variables
next thread id is allocated for thread promoted variables
find total size of local variables in global scope
find number of prime variables and total size
find pointer or bbcore pointer variables
find RANDOM variables
Definition at line 237 of file codegen_helper_visitor.cpp.
|
private |
Find range variables i.e.
ones that are belong to per instance allocation
In order to be compatible with NEURON, we need to print all variables in exact order as NEURON/MOD2C implementation. This is important because memory for all variables is allocated in single 1-D array with certain offset for each variable. The order of variables determine the offset and hence they must be in same order as NEURON.
Here is how order is determined into NEURON/MOD2C implementation:
First, following three lists are created
Once created, we remove some variables due to the following criteria:
Finally we create 4th list:
We now print the variables in following order:
NOTE:
assigned_definition
but these variables are not from ASSIGNED block.comparator to decide the order based on definition
from symbols vector vars
, remove all ion variables which are not ionic currents
if secondary
vector contains any symbol that exist in the primary
then remove it
First come parameters which are range variables.
Second come assigned variables which are range variables.
Third come state variables. All state variables are kind of range by default. Note that some mod files like CaDynamics_E2.mod use cai as state variable which appear in USEION read/write list. These variables are not considered in this variables because non ionic-current variables are removed and printed later.
range_state_vars is copy of state variables but without non ionic current variables
Remaining variables are assigned and ion variables which are not in the previous 3 lists
make sure that variables already present in previous lists are removed to avoid any duplication
sort variables with their definition order
Definition at line 423 of file codegen_helper_visitor.cpp.
|
private |
Definition at line 540 of file codegen_helper_visitor.cpp.
|
staticprivate |
How symbols are stored in NEURON? See notes written in markdown file.
Some variables get printed by iterating over symbol table in mod2c. The example of this is thread variables (and also ions?). In this case we must have to arrange order if we are going keep compatibility with NEURON.
Suppose there are three global variables: bcd, abc, abd, abe They will be in the 'a' bucket in order: abe, abd, abc and in 'b' bucket bcd So when we print thread variables, we first have to sort in the opposite order in which they come and then again order by first character in increasing order.
Note that variables in double array do not need this transformation and it seems like they should just follow definition order.
first sort by global id to get in reverse order
now order by name (to be same as neuron's bucket)
Definition at line 68 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::AfterBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 863 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::BbcorePointer
Implements nmodl::visitor::ConstVisitor.
Definition at line 771 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::BeforeBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 859 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::BinaryExpression
Implements nmodl::visitor::ConstVisitor.
Definition at line 762 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::BreakpointBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 620 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::ConductanceHint
Implements nmodl::visitor::ConstVisitor.
Definition at line 694 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::ConstructorBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 588 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::CvodeBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 633 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::DerivativeBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 609 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::DerivimplicitCallback
Implements nmodl::visitor::ConstVisitor.
Definition at line 615 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::DestructorBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 594 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::DiscreteBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 834 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::EigenLinearSolverBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 677 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::EigenNewtonSolverBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 664 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::ElectrodeCurrent
Implements nmodl::visitor::ConstVisitor.
Definition at line 573 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::FactorDef
Implements nmodl::visitor::ConstVisitor.
Definition at line 757 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::ForNetcon
Implements nmodl::visitor::ConstVisitor.
Definition at line 791 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::FunctionBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 649 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::FunctionCall
Implements nmodl::visitor::ConstVisitor.
Definition at line 683 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::FunctionTableBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 659 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::InitialBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 578 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::LinearBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 826 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::LongitudinalDiffusionBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 887 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::NetReceiveBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 600 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::NonLinearBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 830 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::NrnStateBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 628 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::ProcedureBlock
Implements nmodl::visitor::ConstVisitor.
Definition at line 639 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::Program
Implements nmodl::visitor::ConstVisitor.
Definition at line 802 of file codegen_helper_visitor.cpp.
|
overridevirtual |
Visit statement block and find prime symbols appear in derivative block.
Equation statements in derivative block has prime on the lhs. The order of primes could be different that declaration state block. Also, not all state variables need to appear in equation block. In this case, we want to find out the the primes in the order of equation definition. This is just to keep the same order as neuron implementation.
The primes are already solved and replaced by Dstate or name. And hence we need to check if the lhs variable is derived from prime name. If it's Dstate then we have to lookup state to find out corresponding symbol. This is because prime_variables_by_order should contain state variable name and not the one replaced by solver pass.
STEADYSTATE sparse
solver). Such duplicated DERIVATIVE blocks could be removed by SolveBlockVisitor
, or we have to avoid visiting them here. See e.g. SH_na8st.mod test and original reduced_dentate .mod. Implements nmodl::visitor::ConstVisitor.
Definition at line 726 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::Suffix
Implements nmodl::visitor::ConstVisitor.
Definition at line 561 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::TableStatement
Implements nmodl::visitor::ConstVisitor.
Definition at line 796 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::ThreadSafe
Implements nmodl::visitor::ConstVisitor.
Definition at line 775 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::UpdateDt
Implements nmodl::visitor::ConstVisitor.
Definition at line 838 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit verbatim block and find all symbols used
Implements nmodl::visitor::ConstVisitor.
Definition at line 843 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::Watch
Implements nmodl::visitor::ConstVisitor.
Definition at line 780 of file codegen_helper_visitor.cpp.
|
overridevirtual |
visit node of type ast::WatchStatement
Implements nmodl::visitor::ConstVisitor.
Definition at line 785 of file codegen_helper_visitor.cpp.
|
private |
lhs of assignment in derivative block
Definition at line 73 of file codegen_helper_visitor.hpp.
|
private |
Config variable for enabling/disabling CVODE, see emit_cvode
.
Definition at line 55 of file codegen_helper_visitor.hpp.
|
private |
holds all codegen related information
Definition at line 52 of file codegen_helper_visitor.hpp.
|
private |
symbol table for the program
Definition at line 70 of file codegen_helper_visitor.hpp.
|
private |
table statement found
Definition at line 67 of file codegen_helper_visitor.hpp.
|
private |
if visiting breakpoint block
Definition at line 64 of file codegen_helper_visitor.hpp.
|
private |
if visiting derivative block
Definition at line 61 of file codegen_helper_visitor.hpp.
|
private |
if visiting net receive block
Definition at line 58 of file codegen_helper_visitor.hpp.