Skip to content

Commit

Permalink
Separate compilation: Do not instantiate functions until necessary
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@16425 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Jun 20, 2013
1 parent 88701d3 commit 164b1a6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Compiler/FrontEnd/Env.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1356,7 +1356,8 @@ algorithm

case (FRAME(id,st,ft,clsAndVars,tys,crs,du,it,EXTRA(mo))::fs, _)
equation
mo = List.union(inPMs, mo);
// Cannot List.union stuff that contains arrays; need a comparison function
// mo = List.union(inPMs, mo);
then
FRAME(id,st,ft,clsAndVars,tys,crs,du,it,EXTRA(mo))::fs;

Expand Down
2 changes: 2 additions & 0 deletions Compiler/FrontEnd/MetaModelicaBuiltin.mo
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,8 @@ end equality;
function setGlobalRoot<A>
"Sets the index of the root variable with index 0..1023.
This is a global mutable value and should be used sparingly.
You are recommended not to use 0 or false since the runtime system may treat these values as uninitialized and fail getGlobalRoot later on.
"
input Integer index;
input A value;
Expand Down
50 changes: 39 additions & 11 deletions Compiler/FrontEnd/Static.mo
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import Flags;
protected import Global;
protected import Inline;
protected import Inst;
protected import InnerOuter;
Expand Down Expand Up @@ -7809,6 +7810,11 @@ algorithm
end matchcontinue;
end elabCallArgsMetarecord;

protected uniontype ForceFunctionInst
record FORCE_FUNCTION_INST "Used when blocking function instantiation to instantiate the function anyway" end FORCE_FUNCTION_INST;
record NORMAL_FUNCTION_INST "Used when blocking function instantiation to instantiate the function anyway" end NORMAL_FUNCTION_INST;
end ForceFunctionInst;

public function instantiateDaeFunction "help function to elabCallArgs. Instantiates the function as a dae and adds it to the
functiontree of a newly created dae"
input Env.Cache inCache;
Expand All @@ -7820,9 +7826,23 @@ functiontree of a newly created dae"
output Env.Cache outCache;
output Util.Status status;
algorithm
(outCache,status) := instantiateDaeFunction2(inCache, env, name, builtin, clOpt, Error.getNumErrorMessages(), printErrorMsg);
(outCache,status) := instantiateDaeFunction2(inCache, env, name, builtin, clOpt, Error.getNumErrorMessages(), printErrorMsg, Util.isSome(getGlobalRoot(Global.instOnlyForcedFunctions)), NORMAL_FUNCTION_INST());
end instantiateDaeFunction;

public function instantiateDaeFunctionForceInst "help function to elabCallArgs. Instantiates the function as a dae and adds it to the
functiontree of a newly created dae"
input Env.Cache inCache;
input Env.Env env;
input Absyn.Path name;
input Boolean builtin "builtin functions create empty dae";
input Option<SCode.Element> clOpt "if not present, looked up by name in environment";
input Boolean printErrorMsg "if true, prints an error message if the function could not be instantiated";
output Env.Cache outCache;
output Util.Status status;
algorithm
(outCache,status) := instantiateDaeFunction2(inCache, env, name, builtin, clOpt, Error.getNumErrorMessages(), printErrorMsg, Util.isSome(getGlobalRoot(Global.instOnlyForcedFunctions)), FORCE_FUNCTION_INST());
end instantiateDaeFunctionForceInst;

protected function instantiateDaeFunction2 "help function to elabCallArgs. Instantiates the function as a dae and adds it to the
functiontree of a newly created dae"
input Env.Cache inCache;
Expand All @@ -7832,10 +7852,12 @@ functiontree of a newly created dae"
input Option<SCode.Element> clOpt "if not present, looked up by name in environment";
input Integer numError "if errors were added, do not add a generic error message";
input Boolean printErrorMsg "if true, prints an error message if the function could not be instantiated";
input Boolean instOnlyForcedFunctions;
input ForceFunctionInst forceFunctionInst;
output Env.Cache outCache;
output Util.Status status;
algorithm
(outCache,status) := matchcontinue(inCache,inEnv,inName,builtin,clOpt,numError,printErrorMsg)
(outCache,status) := matchcontinue(inCache,inEnv,inName,builtin,clOpt,numError,printErrorMsg,instOnlyForcedFunctions,forceFunctionInst)
local
Env.Cache cache;
Env.Env env;
Expand All @@ -7844,32 +7866,38 @@ algorithm
DAE.ComponentRef cref;
Absyn.Path name;

