|
| 1 | +//===- TestForwardDataFlowAnalysis.cpp - Test dead code analysis ---------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h" |
| 10 | +#include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h" |
| 11 | +#include "mlir/Analysis/DataFlow/SparseAnalysis.h" |
| 12 | +#include "mlir/Dialect/MemRef/IR/MemRef.h" |
| 13 | +#include "mlir/Interfaces/SideEffectInterfaces.h" |
| 14 | +#include "mlir/Pass/Pass.h" |
| 15 | + |
| 16 | +using namespace mlir; |
| 17 | +using namespace mlir::dataflow; |
| 18 | + |
| 19 | +namespace { |
| 20 | + |
| 21 | +class IntegerState { |
| 22 | +public: |
| 23 | + IntegerState() : value(0) {} |
| 24 | + explicit IntegerState(int value) : value(value) {} |
| 25 | + ~IntegerState() = default; |
| 26 | + |
| 27 | + int get() const { return value; } |
| 28 | + |
| 29 | + bool operator==(const IntegerState &rhs) const { return value == rhs.value; } |
| 30 | + |
| 31 | + static IntegerState join(const IntegerState &lhs, const IntegerState &rhs) { |
| 32 | + return IntegerState{std::max(lhs.get(), rhs.get())}; |
| 33 | + } |
| 34 | + |
| 35 | + void print(llvm::raw_ostream &os) const { |
| 36 | + os << "IntegerState(" << value << ")"; |
| 37 | + } |
| 38 | + |
| 39 | + friend llvm::raw_ostream &operator<<(llvm::raw_ostream &os, |
| 40 | + const IntegerState &state) { |
| 41 | + state.print(os); |
| 42 | + return os; |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + int value; |
| 47 | +}; |
| 48 | + |
| 49 | +/// This lattice represents, for a given value, the set of memory resources that |
| 50 | +/// this value, or anything derived from this value, is potentially written to. |
| 51 | +struct IntegerLattice : public Lattice<IntegerState> { |
| 52 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(IntegerLattice) |
| 53 | + using Lattice::Lattice; |
| 54 | +}; |
| 55 | + |
| 56 | +/// An analysis that, by going backwards along the dataflow graph, annotates |
| 57 | +/// each value with all the memory resources it (or anything derived from it) |
| 58 | +/// is eventually written to. |
| 59 | +class IntegerLatticeAnalysis |
| 60 | + : public SparseForwardDataFlowAnalysis<IntegerLattice> { |
| 61 | +public: |
| 62 | + using SparseForwardDataFlowAnalysis::SparseForwardDataFlowAnalysis; |
| 63 | + |
| 64 | + LogicalResult visitOperation(Operation *op, |
| 65 | + ArrayRef<const IntegerLattice *> operands, |
| 66 | + ArrayRef<IntegerLattice *> results) override; |
| 67 | + |
| 68 | + void setToEntryState(IntegerLattice *lattice) override { |
| 69 | + propagateIfChanged(lattice, lattice->join(IntegerState())); |
| 70 | + } |
| 71 | +}; |
| 72 | + |
| 73 | +LogicalResult IntegerLatticeAnalysis::visitOperation( |
| 74 | + Operation *op, ArrayRef<const IntegerLattice *> operands, |
| 75 | + ArrayRef<IntegerLattice *> results) { |
| 76 | + for (auto *operand : operands) { |
| 77 | + for (auto *result : results) { |
| 78 | + propagateIfChanged(result, result->join(*operand)); |
| 79 | + } |
| 80 | + } |
| 81 | + return success(); |
| 82 | +} |
| 83 | + |
| 84 | +} // end anonymous namespace |
| 85 | + |
| 86 | +namespace { |
| 87 | +struct TestIntegerLatticePass |
| 88 | + : public PassWrapper<TestIntegerLatticePass, OperationPass<>> { |
| 89 | + MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestIntegerLatticePass) |
| 90 | + |
| 91 | + TestIntegerLatticePass() = default; |
| 92 | + TestIntegerLatticePass(const TestIntegerLatticePass &other) |
| 93 | + : PassWrapper(other) {} |
| 94 | + |
| 95 | + StringRef getArgument() const override { return "test-integer-lattice"; } |
| 96 | + |
| 97 | + void runOnOperation() override { |
| 98 | + Operation *op = getOperation(); |
| 99 | + MLIRContext *ctx = &getContext(); |
| 100 | + |
| 101 | + DataFlowSolver solver; |
| 102 | + solver.load<DeadCodeAnalysis>(); |
| 103 | + solver.load<SparseConstantPropagation>(); |
| 104 | + solver.load<IntegerLatticeAnalysis>(); |
| 105 | + if (failed(solver.initializeAndRun(op))) |
| 106 | + return signalPassFailure(); |
| 107 | + |
| 108 | + // Walk the IR and attach operand and result lattices as attributes to each |
| 109 | + // operation. |
| 110 | + op->walk([&](Operation *op) { |
| 111 | + SmallVector<Attribute> operandAttrs; |
| 112 | + SmallVector<Attribute> resultAttrs; |
| 113 | + for (auto [index, operand] : llvm::enumerate(op->getOperands())) { |
| 114 | + const IntegerLattice *lattice = |
| 115 | + solver.lookupState<IntegerLattice>(operand); |
| 116 | + assert(lattice && "expected a sparse lattice"); |
| 117 | + operandAttrs.push_back( |
| 118 | + IntegerAttr::get(IndexType::get(ctx), lattice->getValue().get())); |
| 119 | + } |
| 120 | + for (auto [index, result] : llvm::enumerate(op->getResults())) { |
| 121 | + const IntegerLattice *lattice = |
| 122 | + solver.lookupState<IntegerLattice>(result); |
| 123 | + assert(lattice && "expected a sparse lattice"); |
| 124 | + resultAttrs.push_back( |
| 125 | + IntegerAttr::get(IndexType::get(ctx), lattice->getValue().get())); |
| 126 | + } |
| 127 | + |
| 128 | + op->setAttr("test.operand_lattices", ArrayAttr::get(ctx, operandAttrs)); |
| 129 | + op->setAttr("test.result_lattices", ArrayAttr::get(ctx, resultAttrs)); |
| 130 | + }); |
| 131 | + } |
| 132 | +}; |
| 133 | +} // end anonymous namespace |
| 134 | + |
| 135 | +namespace mlir { |
| 136 | +namespace test { |
| 137 | +void registerTestIntegerLatticePass() { |
| 138 | + PassRegistration<TestIntegerLatticePass>(); |
| 139 | +} |
| 140 | +} // end namespace test |
| 141 | +} // end namespace mlir |
0 commit comments