User Guide
localize_visitor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2023 Blue Brain Project, EPFL.
3  * See the top-level LICENSE file for details.
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #pragma once
9 
10 /**
11  * \file
12  * \brief \copybrief nmodl::visitor::LocalizeVisitor
13  */
14 
15 #include <map>
16 #include <string>
17 
18 #include "symtab/decl.hpp"
19 #include "visitors/ast_visitor.hpp"
20 
21 
22 namespace nmodl {
23 namespace visitor {
24 
25 /**
26  * \addtogroup visitor_classes
27  * \{
28  */
29 
30 /**
31  * \class LocalizeVisitor
32  * \brief %Visitor to transform global variable usage to local
33  *
34  * Motivation: As NMODL doesn't support returning multiple values,
35  * procedures are often written with use of range variables that
36  * can be made local. For example:
37  *
38  * \code{.mod}
39  * NEURON {
40  * RANGE tau, alpha, beta
41  * }
42  *
43  * DERIVATIVE states() {
44  * ...
45  * rates()
46  * alpha = tau + beta
47  * }
48  *
49  * PROCEDURE rates() {
50  * tau = xx * 0.12 * some_var
51  * beta = yy * 0.11
52  * }
53  * \endcode
54  *
55  * In above example we are only interested in variable alpha computed in
56  * DERIVATIVE block. If rates() is inlined into DERIVATIVE block then we
57  * get:
58  *
59  * \code{.mod}
60  * DERIVATIVE states() {
61  * ...
62  * {
63  * tau = xx * 0.12 * some_var
64  * beta = yy * 0.11
65  * }
66  * alpha = tau + beta
67  * }
68  * \endcode
69  *
70  * Now tau and beta could become local variables provided that their values
71  * are not used in any other global blocks.
72  *
73  * Implementation Notes:
74  * - For every global variable in the mod file we have to compute
75  * def-use chains in global blocks (except procedure and functions, which should be
76  * already inlined).
77  * - If every block has "definition" first then that variable is safe to "localize"
78  *
79  * \todo
80  * - We are excluding procedures/functions because they will be still using global
81  * variables. We need to have dead-code removal pass to eliminate unused procedures/
82  * functions before localizer pass.
83  */
85  private:
86  /// ignore verbatim blocks while localizing
87  bool ignore_verbatim = false;
88 
90 
91  std::vector<std::string> variables_to_optimize() const;
92 
93  bool node_for_def_use_analysis(const ast::Node& node) const;
94 
95  bool is_solve_procedure(const ast::Node& node) const;
96 
97  public:
98  LocalizeVisitor() = default;
99 
102 
103  void visit_program(const ast::Program& node) override;
104 };
105 
106 /** \} */ // end of visitor_classes
107 
108 } // namespace visitor
109 } // namespace nmodl
nmodl::visitor::LocalizeVisitor::LocalizeVisitor
LocalizeVisitor()=default
nmodl::visitor::LocalizeVisitor::LocalizeVisitor
LocalizeVisitor(bool ignore_verbatim)
Definition: localize_visitor.hpp:100
nmodl::ast::Node
Base class for all AST node.
Definition: node.hpp:40
nmodl::visitor::ConstAstVisitor
Concrete constant visitor for all AST classes.
Definition: ast_visitor.hpp:168
nmodl::visitor::LocalizeVisitor::variables_to_optimize
std::vector< std::string > variables_to_optimize() const
Definition: localize_visitor.cpp:69
nmodl
encapsulates code generation backend implementations
Definition: ast_common.hpp:26
nmodl::visitor::LocalizeVisitor
Visitor to transform global variable usage to local
Definition: localize_visitor.hpp:84
nmodl::visitor::LocalizeVisitor::ignore_verbatim
bool ignore_verbatim
ignore verbatim blocks while localizing
Definition: localize_visitor.hpp:87
nmodl::visitor::LocalizeVisitor::is_solve_procedure
bool is_solve_procedure(const ast::Node &node) const
Definition: localize_visitor.cpp:59
nmodl::visitor::LocalizeVisitor::visit_program
void visit_program(const ast::Program &node) override
visit node of type ast::Program
Definition: localize_visitor.cpp:103
nmodl::symtab::SymbolTable
Represent symbol table for a NMODL block.
Definition: symbol_table.hpp:57
nmodl::visitor::LocalizeVisitor::node_for_def_use_analysis
bool node_for_def_use_analysis(const ast::Node &node) const
Definition: localize_visitor.cpp:23
nmodl::ast::Program
Represents top level AST node for whole NMODL input.
Definition: program.hpp:39
nmodl::visitor::LocalizeVisitor::program_symtab
symtab::SymbolTable * program_symtab
Definition: localize_visitor.hpp:89
decl.hpp
Forward declarations of symbols in namespace nmodl::symtab.
ast_visitor.hpp
Concrete visitor for all AST classes.