|
| 1 | +#include <llvm/Support/ErrorHandling.h> |
| 2 | + |
| 3 | +#include "llvm/ADT/DepthFirstIterator.h" |
| 4 | +#include "llvm/ADT/STLExtras.h" |
| 5 | +#include "llvm/ADT/TypeSwitch.h" |
| 6 | +#include "p4mlir/Dialect/P4HIR/ParserGraph.h" |
| 7 | +#include "p4mlir/Transforms/Passes.h" |
| 8 | + |
| 9 | +#define DEBUG_TYPE "p4hir-simplify-parsers" |
| 10 | + |
| 11 | +using namespace mlir; |
| 12 | + |
| 13 | +namespace P4::P4MLIR { |
| 14 | +#define GEN_PASS_DEF_SIMPLIFYPARSERS |
| 15 | +#include "p4mlir/Transforms/Passes.cpp.inc" |
| 16 | + |
| 17 | +namespace { |
| 18 | +struct SimplifyParsers : public impl::SimplifyParsersBase<SimplifyParsers> { |
| 19 | + void runOnOperation() override; |
| 20 | + |
| 21 | + private: |
| 22 | + /// Collapses linear sequences of states without branches or annotations. |
| 23 | + void collapseChains(P4HIR::ParserOp parser); |
| 24 | +}; |
| 25 | +} // end anonymous namespace |
| 26 | + |
| 27 | +void SimplifyParsers::collapseChains(P4HIR::ParserOp parser) { |
| 28 | + // TODO: Revisit this to use ParserCallGraph instead |
| 29 | + mlir::DenseMap<P4HIR::ParserStateOp, unsigned> indegree; |
| 30 | + // Initialize indegree[start] to 1 to account for the implicit parser entry edge. |
| 31 | + indegree[parser.getStartState()] = 1; |
| 32 | + for (auto state : parser.states()) { |
| 33 | + for (auto next : state.getNextStates()) { |
| 34 | + ++indegree[next]; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + // succ[s1] = s2 if there is exactly one outgoing edge from s1 to s2 |
| 39 | + // and s2 has exactly one incoming edge from s1. |
| 40 | + mlir::DenseMap<P4HIR::ParserStateOp, P4HIR::ParserStateOp> succ; |
| 41 | + mlir::DenseMap<P4HIR::ParserStateOp, P4HIR::ParserStateOp> pred; |
| 42 | + |
| 43 | + // We diconnect any annotated states since they can't be collapsed |
| 44 | + // and if we kept them they'd poison downstream merging. |
| 45 | + for (auto state : parser.states()) { |
| 46 | + if (!llvm::hasNItems(state.getNextStates(), 1)) continue; |
| 47 | + // Name annotations are special, if they are the only annotation |
| 48 | + // then they can be merged into as heads. |
| 49 | + auto ann = state.getAnnotations(); |
| 50 | + if (ann && !(ann->size() == 1 && ann->contains("name"))) continue; |
| 51 | + P4HIR::ParserStateOp successor = *state.getNextStates().begin(); |
| 52 | + if (indegree[successor] != 1 || successor.getAnnotations()) continue; |
| 53 | + |
| 54 | + succ[state] = successor; |
| 55 | + pred[successor] = state; |
| 56 | + } |
| 57 | + |
| 58 | + // Process each chain head, collapsing states whenever possible. |
| 59 | + for (auto [head, _] : succ) { |
| 60 | + if (pred.contains(head)) continue; |
| 61 | + LLVM_DEBUG(llvm::dbgs() << "Chaining states into '" << head.getName() << "'\n"); |
| 62 | + |
| 63 | + // Walk forward through the chain using successor map |
| 64 | + for (auto it = succ.find(head); it != succ.end(); it = succ.find(it->second)) { |
| 65 | + P4HIR::ParserStateOp next = it->second; |
| 66 | + LLVM_DEBUG(llvm::dbgs() << "\tAdding '" << next.getName() << "' to chain\n"); |
| 67 | + auto &headOps = head.getBody().front().getOperations(); |
| 68 | + |
| 69 | + // Remove the terminator (transition to 'next') from the current head body. |
| 70 | + head.getNextTransition()->erase(); |
| 71 | + |
| 72 | + // Splice all operations from 'next' into the head body. |
| 73 | + headOps.splice(headOps.end(), next.getBody().front().getOperations()); |
| 74 | + |
| 75 | + // Remove the now-empty 'next' state. |
| 76 | + next.erase(); |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +void SimplifyParsers::runOnOperation() { |
| 82 | + getOperation()->walk([&](P4HIR::ParserOp parser) { |
| 83 | + LLVM_DEBUG(llvm::dbgs() << "\n--- Simplifying parser '" << parser.getName() << "' ---\n"); |
| 84 | + llvm::df_iterator_default_set<llvm::GraphTraits<P4HIR::ParserOp>::NodeRef> reachable; |
| 85 | + bool acceptReachable = false, rejectReachable = false; |
| 86 | + |
| 87 | + /// Finds all states reachable from the start state and deletes unreachable states. |
| 88 | + for (auto stateOp : llvm::depth_first_ext(parser, reachable)) { |
| 89 | + auto state = mlir::cast<P4HIR::ParserStateOp>(stateOp); |
| 90 | + LLVM_DEBUG(llvm::dbgs() << "DFS visiting " << state.getName() << "\n"); |
| 91 | + if (state.isAccept()) acceptReachable = true; |
| 92 | + if (state.isReject()) rejectReachable = true; |
| 93 | + } |
| 94 | + if (!acceptReachable && !rejectReachable) |
| 95 | + parser.emitError("Parser never reaches the 'accept' or 'reject' state."); |
| 96 | + LLVM_DEBUG(llvm::dbgs() << "Parser '" << parser.getName() << "' has " << reachable.size() |
| 97 | + << " reachable states\n"); |
| 98 | + |
| 99 | + for (auto state : llvm::make_early_inc_range(parser.states())) { |
| 100 | + if (!reachable.contains(state)) { |
| 101 | + if (state.isAccept()) { |
| 102 | + parser.emitWarning() |
| 103 | + << "Parser has unreachable accept state " << state.getName(); |
| 104 | + } else { |
| 105 | + LLVM_DEBUG(llvm::dbgs() |
| 106 | + << "Removing unreachable state '" << state.getName() << "'\n"); |
| 107 | + state.erase(); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + collapseChains(parser); |
| 113 | + |
| 114 | + return WalkResult::advance(); |
| 115 | + }); |
| 116 | +} |
| 117 | + |
| 118 | +std::unique_ptr<Pass> createSimplifyParsersPass() { return std::make_unique<SimplifyParsers>(); } |
| 119 | +} // namespace P4::P4MLIR |
0 commit comments