Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
- Added initial support for sharing array constants (stored as pointers instead of being constructed at each reference)
  + Currently only done for called functions, but will be extended to simulation code


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9610 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Aug 8, 2011
1 parent 05abc5e commit bd7926d
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 14 deletions.
93 changes: 81 additions & 12 deletions Compiler/BackEnd/SimCode.mo
Expand Up @@ -1243,12 +1243,7 @@ algorithm
case (name, SOME(daeMainFunction), daeElements, metarecordTypes, includes)
equation
// Create FunctionCode
(daeElements,(_,(_,_,literals))) =
DAEUtil.traverseDAEFunctions(
daeMainFunction::daeElements,
Expression.traverseSubexpressionsHelper,
(replaceLiteralExp,(0,HashTableExpToIndex.emptyHashTableSized(BaseHashTable.bigBucketSize),{})));
literals = listReverse(literals);
(daeElements,literals) = findLiterals(daeMainFunction::daeElements);
(mainFunction::fns, extraRecordDecls, includes, includeDirs, libs) = elaborateFunctions(daeElements, metarecordTypes, literals, includes);
checkValidMainFunction(name, mainFunction);
makefileParams = createMakefileParams(includeDirs, libs);
Expand All @@ -1260,12 +1255,7 @@ algorithm
case (name, NONE(), daeElements, metarecordTypes, includes)
equation
// Create FunctionCode
(daeElements,(_,(_,_,literals))) =
DAEUtil.traverseDAEFunctions(
daeElements,
Expression.traverseSubexpressionsHelper,
(replaceLiteralExp,(0,HashTableExpToIndex.emptyHashTableSized(BaseHashTable.bigBucketSize),{})));
literals = listReverse(literals);
(daeElements,literals) = findLiterals(daeElements);
(fns, extraRecordDecls, includes, includeDirs, libs) = elaborateFunctions(daeElements, metarecordTypes, literals, includes);
makefileParams = createMakefileParams(includeDirs, libs);
fnCode = FUNCTIONCODE(name, NONE(), fns, literals, includes, makefileParams, extraRecordDecls);
Expand All @@ -1276,6 +1266,60 @@ algorithm
end match;
end translateFunctions;

protected function findLiterals
"Finds all literal expressions in the DAE"
input list<DAE.Function> fns;
output list<DAE.Function> ofns;
output list<DAE.Exp> literals;
algorithm
(ofns,(_,_,literals)) := DAEUtil.traverseDAEFunctions(
fns, findLiteralsHelper,
(0,HashTableExpToIndex.emptyHashTableSized(BaseHashTable.bigBucketSize),{}));
literals := listReverse(literals);
end findLiterals;

protected function findLiteralsHelper
input tuple<DAE.Exp,tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>>> inTpl;
output tuple<DAE.Exp,tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>>> outTpl;
protected
DAE.Exp exp;
tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>> tpl;
algorithm
(exp,tpl) := inTpl;
((exp,tpl)) := Expression.traverseExp(exp,replaceLiteralExp,tpl);
((exp,tpl)) := Expression.traverseExpTopDown(exp,replaceLiteralArrayExp,tpl);
outTpl := (exp,tpl);
end findLiteralsHelper;

protected function replaceLiteralArrayExp
"The tuples contain:
* The expression to be replaced (or not)
* Index of next literal
* HashTable Exp->Index (Number of the literal)
* The list of literals
Handles only array expressions (needs to be performed in a top-down fashion)
"
input tuple<DAE.Exp,tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>>> inTpl;
output tuple<DAE.Exp,Boolean,tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>>> outTpl;
algorithm
outTpl := matchcontinue inTpl
local
DAE.Exp exp;
tuple<Integer,HashTableExpToIndex.HashTable,list<DAE.Exp>> tpl;
case ((exp as DAE.ARRAY(array=_),_))
equation
isLiteralArrayExp(exp);
((exp,tpl)) = replaceLiteralExp2(inTpl);
then ((exp,false,tpl));
case ((exp as DAE.ARRAY(array=_),tpl))
equation
failure(isLiteralArrayExp(exp));
then ((exp,false,tpl));
case ((exp,tpl)) then ((exp,true,tpl));
end matchcontinue;
end replaceLiteralArrayExp;

protected function replaceLiteralExp
"The tuples contain:
* The expression to be replaced (or not)
Expand Down Expand Up @@ -1395,6 +1439,31 @@ algorithm
end match;
end isTrivialLiteralExp;

