Changeset a79dcbb in OpenModelica


Ignore:
Timestamp:
2024-06-10T14:31:33+02:00 (6 weeks ago)
Author:
kabdelhak <karim.abdelhak@…>
Parents:
9429d1f0
git-author:
kabdelhak <karim.abdelhak@…> (06/10/24 14:29:31)
git-committer:
kabdelhak <karim.abdelhak@…> (06/10/24 14:31:33)
Message:

[NB] correctly handle tuple assignments in when

Location:
OMCompiler/Compiler/NBackEnd
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • OMCompiler/Compiler/NBackEnd/Classes/NBEquation.mo

    rc58c7d4 ra79dcbb  
    26232623      output list<WhenEquationBody> bodies = {};
    26242624    protected
    2625       UnorderedSet<ComponentRef> discr_map = UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual);
    2626       UnorderedSet<ComponentRef> state_map = UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual);
     2625      UnorderedMap<ComponentRef, CrefSet> discr_map = UnorderedMap.new<CrefSet>(ComponentRef.hash, ComponentRef.isEqual);
     2626      UnorderedSet<ComponentRef> state_set = UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual);
     2627      UnorderedSet<ComponentRef> discr_marks = UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual);
    26272628      list<tuple<Expression, list<WhenStatement>>> flat_when;
    26282629      list<tuple<Expression, list<WhenStatement>>> flat_new;
    26292630      list<ComponentRef> discretes, states;
     2631      CrefSet set;
    26302632      Expression condition, acc_condition = Expression.EMPTY(Type.INTEGER());
    2631       list<WhenStatement> stmts;
     2633      list<WhenStatement> stmts, assigns;
    26322634      Option<WhenStatement> stmt;
    26332635      Option<WhenEquationBody> new_body;
     
    26352637      // collect all discretes and states contained in the when equation body
    26362638      // and also flatten the when equation to a list
    2637       flat_when := collectForSplit(SOME(body), discr_map, state_map);
    2638       discretes := UnorderedSet.toList(discr_map);
    2639       states    := UnorderedSet.toList(state_map);
     2639      flat_when := collectForSplit(SOME(body), discr_map, state_set);
     2640      discretes := UnorderedMap.keyList(discr_map);
     2641      states    := UnorderedSet.toList(state_set);
    26402642
    26412643      // create a when equation for each discrete state
    26422644      for disc in discretes loop
    2643         flat_new := {};
    2644         for tpl in flat_when loop
    2645           (condition, stmts) := tpl;
    2646           // get first assignment - each branch should only have one
    2647           // assignment per discrete state
    2648           stmt := getFirstAssignment(disc, stmts);
    2649           // if there is a statement: create the when body and combine with previous
    2650           // conditions. if there is no statement in this branch, save the condition
    2651           // negated for the next branch
    2652           if Util.isSome(stmt) then
    2653             condition := combineConditions(acc_condition, condition, false);
    2654             acc_condition := Expression.EMPTY(Type.INTEGER());
    2655             flat_new := (condition, {Util.getOption(stmt)}) :: flat_new;
     2645        if not UnorderedSet.contains(disc, discr_marks) then
     2646          set := UnorderedMap.getSafe(disc, discr_map, sourceInfo());
     2647          for marked in UnorderedSet.toList(set) loop
     2648            UnorderedSet.add(marked, discr_marks);
     2649          end for;
     2650          flat_new := {};
     2651          for tpl in flat_when loop
     2652            (condition, stmts) := tpl;
     2653            assigns := getAssignments(set, stmts);
     2654            // if there is a statement: create the when body and combine with previous
     2655            // conditions. if there is no statement in this branch, save the condition
     2656            // negated for the next branch
     2657            if not listEmpty(assigns) then
     2658              condition := combineConditions(acc_condition, condition, false);
     2659              acc_condition := Expression.EMPTY(Type.INTEGER());
     2660              flat_new := (condition, assigns) :: flat_new;
     2661            else
     2662              acc_condition := combineConditions(acc_condition, condition, true);
     2663            end if;
     2664          end for;
     2665          // create body from flat list and add to new bodies
     2666          new_body := fromFlatList(flat_new);
     2667          if Util.isSome(new_body) then
     2668            bodies := Util.getOption(new_body) :: bodies;
    26562669          else
    2657             acc_condition := combineConditions(acc_condition, condition, true);
     2670            Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName()
     2671              + " failed because when partition for: " + ComponentRef.toString(disc)
     2672              + " could not be recovered."});
    26582673          end if;
    2659         end for;
    2660         // create body from flat list and add to new bodies
    2661         new_body := fromFlatList(flat_new);
    2662         if Util.isSome(new_body) then
    2663           bodies := Util.getOption(new_body) :: bodies;
    2664         else
    2665           Error.addMessage(Error.INTERNAL_ERROR,{getInstanceName()
    2666             + " failed because when partition for: " + ComponentRef.toString(disc)
    2667             + " could not be recovered."});
    26682674        end if;
    26692675      end for;
     
    27592765
    27602766  protected
     2767    type CrefSet = UnorderedSet<ComponentRef>;
    27612768    function collectForSplit
    27622769      "collects all discrete states and regular states for splitting up
    27632770      of a when equation. also flattens it to a list"
    27642771      input Option<WhenEquationBody> body_opt;
    2765       input UnorderedSet<ComponentRef> discr_map;
    2766       input UnorderedSet<ComponentRef> state_map;
     2772      input UnorderedMap<ComponentRef, CrefSet> discr_map;
     2773      input UnorderedSet<ComponentRef> state_set;
    27672774      output list<tuple<Expression, list<WhenStatement>>> flat_when;
    27682775    protected
     
    27752782            local
    27762783              ComponentRef cref;
     2784              Expression tpl;
     2785
    27772786            case WhenStatement.ASSIGN(lhs = Expression.CREF(cref = cref)) algorithm
    2778               UnorderedSet.add(cref, discr_map);
     2787              addCrefsMap(discr_map, {cref});
     2788            then ();
     2789            case WhenStatement.ASSIGN(lhs = tpl as Expression.TUPLE()) algorithm
     2790              addCrefsMap(discr_map, UnorderedSet.toList(Expression.extractCrefs(tpl)));
    27792791            then ();
    27802792            case WhenStatement.REINIT(stateVar = cref) algorithm
    2781               UnorderedSet.add(cref, state_map);
     2793              UnorderedSet.add(cref, state_set);
    27822794            then ();
    27832795            case WhenStatement.ASSIGN() algorithm
     
    27882800          end match;
    27892801        end for;
    2790         flat_when := (body.condition, body.when_stmts) :: collectForSplit(body.else_when, discr_map, state_map);
     2802        flat_when := (body.condition, body.when_stmts) :: collectForSplit(body.else_when, discr_map, state_set);
    27912803      else
    27922804        flat_when := {};
     
    27942806    end collectForSplit;
    27952807
    2796     function getFirstAssignment
    2797       "returns the first assignment in the list that is solved for cref"
    2798       input ComponentRef cref;
     2808    function addCrefsMap
     2809      input UnorderedMap<ComponentRef, CrefSet> discr_map;
     2810      input list<ComponentRef> crefs;
     2811    protected
     2812      CrefSet set_new, set = UnorderedSet.new(ComponentRef.hash, ComponentRef.isEqual);
     2813    algorithm
     2814      for c in crefs loop
     2815        if UnorderedMap.contains(c, discr_map) then
     2816          set_new := UnorderedMap.getSafe(c, discr_map, sourceInfo());
     2817          if not referenceEq(set, set_new) then
     2818            set := UnorderedSet.union(set, set_new);
     2819          end if;
     2820        else
     2821          UnorderedSet.add(c, set);
     2822        end if;
     2823      end for;
     2824
     2825      for c in crefs loop
     2826        UnorderedMap.add(c, set, discr_map);
     2827      end for;
     2828    end addCrefsMap;
     2829
     2830    function getAssignments
     2831      "returns all assignments for the crefs in crefSet and merges if necessary"
     2832      input UnorderedSet<ComponentRef> crefSet;
    27992833      input list<WhenStatement> stmts;
    2800       output Option<WhenStatement> assign = NONE();
     2834      output list<WhenStatement> assigns = {};
    28012835    algorithm
    28022836      for stmt in stmts loop
    28032837        () := match stmt
    28042838          local
    2805             ComponentRef lhs;
    2806           case WhenStatement.ASSIGN(lhs = Expression.CREF(cref = lhs))
    2807           guard(ComponentRef.isEqual(cref, lhs)) algorithm
    2808             assign := SOME(stmt); break;
     2839            ComponentRef cref;
     2840            Expression tpl;
     2841
     2842          case WhenStatement.ASSIGN(lhs = Expression.CREF(cref = cref))
     2843          guard(UnorderedSet.contains(cref, crefSet)) algorithm
     2844            assigns := stmt :: assigns;
    28092845          then ();
     2846
     2847          case WhenStatement.ASSIGN(lhs = tpl as Expression.TUPLE())
     2848          guard(List.any(list(UnorderedSet.contains(c, crefSet) for c in UnorderedSet.toList(Expression.extractCrefs(tpl))), Util.id)) algorithm
     2849            assigns := stmt :: assigns;
     2850          then ();
     2851
    28102852          else ();
    28112853        end match;
    28122854      end for;
    2813     end getFirstAssignment;
     2855    end getAssignments;
    28142856
    28152857    function getFirstReinit
  • OMCompiler/Compiler/NBackEnd/Util/NBSlice.mo

    rbeb22ab ra79dcbb  
    12021202      d                   := UnorderedMap.getSafe(cref, dep, sourceInfo());
    12031203      (start, _)          := mapping.eqn_AtS[eqn_arr_idx];
    1204       (skip_idx, skip_ty) := resolveSkips(start, ty, d.skips);
     1204      if not UnorderedSet.contains(cref, rep) then
     1205        (skip_idx, skip_ty) := resolveSkips(start, ty, d.skips);
     1206      else
     1207        (skip_idx, skip_ty) := (start, ty);
     1208      end if;
    12051209
    12061210      // get equation and iterator sizes and frames
Note: See TracChangeset for help on using the changeset viewer.