Skip to content

Commit

Permalink
Minor changes in the process of implementing AST CodeGen for Selector
Browse files Browse the repository at this point in the history
  • Loading branch information
crvineeth97 committed Jul 25, 2018
1 parent fdd70e0 commit 1f2a49b
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 30 deletions.
61 changes: 44 additions & 17 deletions source/llvm/ASTNodeCodeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,16 +218,6 @@ llvm::Value* ASTNodeCodeGen::codeGen(const libsbml::ASTNode* ast)
case AST_LAMBDA:
result = notImplemented(ast);
break;
case AST_ORIGINATES_IN_PACKAGE:
{
if (ast->getExtendedType() == AST_LINEAR_ALGEBRA_VECTOR_CONSTRUCTOR)
{

}
if (ast->getExtendedType() == AST_LINEAR_ALGEBRA_SELECTOR)
result = selectorCodeGen(ast);
break;
}
default:
{
stringstream msg;
Expand All @@ -240,6 +230,34 @@ llvm::Value* ASTNodeCodeGen::codeGen(const libsbml::ASTNode* ast)
return result;
}

llvm::Value* ASTNodeCodeGen::arrayCodeGen(const libsbml::ASTNode* ast, const std::string& symbol = NULL,
const llvm::ArrayRef<llvm::Value*>& args = NULL)
{
Value *result = 0;

if (ast == 0)
{
throw_llvm_exception("ASTNode is NULL");
}

switch (ast->getType())
{
case AST_ORIGINATES_IN_PACKAGE:
{
if (ast->getExtendedType() == AST_LINEAR_ALGEBRA_VECTOR_CONSTRUCTOR)
{

}
if (ast->getExtendedType() == AST_LINEAR_ALGEBRA_SELECTOR)
result = selectorCodeGen(ast, symbol, args);
break;

default:
return codeGen(ast);
break;
}
}
}