// Skip function instantiation if we set those flags
case(cache,env,name,_,_,_,_,true,NORMAL_FUNCTION_INST())
equation
// print("Skipping: " +& Absyn.pathString(name) +& "\n");
then (cache,Util.SUCCESS());

// Builtin functions skipped
case(cache,env,name,true,_,_,_) then (cache,Util.SUCCESS());
case(cache,env,name,true,_,_,_,_,_) then (cache,Util.SUCCESS());

// External object functions skipped
case(cache,env,name,_,_,_,_)
case(cache,env,name,_,_,_,_,_,_)
equation
(_,true) = isExternalObjectFunction(cache,env,name);
then (cache,Util.SUCCESS());

// Recursive calls (by looking at envinronment) skipped
case(cache,env,name,_,NONE(),_,_)
case(cache,env,name,_,NONE(),_,_,_,_)
equation
false = Env.isTopScope(env);
true = Absyn.pathSuffixOf(name,Env.getEnvName(env));
then (cache,Util.SUCCESS());

// Recursive calls (by looking in cache) skipped
case(cache,env,name,_,_,_,_)
case(cache,env,name,_,_,_,_,_,_)
equation
(cache,cl,env) = Lookup.lookupClass(cache,env,name,false);
(cache,name) = Inst.makeFullyQualified(cache,env,name);
Env.checkCachedInstFuncGuard(cache,name);
then (cache,Util.SUCCESS());

// class must be looked up
case(cache,env,name,_,NONE(),_,_)
case(cache,env,name,_,NONE(),_,_,_,_)
equation
(cache,cl,env) = Lookup.lookupClass(cache,env,name,false);
(cache,name) = Inst.makeFullyQualified(cache,env,name);
Expand All @@ -7878,28 +7906,28 @@ algorithm
then (cache,Util.SUCCESS());

// class already available
case(cache,env,name,_,SOME(cl),_,_)
case(cache,env,name,_,SOME(cl),_,_,_,_)
equation
(cache,name) = Inst.makeFullyQualified(cache,env,name);
(cache,env,_) = Inst.implicitFunctionInstantiation(cache,env,InnerOuter.emptyInstHierarchy,DAE.NOMOD(),Prefix.NOPRE(),cl,{});
then (cache,Util.SUCCESS());

// call to function reference variable
case (cache,env,name,_,NONE(),_,_)
case (cache,env,name,_,NONE(),_,_,_,_)
equation
cref = pathToComponentRef(name);
(cache,_,DAE.T_FUNCTION(funcArg = _),_,_,_,env,_,_) = Lookup.lookupVar(cache,env,cref);
then (cache,Util.SUCCESS());

case(cache,env,name,_,_,_,true)
case(cache,env,name,_,_,_,true,_,_)
equation
true = Error.getNumErrorMessages() == numError;
envStr = Env.printEnvPathStr(env);
pathStr = Absyn.pathString(name);
Error.addMessage(Error.GENERIC_INST_FUNCTION, {pathStr, envStr});
then fail();

case (cache,env,name,_,_,_,_) then (cache,Util.FAILURE());
else (inCache,Util.FAILURE());
end matchcontinue;
end instantiateDaeFunction2;

Expand Down
1 change: 1 addition & 0 deletions Compiler/Global/Global.mo
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ constant Integer profilerTime1Index = 5;
constant Integer profilerTime2Index = 6;
constant Integer flagsIndex = 7;
constant Integer builtinGraphEnvIndex = 8;
constant Integer instOnlyForcedFunctions = 9;

