Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide CodegenCppVisitor::setup. #1550

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions src/codegen/codegen_cpp_visitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1480,14 +1480,11 @@ class CodegenCppVisitor: public visitor::ConstAstVisitor {

std::string compute_method_name(BlockType type) const;

public:
/** Setup the target backend code generator
*
* Typically called from within \c visit\_program but may be called from
* specialized targets to setup this Code generator as fallback.
*/
// Automatically called as part of `visit_program`.
void setup(const ast::Program& node);

public:


/**
* Main and only member function to call after creating an instance of this class.
Expand All @@ -1496,10 +1493,7 @@ class CodegenCppVisitor: public visitor::ConstAstVisitor {
void visit_program(const ast::Program& program) override;


/****************************************************************************************/
/* Public printing routines for code generation for use in unit tests */
/****************************************************************************************/

protected:

/**
* Print the structure that wraps all range and int variables required for the NMODL
Expand Down
71 changes: 21 additions & 50 deletions test/unit/codegen/codegen_coreneuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,18 @@ std::shared_ptr<CodegenCoreneuronCppVisitor> create_coreneuron_cpp_visitor(
const std::shared_ptr<ast::Program>& ast,
const std::string& /* text */,
std::stringstream& ss) {
ImplicitArgumentVisitor().visit_program(*ast);
1uc marked this conversation as resolved.
Show resolved Hide resolved
/// construct symbol table
SymtabVisitor().visit_program(*ast);

/// run all necessary pass
PerfVisitor().visit_program(*ast);
InlineVisitor().visit_program(*ast);
NeuronSolveVisitor().visit_program(*ast);
SolveBlockVisitor().visit_program(*ast);

/// create C code generation visitor
auto cv = std::make_shared<CodegenCoreneuronCppVisitor>("temp.mod", ss, "double", false);
cv->setup(*ast);
return cv;
}

Expand All @@ -64,19 +65,9 @@ std::shared_ptr<CodegenAccVisitor> create_acc_visitor(const std::shared_ptr<ast:

/// create C code generation visitor
auto cv = std::make_shared<CodegenAccVisitor>("temp.mod", ss, "double", false);
cv->setup(*ast);
return cv;
}

/// print instance structure for testing purpose
std::string get_instance_var_setup_function(std::string& nmodl_text) {
const auto& ast = NmodlDriver().parse_string(nmodl_text);
std::stringstream ss;
auto cvisitor = create_coreneuron_cpp_visitor(ast, nmodl_text, ss);
cvisitor->print_instance_variable_setup();
return reindent_text(ss.str());
}

/// print entire code
std::string get_coreneuron_cpp_code(const std::string& nmodl_text,
const bool generate_gpu_code = false) {
Expand Down Expand Up @@ -147,10 +138,9 @@ SCENARIO("Check instance variable definition order", "[codegen][var_order]") {
inst->ion_cao = nt->_data;
inst->ion_ica = nt->_data;
inst->ion_dicadv = nt->_data;
}
)";
auto const expected = reindent_text(generated_code);
auto const result = get_instance_var_setup_function(nmodl_text);
})";
auto const expected = reindent_text(generated_code, 1);
auto const result = get_coreneuron_cpp_code(nmodl_text);
REQUIRE_THAT(result, ContainsSubstring(expected));
}
}
Expand Down Expand Up @@ -195,11 +185,10 @@ SCENARIO("Check instance variable definition order", "[codegen][var_order]") {
inst->v_unused = ml->data+4*pnodecount;
inst->ion_cai = nt->_data;
inst->ion_cao = nt->_data;
}
)";
})";

auto const expected = reindent_text(generated_code);
auto const result = get_instance_var_setup_function(nmodl_text);
auto const expected = reindent_text(generated_code, 1);
auto const result = get_coreneuron_cpp_code(nmodl_text);
REQUIRE_THAT(result, ContainsSubstring(expected));
}
}
Expand Down Expand Up @@ -291,33 +280,15 @@ SCENARIO("Check instance variable definition order", "[codegen][var_order]") {
inst->ion_ko = nt->_data;
inst->ion_k_erev = nt->_data;
inst->style_k = ml->pdata;
}
)";
})";

