Skip to content

Commit

Permalink
Merge pull request #2208 from davidwzhao/clean-up-provenance-synthesi…
Browse files Browse the repository at this point in the history
…ser-relation

Clean up relation generation for provenance in synthesiser and interpreter
  • Loading branch information
Bernhard Scholz authored Mar 14, 2022
2 parents 390e096 + 16e0caf commit d907cec
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 52 deletions.
2 changes: 2 additions & 0 deletions src/RelationTag.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum class RelationRepresentation {
BTREE, // use btree data-structure
BTREE_DELETE, // use btree_delete data-structure
EQREL, // use union data-structure
PROVENANCE, // use custom btree data-structure with provenance extras
INFO, // info relation for provenance
};

Expand Down Expand Up @@ -167,6 +168,7 @@ inline std::ostream& operator<<(std::ostream& os, RelationRepresentation represe
case RelationRepresentation::BTREE_DELETE: return os << "btree_delete";
case RelationRepresentation::BRIE: return os << "brie";
case RelationRepresentation::EQREL: return os << "eqrel";
case RelationRepresentation::PROVENANCE: return os << "provenance";
case RelationRepresentation::INFO: return os << "info";
case RelationRepresentation::DEFAULT: return os;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ast2ram/provenance/UnitTranslator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ Own<ram::Sequence> UnitTranslator::generateProgram(const ast::TranslationUnit& t
Own<ram::Relation> UnitTranslator::createRamRelation(
const ast::Relation* baseRelation, std::string ramRelationName) const {
auto arity = baseRelation->getArity();
auto representation = baseRelation->getRepresentation();

// All relations in a provenance program should have a provenance data structure
auto representation = RelationRepresentation::PROVENANCE;

// Add in base relation information
std::vector<std::string> attributeNames;
Expand Down
3 changes: 1 addition & 2 deletions src/interpreter/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ RamDomain callStateless(ExecuteFn&& execute, Context& ctxt, Shadow& shadow, souf
Engine::Engine(ram::TranslationUnit& tUnit)
: profileEnabled(Global::config().has("profile")),
frequencyCounterEnabled(Global::config().has("profile-frequency")),
isProvenance(Global::config().has("provenance")),
numOfThreads(number_of_threads(std::stoi(Global::config().get("jobs")))), tUnit(tUnit),
isa(tUnit.getAnalysis<ram::analysis::IndexAnalysis>()), recordTable(numOfThreads),
symbolTable(numOfThreads) {}
Expand Down Expand Up @@ -337,7 +336,7 @@ void Engine::createRelation(const ram::Relation& id, const std::size_t idx) {
res = createEqrelRelation(id, isa.getIndexSelection(id.getName()));
} else if (id.getRepresentation() == RelationRepresentation::BTREE_DELETE) {
res = createBTreeDeleteRelation(id, isa.getIndexSelection(id.getName()));
} else if (isProvenance) {
} else if (id.getRepresentation() == RelationRepresentation::PROVENANCE) {
res = createProvenanceRelation(id, isa.getIndexSelection(id.getName()));
} else {
res = createBTreeRelation(id, isa.getIndexSelection(id.getName()));
Expand Down
2 changes: 0 additions & 2 deletions src/interpreter/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,6 @@ class Engine {
/** If profile is enable in this program */
const bool profileEnabled;
const bool frequencyCounterEnabled;
/** If running a provenance program */
const bool isProvenance;
/** subroutines */
VecOwn<Node> subroutine;
/** main program */
Expand Down
2 changes: 1 addition & 1 deletion src/ram/transform/MakeIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ ExpressionPair MakeIndexTransformer::getLowerUpperExpression(Condition* c, std::
if (auto* binRelOp = as<Constraint>(c)) {
bool interpreter = !Global::config().has("compile") && !Global::config().has("dl-program") &&
!Global::config().has("generate") && !Global::config().has("swig");
bool provenance = Global::config().has("provenance");
bool provenance = rep == RelationRepresentation::PROVENANCE;
bool btree = (rep == RelationRepresentation::BTREE || rep == RelationRepresentation::DEFAULT ||
rep == RelationRepresentation::BTREE_DELETE);
auto op = binRelOp->getOperator();
Expand Down
26 changes: 11 additions & 15 deletions src/synthesiser/Relation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,30 +44,30 @@ std::string Relation::getTypeAttributeString(const std::vector<std::string>& att
}

Own<Relation> Relation::getSynthesiserRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection, bool isProvenance) {
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection) {
Relation* rel;

// Handle the qualifier in souffle code
if (isProvenance) {
rel = new DirectRelation(ramRel, indexSelection, isProvenance, false);
if (ramRel.getRepresentation() == RelationRepresentation::PROVENANCE) {
rel = new DirectRelation(ramRel, indexSelection, true, false);
} else if (ramRel.isNullary()) {
rel = new NullaryRelation(ramRel, indexSelection, isProvenance);
rel = new NullaryRelation(ramRel, indexSelection);
} else if (ramRel.getRepresentation() == RelationRepresentation::BTREE) {
rel = new DirectRelation(ramRel, indexSelection, isProvenance, false);
rel = new DirectRelation(ramRel, indexSelection, false, false);
} else if (ramRel.getRepresentation() == RelationRepresentation::BTREE_DELETE) {
rel = new DirectRelation(ramRel, indexSelection, isProvenance, true);
rel = new DirectRelation(ramRel, indexSelection, false, true);
} else if (ramRel.getRepresentation() == RelationRepresentation::BRIE) {
rel = new BrieRelation(ramRel, indexSelection, isProvenance);
rel = new BrieRelation(ramRel, indexSelection);
} else if (ramRel.getRepresentation() == RelationRepresentation::EQREL) {
rel = new EqrelRelation(ramRel, indexSelection, isProvenance);
rel = new EqrelRelation(ramRel, indexSelection);
} else if (ramRel.getRepresentation() == RelationRepresentation::INFO) {
rel = new InfoRelation(ramRel, indexSelection, isProvenance);
rel = new InfoRelation(ramRel, indexSelection);
} else {
// Handle the data structure command line flag
if (ramRel.getArity() > 6) {
rel = new IndirectRelation(ramRel, indexSelection, isProvenance);
rel = new IndirectRelation(ramRel, indexSelection);
} else {
rel = new DirectRelation(ramRel, indexSelection, isProvenance, false);
rel = new DirectRelation(ramRel, indexSelection, false, false);
}
}

Expand Down Expand Up @@ -544,8 +544,6 @@ void DirectRelation::generateTypeStruct(std::ostream& out) {

/** Generate index set for a indirect indexed relation */
void IndirectRelation::computeIndices() {
assert(!isProvenance && "indirect indexes cannot used for provenance");

// Generate and set indices
auto inds = indexSelection.getAllOrders();

Expand Down Expand Up @@ -880,8 +878,6 @@ void IndirectRelation::generateTypeStruct(std::ostream& out) {

/** Generate index set for a brie relation */
void BrieRelation::computeIndices() {
assert(!isProvenance && "bries cannot be used with provenance");

// Generate and set indices
auto inds = indexSelection.getAllOrders();

Expand Down
42 changes: 17 additions & 25 deletions src/synthesiser/Relation.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ namespace souffle::synthesiser {

class Relation {
public:
Relation(const ram::Relation& rel, const ram::analysis::IndexCluster& indexSelection,
const bool isProvenance = false)
: relation(rel), indexSelection(indexSelection), isProvenance(isProvenance) {}
Relation(const ram::Relation& rel, const ram::analysis::IndexCluster& indexSelection)
: relation(rel), indexSelection(indexSelection) {}

virtual ~Relation() = default;

Expand Down Expand Up @@ -69,8 +68,8 @@ class Relation {
virtual void generateTypeStruct(std::ostream& out) = 0;

/** Factory method to generate a SynthesiserRelation */
static Own<Relation> getSynthesiserRelation(const ram::Relation& ramRel,
const ram::analysis::IndexCluster& indexSelection, bool isProvenance);
static Own<Relation> getSynthesiserRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection);

protected:
/** Ram relation referred to by this */
Expand All @@ -89,17 +88,13 @@ class Relation {
std::set<std::size_t> provenanceIndexNumbers;

/** The number of the master index */
std::size_t masterIndex;

/** Is this relation used with provenance */
const bool isProvenance;
std::size_t masterIndex = -1;
};

class NullaryRelation : public Relation {
public:
NullaryRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection, bool isProvenance)
: Relation(ramRel, indexSelection, isProvenance) {}
NullaryRelation(const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection)
: Relation(ramRel, indexSelection) {}

void computeIndices() override;
std::string getTypeName() override;
Expand All @@ -108,9 +103,8 @@ class NullaryRelation : public Relation {

class InfoRelation : public Relation {
public:
InfoRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection, bool isProvenance)
: Relation(ramRel, indexSelection, isProvenance) {}
InfoRelation(const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection)
: Relation(ramRel, indexSelection) {}

void computeIndices() override;
std::string getTypeName() override;
Expand All @@ -121,21 +115,21 @@ class DirectRelation : public Relation {
public:
DirectRelation(const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection,
bool isProvenance, bool hasErase)
: Relation(ramRel, indexSelection, isProvenance), hasErase(hasErase) {}
: Relation(ramRel, indexSelection), isProvenance(isProvenance), hasErase(hasErase) {}

void computeIndices() override;
std::string getTypeName() override;
void generateTypeStruct(std::ostream& out) override;

private:
const bool isProvenance;
const bool hasErase;
};

class IndirectRelation : public Relation {
public:
IndirectRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection, bool isProvenance)
: Relation(ramRel, indexSelection, isProvenance) {}
IndirectRelation(const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection)
: Relation(ramRel, indexSelection) {}

void computeIndices() override;
std::string getTypeName() override;
Expand All @@ -144,9 +138,8 @@ class IndirectRelation : public Relation {

class BrieRelation : public Relation {
public:
BrieRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection, bool isProvenance)
: Relation(ramRel, indexSelection, isProvenance) {}
BrieRelation(const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection)
: Relation(ramRel, indexSelection) {}

void computeIndices() override;
std::string getTypeName() override;
Expand All @@ -155,9 +148,8 @@ class BrieRelation : public Relation {

class EqrelRelation : public Relation {
public:
EqrelRelation(
const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection, bool isProvenance)
: Relation(ramRel, indexSelection, isProvenance) {}
EqrelRelation(const ram::Relation& ramRel, const ram::analysis::IndexCluster& indexSelection)
: Relation(ramRel, indexSelection) {}

void computeIndices() override;
std::string getTypeName() override;
Expand Down
9 changes: 3 additions & 6 deletions src/synthesiser/Synthesiser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2565,10 +2565,8 @@ void Synthesiser::generateCode(std::ostream& sos, const std::string& id, bool& w

// synthesise data-structures for relations
for (auto rel : prog.getRelations()) {
bool isProvInfo = rel->getRepresentation() == RelationRepresentation::INFO;
auto relationType =
Relation::getSynthesiserRelation(*rel, idxAnalysis.getIndexSelection(rel->getName()),
Global::config().has("provenance") && !isProvInfo);
Relation::getSynthesiserRelation(*rel, idxAnalysis.getIndexSelection(rel->getName()));

generateRelationTypeStruct(os, std::move(relationType));
}
Expand Down Expand Up @@ -2683,9 +2681,8 @@ void Synthesiser::generateCode(std::ostream& sos, const std::string& id, bool& w
const std::string& datalogName = rel->getName();
const std::string& cppName = getRelationName(*rel);

bool isProvInfo = rel->getRepresentation() == RelationRepresentation::INFO;
auto relationType = Relation::getSynthesiserRelation(*rel, idxAnalysis.getIndexSelection(datalogName),
Global::config().has("provenance") && !isProvInfo);
auto relationType =
Relation::getSynthesiserRelation(*rel, idxAnalysis.getIndexSelection(datalogName));
const std::string& type = relationType->getTypeName();

// defining table
Expand Down

0 comments on commit d907cec

Please sign in to comment.