Skip to content

Commit

Permalink
Decouple PBCtx from analysis logic (#613)
Browse files Browse the repository at this point in the history
* Decouple `PBCtx` from analysis logic

Now `PBCtx` objects are reference-counted.

* Make inter-PBCtx transfer type-safe
  • Loading branch information
roastduck authored Apr 20, 2024
1 parent 1872163 commit cd26131
Show file tree
Hide file tree
Showing 18 changed files with 617 additions and 412 deletions.
7 changes: 2 additions & 5 deletions include/analyze/comp_unique_bounds_pb.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class CompUniqueBoundsPB : public CompUniqueBounds {
public:
class Bound : public CompUniqueBounds::Bound {
public: // Visible to CompUniqueBoundsPB's subclasses
Ref<PBCtx> ctx_;
// isl var -> ft expr, the demangling map yielded from GenPBExpr
// shared from CompUniqueBoundsPB::cachedFreeVars_
Ref<std::unordered_map<std::string, Expr>> demangleMap_;
Expand All @@ -30,11 +29,9 @@ class CompUniqueBoundsPB : public CompUniqueBounds {
PBSet bound_;

public:
Bound(Ref<PBCtx> ctx,
Ref<std::unordered_map<std::string, Expr>> demangleMap,
Bound(Ref<std::unordered_map<std::string, Expr>> demangleMap,
PBSet bound)
: ctx_(std::move(ctx)), demangleMap_(std::move(demangleMap)),
bound_(std::move(bound)) {}
: demangleMap_(std::move(demangleMap)), bound_(std::move(bound)) {}

BoundType type() const override { return BoundType::Presburger; }

Expand Down
47 changes: 25 additions & 22 deletions include/analyze/deps.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,6 @@ struct Dependence {
// not only counting the nearest, but all
PBMap later2EarlierIterAllPossible_;
PBMap extConstraint_;
PBCtx &presburger_;
AnalyzeDeps &self_;

// Helper functions
Expand Down Expand Up @@ -363,39 +362,40 @@ class AnalyzeDeps {
static std::string makeCond(GenPBExpr &genPBExpr,
GenPBExpr::VarMap &externals,
bool eraseOutsideVarDef, const AccessPoint &ap);
static PBMap makeAccMapStatic(PBCtx &presburger, const AccessPoint &p,
int iterDim, int accDim,
static PBMap makeAccMapStatic(const Ref<PBCtx> &presburger,
const AccessPoint &p, int iterDim, int accDim,
const std::string &extSuffix,
GenPBExpr::VarMap &externals,
const ASTHashSet<Expr> &noNeedToBeVars,
bool eraseOutsideVarDef);

private:
PBMap makeAccMap(PBCtx &presburger, const AccessPoint &p, int iterDim,
int accDim, const std::string &extSuffix,
PBMap makeAccMap(const Ref<PBCtx> &presburger, const AccessPoint &p,
int iterDim, int accDim, const std::string &extSuffix,
GenPBExpr::VarMap &externals,
const ASTHashSet<Expr> &noNeedToBeVars) {
return makeAccMapStatic(presburger, p, iterDim, accDim, extSuffix,
externals, noNeedToBeVars, eraseOutsideVarDef_);
}

PBMap makeEqForBothOps(PBCtx &presburger,
PBMap makeEqForBothOps(const Ref<PBCtx> &presburger,
const std::vector<std::pair<int, int>> &coord,
int iterDim) const;
PBMap makeIneqBetweenOps(PBCtx &presburger, DepDirection mode, int iterId,
int iterDim) const;
PBMap makeIneqBetweenOps(const Ref<PBCtx> &presburger, DepDirection mode,
int iterId, int iterDim) const;

PBMap makeSerialToAll(PBCtx &presburger, int iterDim,
PBMap makeSerialToAll(const Ref<PBCtx> &presburger, int iterDim,
const std::vector<IterAxis> &point) const;
static int countSerial(const std::vector<IterAxis> &point);

PBMap makeExternalEq(PBCtx &presburger, int iterDim,
PBMap makeExternalEq(const Ref<PBCtx> &presburger, int iterDim,
const std::string &ext1, const std::string &ext2);

PBMap makeConstraintOfSingleLoop(PBCtx &presburger, const ID &loop,
DepDirection mode, int iterDim);
PBMap makeConstraintOfSingleLoop(const Ref<PBCtx> &presburger,
const ID &loop, DepDirection mode,
int iterDim);

PBMap makeConstraintOfParallelScope(PBCtx &presburger,
PBMap makeConstraintOfParallelScope(const Ref<PBCtx> &presburger,
const ParallelScope &parallel,
DepDirection mode, int iterDim,
const AccessPoint &later,
Expand All @@ -415,14 +415,14 @@ class AnalyzeDeps {
*
* There will be no dependences of a[0] across i
*/
PBMap makeEraseVarDefConstraint(PBCtx &presburger,
PBMap makeEraseVarDefConstraint(const Ref<PBCtx> &presburger,
const Ref<AccessPoint> &point, int iterDim);

/**
* Constraint for loops that explicitly marked as no_deps by users
*/
PBMap makeNoDepsConstraint(PBCtx &presburger, const std::string &var,
int iterDim);
PBMap makeNoDepsConstraint(const Ref<PBCtx> &presburger,
const std::string &var, int iterDim);

/**
* Constraint for external variables inside loop
Expand All @@ -438,7 +438,7 @@ class AnalyzeDeps {
* idx[i] + j must be different for the same i but different j, but
* idx[i] + j may be the same for different i
*/
PBMap makeExternalVarConstraint(PBCtx &presburger,
PBMap makeExternalVarConstraint(const Ref<PBCtx> &presburger,
const Ref<AccessPoint> &later,
const Ref<AccessPoint> &earlier,
const GenPBExpr::VarMap &laterExternals,
Expand All @@ -460,13 +460,15 @@ class AnalyzeDeps {
* FindDepsMode::Dep mode, we do not care about the result. Therefore, we
* project out these dimensions
*/
PBMap projectOutPrivateAxis(PBCtx &presburger, int iterDim, int since);
void projectOutPrivateAxis(PBCtx &presburger, const Ref<AccessPoint> &point,
PBMap projectOutPrivateAxis(const Ref<PBCtx> &presburger, int iterDim,
int since);
void projectOutPrivateAxis(const Ref<PBCtx> &presburger,
const Ref<AccessPoint> &point,
const std::vector<Ref<AccessPoint>> &otherList,
std::vector<PBMap> &otherMapList, int iterDim);
int numCommonDims(const Ref<AccessPoint> &p1, const Ref<AccessPoint> &p2);

void checkAgainstCond(PBCtx &presburger, const Ref<AccessPoint> &later,
void checkAgainstCond(const Ref<AccessPoint> &later,
const Ref<AccessPoint> &earlier, const PBMap &depAll,
const PBMap &nearest, const PBMap &laterMap,
const PBMap &earlierMap, const PBMap &extConstraint,
Expand All @@ -485,7 +487,8 @@ class AnalyzeDeps {
checkDepLatestEarlier(const Ref<AccessPoint> &later,
const std::vector<Ref<AccessPoint>> &earlierList);
void
checkDepLatestEarlierImpl(PBCtx &presburger, const Ref<AccessPoint> &later,
checkDepLatestEarlierImpl(const Ref<PBCtx> &presburger,
const Ref<AccessPoint> &later,
const std::vector<Ref<AccessPoint>> &earlierList);

/**
Expand All @@ -498,7 +501,7 @@ class AnalyzeDeps {
void checkDepEarliestLater(const std::vector<Ref<AccessPoint>> &laterList,
const Ref<AccessPoint> &earlier);
void
checkDepEarliestLaterImpl(PBCtx &presburger,
checkDepEarliestLaterImpl(const Ref<PBCtx> &presburger,
const std::vector<Ref<AccessPoint>> &laterList,
const Ref<AccessPoint> &earlier);
};
Expand Down
21 changes: 12 additions & 9 deletions include/math/parse_pb_expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,37 @@ typedef std::vector<SimplePBFuncAST> PBFuncAST;

/**
* Parse a PBFunc to be ASTs
*
* @{
*/
PBFuncAST parsePBFunc(const std::string &str);
PBFuncAST parsePBFunc(const PBFunc::Serialized &f);
PBFuncAST parsePBFunc(const PBSingleFunc::Serialized &f);
/** @} */

/**
* Construct AST from PBSet while preserving min and max with a special hack to
* ISL
*
* @{
*/
PBFuncAST parsePBFuncReconstructMinMax(const PBCtx &ctx, const PBSet &set);
PBFuncAST parsePBFuncReconstructMinMax(const PBCtx &ctx, const PBMap &map);
PBFuncAST parsePBFuncReconstructMinMax(const PBSet &set);
PBFuncAST parsePBFuncReconstructMinMax(const PBMap &map);
/** @} */

/**
* Parse a PBFunc to be ASTs, but only restricted to one contiguous factor
*
* @{
*/
inline SimplePBFuncAST parseSimplePBFunc(const std::string &str) {
auto ret = parsePBFunc(str);
inline SimplePBFuncAST parseSimplePBFunc(const auto &f) {
auto ret = parsePBFunc(f);
if (ret.size() != 1) {
throw ParserError(str + " is not a simple PBFunc");
throw ParserError(FT_MSG << f << " is not a simple PBFunc");
}
return ret.front();
}
inline SimplePBFuncAST parseSimplePBFuncReconstructMinMax(const PBCtx &ctx,
const auto &f) {
auto ret = parsePBFuncReconstructMinMax(ctx, f);
inline SimplePBFuncAST parseSimplePBFuncReconstructMinMax(const auto &f) {
auto ret = parsePBFuncReconstructMinMax(f);
if (ret.size() != 1) {
throw ParserError(FT_MSG << f << " is not a simple PBFunc");
}
Expand Down
Loading

0 comments on commit cd26131

Please sign in to comment.