Skip to content

Commit

Permalink
Automatically perform delimiting in addStmt #147
Browse files Browse the repository at this point in the history
Includes modifying tests to work with this
  • Loading branch information
riftEmber committed Oct 13, 2021
1 parent 9607901 commit 7eb985c
Show file tree
Hide file tree
Showing 4 changed files with 1,412 additions and 1,392 deletions.
32 changes: 25 additions & 7 deletions src/computation/Computation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,15 @@ Set* Computation::trimISDataSpaces(Set* set, std::set<std::string> &trimmedNames
}

void Computation::addStmt(Stmt* stmt) {
transformationLists.push_back({});

delimitDataSpacesInStmt(stmt);

enforceArraySSA(stmt);
locatePhiNodes(stmt);
enforceSSA(stmt);

stmts.push_back(stmt);
transformationLists.push_back({});
}

Stmt* Computation::getStmt(unsigned int index) const { return stmts.at(index); }
Expand Down Expand Up @@ -1544,6 +1548,20 @@ void Computation::fuse (int s1, int s2, int fuseLevel){
}
}

void Computation::delimitDataSpacesInStmt(Stmt *stmt) {
stmt->setStmtSourceCode(delimitDataSpacesInString(stmt->getStmtSourceCode()));
stmt->setIterationSpace(delimitDataSpacesInString(stmt->getIterationSpace()->prettyPrintString()));
stmt->setExecutionSchedule(delimitDataSpacesInString(stmt->getExecutionSchedule()->prettyPrintString()));
for (unsigned int i = 0; i < stmt->getNumReads(); ++i) {
stmt->updateRead(i, delimitDataSpacesInString(stmt->getReadDataSpace(i)),
stmt->getReadRelation(i)->prettyPrintString());
}
for (unsigned int i = 0; i < stmt->getNumWrites(); ++i) {
stmt->updateWrite(i, delimitDataSpacesInString(stmt->getWriteDataSpace(i)),
stmt->getWriteRelation(i)->prettyPrintString());
}
}

