Skip to content

Commit

Permalink
Rename tearingSelect to __OpenModelica_tearingSelect (#11717) (#1…
Browse files Browse the repository at this point in the history
…1900)

Co-authored-by: phannebohm <phannebohm@hsbi.de>
  • Loading branch information
adrpo and phannebohm committed Jan 29, 2024
1 parent 247f5aa commit fc3c2d4
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 87 deletions.
23 changes: 13 additions & 10 deletions OMCompiler/Compiler/BackEnd/BackendDAEUtil.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1131,24 +1131,27 @@ end simplifySubscript;


public function setTearingSelectAttribute
"Returns the expression of the tearingSelect annotation"
"Returns the expression of the __OpenModelica_tearingSelect annotation"
input Option<SCode.Comment> comment;
output Option<BackendDAE.TearingSelect> ts;
protected
SCode.Annotation ann;
Absyn.Exp val;
String ts_str;
algorithm
try
SOME(SCode.COMMENT(annotation_=SOME(ann))) := comment;
SOME(val) := SCodeUtil.lookupAnnotationBinding(ann, "tearingSelect");
ts_str := AbsynUtil.crefIdent(AbsynUtil.expCref(val));
ts := match(ts_str)
case "always" then SOME(BackendDAE.ALWAYS());
case "prefer" then SOME(BackendDAE.PREFER());
case "avoid" then SOME(BackendDAE.AVOID());
case "never" then SOME(BackendDAE.NEVER());
case "default" then SOME(BackendDAE.DEFAULT());
try
SOME(val) := SCodeUtil.lookupAnnotationBinding(ann, "__OpenModelica_tearingSelect");
else
SOME(val) := SCodeUtil.lookupAnnotationBinding(ann, "tearingSelect");
Error.addCompilerWarning("Deprecated vendor annotation 'tearingSelect' found. Use '__OpenModelica_tearingSelect' instead.");
end try;
ts := match AbsynUtil.crefIdent(AbsynUtil.expCref(val))
case "always" then SOME(BackendDAE.ALWAYS());
case "prefer" then SOME(BackendDAE.PREFER());
case "avoid" then SOME(BackendDAE.AVOID());
case "never" then SOME(BackendDAE.NEVER());
case "default" then SOME(BackendDAE.DEFAULT());
else NONE();
end match;
else
Expand Down
46 changes: 23 additions & 23 deletions OMCompiler/Compiler/BackEnd/Tearing.mo
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ algorithm
end if;
columark := arrayCreate(size,-1);

// Collect variables with annotation attribute 'tearingSelect=always', 'tearingSelect=prefer', 'tearingSelect=avoid' and 'tearingSelect=never'
// Collect variables with annotation attribute '__OpenModelica_tearingSelect in [always, prefer, avoid, never]'
(tSel_always,tSel_prefer,tSel_avoid,tSel_never,_) := tearingSelect(var_lst, {}, DAEtypeStr);