auto const expected = reindent_text(generated_code);
auto const result = get_instance_var_setup_function(nmodl_text);
auto const expected = reindent_text(generated_code, 1);
auto const result = get_coreneuron_cpp_code(nmodl_text);
REQUIRE_THAT(result, ContainsSubstring(expected));
}
}
}

std::string get_instance_structure(std::string nmodl_text) {
// parse mod file & print mechanism structure
auto const ast = NmodlDriver{}.parse_string(nmodl_text);
// add implicit arguments
ImplicitArgumentVisitor{}.visit_program(*ast);
// update the symbol table for PerfVisitor
SymtabVisitor{}.visit_program(*ast);
// we need the read/write counts so the codegen knows whether or not
// global variables are used
PerfVisitor{}.visit_program(*ast);
// setup codegen
std::stringstream ss{};
CodegenCoreneuronCppVisitor cv{"temp.mod", ss, "double", false};
cv.setup(*ast);
cv.print_mechanism_range_var_structure(true);
return ss.str();
}

SCENARIO("Check parameter constness with VERBATIM block",
"[codegen][verbatim_variable_constness]") {
Expand All @@ -343,17 +314,17 @@ SCENARIO("Check parameter constness with VERBATIM block",
)";

THEN("Variable used in VERBATIM shouldn't be marked as const") {
auto const generated = get_instance_structure(nmodl_text);
auto const generated = get_coreneuron_cpp_code(nmodl_text);
std::string expected_code = R"(
/** all mechanism instance variables and global variables */
struct IntervalFire_Instance {
double* invl{};
const double* burst_start{};
double* v_unused{};
IntervalFire_Store* global{&IntervalFire_global};
};
)";
REQUIRE(reindent_text(generated) == reindent_text(expected_code));
};)";
expected_code = reindent_text(expected_code, 1);
1uc marked this conversation as resolved.
Show resolved Hide resolved
REQUIRE_THAT(generated, ContainsSubstring(expected_code));
}
}
}
Expand All @@ -371,7 +342,7 @@ SCENARIO("Check NEURON globals are added to the instance struct on demand",
}
)";
THEN("The instance struct should contain these variables") {
auto const generated = get_instance_structure(nmodl_text);
auto const generated = get_coreneuron_cpp_code(nmodl_text);
REQUIRE_THAT(generated, ContainsSubstring("double* celsius{&coreneuron::celsius}"));
REQUIRE_THAT(generated, ContainsSubstring("double* pi{&coreneuron::pi}"));
REQUIRE_THAT(generated,
Expand All @@ -389,7 +360,7 @@ SCENARIO("Check NEURON globals are added to the instance struct on demand",
}
)";
THEN("The instance struct should contain celsius for the implicit 5th argument") {
auto const generated = get_instance_structure(nmodl_text);
auto const generated = get_coreneuron_cpp_code(nmodl_text);
REQUIRE_THAT(generated, ContainsSubstring("celsius"));
}
}
Expand All @@ -400,10 +371,10 @@ SCENARIO("Check NEURON globals are added to the instance struct on demand",
}
)";
THEN("The instance struct should not contain those variables") {
auto const generated = get_instance_structure(nmodl_text);
REQUIRE_THAT(generated, !ContainsSubstring("celsius"));
REQUIRE_THAT(generated, !ContainsSubstring("pi"));
REQUIRE_THAT(generated, !ContainsSubstring("secondorder"));
auto const generated = get_coreneuron_cpp_code(nmodl_text);
REQUIRE_THAT(generated, !ContainsSubstring("double* celsius"));
REQUIRE_THAT(generated, !ContainsSubstring("double* pi"));
REQUIRE_THAT(generated, !ContainsSubstring("int* secondorder"));
}
}
}
Expand Down
1 change: 0 additions & 1 deletion test/unit/codegen/codegen_neuron_cpp_visitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ std::shared_ptr<CodegenNeuronCppVisitor> create_neuron_cpp_visitor(

/// create C code generation visitor
auto cv = std::make_shared<CodegenNeuronCppVisitor>("_test", ss, "double", false);
cv->setup(*ast);
return cv;
}

Expand Down
Loading