end Global;
2 changes: 2 additions & 0 deletions Compiler/Main/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ protected import Env;
protected import Error;
protected import ErrorExt;
protected import Flags;
protected import Global;
protected import Interactive;
protected import List;
protected import Parser;
Expand Down Expand Up @@ -1041,6 +1042,7 @@ algorithm
_ := matchcontinue args
case _
equation
setGlobalRoot(Global.instOnlyForcedFunctions, NONE());
System.realtimeTick(CevalScript.RT_CLOCK_SIMULATE_TOTAL);
args_1 = Flags.new(args);
System.gettextInit(Util.if_(Config.getRunningTestsuite(),"C",Flags.getConfigString(Flags.LOCALE_FLAG)));
Expand Down
21 changes: 20 additions & 1 deletion Compiler/Script/CevalScript.mo
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ protected import Expression;
protected import ExpressionSimplify;
protected import ExpressionDump;
protected import Flags;
protected import Global;
protected import Inst;
protected import InnerOuter;
protected import List;
Expand Down Expand Up @@ -908,6 +909,7 @@ algorithm
Boolean dumpExtractionSteps;
list<tuple<Absyn.Path,list<String>>> uses;
Config.LanguageStandard oldLanguageStd;
SCode.Element cl;

case (cache,env,"parseString",{Values.STRING(str1),Values.STRING(str2)},st,_)
equation
Expand Down Expand Up @@ -1897,10 +1899,24 @@ algorithm
case (cache,env,"generateSeparateCode",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT("AllLoadedClasses")))},(st as Interactive.SYMBOLTABLE(ast = p)),_)
equation
sp = SCodeUtil.translateAbsyn2SCode(p);
setGlobalRoot(Global.instOnlyForcedFunctions,SOME(true));
deps = generateFunctions(cache,env,p,sp,{});
setGlobalRoot(Global.instOnlyForcedFunctions,NONE());
then (cache,Values.BOOL(true),st);

case (cache,env,"generateSeparateCode",{Values.CODE(Absyn.C_TYPENAME(Absyn.IDENT(str)))},(st as Interactive.SYMBOLTABLE(ast = p)),_)
equation
false = stringEqual(str,"AllLoadedClasses");
sp = SCodeUtil.translateAbsyn2SCode(p);
cl = List.getMemberOnTrue(str, sp, SCode.isClassNamed);
setGlobalRoot(Global.instOnlyForcedFunctions,SOME(true));
deps = generateFunctions(cache,env,p,{cl},{});
setGlobalRoot(Global.instOnlyForcedFunctions,NONE());
then (cache,Values.BOOL(true),st);

case (cache,env,"generateSeparateCode",_,st,_)
equation
setGlobalRoot(Global.instOnlyForcedFunctions,NONE());
then (cache,Values.BOOL(false),st);

case (cache,env,"loadModel",{Values.CODE(Absyn.C_TYPENAME(path)),Values.ARRAY(valueLst=cvars),Values.BOOL(b),Values.STRING(str)},
Expand Down Expand Up @@ -4578,6 +4594,7 @@ algorithm
(0,_) = System.regex(file, "ModelicaBuiltin.mo$", 1, false, false);
names = List.map(List.filterOnTrue(List.map(List.filterOnTrue(elementLst, SCode.elementIsClass), SCode.getElementClass), SCode.isFunction), SCode.className);
paths = List.map1r(names,Absyn.makeQualifiedPathFromStrings,name);
// print("paths to generate:" +& stringDelimitList(List.map(paths,Absyn.pathString),",") +& "\n");
cache = instantiateDaeFunctions(cache, env, paths);
funcs = Env.getFunctionTree(cache);
d = List.map1(paths, DAEUtil.getNamedFunction, funcs);
Expand Down Expand Up @@ -4642,7 +4659,9 @@ algorithm
case (cache,env,{}) then cache;
case (cache,env,path::paths)
equation
(cache,Util.SUCCESS()) = Static.instantiateDaeFunction(cache,env,path,false,NONE(),true);
// print("force inst: " +& Absyn.pathString(path));
(cache,Util.SUCCESS()) = Static.instantiateDaeFunctionForceInst(cache,env,path,false,NONE(),true);
// print(" ok\n");
cache = instantiateDaeFunctions(cache,env,paths);
then cache;
end match;
Expand Down

0 comments on commit 164b1a6

Please sign in to comment.