llvm::Value* ASTNodeCodeGen::notImplemented(const libsbml::ASTNode* ast)
{
Expand Down Expand Up @@ -274,7 +292,12 @@ llvm::Value* ASTNodeCodeGen::nameExprCodeGen(const libsbml::ASTNode* ast)
switch(ast->getType())
{
case AST_NAME:
{
// The iterator for a dimension variable
if (!dimensionVal.empty() && dimensionVal.find(ast->getName()) != dimensionVal.end())
return dimensionVal[ast->getName()];
return resolver.loadSymbolValue(ast->getName());
}
case AST_NAME_AVOGADRO:
// TODO: correct this for different units
return ConstantFP::get(builder.getContext(), APFloat(6.02214179e23));
Expand Down Expand Up @@ -927,6 +950,9 @@ llvm::Value* ASTNodeCodeGen::piecewiseCodeGen(const libsbml::ASTNode* ast)
return pn;
}

/*
* Called only when the ast node is of extended type AST_LINEAR_ALGEBRA_SELECTOR
*/
void ASTNodeCodeGen::getASTArrayId(const libsbml::ASTNode* parent, const libsbml::ASTNode* ast, std::string* id)
{
// If the current AST is another selector not related to the parent
Expand All @@ -942,7 +968,7 @@ void ASTNodeCodeGen::getASTArrayId(const libsbml::ASTNode* parent, const libsbml
Log(Logger::LOG_ERROR) << "One of the dimensions have a value that is not an integer";
throw invalid_argument("Dimensions for an assigment rule math is not an integer");
}
int dim = (uint)iptr;
uint dim = (uint)iptr;
(*id) += "-" + std::to_string(dim);
return;
}
Expand All @@ -959,8 +985,8 @@ void ASTNodeCodeGen::getASTArrayId(const libsbml::ASTNode* parent, const libsbml
}
else if (child->getType() == AST_NAME)
{
// This is our actual variable from which we have to select
(*id) += child->getName();
// This is our actual variable from which we have to select
(*id) += child->getName();
}
else
{
Expand All @@ -979,7 +1005,10 @@ void ASTNodeCodeGen::getASTArrayId(const libsbml::ASTNode* parent, const libsbml
}
}

llvm::Value* ASTNodeCodeGen::selectorCodeGen(const libsbml::ASTNode *ast)


llvm::Value* ASTNodeCodeGen::selectorCodeGen(const libsbml::ASTNode *ast, const std::string& symbol,
const llvm::ArrayRef<llvm::Value*>& args)
{
/**
* There are 2 ways to represent a selector a[x][y] and a[x, y]
Expand All @@ -997,10 +1026,8 @@ llvm::Value* ASTNodeCodeGen::selectorCodeGen(const libsbml::ASTNode *ast)
* the second representation and then return the double value required. To do that we
* cannot use the well-written CodeGen function
*/
string id = "";
getASTArrayId(NULL, ast, &id);
const AssignmentRule* asg = dynamic_cast<const AssignmentRule*>(ast->getParentSBMLObject());
// Id should contain our required symbol
return resolver.loadSymbolValue(id);
}

static bool isNegative(const libsbml::ASTNode *ast)
Expand Down
14 changes: 11 additions & 3 deletions source/llvm/ASTNodeCodeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ class ASTNodeCodeGen
~ASTNodeCodeGen();

llvm::Value *codeGen(const libsbml::ASTNode *ast);

llvm::Value *arrayCodeGen(const libsbml::ASTNode *ast, const std::string& symbol = NULL,
const llvm::ArrayRef<llvm::Value*>& args = NULL);
/**
* Get the Array ID of an element through @param id
*/
void getASTArrayId(const libsbml::ASTNode* parent, const libsbml::ASTNode *ast, std::string *id);

private:

/**
Expand Down Expand Up @@ -75,9 +83,8 @@ class ASTNodeCodeGen

llvm::Value *piecewiseCodeGen(const libsbml::ASTNode *ast);

void getASTArrayId(const libsbml::ASTNode* parent, const libsbml::ASTNode *ast, std::string *id);

llvm::Value *selectorCodeGen(const libsbml::ASTNode *ast);
llvm::Value *selectorCodeGen(const libsbml::ASTNode *ast, const std::string& symbol,
const llvm::ArrayRef<llvm::Value*>& args);

/**
* coerces a value to a boolean single bit.
Expand All @@ -93,6 +100,7 @@ class ASTNodeCodeGen
*/
llvm::Value *toDouble(llvm::Value* value);

std::map < std::string, uint > dimensionVal;

llvm::IRBuilder<> &builder;
LoadSymbolResolver &resolver;
Expand Down
27 changes: 24 additions & 3 deletions source/llvm/LLVMModelDataSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ set<string> LLVMModelDataSymbols::getArrayedGlobalParameters(
}
}

vector<uint> LLVMModelDataSymbols::getSizeOfDimensions(
const std::string& id) const
{
StringUIntVectorMap::const_iterator i = sizeOfDimensions.find(id);
if (i != sizeOfDimensions.end())
{
return i->second;
}
else
{
throw LLVMException("Could not find an element with ID " + id, __FUNC__);
}
}

/*
void LLVMModelDataSymbols::initAllocModelDataBuffers(LLVMModelData& m) const
Expand Down Expand Up @@ -658,10 +671,18 @@ void LLVMModelDataSymbols::initArrayGlobalParameters(const Model* model, list<st

// Get the size of the dimension
const string& sizeParamId = arraysParam->getDimension(ind)->getSize();
double sizeOfDimension = model->getParameter(sizeParamId)->getValue();
double val = model->getParameter(sizeParamId)->getValue();
double iptr;
if (modf(val, &iptr) != 0.0 || iptr < 0.0)
{
Log(Logger::LOG_ERROR) << "Dimension " << ind << " of " << sizeParamId <<
" has a value that is not a positive integer";
throw invalid_argument("Dimension " + to_string(ind) + " of parameter " + sizeParamId + " is not a positive integer");
}
sizeOfDimensions[*id].push_back((uint)iptr);

// Create the list of parameters
for (uint i = 0; i < sizeOfDimension; i++)
for (uint i = 0; i < sizeOfDimensions[*id][ind]; i++)
{
// Using hyphen(-) instead of an underscore(_) for the parameter ID's
// because SBML officially allows for SID type to have an underscore and this
Expand All @@ -686,7 +707,7 @@ void LLVMModelDataSymbols::initGlobalParameters(const libsbml::Model* model,
const Parameter *p = parameters->get(i);
const string& id = p->getId();

// p->isPackageEnabled("arrays") is not working so using the value through getNumDimensions
// p->isPackageEnabled("arrays") is not working so using the value through getNumDimensions Vin
#ifndef LIBSBML_HAS_PACKAGE_ARRAYS
if (p->isPackageEnabled("arrays"))
{
Expand Down
8 changes: 8 additions & 0 deletions source/llvm/LLVMModelDataSymbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,11 @@ class LLVMModelDataSymbols
*/
std::set<std::string> getArrayedGlobalParameters(std::string const&) const;

