-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move
IndexRemover
to its own .hpp and .cpp file.
- Loading branch information
Showing
5 changed files
with
102 additions
and
64 deletions.
There are no files selected for viewing
Submodule hpc-coding-conventions
updated
12 files
+1 −1 | .bbp-project.yaml | |
+1 −1 | cpp/clang-format-13 | |
+1 −1 | cpp/clang-format-14 | |
+0 −108 | cpp/clang-format-15 | |
+1 −2 | cpp/cmake/build-time-copy.cmake | |
+12 −33 | cpp/cmake/sanitizers.cmake | |
+4 −32 | cpp/formatting/README.md | |
+1 −1 | cpp/formatting/formatting.py | |
+2 −2 | cpp/formatting/snippets/BreakBeforeConceptDeclarations.cpp | |
+1 −1 | cpp/formatting/snippets/IndentRequires.cpp | |
+0 −23 | cpp/formatting/snippets/RequiresClausePosition.cpp | |
+1 −1 | cpp/lib.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#include "index_remover.hpp" | ||
|
||
#include "utils/logger.hpp" | ||
#include "visitors/visitor_utils.hpp" | ||
|
||
namespace nmodl { | ||
namespace visitor { | ||
|
||
IndexRemover::IndexRemover(std::string index, int value) | ||
: index(std::move(index)) | ||
, value(value) {} | ||
|
||
/// if expression we are visiting is `Name` then return new `Integer` node | ||
std::shared_ptr<ast::Expression> IndexRemover::replace_for_name( | ||
const std::shared_ptr<ast::Expression>& node) const { | ||
if (node->is_name()) { | ||
auto name = std::dynamic_pointer_cast<ast::Name>(node); | ||
if (name->get_node_name() == index) { | ||
return std::make_shared<ast::Integer>(value, nullptr); | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
void IndexRemover::visit_binary_expression(ast::BinaryExpression& node) { | ||
node.visit_children(*this); | ||
if (under_indexed_name) { | ||
/// first recursively replaces children | ||
/// replace lhs & rhs if they have matching index variable | ||
auto lhs = replace_for_name(node.get_lhs()); | ||
auto rhs = replace_for_name(node.get_rhs()); | ||
node.set_lhs(std::move(lhs)); | ||
node.set_rhs(std::move(rhs)); | ||
} | ||
} | ||
|
||
void IndexRemover::visit_indexed_name(ast::IndexedName& node) { | ||
under_indexed_name = true; | ||
node.visit_children(*this); | ||
/// once all children are replaced, do the same for index | ||
auto length = replace_for_name(node.get_length()); | ||
node.set_length(std::move(length)); | ||
under_indexed_name = false; | ||
} | ||
|
||
} // namespace visitor | ||
} // namespace nmodl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2023 Blue Brain Project, EPFL. | ||
* See the top-level LICENSE file for details. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "ast/all.hpp" | ||
#include "visitors/ast_visitor.hpp" | ||
|
||
namespace nmodl { | ||
namespace visitor { | ||
|
||
/** | ||
* \class IndexRemover | ||
* \brief Helper visitor to replace index of array variable with integer | ||
* | ||
* When loop is unrolled, the index variable like `i` : | ||
* | ||
* ca[i] <-> ca[i+1] | ||
* | ||
* has type `Name` in the AST. This needs to be replaced with `Integer` | ||
* for optimizations like constant folding. This pass look at name and | ||
* binary expressions under index variables. | ||
*/ | ||
class IndexRemover: public AstVisitor { | ||
private: | ||
/// index variable name | ||
std::string index; | ||
|
||
/// integer value of index variable | ||
int value; | ||
|
||
/// true if we are visiting index variable | ||
bool under_indexed_name = false; | ||
|
||
public: | ||
IndexRemover(std::string index, int value); | ||
|
||
/// if expression we are visiting is `Name` then return new `Integer` node | ||
std::shared_ptr<ast::Expression> replace_for_name( | ||
const std::shared_ptr<ast::Expression>& node) const; | ||
|
||
void visit_binary_expression(ast::BinaryExpression& node) override; | ||
void visit_indexed_name(ast::IndexedName& node) override; | ||
}; | ||
|
||
} // namespace visitor | ||
} // namespace nmodl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters