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
|
//===- TestDataFlowFramework.cpp - Test data-flow analysis framework ------===//
//
// 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 "mlir/Analysis/DataFlowFramework.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Pass/Pass.h"
#include <optional>
using namespace mlir;
namespace {
/// This analysis state represents an integer that is XOR'd with other states.
class FooState : public AnalysisState {
public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FooState)
using AnalysisState::AnalysisState;
/// Returns true if the state is uninitialized.
bool isUninitialized() const { return !state; }
/// Print the integer value or "none" if uninitialized.
void print(raw_ostream &os) const override {
if (state)
os << *state;
else
os << "none";
}
/// Join the state with another. If either is unintialized, take the
/// initialized value. Otherwise, XOR the integer values.
ChangeResult join(const FooState &rhs) {
if (rhs.isUninitialized())
return ChangeResult::NoChange;
return join(*rhs.state);
}
ChangeResult join(uint64_t value) {
if (isUninitialized()) {
state = value;
return ChangeResult::Change;
}
uint64_t before = *state;
state = before ^ value;
return before == *state ? ChangeResult::NoChange : ChangeResult::Change;
}
/// Set the value of the state directly.
ChangeResult set(const FooState &rhs) {
if (state == rhs.state)
return ChangeResult::NoChange;
state = rhs.state;
return ChangeResult::Change;
}
/// Returns the integer value of the state.
uint64_t getValue() const { return *state; }
private:
/// An optional integer value.
std::optional<uint64_t> state;
};
/// This analysis computes `FooState` across operations and control-flow edges.
/// If an op specifies a `foo` integer attribute, the contained value is XOR'd
/// with the value before the operation.
class FooAnalysis : public DataFlowAnalysis {
public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(FooAnalysis)
using DataFlowAnalysis::DataFlowAnalysis;
LogicalResult initialize(Operation *top) override;
LogicalResult visit(ProgramPoint point) override;
private:
void visitBlock(Block *block);
void visitOperation(Operation *op);
};
struct TestFooAnalysisPass
: public PassWrapper<TestFooAnalysisPass, OperationPass<func::FuncOp>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(TestFooAnalysisPass)
StringRef getArgument() const override { return "test-foo-analysis"; }
void runOnOperation() override;
};
} // namespace
LogicalResult FooAnalysis::initialize(Operation *top) {
if (top->getNumRegions() != 1)
return top->emitError("expected a single region top-level op");
if (top->getRegion(0).getBlocks().empty())
return top->emitError("expected at least one block in the region");
// Initialize the top-level state.
getOrCreate<FooState>(&top->getRegion(0).front())->join(0);
// Visit all nested blocks and operations.
for (Block &block : top->getRegion(0)) {
visitBlock(&block);
for (Operation &op : block) {
if (op.getNumRegions())
return op.emitError("unexpected op with regions");
visitOperation(&op);
}
}
return success();
}
LogicalResult FooAnalysis::visit(ProgramPoint point) {
if (auto *op = llvm::dyn_cast_if_present<Operation *>(point)) {
visitOperation(op);
return success();
}
if (auto *block = llvm::dyn_cast_if_present<Block *>(point)) {
visitBlock(block);
return success();
}
return emitError(point.getLoc(), "unknown point kind");
}
void FooAnalysis::visitBlock(Block *block) {
if (block->isEntryBlock()) {
// This is the initial state. Let the framework default-initialize it.
return;
}
FooState *state = getOrCreate<FooState>(block);
ChangeResult result = ChangeResult::NoChange;
for (Block *pred : block->getPredecessors()) {
// Join the state at the terminators of all predecessors.
const FooState *predState =
getOrCreateFor<FooState>(block, pred->getTerminator());
result |= state->join(*predState);
}
propagateIfChanged(state, result);
}
void FooAnalysis::visitOperation(Operation *op) {
FooState *state = getOrCreate<FooState>(op);
ChangeResult result = ChangeResult::NoChange;
// Copy the state across the operation.
const FooState *prevState;
if (Operation *prev = op->getPrevNode())
prevState = getOrCreateFor<FooState>(op, prev);
else
prevState = getOrCreateFor<FooState>(op, op->getBlock());
result |= state->set(*prevState);
// Modify the state with the attribute, if specified.
if (auto attr = op->getAttrOfType<IntegerAttr>("foo")) {
uint64_t value = attr.getUInt();
result |= state->join(value);
}
propagateIfChanged(state, result);
}
void TestFooAnalysisPass::runOnOperation() {
func::FuncOp func = getOperation();
DataFlowSolver solver;
solver.load<FooAnalysis>();
if (failed(solver.initializeAndRun(func)))
return signalPassFailure();
raw_ostream &os = llvm::errs();
os << "function: @" << func.getSymName() << "\n";
func.walk([&](Operation *op) {
auto tag = op->getAttrOfType<StringAttr>("tag");
if (!tag)
return;
const FooState *state = solver.lookupState<FooState>(op);
assert(state && !state->isUninitialized());
os << tag.getValue() << " -> " << state->getValue() << "\n";
});
}
namespace mlir {
namespace test {
void registerTestFooAnalysisPass() { PassRegistration<TestFooAnalysisPass>(); }
} // namespace test
} // namespace mlir
|