Skip to content

Commit

Permalink
Memory optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
sjoelund committed Apr 16, 2016
1 parent 2aca7e4 commit d6544d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 53 deletions.
11 changes: 6 additions & 5 deletions Compiler/FrontEnd/Inst.mo
Expand Up @@ -3226,7 +3226,7 @@ algorithm
SCode.Element cls, comp, comp2, el;
SCode.Final final_prefix;
SCode.ConnectorType ct;
SCode.Mod m;
SCode.Mod m,oldmod;
SCode.Prefixes prefixes;
SCode.Variability vt;
SCode.Visibility vis;
Expand Down Expand Up @@ -3294,6 +3294,7 @@ algorithm
m = InstUtil.traverseModAddFinal(m);
end if;
comp = if referenceEq(el.modifications, m) then el else SCode.COMPONENT(name, prefixes, attr, ts, m, comment, cond, info);
oldmod = m;

// Fails if multiple decls not identical
already_declared = InstUtil.checkMultiplyDeclared(cache, env, mods, pre, ci_state, (comp, cmod), inst_dims, impl);
Expand All @@ -3304,8 +3305,8 @@ algorithm
// update the component modification to redeclare X = Y
m = InstUtil.chainRedeclares(mods, m);
m = SCodeUtil.expandEnumerationMod(m);
m = InstUtil.traverseModAddDims(cache, env, pre, m, inst_dims, ad);
comp = SCode.COMPONENT(name, prefixes, attr, ts, m, comment, cond, info);
m = InstUtil.traverseModAddDims(cache, env, pre, m, inst_dims);
comp = if referenceEq(oldmod,m) then comp else SCode.COMPONENT(name, prefixes, attr, ts, m, comment, cond, info);
ci_state = ClassInf.trans(ci_state, ClassInf.FOUND_COMPONENT(name));
cref = ComponentReference.makeCrefIdent(name, DAE.T_UNKNOWN_DEFAULT, {});
(cache,_) = PrefixUtil.prefixCref(cache, env, ih, pre, cref); /*mahge: todo: remove me*/
Expand Down Expand Up @@ -3435,8 +3436,8 @@ algorithm
else
isInSM = false;
end if;
else
isInSM = false;
else
isInSM = false;
end if;

