|
| 1 | +#include "backends/p4tools/modules/flay/core/interpreter/partial_evaluator.h" |
| 2 | + |
| 3 | +#include <cstdlib> |
| 4 | + |
| 5 | +#include "backends/p4tools/common/lib/logging.h" |
| 6 | +#include "backends/p4tools/modules/flay/core/control_plane/bfruntime/protobuf.h" |
| 7 | +#include "backends/p4tools/modules/flay/core/control_plane/control_plane_item.h" |
| 8 | +#include "backends/p4tools/modules/flay/core/control_plane/p4runtime/protobuf.h" |
| 9 | +#include "backends/p4tools/modules/flay/core/interpreter/program_info.h" |
| 10 | +#include "backends/p4tools/modules/flay/core/interpreter/target.h" |
| 11 | +#include "backends/p4tools/modules/flay/core/lib/incremental_analysis.h" |
| 12 | +#include "backends/p4tools/modules/flay/core/lib/return_macros.h" |
| 13 | +#include "backends/p4tools/modules/flay/core/specialization/passes/specializer.h" |
| 14 | +#include "backends/p4tools/modules/flay/core/specialization/z3/reachability_map.h" |
| 15 | +#include "backends/p4tools/modules/flay/core/specialization/z3/substitution_map.h" |
| 16 | +#include "backends/p4tools/modules/flay/options.h" |
| 17 | +#include "lib/error.h" |
| 18 | +#include "lib/timer.h" |
| 19 | + |
| 20 | +namespace P4Tools::Flay { |
| 21 | + |
| 22 | +std::string PartialEvaluationStatistics::toFormattedString() const { |
| 23 | + std::stringstream output; |
| 24 | + for (const auto &elimRepl : eliminatedNodes) { |
| 25 | + auto [node, replaced] = elimRepl; |
| 26 | + if (node->getSourceInfo().isValid()) { |
| 27 | + auto sourceFragment = node->getSourceInfo().toSourceFragment(false).trim(); |
| 28 | + if (replaced == nullptr) { |
| 29 | + output << "Eliminated node at line " |
| 30 | + << node->getSourceInfo().toPosition().sourceLine << ": " << sourceFragment; |
| 31 | + } else { |
| 32 | + output << "Replaced node at line " << node->getSourceInfo().toPosition().sourceLine |
| 33 | + << ": " << sourceFragment << " with "; |
| 34 | + replaced->dbprint(output); |
| 35 | + } |
| 36 | + } else { |
| 37 | + ::warning("Invalid source information for node %1%. This should be fixed", node); |
| 38 | + output << "Eliminated node at line (unknown): " << node; |
| 39 | + } |
| 40 | + if (elimRepl != eliminatedNodes.back()) { |
| 41 | + output << "\n"; |
| 42 | + } |
| 43 | + } |
| 44 | + return output.str(); |
| 45 | +} |
| 46 | + |
| 47 | +namespace { |
| 48 | + |
| 49 | +AbstractReachabilityMap *initializeReachabilityMap(ReachabilityMapType mapType, |
| 50 | + const NodeAnnotationMap &nodeAnnotationMap) { |
| 51 | + printInfo("Creating the reachability map..."); |
| 52 | + AbstractReachabilityMap *initializedReachabilityMap = nullptr; |
| 53 | + if (mapType == ReachabilityMapType::kZ3Precomputed) { |
| 54 | + initializedReachabilityMap = new Z3SolverReachabilityMap(nodeAnnotationMap); |
| 55 | + } else { |
| 56 | + initializedReachabilityMap = new IRReachabilityMap(nodeAnnotationMap); |
| 57 | + } |
| 58 | + return initializedReachabilityMap; |
| 59 | +} |
| 60 | + |
| 61 | +AbstractSubstitutionMap *initializeSubstitutionMap(ReachabilityMapType mapType, |
| 62 | + const NodeAnnotationMap &nodeAnnotationMap) { |
| 63 | + printInfo("Creating the substitution map..."); |
| 64 | + AbstractSubstitutionMap *initializedSubstitutionMap = nullptr; |
| 65 | + if (mapType == ReachabilityMapType::kZ3Precomputed) { |
| 66 | + initializedSubstitutionMap = new Z3SolverSubstitutionMap(nodeAnnotationMap); |
| 67 | + } else { |
| 68 | + initializedSubstitutionMap = new IrSubstitutionMap(nodeAnnotationMap); |
| 69 | + } |
| 70 | + return initializedSubstitutionMap; |
| 71 | +} |
| 72 | + |
| 73 | +} // namespace |
| 74 | + |
| 75 | +AbstractReachabilityMap *PartialEvaluation::mutableReachabilityMap() { return _reachabilityMap; } |
| 76 | + |
| 77 | +AbstractSubstitutionMap *PartialEvaluation::mutableSubstitutionMap() { return _substitutionMap; } |
| 78 | + |
| 79 | +const ControlPlaneConstraints &PartialEvaluation::controlPlaneConstraints() const { |
| 80 | + return _controlPlaneConstraints; |
| 81 | +} |
| 82 | + |
| 83 | +ControlPlaneConstraints &PartialEvaluation::mutableControlPlaneConstraints() { |
| 84 | + return _controlPlaneConstraints; |
| 85 | +} |
| 86 | + |
| 87 | +std::optional<bool> PartialEvaluation::checkForSemanticsChange() { |
| 88 | + printInfo("Checking for change in program semantics..."); |
| 89 | + Util::ScopedTimer timer("Check for semantics change"); |
| 90 | + |
| 91 | + auto reachabilityResult = |
| 92 | + mutableReachabilityMap()->recomputeReachability(controlPlaneConstraints()); |
| 93 | + if (!reachabilityResult.has_value()) { |
| 94 | + return std::nullopt; |
| 95 | + } |
| 96 | + auto substitutionResult = |
| 97 | + mutableSubstitutionMap()->recomputeSubstitution(controlPlaneConstraints()); |
| 98 | + if (!substitutionResult.has_value()) { |
| 99 | + return std::nullopt; |
| 100 | + } |
| 101 | + return reachabilityResult.value() || substitutionResult.value(); |
| 102 | +} |
| 103 | + |
| 104 | +std::optional<bool> PartialEvaluation::checkForSemanticsChange(const SymbolSet &symbolSet) { |
| 105 | + printInfo("Checking for change in program semantics with symbol set..."); |
| 106 | + Util::ScopedTimer timer("Check for semantics change with symbol set"); |
| 107 | + |
| 108 | + auto reachabilityResult = |
| 109 | + mutableReachabilityMap()->recomputeReachability(symbolSet, controlPlaneConstraints()); |
| 110 | + if (!reachabilityResult.has_value()) { |
| 111 | + return std::nullopt; |
| 112 | + } |
| 113 | + auto substitutionResult = |
| 114 | + mutableSubstitutionMap()->recomputeSubstitution(symbolSet, controlPlaneConstraints()); |
| 115 | + if (!substitutionResult.has_value()) { |
| 116 | + return std::nullopt; |
| 117 | + } |
| 118 | + return reachabilityResult.value() || substitutionResult.value(); |
| 119 | +} |
| 120 | + |
| 121 | +std::optional<const IR::P4Program *> PartialEvaluation::specializeProgram( |
| 122 | + const IR::P4Program &program) { |
| 123 | + auto flaySpecializer = FlaySpecializer(_refMap, *_reachabilityMap, *_substitutionMap); |
| 124 | + const auto *optimizedProgram = program.apply(flaySpecializer); |
| 125 | + if (::errorCount() > 0) { |
| 126 | + return std::nullopt; |
| 127 | + } |
| 128 | + // Update the list of eliminated nodes. |
| 129 | + _eliminatedNodes = flaySpecializer.eliminatedNodes(); |
| 130 | + return optimizedProgram; |
| 131 | +} |
| 132 | + |
| 133 | +std::optional<SymbolSet> PartialEvaluation::convertControlPlaneUpdate( |
| 134 | + const ControlPlaneUpdate &controlPlaneUpdate) { |
| 135 | + SymbolSet symbolSet; |
| 136 | + if (const auto *p4RuntimeUpdate = controlPlaneUpdate.to<P4RuntimeControlPlaneUpdate>()) { |
| 137 | + auto result = P4Runtime::updateControlPlaneConstraintsWithEntityMessage( |
| 138 | + p4RuntimeUpdate->update.entity(), *flayCompilerResult().getP4RuntimeApi().p4Info, |
| 139 | + _controlPlaneConstraints, p4RuntimeUpdate->update.type(), symbolSet); |
| 140 | + if (result != EXIT_SUCCESS) { |
| 141 | + return std::nullopt; |
| 142 | + } |
| 143 | + } else if (const auto *bfRuntimeUpdate = controlPlaneUpdate.to<BfRuntimeControlPlaneUpdate>()) { |
| 144 | + auto result = BfRuntime::updateControlPlaneConstraintsWithEntityMessage( |
| 145 | + bfRuntimeUpdate->update.entity(), *flayCompilerResult().getP4RuntimeApi().p4Info, |
| 146 | + _controlPlaneConstraints, bfRuntimeUpdate->update.type(), symbolSet); |
| 147 | + if (result != EXIT_SUCCESS) { |
| 148 | + return std::nullopt; |
| 149 | + } |
| 150 | + } else { |
| 151 | + ::error("Unknown control plane update type: %1%", typeid(controlPlaneUpdate).name()); |
| 152 | + return std::nullopt; |
| 153 | + } |
| 154 | + return symbolSet; |
| 155 | +} |
| 156 | + |
| 157 | +PartialEvaluation::PartialEvaluation(const FlayOptions &flayOptions, |
| 158 | + const FlayCompilerResult &flayCompilerResult, |
| 159 | + const ProgramInfo &programInfo, |
| 160 | + const PartialEvaluationOptions &partialEvaluationOptions) |
| 161 | + : IncrementalAnalysis(flayOptions, flayCompilerResult, programInfo), |
| 162 | + _partialEvaluationOptions(partialEvaluationOptions) { |
| 163 | + flayCompilerResult.getProgram().apply(P4::ResolveReferences(&_refMap)); |
| 164 | +} |
| 165 | + |
| 166 | +int PartialEvaluation::initialize() { |
| 167 | + printInfo("Computing initial control plane constraints..."); |
| 168 | + // Gather the initial control-plane configuration. Also from a file input, |
| 169 | + // if present. |
| 170 | + ASSIGN_OR_RETURN( |
| 171 | + _controlPlaneConstraints, |
| 172 | + FlayTarget::computeControlPlaneConstraints(flayCompilerResult(), flayOptions()), |
| 173 | + EXIT_FAILURE); |
| 174 | + |
| 175 | + ExecutionState executionState(&programInfo().getP4Program()); |
| 176 | + |
| 177 | + printInfo("Starting data plane analysis..."); |
| 178 | + Util::ScopedTimer timer("Data plane analysis"); |
| 179 | + const auto *pipelineSequence = programInfo().getPipelineSequence(); |
| 180 | + auto &stepper = |
| 181 | + FlayTarget::getStepper(programInfo(), mutableControlPlaneConstraints(), executionState); |
| 182 | + stepper.initializeState(); |
| 183 | + for (const auto *node : *pipelineSequence) { |
| 184 | + node->apply(stepper); |
| 185 | + } |
| 186 | + /// Substitute any placeholder variables encountered in the execution state. |
| 187 | + printInfo("Substituting placeholder variables..."); |
| 188 | + executionState.substitutePlaceholders(); |
| 189 | + |
| 190 | + printInfo("Setting up analysis maps..."); |
| 191 | + _reachabilityMap = initializeReachabilityMap(_partialEvaluationOptions.get().mapType, |
| 192 | + executionState.nodeAnnotationMap()); |
| 193 | + _substitutionMap = initializeSubstitutionMap(_partialEvaluationOptions.get().mapType, |
| 194 | + executionState.nodeAnnotationMap()); |
| 195 | + |
| 196 | + printInfo("Precomputing reachability and substitution maps with initial constraints..."); |
| 197 | + auto reachabilityResult = _reachabilityMap->recomputeReachability(controlPlaneConstraints()); |
| 198 | + if (!reachabilityResult.has_value()) { |
| 199 | + return EXIT_FAILURE; |
| 200 | + } |
| 201 | + auto substitutionResult = |
| 202 | + mutableSubstitutionMap()->recomputeSubstitution(controlPlaneConstraints()); |
| 203 | + if (!substitutionResult.has_value()) { |
| 204 | + return EXIT_FAILURE; |
| 205 | + } |
| 206 | + return EXIT_SUCCESS; |
| 207 | +} |
| 208 | + |
| 209 | +[[nodiscard]] PartialEvaluationStatistics *PartialEvaluation::computeAnalysisStatistics() const { |
| 210 | + return new PartialEvaluationStatistics{_eliminatedNodes}; |
| 211 | +} |
| 212 | + |
| 213 | +} // namespace P4Tools::Flay |
0 commit comments