protected function isLiteralArrayExp
input DAE.Exp exp;
algorithm
_ := match exp
local
DAE.Exp e1,e2;
list<DAE.Exp> expl;
case DAE.SCONST(_) then ();
case DAE.ICONST(_) then ();
case DAE.RCONST(_) then ();
case DAE.BCONST(_) then ();
case DAE.ARRAY(array=expl) equation Util.listMap0(expl,isLiteralArrayExp); then ();
case DAE.ENUM_LITERAL(index = _) then ();
case DAE.META_OPTION(NONE()) then ();
case DAE.META_OPTION(SOME(exp)) equation isLiteralArrayExp(exp); then ();
case DAE.BOX(exp) equation isLiteralArrayExp(exp); then ();
case DAE.CONS(car = e1, cdr = e2) equation isLiteralArrayExp(e1); isLiteralArrayExp(e2); then ();
case DAE.LIST(valList = expl) equation Util.listMap0(expl,isLiteralArrayExp); then ();
case DAE.META_TUPLE(expl) equation Util.listMap0(expl,isLiteralArrayExp); then ();
case DAE.METARECORDCALL(args=expl) equation Util.listMap0(expl,isLiteralArrayExp); then ();
case DAE.SHARED_LITERAL(index=_) then ();
else fail();
end match;
end isLiteralArrayExp;

protected function isLiteralExp
"Returns if the expression may be replaced by a constant literal"
input DAE.Exp exp;
Expand Down
3 changes: 3 additions & 0 deletions Compiler/FrontEnd/Expression.mo
Expand Up @@ -4024,6 +4024,9 @@ algorithm
case (e as DAE.PATTERN(pattern=_),rel,ext_arg)
then ((e,ext_arg));

case (e as DAE.SHARED_LITERAL(index=_),rel,ext_arg)
then ((e,ext_arg));

case (e,rel,ext_arg)
equation
str = ExpressionDump.printExpStr(e);
Expand Down
4 changes: 2 additions & 2 deletions Compiler/FrontEnd/Types.mo
Expand Up @@ -4178,8 +4178,8 @@ algorithm
equation
true = Expression.dimensionKnown(dim1);
elist_1 = typeConvertArray(elist, ty1, ty2, dim1,printFailtrace);
ety1 = elabType(ty1);
dims = Expression.arrayDimension(ety1);
ety1 = elabType(ty2);
dims = Expression.arrayDimension(elabType(ty1));
a = isArray(ty2);
sc = boolNot(a);
//TODO: Verify correctness of return value.
Expand Down
23 changes: 23 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -4061,6 +4061,7 @@ case STMT_NORETCALL(__) then
>>



end algStmtNoretcall;


Expand Down Expand Up @@ -6369,6 +6370,18 @@ template literalExpConst(Exp lit, Integer index) "These should all be declared s
static const size_t <%name%>_strlen = <%unescapedStringLength(string)%>;
static const char <%name%>[<%intAdd(1,unescapedStringLength(string))%>] = <%name%>_data;
>>
case lit as ARRAY(ty=ty as ET_ARRAY(__)) then
let ndim = listLength(ty.arrayDimensions)
let sty = expTypeShort(ty)
let dims = (ty.arrayDimensions |> dim => dimension(dim) ;separator=", ")
let data = flattenArrayExpToList(lit) |> exp => literalExpConstArrayVal(exp) ; separator=", "
<<
static _index_t <%name%>_dims[<%ndim%>] = {<%dims%>};
static const modelica_<%sty%> <%name%>_data[] = {<%data%>};
static <%sty%>_array const <%name%> = {
<%ndim%>, <%name%>_dims, (void*) <%name%>_data
};
>>
case BOX(exp=exp as ICONST(__)) then
<<
<%meta%> = MMC_IMMEDIATE(MMC_TAGFIXNUM(<%exp.integer%>));
Expand Down Expand Up @@ -6429,6 +6442,16 @@ template literalExpConstBoxedVal(Exp lit)
else error(sourceInfo(), 'literalExpConstBoxedVal failed: <%printExpStr(lit)%>')
end literalExpConstBoxedVal;

template literalExpConstArrayVal(Exp lit)
::=
match lit
case ICONST(__) then integer
case lit as BCONST(__) then if lit.bool then 1 else 0
case RCONST(__) then real
case lit as SHARED_LITERAL(__) then '_OMC_LIT<%lit.index%>'
else error(sourceInfo(), 'literalExpConstArrayVal failed: <%printExpStr(lit)%>')
end literalExpConstArrayVal;

template equationInfo(list<SimEqSystem> eqs)
::=
match eqs
Expand Down
5 changes: 5 additions & 0 deletions Compiler/susan_codegen/SimCode/SimCodeTV.mo
Expand Up @@ -1966,6 +1966,11 @@ package Expression
output Option<Integer> oi;
end realExpIntLit;

function flattenArrayExpToList
input DAE.Exp e;
output list<DAE.Exp> expLst;
end flattenArrayExpToList;

end Expression;

package ExpressionDump
Expand Down

0 comments on commit bd7926d

Please sign in to comment.