std::string Computation::delimitDataSpacesInString(std::string originalString) {
std::ostringstream delimitedString;

Expand Down Expand Up @@ -1571,7 +1589,7 @@ std::string Computation::delimitDataSpacesInString(std::string originalString) {
}

for (unsigned int i = 0; i < quoteSplitStringSegments.size(); ++i) {
// rewrite alternate sections, beginning from the first that is unquoted
// rewrite alternate sections, beginning from the first which is unquoted
if (i % 2 == 0) {
std::string& originalSegment = quoteSplitStringSegments[i];
std::ostringstream rewrittenSegment;
Expand Down Expand Up @@ -2214,17 +2232,17 @@ Stmt::Stmt(std::string stmtSourceCode, std::string iterationSpaceStr,
std::string executionScheduleStr,
std::vector<std::pair<std::string, std::string>> dataReadsStrs,
std::vector<std::pair<std::string, std::string>> dataWritesStrs) {
setStmtSourceCode(Computation::delimitDataSpacesInString(stmtSourceCode));
setIterationSpace(Computation::delimitDataSpacesInString(iterationSpaceStr));
setExecutionSchedule(Computation::delimitDataSpacesInString(executionScheduleStr));
setStmtSourceCode(stmtSourceCode);
setIterationSpace(iterationSpaceStr);
setExecutionSchedule(executionScheduleStr);
for (const auto& readInfo : dataReadsStrs) {
dataReads.push_back(
{Computation::delimitDataSpaceName(readInfo.first),
{readInfo.first,
std::unique_ptr<Relation>(new Relation(readInfo.second))});
}
for (const auto& writeInfo : dataWritesStrs) {
dataWrites.push_back(
{Computation::delimitDataSpaceName(writeInfo.first),
{writeInfo.first,
std::unique_ptr<Relation>(new Relation(writeInfo.second))});
}
};
Expand Down
14 changes: 7 additions & 7 deletions src/computation/Computation.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ class Computation {
//! in the computation.
void deleteDeadStatements();

//! add stmt
//! Adopt a statement into this Computation.
//! This may modify the statement to fit the needs of the Computation.
void addStmt(Stmt* stmt);
//! Get a statement by index
Stmt* getStmt(unsigned int index) const;
Expand Down Expand Up @@ -207,7 +208,7 @@ class Computation {
const std::vector<std::string>& arguments = {});

//! Pads each statement's execution schedule with 0's such the all
// execution schedules have the same arity
// execution schedules have the same arity, beginning from the first that is unquoted
// Caller is responsible for deallocating Relation*
std::vector<Relation*> padExecutionSchedules() const;

Expand Down Expand Up @@ -289,14 +290,15 @@ class Computation {
// S0: {[0,i,0,j,0] | stuff}; S1:{[0,i,1,j,0] | stuff}
void fuse (int s1, int s2, int fuseLevel);

//! Delimit data spaces in a statement so that it may be used properly in this Computation.
//! Modifies the given statement in-place.
void delimitDataSpacesInStmt(Stmt* stmt);
//! Go through the string, potentially consisting of C source code, and delimit all data space names.
std::string delimitDataSpacesInString(std::string originalString);

//! Wrap the given data space name in delimiters
static std::string delimitDataSpaceName(std::string dataSpaceName);

//! Get a new string with all instances of the data space delimiter removed from the original string
static std::string stripDataSpaceDelimiter(std::string delimitedStr);

//! Check if a data space name is delimited, excepting if it is only delimited on one side.
static bool nameIsDelimited(std::string name);

Expand Down Expand Up @@ -336,8 +338,6 @@ class Computation {

//! Parameters of the computation. All parameters should also be data spaces.
std::vector<std::string> parameters;
//! Non-delimited parameter names
std::vector<std::string> originalParameterNames;
//! Names of values that are returned if this Computation is called. May be
//! data space names or literals. This is an ordered list because some
//! languages allow multiple returns.
Expand Down
18 changes: 10 additions & 8 deletions src/computation/computation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ for (i = 0; i < a; i++) { \
return 0; \
";

Stmt* s0 = new Stmt("int i;", "{}", "{[]->[0,0,0,0,0]}", {}, {});
Stmt* s1 = new Stmt("int j;", "{}", "{[]->[1,0,0,0,0]}", {}, {});
Stmt* s0 = new Stmt("int i;", "{}", "{[0]->[0,0,0,0,0]}", {}, {});
Stmt* s1 = new Stmt("int j;", "{}", "{[0]->[1,0,0,0,0]}", {}, {});
Stmt* s2 = new Stmt("product[i] = 0;", "{[i]: i >= 0 && i < a}",
"{[i]->[2,i,0,0,0]}", {}, {{"product", "{[i]->[i]}"}});
Stmt* s3 = new Stmt("product[i] += x[i][j] * y[j];",
Expand All @@ -250,7 +250,7 @@ return 0; \
{"x", "{[i,j]->[i,j]}"},
{"y", "{[i,j]->[j]}"}},
{{"product", "{[i,j]->[i]}"}});
Stmt* s4 = new Stmt("return 0;", "{}", "{[]->[3,0,0,0,0]}", {}, {});
Stmt* s4 = new Stmt("return 0;", "{}", "{[0]->[3,0,0,0,0]}", {}, {});

Computation* comp = new Computation();
comp->addStmt(s0);
Expand Down Expand Up @@ -287,24 +287,24 @@ TEST_F(ComputationTest, ForwardTriangularSolve) {

std::vector<std::pair<std::string, std::string> > dataReads;
std::vector<std::pair<std::string, std::string> > dataWrites;
dataWrites.push_back(make_pair("tmp", "{[i]->[]}"));
dataWrites.push_back(make_pair("tmp", "{[i]->[0]}"));
dataReads.push_back(make_pair("f", "{[i]->[i]}"));
Computation* forwardSolve = new Computation();
Stmt* ss0 = new Stmt("tmp = f[i];", "{[i]: 0 <= i < NR}", "{[i] ->[i,0,0,0]}",
dataReads, dataWrites);
dataReads.clear();
dataWrites.clear();
dataReads.push_back(make_pair("tmp", "{[i,k]->[]}"));
dataReads.push_back(make_pair("tmp", "{[i,k]->[0]}"));
dataReads.push_back(make_pair("val", "{[i,k]->[k]}"));
dataReads.push_back(make_pair("u", "{[i,k]->[t]: t = col(k)}"));
dataWrites.push_back(make_pair("tmp", "{[i,k]->[]}"));
dataWrites.push_back(make_pair("tmp", "{[i,k]->[0]}"));

Stmt* ss1 = new Stmt("tmp -= val[k] * u[col[k]];",
"{[i,k]: 0 <= i && i < NR && rowptr(i) <= k && k < rowptr(i+1)-1}",
"{[i,k] -> [i,1,k,0]}", dataReads, dataWrites);
dataReads.clear();

dataReads.push_back(make_pair("tmp","{[i]->[]}"));
dataReads.push_back(make_pair("tmp","{[i]->[0]}"));
dataReads.push_back(make_pair("val","{[i]->[t]: t = rowptr(i+1) - 1}"));
dataWrites.push_back(make_pair("u","{[i]->[i]}"));

Expand Down Expand Up @@ -802,7 +802,7 @@ __x6 = 0 && B_0(__x0,i,__x2,j,__x4,k) = 0 && __x4 = 1 \
"{[i]->[j]: 0 <= i && i < N && 0 <= j && j < N }",
"{[i] -> [j] : 0 <= i < N && 0 <= j < N}");
// empty relation
checkOmegaRelationConversion("{[]->[]}", "{ TRUE }");
checkOmegaRelationConversion("{[0]->[0]}", "{ TRUE }");
// with simple UF constraints
checkOmegaRelationConversion(
"{[i,j]->[k]: 0 <= i && i < N && 0 <= j && j < M && i=foo(i+1)}",
Expand Down Expand Up @@ -1525,6 +1525,8 @@ TEST_F(ComputationTest, FusionUnitTest){
TEST_F(ComputationTest, DelimitDataSpacesTest) {
auto *comp = new Computation();
comp->addDataSpace("asdf", "int");
comp->addDataSpace("a", "int");
comp->addDataSpace("sd", "int");
comp->addDataSpace("x", "int");
comp->addDataSpace("z", "string");

Expand Down
Loading

0 comments on commit 7eb985c

Please sign in to comment.