// determine tvars and do cheap matching until a maximum matching is there
Expand Down Expand Up @@ -917,7 +917,7 @@ algorithm
case (tvar::rest,{})
equation
if listMember(tvar,tSel_never) then
Error.addCompilerWarning("There are tearing variables with annotation attribute 'tearingSelect = never'. Use -d=tearingdump and -d=tearingdumpV for more information.");
Error.addCompilerWarning("There are tearing variables with annotation attribute '__OpenModelica_tearingSelect = never'. Use -d=tearingdump and -d=tearingdumpV for more information.");
end if;
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nForced selection of Tearing Variable:\n" + UNDERLINE + "\n");
Expand Down Expand Up @@ -1017,7 +1017,7 @@ algorithm
false = listEmpty(unsolvables);
tvar = listHead(unsolvables);
if listMember(tvar,tSel_never) then
Error.addCompilerWarning("There are tearing variables with annotation attribute 'tearingSelect = never'. Use -d=tearingdump and -d=tearingdumpV for more information.");
Error.addCompilerWarning("There are tearing variables with annotation attribute '__OpenModelica_tearingSelect = never'. Use -d=tearingdump and -d=tearingdumpV for more information.");
end if;
if Flags.isSet(Flags.TEARING_DUMP) or Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nForced selection of Tearing Variable:\n" + UNDERLINE + "\n");
Expand Down Expand Up @@ -1063,12 +1063,12 @@ algorithm
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Points after 'discriminateDiscrete':\n" + stringDelimitList(List.map(arrayList(points),intString),",") + "\n\n");
end if;
// 4th: Prefer variables with annotation attribute 'tearingSelect=prefer'
// 4th: Prefer variables with annotation attribute '__OpenModelica_tearingSelect = prefer'
pointsLst = preferAvoidVariables(freeVars, arrayList(points), tSel_prefer, 3.0);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Points after preferring variables with attribute 'prefer':\n" + stringDelimitList(List.map(pointsLst,intString),",") + "\n\n");
end if;
// 5th: Avoid variables with annotation attribute 'tearingSelect=avoid'
// 5th: Avoid variables with annotation attribute '__OpenModelica_tearingSelect = avoid'
pointsLst = preferAvoidVariables(freeVars, pointsLst, tSel_avoid, 0.334);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("Points after discrimination against variables with attribute 'avoid':\n" + stringDelimitList(List.map(pointsLst,intString),",") + "\n\n");
Expand All @@ -1079,7 +1079,7 @@ algorithm
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("tVar: " + intString(tvar) + " (" + intString(listGet(pointsLst,tvar)) + " points)\n\n");
elseif listMember(tvar,tSel_avoid) then
Error.addCompilerWarning("The Tearing heuristic has chosen variables with annotation attribute 'tearingSelect = avoid'. Use -d=tearingdump and -d=tearingdumpV for more information.");
Error.addCompilerWarning("The Tearing heuristic has chosen variables with annotation attribute '__OpenModelica_tearingSelect = avoid'. Use -d=tearingdump and -d=tearingdumpV for more information.");
end if;
then
tvar;
Expand Down Expand Up @@ -1728,7 +1728,7 @@ try
unsolvedDiscreteVars := findDiscreteWarnTearingSelect(var_lst);
// print("All discrete Vars: " + stringDelimitList(List.map(unsolvedDiscreteVars,intString),",") + "\n");

// also find $cse vars and try to make them inner vars since they have implicit tearingSelect never
// also find $cse vars and try to make them inner vars since they have implicit '__OpenModelica_tearingSelect = never'
unsolvedCSEVars := findCSE(var_lst);
// print("All CSE Vars: " + stringDelimitList(List.map(unsolvedCSEVars,intString),",") + "\n");

Expand Down Expand Up @@ -2086,7 +2086,7 @@ algorithm
print("\nDiscrete Vars:\n" + stringDelimitList(List.map(discreteVars,intString),",") + "\n\n");
end if;

// Collect variables with annotation attribute 'tearingSelect=always', 'tearingSelect=prefer', 'tearingSelect=avoid' and 'tearingSelect=never'
// Collect variables with annotation attribute '__OpenModelica_tearingSelect in [always, prefer, avoid, never]'
(tSel_always, tSel_prefer, tSel_avoid, tSel_never, tSel_alwaysByUser) := tearingSelect(var_lst, tearingSelect_always, DAEtypeStr);
if not listEmpty(tSel_alwaysByUser) then
Error.addMessage(Error.USER_TEARING_VARS, {intString(strongComponentIndex), BackendDump.printBackendDAEType2String(DAEtype), BackendDump.dumpMarkedVarList(var_lst, tSel_alwaysByUser)});
Expand Down Expand Up @@ -2271,15 +2271,15 @@ end CellierTearing;


protected function tearingSelect
"collects variables with annotation attribute 'tearingSelect=always', 'tearingSelect=prefer', 'tearingSelect=avoid' and 'tearingSelect=never'
"collects variables with annotation attribute '__OpenModelica_tearingSelect in [always, prefer, avoid, never]'
author: ptaeuber FHB 2014-05"
input list<BackendDAE.Var> var_lstIn;
input output list<Integer> always;
input String DAEtypeStr;
output list<Integer> prefer = {};
output list<Integer> avoid = {};
output list<Integer> never = {};
output list<Integer> alwaysByUser = always "distinguish betwween user choice and compiler choice";
output list<Integer> alwaysByUser = always "distinguish between user choice and compiler choice";
protected
BackendDAE.Var var;
Integer index = 1;
Expand All @@ -2290,7 +2290,7 @@ protected
algorithm
preferTVarsWithStartValue := Flags.getConfigBool(Flags.PREFER_TVARS_WITH_START_VALUE) and (DAEtypeStr == "initialization");
for var in var_lstIn loop
// Get the value of the variable's tearingSelect attribute.
// Get the value of the variable's __OpenModelica_tearingSelect attribute.
BackendDAE.VAR(tearingSelectOption = ts) := var;

