Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 1eeffa0

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Evaluate functions with constant arguments.
- Implemented -d=nfEvalConstArgFuncs to turn on evaluation of functions with constant arguments (on by default). Evaluation failures are silently ignored, -d=failtrace can be used to display errors. - Remove empty algorithms in SimplifyModel. Belonging to [master]: - #2636 - OpenModelica/OpenModelica-testsuite#1027
1 parent eb0909d commit 1eeffa0

File tree

3 files changed

+53
-7
lines changed

3 files changed

+53
-7
lines changed

Compiler/NFFrontEnd/NFSimplifyExp.mo

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ import ComponentRef = NFComponentRef;
4949
import ExpandExp = NFExpandExp;
5050
import TypeCheck = NFTypeCheck;
5151
import Absyn;
52+
import ErrorExt;
53+
import Flags;
54+
import Debug;
5255

5356
public
5457

@@ -137,7 +140,7 @@ function simplifyCall
137140
protected
138141
Call call;
139142
list<Expression> args;
140-
Boolean builtin;
143+
Boolean builtin, is_pure;
141144
algorithm
142145
Expression.CALL(call = call) := callExp;
143146

@@ -147,12 +150,19 @@ algorithm
147150
args := list(simplify(arg) for arg in call.arguments);
148151
call.arguments := args;
149152
builtin := Function.isBuiltin(call.fn);
153+
is_pure := not Function.isImpure(call.fn);
150154

151155
// Use Ceval for builtin pure functions with literal arguments.
152-
if builtin and not Function.isImpure(call.fn) and List.all(args, Expression.isLiteral) then
153-
callExp := Ceval.evalCall(call, EvalTarget.IGNORE_ERRORS());
156+
if builtin then
157+
if is_pure and List.all(args, Expression.isLiteral) then
158+
callExp := Ceval.evalCall(call, EvalTarget.IGNORE_ERRORS());
159+
else
160+
callExp := simplifyBuiltinCall(Function.nameConsiderBuiltin(call.fn), args, call);
161+
end if;
162+
elseif Flags.isSet(Flags.NF_EVAL_CONST_ARG_FUNCS) and is_pure and List.all(args, Expression.isLiteral) then
163+
callExp := simplifyCall2(call);
154164
else
155-
callExp := simplifyBuiltinCall(Function.nameConsiderBuiltin(call.fn), args, call);
165+
callExp := Expression.CALL(call);
156166
end if;
157167
then
158168
callExp;
@@ -168,6 +178,27 @@ algorithm
168178
end match;
169179
end simplifyCall;
170180

181+
function simplifyCall2
182+
input Call call;
183+
output Expression outExp;
184+
algorithm
185+
ErrorExt.setCheckpoint(getInstanceName());
186+
187+
try
188+
outExp := Ceval.evalCall(call, EvalTarget.IGNORE_ERRORS());
189+
ErrorExt.delCheckpoint(getInstanceName());
190+
else
191+
if Flags.isSet(Flags.FAILTRACE) then
192+
ErrorExt.delCheckpoint(getInstanceName());
193+
Debug.traceln("- " + getInstanceName() + " failed to evaluate " + Call.toString(call) + "\n");
194+
else
195+
ErrorExt.rollBack(getInstanceName());
196+
end if;
197+
198+
outExp := Expression.CALL(call);
199+
end try;
200+
end simplifyCall2;
201+
171202
function simplifyBuiltinCall
172203
input Absyn.Path name;
173204
input list<Expression> args;

Compiler/NFFrontEnd/NFSimplifyModel.mo

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ algorithm
6161
flatModel.variables := list(simplifyVariable(v) for v in flatModel.variables);
6262
flatModel.equations := simplifyEquations(flatModel.equations);
6363
flatModel.initialEquations := simplifyEquations(flatModel.initialEquations);
64-
flatModel.algorithms := list(simplifyAlgorithm(a) for a in flatModel.algorithms);
65-
flatModel.initialAlgorithms := list(simplifyAlgorithm(a) for a in flatModel.initialAlgorithms);
64+
flatModel.algorithms := simplifyAlgorithms(flatModel.algorithms);
65+
flatModel.initialAlgorithms := simplifyAlgorithms(flatModel.initialAlgorithms);
6666

6767
functions := FunctionTree.map(functions, simplifyFunction);
6868

@@ -185,6 +185,21 @@ algorithm
185185
end match;
186186
end simplifyEquation;
187187

188+
function simplifyAlgorithms
189+
input list<Algorithm> algs;
190+
output list<Algorithm> outAlgs = {};
191+
algorithm
192+
for alg in algs loop
193+
alg := simplifyAlgorithm(alg);
194+
195+
if not listEmpty(alg.statements) then
196+
outAlgs := alg :: outAlgs;
197+
end if;
198+
end for;
199+
200+
outAlgs := listReverseInPlace(outAlgs);
201+
end simplifyAlgorithms;
202+
188203
function simplifyAlgorithm
189204
input output Algorithm alg;
190205
algorithm

Compiler/Util/Flags.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ constant DebugFlag DEBUG_DAEMODE = DEBUG_FLAG(178, "debugDAEmode", false,
532532
Util.gettext("Dump debug output for the DAEmode."));
533533
constant DebugFlag NF_SCALARIZE = DEBUG_FLAG(179, "nfScalarize", true,
534534
Util.gettext("Run scalarization in NF, default true."));
535-
constant DebugFlag NF_EVAL_CONST_ARG_FUNCS = DEBUG_FLAG(180, "nfEvalConstArgFuncs", false,
535+
constant DebugFlag NF_EVAL_CONST_ARG_FUNCS = DEBUG_FLAG(180, "nfEvalConstArgFuncs", true,
536536
Util.gettext("Evaluate all functions with constant arguments in the new frontend."));
537537
constant DebugFlag NF_EXPAND_OPERATIONS = DEBUG_FLAG(181, "nfExpandOperations", true,
538538
Util.gettext("Expand all unary/binary operations to scalar expressions in the new frontend."));

0 commit comments

Comments
 (0)