34 std::sort(target.begin(), target.end(),
36 assert(std::addressof(l) == std::addressof(r) || l != r);
37 return l.first < r.first;
45 unsigned numTensors,
unsigned numLoops,
unsigned maxRank)
46 : linalgOp(linop), sparseOptions(opts),
47 latticeMerger(numTensors, numLoops, maxRank), loopEmitter(),
48 sparseOut(nullptr), outerParNest(-1u), insChain(), expValues(),
49 expFilled(), expAdded(), expCount(), redVal(), redExp(detail::
kInvalidId),
50 redCustom(detail::
kInvalidId), redValidLexInsert() {}
63 assert(insChain ==
nullptr &&
"must only start emitting once");
65 insChain = sparseOut->
get();
78 for (
OpOperand &t : linalgOp->getOpOperands()) {
79 tensors.push_back(t.get());
81 const Level lvlRank = linalgOp.getMatchingIndexingMap(&t).getNumResults();
84 assert(!enc || lvlRank == enc.getLvlRank());
85 for (
Level lvl = 0; lvl < lvlRank; lvl++)
92 linalg::GenericOp::getOperationName()),
99 Level lvl) -> std::vector<std::pair<TensorLevel, unsigned>> {
102 std::vector<std::pair<TensorLevel, unsigned>> ret;
103 ret.reserve(rLoops.size());
104 for (
auto [loop, coeff] : rLoops) {
106 ret.emplace_back(tl, coeff);
117 params.push_back(redVal);
118 if (redValidLexInsert)
119 params.push_back(redValidLexInsert);
121 assert(!redValidLexInsert);
124 params.push_back(expCount);
125 if (insChain !=
nullptr)
126 params.push_back(insChain);
127 auto r = callback(params);
131 if (redValidLexInsert)
136 if (insChain !=
nullptr)
150 for (utils::IteratorType it : linalgOp.getIteratorTypesArray()) {
151 if (it == utils::IteratorType::reduction) {
158 OpOperand *lhs = linalgOp.getDpsInitOperand(0);
178 const auto iteratorTypes = linalgOp.getIteratorTypesArray();
179 for (
unsigned i = 0, e =
getLoopNum(); i < e; i++) {
187 assert(
static_cast<int64_t
>(outerParNest) >=
188 linalgOp.getRank(linalgOp.getDpsInitOperand(0)) - 1);
205 assert(sparseOut !=
nullptr && insChain !=
nullptr);
211 return sparseOut == o && outerParNest ==
static_cast<LoopOrd>(rank - 1) &&
217 assert(sparseOut !=
nullptr && expValues ==
nullptr);
225 assert(sparseOut !=
nullptr && expValues !=
nullptr);
230 assert(sparseOut !=
nullptr && expValues !=
nullptr);
231 expValues = expFilled = expAdded = expCount =
Value();
263 redValidLexInsert = val;
268 redValidLexInsert =
Value();
278 return dyn_cast<sparse_tensor::ReduceOp>(
exp(redCustom).
op).getIdentity();
static bool isMaterializing(Value val)
Returns true if tensor materializes uninitialized into the computation.
static void sortDependentLoops(std::vector< LoopCoeffPair > &target)
Sorts the dependent loops such that it is ordered in the same sequence in which loops will be generat...
IRValueT get() const
Return the current value being used by this operand.
This class represents an operand of an operation.
unsigned getOperandNumber()
Return which operand this is in the OpOperand list of the Operation.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Operation * getDefiningOp() const
If this value is the result of an operation, return the operation that defines it.
void startReduc(ExprId exp, Value val)
std::optional< Operation * > genLoopBoundary(function_ref< std::optional< Operation * >(MutableArrayRef< Value > parameters)> callback)
Generates loop boundary statements (entering/exiting loops).
bool isAdmissibleTensorExp(ExprId e)
Whether the tensor expression is admissible for codegen.
bool isCustomReduc() const
CodegenEnv(linalg::GenericOp linop, SparsificationOptions opts, unsigned numTensors, unsigned numLoops, unsigned maxRank)
Constructs a code generation environment which can be passed around during sparsification for bookkee...
TensorLevel makeTensorLevel(TensorId t, Level l) const
constexpr TensorId makeTensorId(unsigned t) const
void startExpand(Value values, Value filled, Value added, Value count)
void clearValidLexInsert()
bool atExpandLevel(OpOperand *o, unsigned rank, LoopOrd n) const
unsigned getLoopNum() const
void updateInsertionChain(Value chain)
void startCustomReduc(ExprId exp)
linalg::GenericOp op() const
Value getLoopVar(LoopId i) const
Returns the induction-variable for the loop identified by the given LoopId.
LogicalResult initTensorExp()
void setValidLexInsert(Value val)
const TensorExp & exp(ExprId e) const
void updateExpandCount(Value count)
void updateReduc(Value val)
void initialize(ValueRange tensors, StringAttr loopTag=nullptr, bool hasOutput=false, bool isSparseOut=false, unsigned numLoops=0, DependentLvlGetter getter=nullptr)
Takes an array of input tensors, which the generated loops will iterate over.
Value getLoopIV(LoopOrd n) const
Gets loop induction variable for the given LoopOrd.
void setHasSparseOut(bool s)
Sets whether the output tensor is sparse or not.
bool isSingleCondition(TensorId t, ExprId e) const
Returns true if given tensor iterates only in the given tensor expression.
bool hasNegateOnOut(ExprId e) const
Returns true if the expression contains a negation on output tensor.
std::optional< ExprId > buildTensorExpFromLinalg(linalg::GenericOp op)
Builds a tensor expression from the given Linalg operation.
void clearExprValue(ExprId e)
Clears the value associated with the expression.
std::vector< LoopCoeffPair > & getDependentLoops(TensorId t, Level lvl)
Returns the list of loop indices which appear in the non-trivial index expression on t_l,...
void setExprValue(ExprId e, Value v)
Sets the expression to have the associated value.
bool isAllDense() const
Returns true for tensors where every level is dense.
bool isReductionIterator(utils::IteratorType iteratorType)
Check if iterator type has "reduction" semantics.
static constexpr unsigned kInvalidId
A constant serving as the canonically invalid identifier, regardless of the identifier type.
uint64_t Level
The type of level identifiers and level-ranks.
unsigned LoopId
Loop identifiers.
SparseTensorEncodingAttr getSparseTensorEncoding(Type type)
Convenience method to get a sparse encoding attribute from a type.
unsigned LoopOrd
The position of a loop in the loop-stack, or the position of a LoopId in a topologically-sorted list ...
SparseTensorType getSparseTensorType(Value val)
Convenience methods to obtain a SparseTensorType from a Value.
unsigned ExprId
TensorExp identifiers.
std::pair< LoopId, unsigned > LoopCoeffPair
A pair of loop id and its coefficients.
unsigned TensorId
Tensor identifiers, chosen to be the BlockArgument::getArgNumber of the value passed to Merger::build...
Include the generated interface declarations.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
This class represents an efficient way to signal success or failure.
Options for the Sparsification pass.