// Add the variable's index to the appropriate list.
Expand Down Expand Up @@ -2324,7 +2324,7 @@ algorithm
end for;

if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nExternal influence on selection of iteration variables by variable annotations (tearingSelect)" + (if preferTVarsWithStartValue then " and preference of variables with start attribute" else "") + ":\n");
print("\nExternal influence on selection of iteration variables by variable annotations (__OpenModelica_tearingSelect)" + (if preferTVarsWithStartValue then " and preference of variables with start attribute" else "") + ":\n");
print("Always: " + stringDelimitList(List.map(always, intString), ",") + "\n");
print("Prefer: " + stringDelimitList(List.map(prefer, intString), ",")+ "\n");
print("Avoid: " + stringDelimitList(List.map(avoid, intString), ",")+ "\n");
Expand Down Expand Up @@ -2375,11 +2375,11 @@ algorithm

_ := match(var.tearingSelectOption)
case SOME(BackendDAE.ALWAYS()) algorithm
Error.addSourceMessage(Error.COMPILER_WARNING,{"Minimal Tearing is ignoring tearingSelect=always annotation for discrete variable: "
Error.addSourceMessage(Error.COMPILER_WARNING,{"Minimal Tearing is ignoring '__OpenModelica_tearingSelect = always' annotation for discrete variable: "
+ BackendDump.varString(var)},ElementSource.getInfo(var.source));
then ();
case SOME(BackendDAE.PREFER()) algorithm
Error.addSourceMessage(Error.COMPILER_WARNING,{"Minimal Tearing is ignoring tearingSelect=prefer annotation for discrete variable: "
Error.addSourceMessage(Error.COMPILER_WARNING,{"Minimal Tearing is ignoring '__OpenModelica_tearingSelect = prefer' annotation for discrete variable: "
+ BackendDump.varString(var)},ElementSource.getInfo(var.source));
then ();
else ();
Expand Down Expand Up @@ -2476,7 +2476,7 @@ algorithm
list<Integer> tvars,unsolvables,tVar_never,tVar_discrete,order;
Boolean causal;
// case: There are no unsolvables and no variables with annotation 'tearingSelect = always'
// case: There are no unsolvables and no variables with annotation '__OpenModelica_tearingSelect = always'
case ({},{})
equation
// select tearing Var
Expand Down Expand Up @@ -2538,15 +2538,15 @@ algorithm
then
(tvars,order);
// case: There are unsolvables and/or variables with annotation 'tearingSelect = always'
// case: There are unsolvables and/or variables with annotation '__OpenModelica_tearingSelect = always'
else
equation
// First choose unsolvables and 'always'-vars as tVars
tvars = List.unique(listAppend(Unsolvables,tSel_always));
tVar_never = List.intersectionOnTrue(tSel_never,tvars,intEq);
tVar_discrete = List.intersectionOnTrue(discreteVars,tvars,intEq);
if not listEmpty(tVar_never) then
Error.addCompilerWarning("There are tearing variables with annotation attribute 'tearingSelect = never'. Use -d=tearingdump and -d=tearingdumpV for more information.");
Error.addCompilerWarning("There are tearing variables with annotation attribute '__OpenModelica_tearingSelect = never'. Use -d=tearingdump and -d=tearingdumpV for more information.");
end if;
if not listEmpty(tVar_discrete) then
Error.addCompilerWarning("There are discrete tearing variables because otherwise the system could not have been torn (unsolvables). This may lead to problems during simulation.");
Expand Down Expand Up @@ -2653,7 +2653,7 @@ algorithm
end try;
if listMember(OutTVar,tSel_avoid) then
Error.addCompilerWarning("The Tearing heuristic has chosen variables with annotation attribute 'tearingSelect = avoid'. Use -d=tearingdump and -d=tearingdumpV for more information.");
Error.addCompilerWarning("The Tearing heuristic has chosen variables with annotation attribute '__OpenModelica_tearingSelect = avoid'. Use -d=tearingdump and -d=tearingdumpV for more information.");
end if;
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
print("\nEND of TearingHeuristic\n" + BORDER + "\n\n");
Expand Down Expand Up @@ -3154,10 +3154,10 @@ algorithm
// 3. Remove variables we don't want as tearing variables
// ******************************************************
// Remove variables with attribute tearingSelect=never
// Remove variables with attribute '__OpenModelica_tearingSelect = never'
(_,potentialTVars,_) := List.intersection1OnTrue(potentialTVars,tSel_never,intEq);
if listEmpty(potentialTVars) then
Error.addCompilerError("It is not possible to select a new tearing variable, because all remaining variables have the attribute tearingSelect=never");
Error.addCompilerError("It is not possible to select a new tearing variable, because all remaining variables have the attribute '__OpenModelica_tearingSelect = never'.");
return;
end if;
Expand Down Expand Up @@ -3201,7 +3201,7 @@ algorithm
end if;
if debug then execStat("TEARINGHEURISTIC4_3"); end if;
// 4.4 Prefer variables with annotation attribute 'tearingSelect=prefer'
// 4.4 Prefer variables with annotation attribute '__OpenModelica_tearingSelect = prefer'
if not listEmpty(tSel_prefer) then
points := preferAvoidVariables(potentialTVars, points, tSel_prefer, 3.0);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
Expand All @@ -3210,7 +3210,7 @@ algorithm
end if;
if debug then execStat("TEARINGHEURISTIC4_4"); end if;
// 4.5 Avoid variables with annotation attribute 'tearingSelect=avoid'
// 4.5 Avoid variables with annotation attribute '__OpenModelica_tearingSelect = avoid'
if not listEmpty(tSel_avoid) then
points := preferAvoidVariables(potentialTVars, points, tSel_avoid, 0.334);
if Flags.isSet(Flags.TEARING_DUMPVERBOSE) then
Expand Down Expand Up @@ -3322,7 +3322,7 @@ end ModifiedCellierHeuristic_4;
protected function preferAvoidVariables
"multiplies points of variables with annotation attribute 'tearingSelect=prefer' or 'tearingSelect=avoid' with factor
"multiplies points of variables with annotation attribute '__OpenModelica_tearingSelect in [prefer, avoid]' with factor
author: ptaeuber FHB 2014-05"
input list<Integer> varsIn;
input output list<Integer> points;
Expand Down
93 changes: 61 additions & 32 deletions OMCompiler/Compiler/NFFrontEnd/NFBackendExtension.mo
Original file line number Diff line number Diff line change
Expand Up @@ -904,37 +904,29 @@ public
function stateSelectString
input Option<StateSelect> optStateSelect;
input output list<String> buffer;
protected
StateSelect stateSelect;
algorithm
if isSome(optStateSelect) then
SOME(stateSelect) := optStateSelect;
buffer := match stateSelect
case StateSelect.NEVER then "StateSelect = never" :: buffer;
case StateSelect.AVOID then "StateSelect = avoid" :: buffer;
case StateSelect.DEFAULT then "StateSelect = default" :: buffer;
case StateSelect.PREFER then "StateSelect = prefer" :: buffer;
case StateSelect.ALWAYS then "StateSelect = always" :: buffer;
end match;
end if;
buffer := match optStateSelect
case SOME(StateSelect.NEVER) then "StateSelect = never" :: buffer;
case SOME(StateSelect.AVOID) then "StateSelect = avoid" :: buffer;
case SOME(StateSelect.DEFAULT) then "StateSelect = default" :: buffer;
case SOME(StateSelect.PREFER) then "StateSelect = prefer" :: buffer;
case SOME(StateSelect.ALWAYS) then "StateSelect = always" :: buffer;
else buffer;
end match;
end stateSelectString;

function tearingSelectString
input Option<TearingSelect> optTearingSelect;
input output list<String> buffer;
protected
TearingSelect tearingSelect;
algorithm
if isSome(optTearingSelect) then
SOME(tearingSelect) := optTearingSelect;
buffer := match tearingSelect
case TearingSelect.NEVER then "TearingSelect = never" :: buffer;
case TearingSelect.AVOID then "TearingSelect = avoid" :: buffer;
case TearingSelect.DEFAULT then "TearingSelect = default" :: buffer;
case TearingSelect.PREFER then "TearingSelect = prefer" :: buffer;
case TearingSelect.ALWAYS then "TearingSelect = always" :: buffer;
end match;
end if;
buffer := match optTearingSelect
case SOME(TearingSelect.NEVER) then "TearingSelect = never" :: buffer;
case SOME(TearingSelect.AVOID) then "TearingSelect = avoid" :: buffer;
case SOME(TearingSelect.DEFAULT) then "TearingSelect = default" :: buffer;
case SOME(TearingSelect.PREFER) then "TearingSelect = prefer" :: buffer;
case SOME(TearingSelect.ALWAYS) then "TearingSelect = always" :: buffer;
else buffer;
end match;
end tearingSelectString;

function createReal
Expand Down Expand Up @@ -1201,7 +1193,7 @@ public
end getStateSelectName;

function createTearingSelect
"tearingSelect is an annotation and has to be extracted from the comment."
"__OpenModelica_tearingSelect is an annotation and has to be extracted from the comment."
input Option<SCode.Comment> optComment;
output Option<TearingSelect> tearingSelect;
protected
Expand All @@ -1211,7 +1203,12 @@ public
algorithm
try
SOME(SCode.COMMENT(annotation_=SOME(anno))) := optComment;
SOME(val) := SCodeUtil.lookupAnnotationBinding(anno, "tearingSelect");
try
SOME(val) := SCodeUtil.lookupAnnotationBinding(anno, "__OpenModelica_tearingSelect");
else
SOME(val) := SCodeUtil.lookupAnnotationBinding(anno, "tearingSelect");
Error.addCompilerWarning("Deprecated vendor annotation 'tearingSelect' found. Use '__OpenModelica_tearingSelect' instead.");
end try;
name := AbsynUtil.crefIdent(AbsynUtil.expCref(val));
tearingSelect := SOME(lookupTearingSelectMember(name));
else
Expand Down Expand Up @@ -1242,11 +1239,11 @@ public
output StateSelect stateSelect;
algorithm
stateSelect := match name
case "never" then TearingSelect.NEVER;
case "avoid" then TearingSelect.AVOID;
case "default" then TearingSelect.DEFAULT;
case "prefer" then TearingSelect.PREFER;
case "always" then TearingSelect.ALWAYS;
case "never" then TearingSelect.NEVER;
case "avoid" then TearingSelect.AVOID;
case "default" then TearingSelect.DEFAULT;
case "prefer" then TearingSelect.PREFER;
case "always" then TearingSelect.ALWAYS;
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown TearingSelect literal " + name, sourceInfo());
Expand Down Expand Up @@ -1275,5 +1272,37 @@ public
end DISTRIBUTION;
end Distribution;

annotation(__OpenModelica_Interface="frontend");
uniontype Annotations
record ANNOTATIONS
"all annotations that are vendor specific
note: doesn't include __OpenModelica_tearingSelect, this is considered a first class attribute"
Boolean hideResult;
end ANNOTATIONS;

function create
input Option<SCode.Comment> comment;
output Annotations annotations = EMPTY_ANNOTATIONS;
protected
SCode.Mod mod;
algorithm
_ := match comment
case SOME(SCode.COMMENT(annotation_=SOME(SCode.ANNOTATION(modification=mod as SCode.MOD())))) algorithm
for submod in mod.subModLst loop
_ := match submod
case SCode.NAMEMOD(ident = "HideResult", mod = SCode.MOD(binding = SOME(Absyn.BOOL(true)))) algorithm
annotations.hideResult := true;
then ();
else ();
end match;
end for;
then ();
else ();
end match;
end create;
end Annotations;

constant Annotations EMPTY_ANNOTATIONS = ANNOTATIONS(false);

annotation(__OpenModelica_Interface="frontend");

end NFBackendExtension;

0 comments on commit fc3c2d4

Please sign in to comment.