Skip to content

Commit

Permalink
Improve handling of CLI and annotation flags/options.
Browse files Browse the repository at this point in the history
  - Consider `experiment` annotation when using omc on the CLI.
  - Consider `__OpenModelica_commandLineOptions` annotation when using omc on the CLI.
  - Consider flag `--newBackend` when specified on the CLI.

  These have required some restructuring of the translateModel workflow.
  There is still so much more that can be cleaned up and refactored to make
  more sense.

  Fixes OpenModelica#7860.
  Fixes OpenModelica#8122.
  Fixes OpenModelica#10142.
  • Loading branch information
mahge committed Mar 20, 2023
1 parent 4a28b19 commit c1e6c5f
Show file tree
Hide file tree
Showing 4 changed files with 388 additions and 109 deletions.
78 changes: 52 additions & 26 deletions OMCompiler/Compiler/Main/Main.mo
Expand Up @@ -399,15 +399,16 @@ algorithm
local
Absyn.Program p, pLibs;
DAE.DAElist d;
String s,str,f;
String flatString,str,f;
list<String> libs;
Absyn.Path cname;
Boolean silent,notsilent;
Boolean runBackend, runSilent;
GlobalScript.Statements stmts;
FCore.Cache cache;
FCore.Graph env;
DAE.FunctionTree funcs;
list<Absyn.Class> cls;
String cls, fileNamePrefix;
SimCode.SimulationSettings sim_settings;

// A .mo-file, followed by an optional list of extra .mo-files and libraries.
// The last class in the first file will be instantiated.
Expand Down Expand Up @@ -439,35 +440,60 @@ algorithm
execStat("Parsed file");

// Instantiate the program.
(cache, env, d, cname, s) := instantiate();
p := SymbolTable.getAbsyn();
// (cache, env, d, cname, flatString) := instantiate();

d := if Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d;
cls := Config.classToInstantiate();
// If no class was explicitly specified, instantiate the last class in the
// program. Otherwise, instantiate the given class name.
cname := if stringEmpty(cls) then AbsynUtil.lastClassname(SymbolTable.getAbsyn()) else AbsynUtil.stringPath(cls);
fileNamePrefix := Util.stringReplaceChar(AbsynUtil.pathString(cname), ".", "_");

funcs := FCore.getFunctionTree(cache);
runBackend := Config.simulationCg() or Config.simulation();
runSilent := Config.silent();

Print.clearBuf();
execStat("Transformations before Dump");
if stringEmpty(s) and not Config.silent() then
s := DAEDump.dumpStr(d, funcs);
execStat("DAEDump done");
end if;
Print.printBuf(s);
if Flags.isSet(Flags.DAE_DUMP_GRAPHV) then
DAEDump.dumpGraphviz(d);
end if;
execStat("Misc Dump");
// If accepting parModelica create a slightly different default settings.
// Temporary solution for now since Intel OpenCL dll calls hang.
// sim_settings := if Config.acceptParModelicaGrammar() then
// SimCodeMain.createSimulationSettings(0.0, 1.0, 1, 1e-6, "dassl", "", "plt", ".*", "") else
// SimCodeMain.createSimulationSettings(0.0, 1.0, 500, 1e-6, "dassl", "", "mat", ".*", "");

// Do any transformations required before going into code generation, e.g. if-equations to expressions.
d := if boolNot(Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP)) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d;
(_ , _, _, _, _) := CevalScriptBackend.translateModel(FCore.emptyCache(), FGraph.empty(), cname,
fileNamePrefix, runBackend, runSilent, NONE());

if not Config.silent() then
print(Print.getString());
end if;
execStat("Transformations before backend");

// Run the backend.
optimizeDae(cache, env, d, p, cname);





// p := SymbolTable.getAbsyn();

// d := if Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d;

// funcs := FCore.getFunctionTree(cache);

// Print.clearBuf();
// execStat("Transformations before Dump");
// if stringEmpty(flatString) and not Config.silent() then
// flatString := DAEDump.dumpStr(d, funcs);
// execStat("DAEDump done");
// end if;
// Print.printBuf(flatString);
// if Flags.isSet(Flags.DAE_DUMP_GRAPHV) then
// DAEDump.dumpGraphviz(d);
// end if;
// execStat("Misc Dump");

// // Do any transformations required before going into code generation, e.g. if-equations to expressions.
// d := if boolNot(Flags.isSet(Flags.TRANSFORMS_BEFORE_DUMP)) then DAEUtil.transformationsBeforeBackend(cache,env,d) else d;

// if not Config.silent() then
// print(Print.getString());
// end if;
// execStat("Transformations before backend");

