Changeset 961c6386 in OpenModelica


Ignore:
Timestamp:
2024-06-14T12:22:30+02:00 (4 weeks ago)
Author:
kabdelhak <karim.abdelhak@…>
Parents:
0051418c
git-author:
kabdelhak <karim.abdelhak@…> (06/14/24 11:00:48)
git-committer:
kabdelhak <karim.abdelhak@…> (06/14/24 12:22:30)
Message:

[NB] update record function handling

  • [NF] correctly identify all non default constructor functions
  • inline record constructors after inlining functions in replacements
  • add tuples to the check for inlining record elemetns
Location:
OMCompiler/Compiler
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Compiler/NBackEnd/Modules/2_Pre/NBInline.mo

    r2ab36e87 r961c6386  
    186186    UnorderedMap<Absyn.Path, Function> replacements "rules for replacements are stored inside here";
    187187    UnorderedSet<VariablePointer> set "new iterators from function bodies";
     188    VariablePointers variables = VarData.getVariables(varData);
    188189  algorithm
    189190    // collect functions
     
    192193
    193194    // apply replacements
    194     eqData  := Replacements.replaceFunctions(eqData, replacements);
     195    eqData  := Replacements.replaceFunctions(eqData, variables, replacements);
    195196
    196197    // replace record constucters after functions because record operator
    197198    // functions will produce record constructors once inlined
    198     eqData  := inlineRecordsTuples(eqData, VarData.getVariables(varData));
     199    eqData  := inlineRecordsTuples(eqData, variables);
    199200
    200201    // collect new iterators from replaced function bodies
    201202    set     := UnorderedSet.new(BVariable.hash, BVariable.equalName);
    202     eqData  := EqData.map(eqData, function BackendDAE.lowerEquationIterators(variables = VarData.getVariables(varData), set = set));
     203    eqData  := EqData.map(eqData, function BackendDAE.lowerEquationIterators(variables = variables, set = set));
    203204    varData := VarData.addTypedList(varData, UnorderedSet.toList(set), NBVariable.VarData.VarType.ITERATOR);
    204     eqData  := EqData.mapExp(eqData, function BackendDAE.lowerComponentReferenceExp(variables = VarData.getVariables(varData)));
     205    eqData  := EqData.mapExp(eqData, function BackendDAE.lowerComponentReferenceExp(variables = variables));
    205206  end inline;
    206207
     
    314315    tmp_eqns := Pointer.access(record_eqns);
    315316    for i in 1:recordSize loop
    316       new_lhs := Expression.nthRecordElement(i, lhs);
    317       new_rhs := Expression.nthRecordElement(i, rhs);
    318 
    319       // lower indexed record constructor elements
    320       new_lhs := Expression.map(new_lhs, inlineRecordConstructorElements);
    321       new_rhs := Expression.map(new_rhs, inlineRecordConstructorElements);
    322 
    323       // lower the new component references of record attributes
    324       new_lhs := Expression.map(new_lhs, function BackendDAE.lowerComponentReferenceExp(variables = variables));
    325       new_rhs := Expression.map(new_rhs, function BackendDAE.lowerComponentReferenceExp(variables = variables));
     317      new_lhs := inlineRecordExp(lhs, i, variables);
     318      new_rhs := inlineRecordExp(rhs, i, variables);
    326319
    327320      // create new equation
     
    344337  end inlineRecordEquationWork;
    345338
     339public
     340  function inlineRecordExp
     341    "inlines record constructors in a single expression"
     342    input output Expression exp;
     343    input Integer index;
     344    input VariablePointers variables;
     345  algorithm
     346    exp := Expression.nthRecordElement(index, exp);
     347    // lower indexed record constructor elements
     348    exp := Expression.map(exp, inlineRecordConstructorElements);
     349    // lower the new component references of record attributes
     350    exp := Expression.map(exp, function BackendDAE.lowerComponentReferenceExp(variables = variables));
     351  end inlineRecordExp;
     352
     353protected
    346354  function inlineRecordConstructorElements
    347355    "removes indexed constructor element calls
  • OMCompiler/Compiler/NBackEnd/Util/NBReplacements.mo

    r3dac23f r961c6386  
    6565  import BVariable = NBVariable;
    6666  import NBEquation.{EqData, Equation, EquationPointers};
     67  import Inline = NBInline;
    6768  import Solve = NBSolve;
    6869  import StrongComponent = NBStrongComponent;
     
    266267    if possible."
    267268    input output EqData eqData;
     269    input VariablePointers variables;
    268270    input UnorderedMap<Absyn.Path, Function> replacements;
    269271  algorithm
    270272        // do nothing if replacements are empty
    271273    if UnorderedMap.isEmpty(replacements) then return; end if;
    272     eqData := EqData.mapExp(eqData, function applyFuncExp(replacements = replacements));
     274    eqData := EqData.mapExp(eqData, function applyFuncExp(replacements = replacements, variables = variables));
    273275  end replaceFunctions;
    274276
     
    277279    input output Expression exp                               "Replacement happens inside this expression";
    278280    input UnorderedMap<Absyn.Path, Function> replacements     "rules for replacements are stored inside here";
     281    input VariablePointers variables;
    279282  algorithm
    280283    exp := match exp
     
    321324        body_exp := SimplifyExp.combineBinaries(body_exp);
    322325        body_exp := SimplifyExp.simplifyDump(body_exp, true, getInstanceName(), "\n");
     326        // inline possible record constructors
     327        //body_exp := Expression.map(body_exp, function applyFuncTupleExp(variables = variables));
     328
    323329
    324330        if Flags.isSet(Flags.DUMPBACKENDINLINE) then
     
    331337    end match;
    332338  end applyFuncExp;
     339
     340  function applyFuncTupleExp
     341    input output Expression exp;
     342    input VariablePointers variables;
     343  protected
     344    Type ty;
     345    Option<Integer> sz;
     346    list<Expression> inlined_record = {};
     347  algorithm
     348    ty := Expression.typeOf(exp);
     349    sz := Type.complexSize(ty);
     350
     351    // if the call returns a record constructor, it has to be inlined
     352    if Util.isSome(sz) then
     353      for i in Util.getOption(sz):-1:1 loop
     354        inlined_record := Inline.inlineRecordExp(exp, i, variables) :: inlined_record;
     355      end for;
     356      exp := Expression.TUPLE(ty, inlined_record);
     357    end if;
     358  end applyFuncTupleExp;
    333359
    334360  function addInputArgTpl
     
    360386
    361387        // if it is a basic record, take its elements
    362         case Expression.RECORD() then arg.elements;
     388        case Expression.RECORD()  then arg.elements;
     389        case Expression.TUPLE()   then arg.elements;
    363390
    364391        // if the argument is a record constructor, map it to its attributes
  • OMCompiler/Compiler/NFFrontEnd/NFFunction.mo

    re7e9366c r961c6386  
    19911991  function isNonDefaultRecordConstructor
    19921992    input Function fn;
    1993     output Boolean isConstructor;
    1994   algorithm
    1995     isConstructor := match fn.path
    1996       case Absyn.Path.QUALIFIED(path = Absyn.Path.QUALIFIED(name = "'constructor'")) then true;
    1997       else false;
    1998     end match;
     1993    output Boolean b = isNonDefaultRecordConstructorPath(fn.path);
     1994    function isNonDefaultRecordConstructorPath
     1995      input Absyn.Path path;
     1996      output Boolean b;
     1997    algorithm
     1998      b := match path
     1999        case Absyn.Path.QUALIFIED(name = "'constructor'") then true;
     2000        case Absyn.Path.QUALIFIED()                       then isNonDefaultRecordConstructorPath(path.path);
     2001        else false;
     2002      end match;
     2003    end isNonDefaultRecordConstructorPath;
    19992004  end isNonDefaultRecordConstructor;
    20002005
Note: See TracChangeset for help on using the changeset viewer.