|
| 1 | +#include <mhlo/IR/hlo_ops.h> |
| 2 | +#include <mlir/Dialect/Func/IR/FuncOps.h> |
| 3 | +#include <mlir/IR/BuiltinDialect.h> |
| 4 | +#include <mlir/IR/Matchers.h> |
| 5 | +#include <mlir/IR/PatternMatch.h> |
| 6 | +#include <mlir/Pass/Pass.h> |
| 7 | +#include <mlir/Transforms/DialectConversion.h> |
| 8 | + |
| 9 | +#include <llvm/Support/Format.h> |
| 10 | +#include <llvm/Support/raw_ostream.h> |
| 11 | +#include <mlir/Analysis/CallGraph.h> |
| 12 | +#include <mlir/IR/Action.h> |
| 13 | +#include <mlir/IR/Builders.h> |
| 14 | +#include <mlir/IR/MLIRContext.h> |
| 15 | +#include <mlir/IR/PatternMatch.h> |
| 16 | +#include <mlir/Parser/Parser.h> |
| 17 | +#include <mlir/Pass/PassManager.h> |
| 18 | +#include <mlir/Transforms/GreedyPatternRewriteDriver.h> |
| 19 | +#include <mlir/Transforms/Passes.h> |
| 20 | + |
| 21 | +#include "passes/Pow2.h" |
| 22 | +#include "llvm/Support/Casting.h" |
| 23 | +#include "llvm/Support/Debug.h" |
| 24 | + |
| 25 | +#include <memory> |
| 26 | +#include <numeric> |
| 27 | + |
| 28 | +using namespace mlir; |
| 29 | + |
| 30 | +namespace { |
| 31 | +// We add MLIR actions here as an example. |
| 32 | +/// A custom Action can be defined minimally by deriving from |
| 33 | +/// `tracing::ActionImpl`. The action is same as the pass declaration with tddr |
| 34 | +/// rules. only for `xxx-opt` binary. and run with `--log-actions-to=-` to dump |
| 35 | +/// the actions. |
| 36 | +class EchoAction : public tracing::ActionImpl<EchoAction> { |
| 37 | +public: |
| 38 | + using Base = tracing::ActionImpl<EchoAction>; |
| 39 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(EchoAction) |
| 40 | + |
| 41 | + /// Actions are initialized with an array of IRUnit (that is either Operation, |
| 42 | + /// Block, or Region) that provide context for the IR affected by a |
| 43 | + /// transformation. |
| 44 | + EchoAction(ArrayRef<IRUnit> irUnits, int iteration) |
| 45 | + : Base(irUnits), iteration(iteration) {} |
| 46 | + /// This tag should uniquely identify this action, it can be matched for |
| 47 | + /// filtering during processing. |
| 48 | + static constexpr StringLiteral tag = "echo-action"; |
| 49 | + static constexpr StringLiteral desc = "Just echo the iteration"; |
| 50 | + |
| 51 | + void print(raw_ostream &os) const override { |
| 52 | + os << "EchoAction: " << iteration << "\n"; |
| 53 | + } |
| 54 | + |
| 55 | +private: |
| 56 | + int iteration; |
| 57 | +}; |
| 58 | +} // namespace |
| 59 | + |
| 60 | +namespace { |
| 61 | +bool ValueEql2(Value operand) { |
| 62 | + FloatAttr::ValueType FValue = FloatAttr::ValueType(2.0); |
| 63 | + if (matchPattern(operand, m_ConstantFloat(&FValue))) { |
| 64 | + if (FValue.convertToFloat() == 2.0) { |
| 65 | + return true; |
| 66 | + } |
| 67 | + } |
| 68 | + return false; |
| 69 | +} |
| 70 | + |
| 71 | +static LogicalResult Eqn2Impl(PatternRewriter &rewriter, Value value) { |
| 72 | + return success(ValueEql2(value)); |
| 73 | +} |
| 74 | + |
| 75 | +} // namespace |
| 76 | + |
| 77 | +void registerNativeConstraints(RewritePatternSet &patterns) { |
| 78 | + patterns.getPDLPatterns().registerConstraintFunction("Eqn2", Eqn2Impl); |
| 79 | +} |
| 80 | + |
| 81 | +namespace { |
| 82 | +/// Include the patterns defined in the Declarative Rewrite framework. |
| 83 | +#include "Pow2.inc" |
| 84 | +#include "Pow2Pdll.inc" |
| 85 | +} // namespace |
| 86 | + |
| 87 | +namespace { |
| 88 | +struct SubstitutePow2Pass |
| 89 | + : public PassWrapper<SubstitutePow2Pass, OperationPass<func::FuncOp>> { |
| 90 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SubstitutePow2Pass) |
| 91 | + |
| 92 | + void runOnOperation() final; |
| 93 | +}; |
| 94 | +} // namespace |
| 95 | + |
| 96 | +void SubstitutePow2Pass::runOnOperation() { |
| 97 | + auto op = getOperation(); |
| 98 | + RewritePatternSet patterns(&getContext()); |
| 99 | + patterns.add<Pow2OptPattern>(&getContext()); |
| 100 | + if (failed(applyPatternsAndFoldGreedily(op, std::move(patterns)))) |
| 101 | + signalPassFailure(); |
| 102 | +} |
| 103 | + |
| 104 | +namespace { |
| 105 | +struct SubstitutePow2PdllPass |
| 106 | + : public PassWrapper<SubstitutePow2PdllPass, OperationPass<func::FuncOp>> { |
| 107 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(SubstitutePow2PdllPass) |
| 108 | + |
| 109 | + void runOnOperation() final; |
| 110 | +}; |
| 111 | +} // namespace |
| 112 | + |
| 113 | +void SubstitutePow2PdllPass::runOnOperation() { |
| 114 | + auto op = getOperation(); |
| 115 | + RewritePatternSet patterns(&getContext()); |
| 116 | + // --- insert the native constraints --- |
| 117 | + registerNativeConstraints(patterns); |
| 118 | + // --- insert the native constraints --- |
| 119 | + patterns.add<Pow2PdllOptPattern>(&getContext()); |
| 120 | + if (failed(applyPatternsAndFoldGreedily(op, std::move(patterns)))) |
| 121 | + signalPassFailure(); |
| 122 | +} |
| 123 | + |
| 124 | +namespace { |
| 125 | +#define GEN_PASS_DEF_POW2PASS |
| 126 | +#include "Pow2Pass.inc" |
| 127 | +} // namespace |
| 128 | + |
| 129 | +namespace { |
| 130 | +struct SubstitutePow2PdllGenPass |
| 131 | + : impl::Pow2PassBase<SubstitutePow2PdllGenPass> { |
| 132 | + using Pow2PassBase::Pow2PassBase; |
| 133 | + |
| 134 | + void runOnOperation() final { |
| 135 | + auto op = getOperation(); |
| 136 | + MLIRContext *context = &getContext(); |
| 137 | + RewritePatternSet patterns(&*context); |
| 138 | + // --- insert the native constraints --- |
| 139 | + registerNativeConstraints(patterns); |
| 140 | + // --- insert the native constraints --- |
| 141 | + patterns.add<Pow2PdllOptPattern>(&*context); |
| 142 | + // Here, we wrap the applyPatternsAndFoldGreedily in a lambda function and |
| 143 | + // pass it to the MLIR Action. |
| 144 | + Operation *opp = getOperation(); |
| 145 | + ArrayRef<IRUnit> irUnits{opp}; |
| 146 | + context->executeAction<EchoAction>( |
| 147 | + [&]() { |
| 148 | + // Here is the pass body. |
| 149 | + if (failed(applyPatternsAndFoldGreedily(op, std::move(patterns)))) |
| 150 | + signalPassFailure(); |
| 151 | + }, |
| 152 | + /*irUnits=*/irUnits, /*iteration=*/10); |
| 153 | + // Above, we pass the irUnits and iteration to the EchoAction. |
| 154 | + statistic++; |
| 155 | + }; |
| 156 | +}; |
| 157 | +} // namespace |
| 158 | + |
| 159 | +std::unique_ptr<mlir::Pass> mhlo::createSubstitutePow2Pass() { |
| 160 | + // There are 2 methods to achieve the same goal: |
| 161 | + // 1. use the tddr rules to rewrite the IR |
| 162 | + // return std::make_unique<SubstitutePow2Pass>(); |
| 163 | + // 2. use the pdll to rewrite the IR |
| 164 | + // return std::make_unique<SubstitutePow2PdllPass>(); |
| 165 | + // 3. use tddr to generate pass declaration. |
| 166 | + return std::make_unique<SubstitutePow2PdllGenPass>(); |
| 167 | +} |
| 168 | + |
| 169 | +/// An interesting analysis. |
| 170 | +struct StaticOpCounterAnalysis { |
| 171 | + llvm::StringMap<int> opCount; |
| 172 | + // Compute this analysis with the provided operation. |
| 173 | + StaticOpCounterAnalysis(Operation *op) : opCount({}){}; |
| 174 | + |
| 175 | + void add(Operation *op) { |
| 176 | + auto opName = op->getName().getStringRef(); |
| 177 | + opCount.find(opName) == opCount.end() |
| 178 | + ? opCount[opName] = 1 |
| 179 | + : opCount[opName] = opCount[opName] + 1; |
| 180 | + } |
| 181 | + |
| 182 | + llvm::StringMap<int> getOpCount() const { return opCount; }; |
| 183 | +}; |
| 184 | + |
| 185 | +struct StaticOpCounterAnalysisWithDependency { |
| 186 | + StaticOpCounterAnalysisWithDependency(Operation *op, AnalysisManager &am) { |
| 187 | + // Request other analysis as dependency |
| 188 | + StaticOpCounterAnalysis &otherAnalysis = |
| 189 | + am.getAnalysis<StaticOpCounterAnalysis>(); |
| 190 | + } |
| 191 | + |
| 192 | + bool isInvalidated(const AnalysisManager::PreservedAnalyses &pa) { |
| 193 | + // Check if analysis or its dependency were invalidated |
| 194 | + return !pa.isPreserved<StaticOpCounterAnalysisWithDependency>() || |
| 195 | + !pa.isPreserved<StaticOpCounterAnalysis>(); |
| 196 | + } |
| 197 | +}; |
| 198 | + |
| 199 | +namespace { |
| 200 | +struct StaticOpCounter |
| 201 | + : public PassWrapper<StaticOpCounter, OperationPass<func::FuncOp>> { |
| 202 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(StaticOpCounter) |
| 203 | + |
| 204 | + void runOnOperation() final; |
| 205 | +}; |
| 206 | +} // namespace |
| 207 | + |
| 208 | +void StaticOpCounter::runOnOperation() { |
| 209 | + StaticOpCounterAnalysis &myAnalysis = getAnalysis<StaticOpCounterAnalysis>(); |
| 210 | + auto module_op = getOperation(); |
| 211 | + for (auto &op : module_op.getOps()) { |
| 212 | + myAnalysis.add(&op); |
| 213 | + } |
| 214 | + |
| 215 | + const char *str1 = "NAME"; |
| 216 | + const char *str2 = "#N DIRECT CALLS"; |
| 217 | + |
| 218 | + llvm::dbgs() << "=================================================" |
| 219 | + << "\n"; |
| 220 | + llvm::dbgs() << "MLIR-PASS-TUTOR: static analysis results\n"; |
| 221 | + llvm::dbgs() << "=================================================\n"; |
| 222 | + llvm::dbgs() << llvm::format("%-20s %-10s\n", str1, str2); |
| 223 | + llvm::dbgs() << "-------------------------------------------------" |
| 224 | + << "\n"; |
| 225 | + for (auto &CallCount : myAnalysis.getOpCount()) { |
| 226 | + llvm::dbgs() << llvm::format("%-20s %-10lu\n", |
| 227 | + CallCount.first().str().c_str(), |
| 228 | + CallCount.getValue()); |
| 229 | + } |
| 230 | + |
| 231 | + llvm::dbgs() << "-------------------------------------------------" |
| 232 | + << "\n\n"; |
| 233 | +} |
| 234 | + |
| 235 | +std::unique_ptr<mlir::Pass> mhlo::createStaticOpCounter() { |
| 236 | + // There are 2 methods to achieve the same goal: |
| 237 | + // 1. use the tddr rules to rewrite the IR |
| 238 | + // return std::make_unique<SubstitutePow2Pass>(); |
| 239 | + // 2. use the pdll to rewrite the IR |
| 240 | + // return std::make_unique<SubstitutePow2PdllPass>(); |
| 241 | + // 3. use tddr to generate pass declaration. |
| 242 | + return std::make_unique<StaticOpCounter>(); |
| 243 | +} |
0 commit comments