Skip to content

Commit

Permalink
Print equation guards only for used variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaquinffernandez committed Nov 3, 2022
1 parent dc62b9d commit dabc29b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 38 deletions.
9 changes: 6 additions & 3 deletions src/mmoc/ir/equation_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,13 @@ void EquationPrinter::setup(Equation eq)
}
const bool QSS_EQS = eq.type() == EQUATION::QSSDerivative || eq.type() == EQUATION::ZeroCrossing;
_return_stm = (QSS_EQS) ? FUNCTION_PRINTER::ReturnStatementType::Return : FUNCTION_PRINTER::ReturnStatementType::Continue;
_used_variables = eq.usedVariables();
}

FUNCTION_PRINTER::ReturnStatementType EquationPrinter::returnStm() const { return _return_stm; }

std::multimap<std::string, int> EquationPrinter::usedVariables() const { return _used_variables; }

string EquationPrinter::equationId() const
{
stringstream buffer;
Expand Down Expand Up @@ -228,7 +231,7 @@ string DerivativePrinter::print() const
tabs += TAB;
buffer << printer.beginExpression(identifier(), _range);
buffer << algCode();
buffer << printer.beginDimGuards(equationId(), arguments, _range);
buffer << printer.beginDimGuards(equationId(), arguments, _range, usedVariables());
buffer << tabs << prefix() << lhs() << " = " << _rhs << ";" << endl;
buffer << generateDerivatives(tabs) << endl;
buffer << endl << TAB << printer.endDimGuards(_range);
Expand Down Expand Up @@ -275,7 +278,7 @@ string OutputPrinter::print() const
}
block += TAB;
buffer << fp.beginExpression(identifier(), _range);
buffer << fp.beginDimGuards(equationId(), arguments, _range);
buffer << fp.beginDimGuards(equationId(), arguments, _range, usedVariables());
buffer << algCode();
buffer << block << prefix() << lhs() << " = " << _rhs << ";";
buffer << endl << TAB << fp.endDimGuards(_range);
Expand Down Expand Up @@ -368,7 +371,7 @@ string DependencyPrinter::print() const
}
}
tabs += TAB;
buffer << printer.beginDimGuards(equationId(), arguments, _range);
buffer << printer.beginDimGuards(equationId(), arguments, _range, usedVariables());
buffer << beginParallelMap(tabs);
buffer << tabs << prefix() << lhs(FIRST_ORDER) << " = " << _rhs << ";" << endl;
buffer << generateDerivatives(tabs, SECOND_ORDER) << endl;
Expand Down
4 changes: 3 additions & 1 deletion src/mmoc/ir/equation_printer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class EquationPrinter {
std::string lhs(int order = 0) const;
virtual std::string equationId() const;
FUNCTION_PRINTER::ReturnStatementType returnStm() const;
std::multimap<std::string, int> usedVariables() const;

protected:
void setup(Equation eq);
Expand All @@ -56,6 +57,7 @@ class EquationPrinter {
Expression _lhs;
std::string _alg_code;
FUNCTION_PRINTER::ReturnStatementType _return_stm;
std::multimap<std::string, int> _used_variables;
};

EquationPrinter* getPrinter(Equation eq);
Expand Down Expand Up @@ -127,7 +129,7 @@ class DependencyPrinter : public DerivativePrinter {
std::string print() const override;

protected:
std::string beginParallelMap(std::string &tabs) const;
std::string beginParallelMap(std::string& tabs) const;
std::string endParallelMap() const;

private:
Expand Down
2 changes: 1 addition & 1 deletion src/mmoc/ir/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ string Event::handler(EVENT::Type type) const
}
block += TAB;
buffer << fp.beginExpression(_zero_crossing.identifier(), _range);
buffer << fp.beginDimGuards(_zero_crossing.identifier(), arguments, _range);
buffer << fp.beginDimGuards(_zero_crossing.identifier(), arguments, _range, _zero_crossing.usedVariables());
StatementTable::iterator it;
for (Statement stm = stms.begin(it); !stms.end(it); stm = stms.next(it)) {
buffer << block << stm << endl;
Expand Down
46 changes: 23 additions & 23 deletions src/mmoc/ir/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@

#include <sstream>

#include <generator/macros.h>
#include "helpers.h"
#include "built_in_functions.h"
#include "expression.h"
#include "equation.h"
#include "../generator/macros.h"
#include "../util/error.h"
#include "../util/model_config.h"
#include "../util/util.h"
#include "../util/visitors/get_index_variables.h"
#include "../util/visitors/is_constant_expression.h"
#include <ir/built_in_functions.h>
#include <ir/equation.h>
#include <ir/expression.h>
#include <util/error.h>
#include <util/model_config.h>
#include <util/util.h>
#include <util/visitors/get_index_variables.h>
#include <util/visitors/is_constant_expression.h>

namespace MicroModelica {
using namespace Deps;
Expand All @@ -38,10 +38,7 @@ namespace IR {

/* ExternalFunction Class Implementation */

ExternalFunction::ExternalFunction(string lvalue, string name, AST_ExpressionList args)
: _lvalue(lvalue), _name(name), _args(args)
{
}
ExternalFunction::ExternalFunction(string lvalue, string name, AST_ExpressionList args) : _lvalue(lvalue), _name(name), _args(args) {}

std::ostream& operator<<(std::ostream& out, const ExternalFunction& e)
{
Expand Down Expand Up @@ -229,7 +226,7 @@ string FunctionPrinter::endExpression(Option<Range> range, FUNCTION_PRINTER::Ret
}
break;
}
case FUNCTION_PRINTER::Continue:
case FUNCTION_PRINTER::Continue:
buffer << "continue;" << endl;
break;
}
Expand All @@ -250,18 +247,21 @@ string FunctionPrinter::endDimGuards(Option<Range> range) const
return "";
}

string FunctionPrinter::beginDimGuards(string token, string args, Option<Range> range) const
string FunctionPrinter::beginDimGuards(string token, string args, Option<Range> range, std::multimap<std::string, int> used_variables) const
{
stringstream buffer;
if (range) {
buffer << TAB << "_apply_usage" << token << "(" << args << ");" << endl;
RangeDefinitionTable rdt = range->definition();
RangeDefinitionTable ranges = range->definition();
RangeDefinitionTable::iterator it;
int size = rdt.size(), i = 0, idx = 0;
int size = ranges.size(), idx = 1;
buffer << TAB << "if (";
for (RangeDefinition rd = rdt.begin(it); !rdt.end(it); rd = rdt.next(it), idx++) {
buffer << "(" << rdt.key(it) << " >= " << rd.begin() << " && " << rdt.key(it) << " <= " << rd.end() << ")";
buffer << (++i < size ? " && " : "");
for (RangeDefinition rd = ranges.begin(it); !ranges.end(it); rd = ranges.next(it), idx++) {
string variable = ranges.key(it);
if (used_variables.find(variable) != used_variables.end()) {
buffer << "(" << ranges.key(it) << " >= " << rd.begin() << " && " << ranges.key(it) << " <= " << rd.end() << ")";
buffer << (idx < size ? " && " : "");
}
}
buffer << ") {" << endl;
range->addLocalVariables();
Expand Down Expand Up @@ -348,7 +348,7 @@ string FunctionPrinter::printAlgebraicGuards(Equation alg, Index usage)
Index revert = usage.replace();
arguments = revert.usageExp();
}
buffer << printer.beginDimGuards(alg.applyId(), arguments, range);
buffer << printer.beginDimGuards(alg.applyId(), arguments, range, alg.usedVariables());
return buffer.str();
}

Expand Down Expand Up @@ -409,7 +409,7 @@ string FunctionPrinter::equationVariableMacros(Option<Range> range, Expression l
GetIndexVariables index_usage;
RangeDefinitionTable range_def = range->definition();
buffer << "#define _get" << id << "_var_idxs";
buffer << "(row, var)\\" << endl;
buffer << "(row, var)\\" << endl;
multimap<string, int> usage = index_usage.apply(lhs.expression());
map<string, string> parse_row = parseIndexes("row", range, 1);
map<int, string> ctes = parseConstants(lhs);
Expand All @@ -426,7 +426,7 @@ string FunctionPrinter::equationVariableMacros(Option<Range> range, Expression l
for (auto index : ctes) {
string local_range_var = range->getDimensionVar(index.first, USE_RANGE_DIM_VARS);
buffer << TAB << local_range_var << " = " << index.second << ";\\\n";
}
}
vector<string> exps;
exps.push_back(lhs.dimVariables(USE_RANGE_DIM_VARS));
Expression a_exp = Expression::generate(lhs.reference()->name(), exps);
Expand Down
21 changes: 11 additions & 10 deletions src/mmoc/ir/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
******************************************************************************/

#pragma once
#pragma once

#include <string>

#include "annotation.h"
#include "index.h"
#include "equation.h"
#include "expression.h"
#include "../util/symbol_table.h"
#include "../util/util.h"
#include <ir/annotation.h>
#include <ir/index.h>
#include <ir/equation.h>
#include <ir/expression.h>
#include <util/symbol_table.h>
#include <util/util.h>

namespace MicroModelica {
namespace IR {
Expand All @@ -37,7 +37,7 @@ namespace IR {
*/
class ExternalFunction {
public:
ExternalFunction() {};
ExternalFunction(){};
ExternalFunction(std::string lvalue, std::string name, AST_ExpressionList args);
~ExternalFunction() = default;
friend std::ostream& operator<<(std::ostream& out, const ExternalFunction& e);
Expand All @@ -53,7 +53,8 @@ typedef ModelTable<int, ExternalFunction> ExternalFunctionTable;
class CompiledFunction {
public:
CompiledFunction();
CompiledFunction(std::string name, std::string includeDir, std::string libraryDir, Util::SymbolTable& libraries, std::string prefix = "__");
CompiledFunction(std::string name, std::string includeDir, std::string libraryDir, Util::SymbolTable& libraries,
std::string prefix = "__");
~CompiledFunction() = default;
inline bool hasIncludeDirectory() const { return !_includeDirectory.empty(); };
inline bool hasLibraryDirectory() const { return !_libraryDirectory.empty(); };
Expand Down Expand Up @@ -123,7 +124,7 @@ class FunctionPrinter {
std::string getIndexes(string var, Option<Range> range, int offset, bool modelica_index) const;
std::map<std::string, std::string> parseIndexes(string var, Option<Range> range, int offset, bool modelica_index = true) const;
std::map<int, std::string> parseConstants(Expression ref) const;
std::string beginDimGuards(std::string token, string args, Option<Range> range) const;
std::string beginDimGuards(std::string token, string args, Option<Range> range, std::multimap<std::string, int> used_variables) const;
std::string endDimGuards(Option<Range> range) const;
std::string printAlgebraicGuards(Equation alg, Index usage);
/// TODO: Review modelica_index parameter usage.
Expand Down

0 comments on commit dabc29b

Please sign in to comment.