/**
* Get the size of all dimensions of an element
*/
std::vector<uint> getSizeOfDimensions(std::string const&) const;

uint getRateRuleIndex(std::string const&) const;

uint getRateRuleSize() const;
Expand Down Expand Up @@ -620,6 +625,9 @@ class LLVMModelDataSymbols
// Store the expanded IDs of a parameter that has a list of dimensions
std::map<std::string, std::set<std::string> > arrayedGlobalParameters;

// Store the size of each dimension of parameters
StringUIntVectorMap sizeOfDimensions;

/**
* map of all identified species reference (species references with ids)
* to their indices in the stoichiometric matrix.
Expand Down
63 changes: 61 additions & 2 deletions source/llvm/LLVMModelSymbols.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <sbml/math/ASTNode.h>
#include <sbml/math/FormulaFormatter.h>
#include <sbml/SBMLDocument.h>
#ifdef LIBSBML_HAS_PACKAGE_ARRAYS
#include <sbml/packages/arrays/common/ArraysExtensionTypes.h>
#endif

using namespace libsbml;
using namespace llvm;
Expand Down Expand Up @@ -146,8 +149,8 @@ bool LLVMModelSymbols::visit(const libsbml::AssignmentRule& x)
{
Log(Logger::LOG_TRACE) << "processing AssignmentRule, id: " << x.getId();
SBase *element = const_cast<Model*>(model)->getElementBySId(x.getVariable());

if (element)
if (element)
{
processElement(assigmentRules, element, x.getMath());
}
Expand Down Expand Up @@ -183,6 +186,7 @@ bool LLVMModelSymbols::visit (const libsbml::Event &event)
return true;
}

// Need to change this function to take in arrays
void LLVMModelSymbols::processElement(SymbolForest& currentSymbols,
const libsbml::SBase *element, const ASTNode* math)
{
Expand All @@ -200,6 +204,7 @@ void LLVMModelSymbols::processElement(SymbolForest& currentSymbols,
}
else if ((param = dynamic_cast<const Parameter*>(element)))
{
// processParameter(currentSymbols, param, math);
currentSymbols.globalParameters[param->getId()] = math;
}
else if ((species = dynamic_cast<const Species*>(element)))
Expand Down Expand Up @@ -420,6 +425,60 @@ void LLVMModelSymbols::processSpecies(SymbolForest &currentSymbols,
}
}

//const ASTNode* LLVMModelSymbols::processArrayAST(SymbolForest &currentSymbols, vector<uint> *sizeOfDimensions, uint ind, const ASTNode *lhsMath, const ASTNode *rhsMath)
//{
// if (ind == sizeOfDimensions->size())
// {
// string lhs = "";
// string rhs = "";
//
// ASTNodeCodeGen().getASTArrayId(NULL, lhsMath, lhs);
// currentSymbols.globalParameters[]
// arrayedGlobalParameters[*id].insert(arrayId);
// return;
// }
//
// // Get the size of the dimension
// const string& sizeParamId = arraysParam->getDimension(ind)->getSize();
// double val = model->getParameter(sizeParamId)->getValue();
// double iptr;
// if (modf(val, &iptr) != 0.0 || iptr < 0.0)
// {
// Log(Logger::LOG_ERROR) << "Dimension " << ind << " of " << sizeParamId <<
// " has a value that is not a positive integer";
// throw invalid_argument("Dimension " + to_string(ind) + " of parameter " + sizeParamId + " is not a positive integer");
// }
// sizeOfDimensions[*id].push_back((uint)iptr);
//
// // Create the list of parameters
// for (uint i = 0; i < sizeOfDimensions[*id][ind]; i++)
// {
//// // Using hyphen(-) instead of an underscore(_) for the parameter ID's
//// // because SBML officially allows for SID type to have an underscore and this
//// // will cause problems while dereferencing the ID if another paramter has an
//// // underscore in its ID
//// initArrayGlobalParameters(model, param, id, ind + 1, arrayId + "-" + to_string(i));
// }
//}
//
//void LLVMModelSymbols::processParameter(SymbolForest &currentSymbols,
// const Parameter *param, const ASTNode *math)
//{
//#ifndef LIBSBML_HAS_PACKAGE_ARRAYS
// currentSymbols.globalParameters[param->getId()] = math;
// return;
//#endif
// const ArraysSBasePlugin * arraysParam = static_cast<const ArraysSBasePlugin*>(param->getPlugin("arrays"));
// uint numDimensions = arraysParam->getNumDimensions();
// if (numDimensions)
// {
// currentSymbols.globalParameters[param->getId()] = math;
// return;
// }
// vector<uint> sizeOfDimensions = this->symbols.getSizeOfDimensions(param->getId());
// this->
//}

const ASTNode* LLVMModelSymbols::getSpeciesReferenceStoichMath(
const libsbml::SpeciesReference* reference)
{
Expand Down
9 changes: 8 additions & 1 deletion source/llvm/LLVMModelSymbols.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ class LLVMModelSymbols: private libsbml::SBMLVisitor
void processSpecies(SymbolForest &currentSymbols,
const libsbml::Species *element, const libsbml::ASTNode *math);

/**
* In the case of an array of parameters, it is essential to ensure that the
* math of the indices is handled correctly. To do that, the dimensions have
* to be replaced correctly to get the correct math expression for the variable
*/
void processParameter(SymbolForest &currentSymbols,
const libsbml::Parameter *param, const libsbml::ASTNode *math);

