1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
|
//===- CodegenEnv.cpp - Code generation environment class ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "CodegenEnv.h"
#include "mlir/Dialect/Bufferization/IR/Bufferization.h"
#include "mlir/Dialect/Linalg/Utils/Utils.h"
#include "mlir/Dialect/SparseTensor/IR/SparseTensorType.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include <optional>
using namespace mlir;
using namespace mlir::sparse_tensor;
//===----------------------------------------------------------------------===//
// Code generation environment helper functions
//===----------------------------------------------------------------------===//
/// Returns true if tensor materializes uninitialized into the computation.
static bool isMaterializing(Value val) {
return val.getDefiningOp<tensor::EmptyOp>() ||
val.getDefiningOp<bufferization::AllocTensorOp>();
}
/// Sorts the dependent loops such that it is ordered in the same sequence in
/// which loops will be generated.
static void sortDependentLoops(std::vector<LoopCoeffPair> &target) {
std::sort(target.begin(), target.end(),
[](const LoopCoeffPair &l, const LoopCoeffPair &r) {
assert(std::addressof(l) == std::addressof(r) || l != r);
return l.first < r.first;
});
}
//===----------------------------------------------------------------------===//
// Code generation environment constructor and general methods
//===----------------------------------------------------------------------===//
CodegenEnv::CodegenEnv(linalg::GenericOp linop, SparsificationOptions opts,
unsigned numTensors, unsigned numLoops, unsigned maxRank)
: linalgOp(linop), sparseOptions(opts),
latticeMerger(numTensors, numLoops, maxRank), loopEmitter(),
sparseOut(nullptr), outerParNest(-1u), insChain(), expValues(),
expFilled(), expAdded(), expCount(), redVal(), redExp(detail::kInvalidId),
redCustom(detail::kInvalidId), redValidLexInsert() {}
LogicalResult CodegenEnv::initTensorExp() {
// Builds the tensor expression for the Linalg operation in SSA form.
std::optional<ExprId> optExp = latticeMerger.buildTensorExpFromLinalg(op());
if (!optExp || !isAdmissibleTensorExp(*optExp))
return failure();
tensorExp = *optExp;
return success();
}
void CodegenEnv::startEmit(SparseEmitStrategy emitStrategy) {
assert(insChain == nullptr && "must only start emitting once");
if (sparseOut) {
insChain = sparseOut->get();
latticeMerger.setHasSparseOut(true);
}
// Sort the related loop array such that they are in the same order as they
// appears on the topoOrder.
// TODO: since we only handle affine addition for slice based codegen, and
// addition is assoicative, the order how we evaluate the expression does
// not matter. However, to support multiplication, the order of the loop
// index should match the evaluation order to the affine expression AST.
// Initialize loop emitter.
SmallVector<Value> tensors; // input tensors passed to loop emitter
for (OpOperand &t : linalgOp->getOpOperands()) {
tensors.push_back(t.get());
const TensorId tid = makeTensorId(t.getOperandNumber());
const Level lvlRank = linalgOp.getMatchingIndexingMap(&t).getNumResults();
const auto enc = getSparseTensorEncoding(t.get().getType());
(void)enc;
assert(!enc || lvlRank == enc.getLvlRank());
for (Level lvl = 0; lvl < lvlRank; lvl++)
sortDependentLoops(latticeMerger.getDependentLoops(tid, lvl));
}
loopEmitter.initialize(
tensors,
StringAttr::get(linalgOp.getContext(),
linalg::GenericOp::getOperationName()),
/*hasOutput=*/true,
/*isSparseOut=*/sparseOut != nullptr, /*numLoops=*/getLoopNum(),
// TODO: compute the map and pass it to loop emitter directly instead of
// passing in a callback.
/*dependentLvlGetter=*/
[this](TensorId t, Level lvl) -> std::vector<LoopCoeffPair> {
return merger().getDependentLoops(t, lvl);
},
emitStrategy);
}
std::optional<Operation *> CodegenEnv::genLoopBoundary(
function_ref<std::optional<Operation *>(MutableArrayRef<Value> parameters)>
callback) {
SmallVector<Value> params;
if (isReduc()) {
params.push_back(redVal);
if (isValidLexInsert())
params.push_back(redValidLexInsert);
} else {
assert(!isValidLexInsert());
}
if (isExpand())
params.push_back(expCount);
if (insChain != nullptr)
params.push_back(insChain);
auto r = callback(params); // may update parameters
unsigned i = 0;
if (isReduc()) {
updateReduc(params[i++]);
if (isValidLexInsert())
updateValidLexInsert(params[i++]);
}
if (isExpand())
updateExpandCount(params[i++]);
if (insChain != nullptr)
updateInsertionChain(params[i]);
return r;
}
//===----------------------------------------------------------------------===//
// Code generation environment verify functions.
//===----------------------------------------------------------------------===//
bool CodegenEnv::isAdmissibleTensorExp(ExprId exp) {
// We reject any expression that makes a reduction from `-outTensor`, as those
// expressions create a dependency between the current iteration (i) and the
// previous iteration (i-1). It would require iterating over the whole
// coordinate space, which prevent exploiting sparsity for faster code.
for (utils::IteratorType it : linalgOp.getIteratorTypesArray()) {
if (it == utils::IteratorType::reduction) {
if (latticeMerger.hasNegateOnOut(exp))
return false;
break;
}
}
OpOperand *lhs = linalgOp.getDpsInitOperand(0);
const TensorId tensor = makeTensorId(lhs->getOperandNumber());
// An non-annotated output tensor is assumed dense, and becomes a random
// access n-dim memref. Admissible since insertions cannot occur.
if (getSparseTensorType(lhs->get()).isAllDense())
return true;
// A tensor expression with a sparse output tensor that changes its values
// but not its nonzero structure, an operation called "simply dynamic" in
// [Bik96,Ch9], is also admissible without special env.
if (latticeMerger.isSingleCondition(tensor, exp))
return true;
// Accept "truly dynamic" if the output tensor materializes uninitialized
// into the computation and insertions occur in lexicographic index order.
sparseOut = lhs;
// Find the outermost parallel nest to determine whether compress/expand is
// needed.
outerParNest = 0;
const auto iteratorTypes = linalgOp.getIteratorTypesArray();
for (unsigned i = 0, e = getLoopNum(); i < e; i++) {
if (linalg::isReductionIterator(iteratorTypes[i]))
break; // terminate at first reduction
outerParNest++;
}
// Inadmissible kernel should have already been rejected by the previous
// path during loop scheduling.
assert(static_cast<int64_t>(outerParNest) >=
linalgOp.getRank(linalgOp.getDpsInitOperand(0)) - 1);
return isMaterializing(lhs->get());
}
//===----------------------------------------------------------------------===//
// Code generation environment topological sort methods
//===----------------------------------------------------------------------===//
Value CodegenEnv::getLoopVar(LoopId i) const {
return loopEmitter.getLoopIV(i);
}
//===----------------------------------------------------------------------===//
// Code generation environment sparse tensor output and expansion methods
//===----------------------------------------------------------------------===//
void CodegenEnv::updateInsertionChain(Value chain) {
assert(sparseOut != nullptr && insChain != nullptr);
insChain = chain;
}
bool CodegenEnv::atExpandLevel(OpOperand *o, unsigned rank, LoopId n) const {
return sparseOut == o && outerParNest == static_cast<LoopId>(rank - 1) &&
outerParNest == n;
}
void CodegenEnv::startExpand(Value values, Value filled, Value added,
Value count) {
assert(sparseOut != nullptr && expValues == nullptr);
expValues = values;
expFilled = filled;
expAdded = added;
expCount = count;
}
void CodegenEnv::updateExpandCount(Value count) {
assert(sparseOut != nullptr && expValues != nullptr);
expCount = count;
}
void CodegenEnv::endExpand() {
assert(sparseOut != nullptr && expValues != nullptr);
expValues = expFilled = expAdded = expCount = Value();
}
//===----------------------------------------------------------------------===//
// Code generation environment reduction methods
//===----------------------------------------------------------------------===//
void CodegenEnv::startReduc(ExprId exp, Value val) {
assert(!isReduc() && exp != detail::kInvalidId && val);
redExp = exp;
redVal = val;
latticeMerger.setExprValue(exp, val);
}
void CodegenEnv::updateReduc(Value val) {
assert(isReduc() && val);
redVal = val;
latticeMerger.clearExprValue(redExp);
latticeMerger.setExprValue(redExp, val);
}
Value CodegenEnv::endReduc() {
assert(isReduc());
Value val = redVal;
redVal = val;
latticeMerger.clearExprValue(redExp);
redExp = detail::kInvalidId;
return val;
}
void CodegenEnv::startValidLexInsert(Value val) {
assert(!isValidLexInsert() && isReduc() && val);
redValidLexInsert = val;
}
void CodegenEnv::updateValidLexInsert(Value val) {
assert(redValidLexInsert && isReduc() && val);
redValidLexInsert = val;
}
void CodegenEnv::endValidLexInsert() {
assert(isValidLexInsert() && !isReduc());
redValidLexInsert = Value();
}
void CodegenEnv::startCustomReduc(ExprId exp) {
assert(!isCustomReduc() && exp != detail::kInvalidId);
redCustom = exp;
}
Value CodegenEnv::getCustomRedId() const {
assert(isCustomReduc());
return dyn_cast<sparse_tensor::ReduceOp>(exp(redCustom).op).getIdentity();
}
void CodegenEnv::endCustomReduc() {
assert(isCustomReduc());
redCustom = detail::kInvalidId;
}
|