Skip to content

Commit

Permalink
[NF/NB] add and parse HideResult annotation (#11395)
Browse files Browse the repository at this point in the history
* [NF/NB] add and parse HideResult annotation

 - also add backbone for future annotations
 - set function alias variables as HideResult = true

* [testuite] update test
  • Loading branch information
kabdelhak committed Oct 18, 2023
1 parent e89eebd commit f1f3f79
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 deletions.
5 changes: 3 additions & 2 deletions OMCompiler/Compiler/NBackEnd/Classes/NBVariable.mo
Expand Up @@ -87,7 +87,7 @@ public
constant Variable TIME_VARIABLE = Variable.VARIABLE(NFBuiltin.TIME_CREF, Type.REAL(),
NFBinding.EMPTY_BINDING, NFPrefixes.Visibility.PUBLIC, NFAttributes.DEFAULT_ATTR,
{}, {}, NONE(), SCodeUtil.dummyInfo, BackendExtension.BACKEND_INFO(
VariableKind.TIME(), NFBackendExtension.EMPTY_VAR_ATTR_REAL));
VariableKind.TIME(), NFBackendExtension.EMPTY_VAR_ATTR_REAL, NFBackendExtension.EMPTY_ANNOTATIONS));

constant String DERIVATIVE_STR = "$DER";
constant String DUMMY_DERIVATIVE_STR = "$dDER";
Expand Down Expand Up @@ -1017,8 +1017,9 @@ public
node := InstNode.VAR_NODE(name + "_" + intString(uniqueIndex), Pointer.create(DUMMY_VARIABLE));
cref := ComponentRef.CREF(node, {}, ty, NFComponentRef.Origin.CREF, ComponentRef.EMPTY());
var := fromCref(cref);
// update the variable kind
// update the variable kind and set hideResult = true
var.backendinfo := BackendExtension.BackendInfo.setVarKind(var.backendinfo, if makeParam then VariableKind.PARAMETER() else VariableKind.fromType(ty));
var.backendinfo := BackendExtension.BackendInfo.setHideResult(var.backendinfo, true);

// create the new variable pointer and safe it to the component reference
(var_ptr, cref) := makeVarPtrCyclic(var, cref);
Expand Down
8 changes: 5 additions & 3 deletions OMCompiler/Compiler/NBackEnd/Classes/NBackendDAE.mo
Expand Up @@ -518,19 +518,21 @@ protected
input Variable var;
output Pointer<Variable> var_ptr;
protected
BackendExtension.VariableAttributes attributes;
BackendExtension.VariableKind varKind;
BackendExtension.VariableAttributes attributes;
BackendExtension.Annotations annotations;
algorithm
// ToDo! extract tearing select option
try
attributes := BackendExtension.VariableAttributes.create(var.typeAttributes, var.ty, var.attributes, var.children, var.comment);
annotations := BackendExtension.Annotations.create(var.comment);

// only change varKind if unset (Iterators are set before)
var.backendinfo := match var.backendinfo
case BackendExtension.BACKEND_INFO(varKind = BackendExtension.FRONTEND_DUMMY()) algorithm
(varKind, attributes) := lowerVariableKind(Variable.variability(var), attributes, var.ty);
then BackendExtension.BACKEND_INFO(varKind, attributes);
else BackendExtension.BackendInfo.setAttributes(var.backendinfo, attributes);
then BackendExtension.BACKEND_INFO(varKind, attributes, annotations);
else BackendExtension.BackendInfo.setAttributes(var.backendinfo, attributes, annotations);
end match;

// Remove old type attribute information since it has been converted.
Expand Down
69 changes: 61 additions & 8 deletions OMCompiler/Compiler/NFFrontEnd/NFBackendExtension.mo
Expand Up @@ -68,6 +68,7 @@ public
record BACKEND_INFO
VariableKind varKind "Structural kind: state, algebraic...";
VariableAttributes attributes "values on built-in attributes";
Annotations annotations "values on annotations (vendor specific)";
end BACKEND_INFO;

function toString
Expand Down Expand Up @@ -103,10 +104,27 @@ public
function setAttributes
input output BackendInfo binfo;
input VariableAttributes attributes;
input Annotations annotations;
algorithm
binfo.attributes := attributes;
binfo.annotations := annotations;
end setAttributes;

function setHideResult
input output BackendInfo binfo;
input Boolean hideResult;
algorithm
binfo := match binfo
local
Annotations anno;
case BackendInfo.BACKEND_INFO(annotations = anno as ANNOTATIONS()) algorithm
anno.hideResult := hideResult;
binfo.annotations := anno;
then binfo;
else binfo;
end match;
end setHideResult;

function scalarize
input BackendInfo binfo;
input Integer length;
Expand All @@ -118,12 +136,12 @@ public
case VariableKind.FRONTEND_DUMMY() then List.fill(binfo, length);
else algorithm
scalar_attributes := VariableAttributes.scalarize(binfo.attributes, length);
then list(BACKEND_INFO(binfo.varKind, attr) for attr in scalar_attributes);
then list(BACKEND_INFO(binfo.varKind, attr, binfo.annotations) for attr in scalar_attributes);
end match;
end scalarize;
end BackendInfo;

