|
| 1 | +#include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h" |
| 2 | +#include "mlir/Analysis/DataFlow/SparseAnalysis.h" |
| 3 | +#include "mlir/Analysis/DataFlowFramework.h" |
| 4 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 5 | +#include "mlir/Dialect/Linalg/IR/Linalg.h" |
| 6 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 7 | +#include "mlir/IR/Block.h" |
| 8 | +#include "mlir/IR/BuiltinAttributes.h" |
| 9 | +#include "mlir/IR/BuiltinTypes.h" |
| 10 | +#include "mlir/IR/MLIRContext.h" |
| 11 | +#include "mlir/IR/Operation.h" |
| 12 | +#include "mlir/IR/Value.h" |
| 13 | +#include "mlir/Pass/Pass.h" |
| 14 | +#include "mlir/Support/LLVM.h" |
| 15 | + |
| 16 | +using namespace mlir; |
| 17 | +using namespace mlir::dataflow; |
| 18 | + |
| 19 | +enum class ResidencyKind { |
| 20 | + Uninitialized, // 由框架隐式表示,通常不单独存 |
| 21 | + Unknown, // 什么都不知道 |
| 22 | + DDR, // 主存/外存 |
| 23 | + FastMem, // L1/L2/shared/local SRAM |
| 24 | + Conflict // 来自不同来源且无法统一 |
| 25 | +}; |
| 26 | + |
| 27 | +struct ResidencyValue { |
| 28 | + enum Kind { Unknown, DDR, FastMem, Conflict } kind = Unknown; |
| 29 | + |
| 30 | + static ResidencyValue getPessimisticValueState(MLIRContext *) { |
| 31 | + return ResidencyValue{Unknown}; |
| 32 | + } |
| 33 | + |
| 34 | + static ResidencyValue getPessimisticValueState(Value value) { |
| 35 | + if (auto mt = dyn_cast<MemRefType>(value.getType())) { |
| 36 | + if (!mt.getMemorySpace()) |
| 37 | + return ResidencyValue{DDR}; |
| 38 | + |
| 39 | + if (auto i = dyn_cast<IntegerAttr>(mt.getMemorySpace())) { |
| 40 | + if (i.getInt() == 0) |
| 41 | + return ResidencyValue{DDR}; |
| 42 | + if (i.getInt() == 1) |
| 43 | + return ResidencyValue{FastMem}; |
| 44 | + } |
| 45 | + } |
| 46 | + return ResidencyValue{Unknown}; |
| 47 | + } |
| 48 | + |
| 49 | + static ResidencyValue join(const ResidencyValue &lhs, |
| 50 | + const ResidencyValue &rhs) { |
| 51 | + if (lhs.kind == rhs.kind) |
| 52 | + return lhs; |
| 53 | + if (lhs.kind == Unknown) |
| 54 | + return rhs; |
| 55 | + if (rhs.kind == Unknown) |
| 56 | + return lhs; |
| 57 | + return ResidencyValue{Conflict}; |
| 58 | + } |
| 59 | + |
| 60 | + bool operator==(const ResidencyValue &rhs) const { return kind == rhs.kind; } |
| 61 | + |
| 62 | + void print(raw_ostream &os) const { |
| 63 | + switch (kind) { |
| 64 | + case Unknown: |
| 65 | + os << "unknown"; |
| 66 | + break; |
| 67 | + case DDR: |
| 68 | + os << "ddr"; |
| 69 | + break; |
| 70 | + case FastMem: |
| 71 | + os << "fastmem"; |
| 72 | + break; |
| 73 | + case Conflict: |
| 74 | + os << "conflict"; |
| 75 | + break; |
| 76 | + } |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +using ResidencyLattice = Lattice<ResidencyValue>; |
| 81 | + |
| 82 | +class ResidencyAnalysis |
| 83 | + : public SparseForwardDataFlowAnalysis<ResidencyLattice> { |
| 84 | +public: |
| 85 | + using SparseForwardDataFlowAnalysis< |
| 86 | + ResidencyLattice>::SparseForwardDataFlowAnalysis; |
| 87 | + |
| 88 | + LogicalResult visitOperation(Operation *op, |
| 89 | + ArrayRef<const ResidencyLattice *> operands, |
| 90 | + ArrayRef<ResidencyLattice *> results) override { |
| 91 | + |
| 92 | + // Rule 1: memref.alloc |
| 93 | + if (auto alloc = dyn_cast<memref::AllocOp>(op)) { |
| 94 | + ResidencyValue v = |
| 95 | + ResidencyValue::getPessimisticValueState(alloc.getResult()); |
| 96 | + auto *lattice = getLatticeElement(alloc.getResult()); |
| 97 | + propagateIfChanged(lattice, lattice->join(v)); |
| 98 | + return success(); |
| 99 | + } |
| 100 | + |
| 101 | + // Rule 2: memref.subview / cast / reinterpret_cast |
| 102 | + if (isa<memref::SubViewOp, memref::CastOp, memref::ReinterpretCastOp>(op)) { |
| 103 | + if (op->getNumOperands() >= 1 && op->getNumResults() >= 1) { |
| 104 | + auto *srcLat = operands.front(); |
| 105 | + if (srcLat) |
| 106 | + join(results.front(), *srcLat); |
| 107 | + } |
| 108 | + return success(); |
| 109 | + } |
| 110 | + |
| 111 | + // Rule 3: memref.copy |
| 112 | + if (auto copy = dyn_cast<memref::CopyOp>(op)) { |
| 113 | + Value target = copy.getTarget(); |
| 114 | + auto *lattice = getLatticeElement(target); |
| 115 | + if (const ResidencyLattice *srcLat = operands.front()) |
| 116 | + propagateIfChanged(lattice, lattice->join(srcLat->getValue())); |
| 117 | + |
| 118 | + ResidencyValue dst = ResidencyValue::getPessimisticValueState(target); |
| 119 | + propagateIfChanged(lattice, lattice->join(dst)); |
| 120 | + return success(); |
| 121 | + } |
| 122 | + |
| 123 | + // Rule 4: linalg generic / matmul |
| 124 | + if (isa<linalg::LinalgOp>(op)) { |
| 125 | + auto linalgOp = cast<linalg::LinalgOp>(op); |
| 126 | + for (Value v : linalgOp.getDpsInits()) { |
| 127 | + ResidencyValue rv = ResidencyValue::getPessimisticValueState(v); |
| 128 | + auto *lattice = getLatticeElement(v); |
| 129 | + propagateIfChanged(lattice, lattice->join(rv)); |
| 130 | + } |
| 131 | + return success(); |
| 132 | + } |
| 133 | + |
| 134 | + if (auto msc = dyn_cast<memref::MemorySpaceCastOp>(op)) { |
| 135 | + if (!operands.empty() && operands.front() && !results.empty()) { |
| 136 | + join(results.front(), *operands.front()); |
| 137 | + } |
| 138 | + return success(); |
| 139 | + } |
| 140 | + |
| 141 | + // 默认策略:如果 op 只是透传一个值,可传播第一个操作数状态 |
| 142 | + if (op->getNumOperands() == 1 && op->getNumResults() == 1) { |
| 143 | + if (!operands.empty() && operands.front()) |
| 144 | + join(results.front(), *operands.front()); |
| 145 | + return success(); |
| 146 | + } |
| 147 | + |
| 148 | + setAllToEntryStates(results); |
| 149 | + return success(); |
| 150 | + } |
| 151 | + |
| 152 | +protected: |
| 153 | + void setToEntryState(ResidencyLattice *lattice) override { |
| 154 | + propagateIfChanged(lattice, |
| 155 | + lattice->join(ResidencyValue::getPessimisticValueState( |
| 156 | + lattice->getAnchor()))); |
| 157 | + } |
| 158 | +}; |
| 159 | + |
| 160 | +struct ResidencyAnalysisPass |
| 161 | + : public PassWrapper<ResidencyAnalysisPass, OperationPass<func::FuncOp>> { |
| 162 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(ResidencyAnalysisPass) |
| 163 | + |
| 164 | + static void printMemrefMemoryInfo(Value value, raw_ostream &os, |
| 165 | + DataFlowSolver &solver) { |
| 166 | + os << " value="; |
| 167 | + value.print(os); |
| 168 | + os << " type="; |
| 169 | + value.getType().print(os); |
| 170 | + |
| 171 | + auto memrefType = dyn_cast<MemRefType>(value.getType()); |
| 172 | + if (!memrefType) { |
| 173 | + os << " memory_space=<not-memref>\n"; |
| 174 | + return; |
| 175 | + } |
| 176 | + |
| 177 | + os << " memory_space="; |
| 178 | + Attribute memorySpace = memrefType.getMemorySpace(); |
| 179 | + if (!memorySpace) { |
| 180 | + os << "default"; |
| 181 | + } else if (auto intAttr = dyn_cast<IntegerAttr>(memorySpace)) { |
| 182 | + os << intAttr.getInt(); |
| 183 | + } else { |
| 184 | + memorySpace.print(os); |
| 185 | + } |
| 186 | + os << " residency="; |
| 187 | + solver.lookupState<ResidencyLattice>(value)->getValue().print(os); |
| 188 | + |
| 189 | + if (Operation *defOp = value.getDefiningOp()) |
| 190 | + os << " defined_by=" << defOp->getName(); |
| 191 | + os << "\n"; |
| 192 | + } |
| 193 | + |
| 194 | + static void printLinalgOperandMemoryInfo(linalg::LinalgOp linalgOp, |
| 195 | + DataFlowSolver &solver) { |
| 196 | + llvm::errs() << "linalg op: " << linalgOp->getName() << "\n"; |
| 197 | + llvm::errs() << " ins:\n"; |
| 198 | + for (Value value : linalgOp.getDpsInputs()) |
| 199 | + printMemrefMemoryInfo(value, llvm::errs(), solver); |
| 200 | + |
| 201 | + llvm::errs() << " outs:\n"; |
| 202 | + for (Value value : linalgOp.getDpsInits()) |
| 203 | + printMemrefMemoryInfo(value, llvm::errs(), solver); |
| 204 | + } |
| 205 | + |
| 206 | + static StringRef getResidencyTag(ResidencyValue::Kind kind) { |
| 207 | + switch (kind) { |
| 208 | + case ResidencyValue::Unknown: |
| 209 | + return "unknown"; |
| 210 | + case ResidencyValue::DDR: |
| 211 | + return "ddr"; |
| 212 | + case ResidencyValue::FastMem: |
| 213 | + return "fastmem"; |
| 214 | + case ResidencyValue::Conflict: |
| 215 | + return "conflict"; |
| 216 | + } |
| 217 | + |
| 218 | + llvm_unreachable("unexpected residency kind"); |
| 219 | + } |
| 220 | + |
| 221 | + void runOnOperation() override { |
| 222 | + func::FuncOp func = getOperation(); |
| 223 | + DataFlowSolver solver; |
| 224 | + solver.load<DeadCodeAnalysis>(); |
| 225 | + solver.load<ResidencyAnalysis>(); |
| 226 | + if (failed(solver.initializeAndRun(func))) { |
| 227 | + signalPassFailure(); |
| 228 | + return; |
| 229 | + } |
| 230 | + |
| 231 | + func.walk([&](Operation *op) { |
| 232 | + if (auto linalgOp = dyn_cast<linalg::LinalgOp>(op)) |
| 233 | + printLinalgOperandMemoryInfo(linalgOp, solver); |
| 234 | + |
| 235 | + for (Value result : op->getResults()) { |
| 236 | + if (!isa<MemRefType>(result.getType())) |
| 237 | + continue; |
| 238 | + |
| 239 | + auto *lat = solver.lookupState<ResidencyLattice>(result); |
| 240 | + if (!lat) |
| 241 | + continue; |
| 242 | + |
| 243 | + op->emitRemark() << "result residency = " |
| 244 | + << getResidencyTag(lat->getValue().kind); |
| 245 | + } |
| 246 | + }); |
| 247 | + |
| 248 | + func.emitRemark() << "=== function args ==="; |
| 249 | + for (BlockArgument arg : func.getArguments()) { |
| 250 | + if (!isa<MemRefType>(arg.getType())) |
| 251 | + continue; |
| 252 | + |
| 253 | + if (auto *lat = solver.lookupState<ResidencyLattice>(arg)) { |
| 254 | + llvm::errs() << "arg: "; |
| 255 | + lat->getValue().print(llvm::errs()); |
| 256 | + llvm::errs() << "\n"; |
| 257 | + } |
| 258 | + } |
| 259 | + |
| 260 | + func.walk([&](Operation *op) { |
| 261 | + for (Value result : op->getResults()) { |
| 262 | + if (!isa<MemRefType>(result.getType())) |
| 263 | + continue; |
| 264 | + |
| 265 | + if (auto *lat = solver.lookupState<ResidencyLattice>(result)) { |
| 266 | + llvm::errs() << "op result @" << op->getName() << " : "; |
| 267 | + lat->getValue().print(llvm::errs()); |
| 268 | + llvm::errs() << "\n"; |
| 269 | + } |
| 270 | + } |
| 271 | + }); |
| 272 | + } |
| 273 | +}; |
| 274 | + |
| 275 | +namespace mlir { |
| 276 | +std::unique_ptr<Pass> createResidencyAnalysisPass() { |
| 277 | + return std::make_unique<ResidencyAnalysisPass>(); |
| 278 | +} |
| 279 | +} // namespace mlir |
0 commit comments