/**
* get the MathML element for a SpeciesReference if it is set, otherwise,
* create a ASTNode from its stoichiometry field.
Expand All @@ -191,7 +199,6 @@ class LLVMModelSymbols: private libsbml::SBMLVisitor

SymbolForest initialValues;

// Have to change the forest to include arrays Vin
SymbolForest assigmentRules;

SymbolForest initialAssignmentRules;
Expand Down
4 changes: 2 additions & 2 deletions source/llvm/ModelDataSymbolResolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ llvm::Value* ModelDataLoadSymbolResolver::loadSymbolValue(
/* AssignmentRule */
/*************************************************************************/
{
string arrayedSymbol = modelDataSymbols.decodeArrayId(symbol);
const string& arrayedSymbol = modelDataSymbols.decodeArrayId(symbol);
SymbolForest::ConstIterator i = modelSymbols.getAssigmentRules().find(
arrayedSymbol);
if (i != modelSymbols.getAssigmentRules().end())
{
recursiveSymbolPush(symbol);
Value* result = ASTNodeCodeGen(builder, *this).codeGen(i->second);
Value* result = ASTNodeCodeGen(builder, *this).codeGen(i->second, symbol, args);
recursiveSymbolPop();
return cacheValue(symbol, args, result);
}
Expand Down
2 changes: 0 additions & 2 deletions source/llvm/ModelDataSymbolResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class ModelDataLoadSymbolResolver: public LoadSymbolResolverBase

virtual ~ModelDataLoadSymbolResolver() {};

//virtual std::string decodeArraySymbol(const std::string& id) const;

virtual llvm::Value *loadSymbolValue(const std::string& symbol,
const llvm::ArrayRef<llvm::Value*>& args =
llvm::ArrayRef<llvm::Value*>());
Expand Down

0 comments on commit 1f2a49b

Please sign in to comment.