constant BackendInfo DUMMY_BACKEND_INFO = BACKEND_INFO(FRONTEND_DUMMY(), EMPTY_VAR_ATTR_REAL);
constant BackendInfo DUMMY_BACKEND_INFO = BACKEND_INFO(FRONTEND_DUMMY(), EMPTY_VAR_ATTR_REAL, EMPTY_ANNOTATIONS);

uniontype VariableKind
record TIME end TIME;
Expand Down Expand Up @@ -1224,11 +1242,11 @@ public
output StateSelect stateSelect;
algorithm
stateSelect := match name
case "never" then StateSelect.NEVER;
case "avoid" then StateSelect.AVOID;
case "default" then StateSelect.DEFAULT;
case "prefer" then StateSelect.PREFER;
case "always" then StateSelect.ALWAYS;
case "never" then StateSelect.NEVER;
case "avoid" then StateSelect.AVOID;
case "default" then StateSelect.DEFAULT;
case "prefer" then StateSelect.PREFER;
case "always" then StateSelect.ALWAYS;
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown StateSelect literal " + name, sourceInfo());
Expand Down Expand Up @@ -1275,5 +1293,40 @@ public
end DISTRIBUTION;
end Distribution;

annotation(__OpenModelica_Interface="frontend");
uniontype Annotations
record ANNOTATIONS
"all annotations that are vendor specific
note: doesn't inculde 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 ();
// do not create a notification for TearingSelect since its parsed in attributes
case SCode.NAMEMOD(ident = "TearingSelect") then ();
else algorithm
Error.addCompilerNotification("Could not parse annotation \"" + submod.ident + "\" in the backend.");
then ();
end match;
end for;
then ();
else ();
end match;
end create;
end Annotations;

constant Annotations EMPTY_ANNOTATIONS = ANNOTATIONS(false);

annotation(__OpenModelica_Interface="frontend");
end NFBackendExtension;
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFScalarize.mo
Expand Up @@ -233,7 +233,7 @@ algorithm
(name, index) := tpl;
elem_var := var;
elem_var.name := ComponentRef.prepend(elem_var.name, ComponentRef.rename(name, elem_var.name));
elem_var.backendinfo := BackendInfo.setAttributes(elem_var.backendinfo, attr.childrenAttr[index]);
elem_var.backendinfo := BackendInfo.setAttributes(elem_var.backendinfo, attr.childrenAttr[index], var.backendinfo.annotations);
// update the types accordingly
elem_var.ty := VariableAttributes.elemType(attr.childrenAttr[index]);
elem_var.name := ComponentRef.setNodeType(elem_var.ty, elem_var.name);
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NFFrontEnd/NFVariable.mo
Expand Up @@ -91,7 +91,7 @@ public
// conversion to backend process (except for iterators). NBackendDAE.lower
if ComponentRef.isIterator(cref) then
binding := NFBinding.EMPTY_BINDING;
variable := VARIABLE(cref, ty, binding, vis, attr, {}, {}, cmt, info, BackendExtension.BACKEND_INFO(BackendExtension.ITERATOR(), NFBackendExtension.EMPTY_VAR_ATTR_REAL));
variable := VARIABLE(cref, ty, binding, vis, attr, {}, {}, cmt, info, BackendExtension.BACKEND_INFO(BackendExtension.ITERATOR(), NFBackendExtension.EMPTY_VAR_ATTR_REAL, NFBackendExtension.EMPTY_ANNOTATIONS));
else
binding := Component.getImplicitBinding(comp);
variable := VARIABLE(cref, ty, binding, vis, attr, {}, {}, cmt, info, NFBackendExtension.DUMMY_BACKEND_INFO);
Expand Down
2 changes: 1 addition & 1 deletion OMCompiler/Compiler/NSimCode/NSimVar.mo
Expand Up @@ -183,7 +183,7 @@ public
numArrayElement = {},
isValueChangeable = isValueChangeable,
isProtected = isProtected,
hideResult = false,
hideResult = var.backendinfo.annotations.hideResult,
inputIndex = NONE(),
matrixName = NONE(),
variability = NONE(),
Expand Down
Expand Up @@ -168,13 +168,13 @@ simulate(implicitEquation); getErrorString();
// algVars (2)
// ----------------------
// index:0: x (no alias) initial: no arrCref index:(1) []
// index:1: $FUN_1 (no alias) initial: no arrCref index:(2) []
// index:1: $FUN_1 (no alias) hideResult initial: no arrCref index:(2) []
// functions:
// -----------
//
// record SimulationResult
// resultFile = "implicitEquation_res.mat",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-06, method = 'dassl', fileNamePrefix = 'implicitEquation', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// simulationOptions = "startTime = 0.0, stopTime = 1.0, numberOfIntervals = 500, tolerance = 1e-6, method = 'dassl', fileNamePrefix = 'implicitEquation', options = '', outputFormat = 'mat', variableFilter = '.*', cflags = '', simflags = ''",
// messages = "LOG_SUCCESS | info | The initialization finished successfully without homotopy method.
// LOG_SUCCESS | info | The simulation finished successfully.
// "
Expand Down

0 comments on commit f1f3f79

Please sign in to comment.