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 
17 #include "symtab/decl.hpp"
18 #include "visitors/ast_visitor.hpp"
19 
20 
21 namespace nmodl {
22 namespace visitor {
23 
24 /**
25  * \addtogroup visitor_classes
26  * \{
27  */
28 
29 /**
30  * \class LocalizeVisitor
31  * \brief %Visitor to transform global variable usage to local
32  *
33  * Motivation: As NMODL doesn't support returning multiple values,
34  * procedures are often written with use of range variables that
35  * can be made local. For example:
36  *
37  * \code{.mod}
38  * NEURON {
39  * RANGE tau, alpha, beta
40  * }
41  *
42  * DERIVATIVE states() {
43  * ...
44  * rates()
45  * alpha = tau + beta
46  * }
47  *
48  * PROCEDURE rates() {
49  * tau = xx * 0.12 * some_var
50  * beta = yy * 0.11
51  * }
52  * \endcode
53  *
54  * In above example we are only interested in variable alpha computed in
55  * DERIVATIVE block. If rates() is inlined into DERIVATIVE block then we
56  * get:
57  *
58  * \code{.mod}
59  * DERIVATIVE states() {
60  * ...
61  * {
62  * tau = xx * 0.12 * some_var
63  * beta = yy * 0.11
64  * }
65  * alpha = tau + beta
66  * }
67  * \endcode
68  *
69  * Now tau and beta could become local variables provided that their values
70  * are not used in any other global blocks.
71  *
72  * Implementation Notes:
73  * - For every global variable in the mod file we have to compute
74  * def-use chains in global blocks (except procedure and functions, which should be
75  * already inlined).
76  * - If every block has "definition" first then that variable is safe to "localize"
77  *
78  * \todo
79  * - We are excluding procedures/functions because they will be still using global
80  * variables. We need to have dead-code removal pass to eliminate unused procedures/
81  * functions before localizer pass.
82  */
84  private:
85  /// ignore verbatim blocks while localizing
86  bool ignore_verbatim = false;
87 
89 
90  std::vector<std::string> variables_to_optimize() const;
91 
92  bool node_for_def_use_analysis(const ast::Node& node) const;
93 
94  bool is_solve_procedure(const ast::Node& node) const;
95 
96  public:
97  LocalizeVisitor() = default;
98 
101 
102  void visit_program(const ast::Program& node) override;
103 };
104 
105 /** \} */ // end of visitor_classes
106 
107 } // namespace visitor
108 } // namespace nmodl
nmodl::visitor::LocalizeVisitor::LocalizeVisitor
LocalizeVisitor()=default
nmodl::visitor::LocalizeVisitor::LocalizeVisitor
LocalizeVisitor(bool ignore_verbatim)
Definition: localize_visitor.hpp:99
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:166
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:83
nmodl::visitor::LocalizeVisitor::ignore_verbatim
bool ignore_verbatim
ignore verbatim blocks while localizing
Definition: localize_visitor.hpp:86
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:88
decl.hpp
Forward declarations of symbols in namespace nmodl::symtab.
ast_visitor.hpp
Concrete visitor for all AST classes.