(cache, comp_env, ih, store, dae, csets, ty, graph_new) = InstVar.instVar(cache,
Expand Down
74 changes: 30 additions & 44 deletions Compiler/FrontEnd/InstUtil.mo
Expand Up @@ -2500,6 +2500,8 @@ public function chainRedeclares "
input DAE.Mod inModOuter "the outer mod which should overwrite the inner mod";
input SCode.Mod inModInner "the inner mod";
output SCode.Mod outMod;
protected
Boolean b;
algorithm
outMod := match(inModOuter,inModInner)
/*
Expand All @@ -2513,8 +2515,8 @@ algorithm
*/
case(_, _)
equation
outMod = chainRedeclare_dispatch(inModOuter,inModInner);
then outMod;
(outMod,b) = chainRedeclare_dispatch(inModOuter,inModInner);
then if b then outMod else inModInner;
end match;
end chainRedeclares;

Expand All @@ -2525,8 +2527,9 @@ public function chainRedeclare_dispatch "
input DAE.Mod inModOuter "the outer mod which should overwrite the inner mod";
input SCode.Mod inModInner "the inner mod";
output SCode.Mod outMod;
output Boolean change;
algorithm
outMod := matchcontinue (inModOuter,inModInner)
(outMod,change) := matchcontinue (inModOuter,inModInner)
local
SCode.Final f;
SCode.Each e;
Expand All @@ -2542,36 +2545,32 @@ algorithm
case (_,SCode.REDECL(f, e, SCode.CLASS(name = nInner, classDef = SCode.DERIVED(typeSpec = Absyn.TPATH(path = Absyn.IDENT(nDerivedInner))))))
equation
// lookup the class mod in the outer
DAE.REDECL(element = cls) = Mod.lookupModificationP(inModOuter, Absyn.IDENT(nDerivedInner));
DAE.REDECL(element = cls) = Mod.lookupCompModification(inModOuter, nDerivedInner);
cls = SCode.setClassName(nInner, cls);
then
SCode.REDECL(f, e, cls);
then (SCode.REDECL(f, e, cls),true);

// outer B(redeclare X = Y), inner B(redeclare X = Z) -> B(redeclare X = Z)
case (_,SCode.REDECL(f, e, SCode.CLASS(name = nInner, classDef = SCode.DERIVED(typeSpec = Absyn.TPATH(path = Absyn.IDENT(_))))))
equation
// lookup the class mod in the outer
DAE.REDECL(element = cls) = Mod.lookupModificationP(inModOuter, Absyn.IDENT(nInner));
then
SCode.REDECL(f, e, cls);
DAE.REDECL(element = cls) = Mod.lookupCompModification(inModOuter, nInner);
then (SCode.REDECL(f, e, cls),true);

// a mod with a name mod
case (_, SCode.MOD(f, e, SCode.NAMEMOD(name, m as SCode.REDECL())::rest, b, info))
equation
// lookup the class mod in the outer
m2 = chainRedeclare_dispatch(inModOuter, m);
SCode.MOD(subModLst = subs) = chainRedeclare_dispatch(inModOuter, SCode.MOD(f, e, rest, b, info));
then
SCode.MOD(f, e, SCode.NAMEMOD(name, m2)::subs, b, info);
(SCode.MOD(subModLst = subs),_) = chainRedeclare_dispatch(inModOuter, SCode.MOD(f, e, rest, b, info));
then (SCode.MOD(f, e, SCode.NAMEMOD(name, m2)::subs, b, info), true);

// something else, move along!
case (_, SCode.MOD(f, e, sm::rest, b, info))
equation
SCode.MOD(subModLst = subs) = chainRedeclare_dispatch(inModOuter, SCode.MOD(f, e, rest, b, info));
then
SCode.MOD(f, e, sm::subs, b, info);
(SCode.MOD(subModLst = subs), change) = chainRedeclare_dispatch(inModOuter, SCode.MOD(f, e, rest, b, info));
then (SCode.MOD(f, e, sm::subs, b, info), change);

else inModInner;
else (inModInner, false);

end matchcontinue;
end chainRedeclare_dispatch;
Expand Down Expand Up @@ -6711,10 +6710,9 @@ public function traverseModAddDims
input Prefix.Prefix inPrefix;
input SCode.Mod inMod;
input list<list<DAE.Dimension>> inInstDims;
input list<Absyn.Subscript> inDecDims;
output SCode.Mod outMod;
algorithm
outMod := matchcontinue(inCache,inEnv,inPrefix,inMod,inInstDims,inDecDims)
outMod := matchcontinue(inCache,inEnv,inPrefix,inMod,inInstDims)
local
FCore.Cache cache;
FCore.Graph env;
Expand All @@ -6726,27 +6724,17 @@ algorithm
list<list<Absyn.Exp>> aexps;
list<Option<Absyn.Exp>> adims;

case (_,_,_,mod,_,_) //If arrays are expanded, no action is needed
case (_,_,_,mod,_) //If arrays are expanded, no action is needed
equation
true = Config.splitArrays();
then
mod;
/* case (_,_,_,mod,inst_dims,decDims)
equation
subs = List.flatten(inst_dims);
exps = List.map(subs,Expression.subscriptNonExpandedExp);
aexps = List.map(exps, Expression.unelabExp);
adims = List.map(decDims, Absyn.subscriptExpOpt);
mod2 = traverseModAddDims2(mod, aexps, adims, true);
then
mod2;*/
case (cache,env,pre,mod,inst_dims,decDims)
case (_,_,_,_,{}) then inMod;
case (cache,env,pre,mod,inst_dims)
equation
exps = List.map1(inst_dims,Expression.dimensionsToExps,{});
aexps = List.mapList(exps, Expression.unelabExp);
adims = List.map(decDims, Absyn.subscriptExpOpt);
mod2 = traverseModAddDims4(cache,env,pre,mod, aexps, adims, true);
mod2 = traverseModAddDims4(cache,env,pre,mod, aexps, true);

then
mod2;
Expand All @@ -6760,11 +6748,10 @@ protected function traverseModAddDims4
input Prefix.Prefix inPrefix;
input SCode.Mod inMod;
input list<list<Absyn.Exp>> inExps;
input list<Option<Absyn.Exp>> inExpOpts;
input Boolean inIsTop;
output SCode.Mod outMod;
algorithm
outMod := match(inCache,inEnv,inPrefix,inMod,inExps,inExpOpts,inIsTop)
outMod := match(inCache,inEnv,inPrefix,inMod,inExps,inIsTop)
local
FCore.Cache cache;
FCore.Graph env;
Expand All @@ -6777,11 +6764,11 @@ algorithm
list<Option<Absyn.Exp>> expOpts;
SourceInfo info;

case (_,_,_,SCode.NOMOD(),_,_,_) then SCode.NOMOD();
case (_,_,_,mod as SCode.REDECL(),_,_,_) then mod; // Though redeclarations may need some processing as well
case (cache,env,pre,SCode.MOD(f, SCode.NOT_EACH(),submods, binding, info),exps,expOpts,_)
case (_,_,_,SCode.NOMOD(),_,_) then inMod;
case (_,_,_,SCode.REDECL(),_,_) then inMod; // Though redeclarations may need some processing as well
case (cache,env,pre,SCode.MOD(f, SCode.NOT_EACH(),submods, binding, info),exps,_)
equation
submods2 = traverseModAddDims5(cache,env,pre,submods,exps,expOpts);
submods2 = traverseModAddDims5(cache,env,pre,submods,exps);
binding = insertSubsInBinding(binding, exps);
then
SCode.MOD(f, SCode.NOT_EACH(),submods2, binding, info);
Expand All @@ -6795,22 +6782,21 @@ protected function traverseModAddDims5
input Prefix.Prefix inPrefix;
input list<SCode.SubMod> inMods;
input list<list<Absyn.Exp>> inExps;
input list<Option<Absyn.Exp>> inExpOpts;
output list<SCode.SubMod> outMods;
algorithm
outMods := match(inCache,inEnv,inPrefix,inMods,inExps,inExpOpts)
outMods := match(inCache,inEnv,inPrefix,inMods,inExps)
local
FCore.Cache cache;
FCore.Graph env;
Prefix.Prefix pre;
SCode.Mod mod,mod2;
list<SCode.SubMod> smods,smods2;
Ident n;
case (_,_,_,{},_,_) then {};
case (cache,env,pre,SCode.NAMEMOD(n,mod)::smods,_,_)
case (_,_,_,{},_) then {};
case (cache,env,pre,SCode.NAMEMOD(n,mod)::smods,_)
equation
mod2 = traverseModAddDims4(cache,env,pre,mod,inExps,inExpOpts,false);
smods2 = traverseModAddDims5(cache,env,pre,smods,inExps,inExpOpts);
mod2 = traverseModAddDims4(cache,env,pre,mod,inExps,false);
smods2 = traverseModAddDims5(cache,env,pre,smods,inExps);
then
SCode.NAMEMOD(n,mod2)::smods2;
end match;
Expand Down
8 changes: 4 additions & 4 deletions Compiler/FrontEnd/Lookup.mo
Expand Up @@ -2366,7 +2366,7 @@ algorithm
// adrpo: this was wrong, you won't find any id modification there!!!
// bjozac: This was right, you will find id modification unless modifers does not belong to component!
// adrpo 2009-11-23 -> solved by selecting the full modifier if the component modifier is empty!
compMod = Mod.lookupModificationP(mod_1,Absyn.IDENT(id));
compMod = Mod.lookupCompModification(mod_1,id);
fullMod = mod_1;
selectedMod = selectModifier(compMod, fullMod); // if the first one is empty use the other one.
(cache,cmod) = Mod.updateMod(cache,env,InnerOuter.emptyInstHierarchy,Prefix.NOPRE(),cmod,true,info);
Expand Down Expand Up @@ -2394,7 +2394,7 @@ algorithm
// adrpo: this was wrong, you won't find any id modification there!!!
// bjozac: This was right, you will find id modification unless modifers does not belong to component!
// adrpo 2009-11-23 -> solved by selecting the full modifier if the component modifier is empty!
compMod = Mod.lookupModificationP(mod_1,Absyn.IDENT(id));
compMod = Mod.lookupCompModification(mod_1,id);
fullMod = mod_1;
selectedMod = selectModifier(compMod, fullMod); // if the first one is empty use the other one.
(cache,cmod) = Mod.updateMod(cache,env,InnerOuter.emptyInstHierarchy,Prefix.NOPRE(),cmod,true,info);
Expand All @@ -2421,7 +2421,7 @@ algorithm
// adrpo: this was wrong, you won't find any id modification there!!!
// bjozac: This was right, you will find id modification unless modifers does not belong to component!
// adrpo 2009-11-23 -> solved by selecting the full modifier if the component modifier is empty!
compMod = Mod.lookupModificationP(mod_1,Absyn.IDENT(id));
compMod = Mod.lookupCompModification(mod_1,id);
fullMod = mod_1;
selectedMod = selectModifier(compMod, fullMod); // if the first one is empty use the other one.
(cache,cmod) = Mod.updateMod(cache,env,InnerOuter.emptyInstHierarchy,Prefix.NOPRE(),cmod,true,info);
Expand All @@ -2448,7 +2448,7 @@ algorithm
// adrpo: this was wrong, you won't find any id modification there!!!
// bjozac: This was right, you will find id modification unless modifers does not belong to component!
// adrpo 2009-11-23 -> solved by selecting the full modifier if the component modifier is empty!
compMod = Mod.lookupModificationP(mod_1,Absyn.IDENT(id));
compMod = Mod.lookupCompModification(mod_1,id);
fullMod = mod_1;
selectedMod = selectModifier(compMod, fullMod); // if the first one is empty use the other one.
(cache,cmod) = Mod.updateMod(cache,env,InnerOuter.emptyInstHierarchy,Prefix.NOPRE(),cmod,true,info);
Expand Down

0 comments on commit d6544d1

Please sign in to comment.