// // Run the backend.
// optimizeDae(cache, env, d, p, cname);
// Show any errors or warnings if there are any!
showErrors(Print.getErrorString(), ErrorExt.printMessagesStr(false));
then ();
Expand Down
112 changes: 31 additions & 81 deletions OMCompiler/Compiler/Script/CevalScriptBackend.mo
Expand Up @@ -1233,7 +1233,7 @@ algorithm
case ("translateModel",vals as {Values.CODE(Absyn.C_TYPENAME(className)),_,_,_,_,_,Values.STRING(filenameprefix),_,_,_,_,_})
equation
(outCache,simSettings) = calculateSimulationSettings(outCache, vals);
(b,outCache) = translateModel(outCache, inEnv, className, filenameprefix, true, SOME(simSettings));
(b,outCache) = translateModel(outCache, inEnv, className, filenameprefix, true, true, SOME(simSettings));
then
Values.BOOL(b);

Expand Down Expand Up @@ -3183,7 +3183,7 @@ algorithm
end getAdjacencyMatrix;
/* -------------------------------------------------------------------
RUN OLD FRONTEND
RUN FRONTEND
------------------------------------------------------------------- */
public function runFrontEnd
input output FCore.Cache cache;
Expand Down Expand Up @@ -3382,22 +3382,22 @@ algorithm
end if;
end runFrontEndWorkNF;
protected function translateModel " author: x02lucpo
translates a model into cpp code and writes also a makefile"
public function translateModel
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well.";
input Boolean runSilent "if true, flat modelica code will not be dumped to out stream";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
output Boolean success;
output FCore.Cache outCache;
output list<String> outStringLst;
output list<String> outLibs;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outStringLst,outFileDir,resultValues):=
match (inCache,inEnv,className,inFileNamePrefix,addDummy,inSimSettingsOpt)
(outCache,outLibs,outFileDir,resultValues):=
match (inCache,inEnv,className,inFileNamePrefix,inSimSettingsOpt)
local
FCore.Cache cache;
FCore.Graph env;
Expand All @@ -3408,79 +3408,25 @@ algorithm
String commandLineOptions;
list<String> args;
Boolean haveAnnotation;
SimCode.SimulationSettings simSettings;
GlobalScript.SimulationOptions defaulSimOpt;
case (cache,env,_,fileNamePrefix,_,_)
case (cache,env,_,fileNamePrefix,_)
algorithm
if Config.ignoreCommandLineOptionsAnnotation() then
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache,env,className,fileNamePrefix,inSimSettingsOpt);
if isSome(inSimSettingsOpt) then
SOME(simSettings) := inSimSettingsOpt;
else
// read the __OpenModelica_commandLineOptions
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
haveAnnotation := boolNot(stringEq(commandLineOptions, ""));
// backup the flags.
flags := if haveAnnotation then FlagsUtil.backupFlags() else FlagsUtil.loadFlags();
try
// apply if there are any new flags
if haveAnnotation then
args := System.strtok(commandLineOptions, " ");
FlagsUtil.readArgs(args);
end if;
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache,env,className,fileNamePrefix,inSimSettingsOpt);
// reset to the original flags
FlagsUtil.saveFlags(flags);
else
FlagsUtil.saveFlags(flags);
fail();
end try;
defaulSimOpt := buildSimulationOptionsFromModelExperimentAnnotation(className, fileNamePrefix, SOME(defaultSimulationOptions));
simSettings := convertSimulationOptionsToSimCode(defaulSimOpt);
end if;
then
(cache,libs,file_dir,resultValues);
end match;
end translateModel;
protected function translateLabeledModel " author: Fatima
translates a labeled model into cpp code and writes also a makefile"
input FCore.Cache inCache;
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input String inFileNamePrefix;
input Boolean addDummy "if true, add a dummy state";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
input list<Absyn.NamedArg> inLabelstoCancel;
output FCore.Cache outCache;
output BackendDAE.BackendDAE outBackendDAE;
output list<String> outStringLst;
output String outFileDir;
output list<tuple<String,Values.Value>> resultValues;
algorithm
(outCache,outStringLst,outFileDir,resultValues):=
match (inCache,inEnv,className,inFileNamePrefix,addDummy,inSimSettingsOpt,inLabelstoCancel)
local
FCore.Cache cache;
FCore.Graph env;
BackendDAE.BackendDAE indexed_dlow;
list<String> libs;
String file_dir, fileNamePrefix;
Absyn.Program p;
Flags.Flag flags;
String commandLineOptions;
list<String> args;
Boolean haveAnnotation;
list<Absyn.NamedArg> labelstoCancel;
case (cache,env,_,fileNamePrefix,_,_,labelstoCancel)
algorithm
if Config.ignoreCommandLineOptionsAnnotation() then
(true, cache, libs, file_dir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel));
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache, env, className, fileNamePrefix, runBackend, runSilent, SOME(simSettings));
else
// read the __OpenModelica_commandLineOptions
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
Absyn.STRING(commandLineOptions) := Interactive.getNamedAnnotation(className, SymbolTable.getAbsyn(), Absyn.IDENT
("__OpenModelica_commandLineOptions"), SOME(Absyn.STRING("")), Interactive.getAnnotationExp);
haveAnnotation := boolNot(stringEq(commandLineOptions, ""));
// backup the flags.
flags := if haveAnnotation then FlagsUtil.backupFlags() else FlagsUtil.loadFlags();
Expand All @@ -3491,8 +3437,8 @@ algorithm
FlagsUtil.readArgs(args);
end if;
(true, cache, libs, file_dir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt,Absyn.FUNCTIONARGS({},argNames =labelstoCancel));
(success, cache, libs, file_dir, resultValues) :=
callTranslateModel(cache, env, className, fileNamePrefix, runBackend, runSilent, SOME(simSettings));
// reset to the original flags
FlagsUtil.saveFlags(flags);
else
Expand All @@ -3504,7 +3450,7 @@ algorithm
(cache,libs,file_dir,resultValues);
end match;
end translateLabeledModel;
end translateModel;
protected function callTranslateModel
"Call the main translate function. This function
Expand All @@ -3514,6 +3460,8 @@ protected function callTranslateModel
input FCore.Graph inEnv;
input Absyn.Path className "path for the model";
input String inFileNamePrefix;
input Boolean runBackend "if true, run the backend as well. This will run SimCode and Codegen as well.";
input Boolean runSilent "if true, flat modelica code will not be dumped to out stream";
input Option<SimCode.SimulationSettings> inSimSettingsOpt;
output Boolean success;
output FCore.Cache outCache;
Expand All @@ -3528,8 +3476,8 @@ algorithm
success := true;
else
(success, outCache, outStringLst, outFileDir, resultValues) :=
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(),inCache,inEnv,
className,inFileNamePrefix,true,inSimSettingsOpt,Absyn.FUNCTIONARGS({},{}));
SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.NORMAL(), inCache, inEnv,
className, inFileNamePrefix, runBackend, runSilent, inSimSettingsOpt, Absyn.FUNCTIONARGS({},{}));
end if;
end callTranslateModel;
Expand Down Expand Up @@ -4013,7 +3961,8 @@ algorithm
FlagsUtil.setConfigBool(Flags.BUILDING_FMU, true);
FlagsUtil.setConfigString(Flags.FMI_VERSION, FMUVersion);
try
(success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUType, fmuTargetName), cache, inEnv, className, filenameprefix, addDummy, SOME(simSettings));
(success, cache, libs, _, _) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.FMU(FMUType, fmuTargetName),
cache, inEnv, className, filenameprefix, true, true, SOME(simSettings));
true := success;
outValue := Values.STRING((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fmuTargetName + ".fmu");
else
Expand Down Expand Up @@ -4209,7 +4158,8 @@ protected function translateModelXML " author: Alachew
protected
Boolean success;
algorithm
(success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(),cache,env,className,fileNamePrefix,addDummy,inSimSettingsOpt);
(success,cache) := SimCodeMain.translateModel(SimCodeMain.TranslateModelKind.XML(), cache, env, className,
fileNamePrefix, true, true, inSimSettingsOpt);
outValue := Values.STRING(if success then ((if not Testsuite.isRunning() then System.pwd() + Autoconf.pathDelimiter else "") + fileNamePrefix+".xml") else "");
end translateModelXML;

Expand Down Expand Up @@ -5605,7 +5555,7 @@ algorithm
(cache,simSettings) := calculateSimulationSettings(cache, values);
SimCode.SIMULATION_SETTINGS(method = method_str, outputFormat = outputFormat_str) := simSettings;

(success,cache,libsAndLibDirs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix,true, SOME(simSettings));
(success,cache,libsAndLibDirs,file_dir,resultValues) := translateModel(cache,env, classname, filenameprefix, true, true, SOME(simSettings));
//cname_str = AbsynUtil.pathString(classname);
//SimCodeUtil.generateInitData(indexed_dlow_1, classname, filenameprefix, init_filename,
// starttime_r, stoptime_r, interval_r, tolerance_r, method_str,options_str,outputFormat_str);
Expand Down

0 comments on commit c1e6c5f

Please sign in to comment.