-
Notifications
You must be signed in to change notification settings - Fork 50
[Buffers] fix non-CFDFC paths having non-zero occupancy #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ | |
| #include "mlir/IR/Value.h" | ||
| #include "mlir/Support/LLVM.h" | ||
| #include "mlir/Support/LogicalResult.h" | ||
| #include "llvm/ADT/DenseSet.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
| #include "llvm/ADT/SmallVector.h" | ||
| #include "llvm/Support/Debug.h" | ||
|
|
@@ -150,51 +151,57 @@ void OccupancyBalancingLP::setup() { | |
| return; | ||
| } | ||
|
|
||
| /// Target II = 1 for maximum throughput | ||
| double targetII = latencyResult.targetII; | ||
| if (targetII <= 0.0) { | ||
| targetII = 1.0; | ||
| } | ||
| /// Create variables for each channel | ||
| /// N_c: Maximal token occupancy on channel c. | ||
|
|
||
| /// (Paper: Section 5, Table 2) | ||
| /// N_c: Maximal token occupancy on channel c. | ||
| this->addOccupancyVars(allChannels, channelOccupancy, MAX_OCCUPANCY); | ||
|
|
||
| /// (Paper: Section 5, Equation 8): N_c >= L_c / II | ||
| /// We enforce this for the global II, but also for each CFDFC's specific II | ||
| /// to ensure sufficient buffering in faster loops. | ||
| /// (Paper: Section 5, Equation 15): Making the required occupancy the | ||
| /// maximum of all CFDFCs' II. | ||
| /// Determine which channels belong to at least one CFDFC. | ||
| llvm::DenseSet<Value> cfdfcChannels; | ||
| for (CFDFC *cfdfc : cfdfcs) | ||
| for (Value channel : cfdfc->channels) | ||
| cfdfcChannels.insert(channel); | ||
|
|
||
| /// (Paper: Section 5, Equation 8): N_c >= L_c / II | ||
| /// Per-channel lower bound ensuring the pipeline has enough tokens in flight. | ||
| /// For non-CFDFC channels (executed once), N_c >= 1 suffices. | ||
| llvm::MapVector<Value, double> requiredOccupancy; | ||
|
|
||
| // Initialize with global II constraint | ||
| for (Value channel : allChannels) { | ||
| unsigned latency = latencyResult.channelExtraLatency.lookup(channel); | ||
| requiredOccupancy[channel] = static_cast<double>(latency) / targetII; | ||
| if (cfdfcChannels.contains(channel)) | ||
| requiredOccupancy[channel] = static_cast<double>(latency) / targetII; | ||
|
Comment on lines
+175
to
+176
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this a duplicate of the logic below? |
||
| else | ||
| requiredOccupancy[channel] = (latency > 0) ? 1.0 : 0.0; | ||
| } | ||
|
|
||
| // Update by taking the maximum of the per-CFDFC constraints | ||
| /// (Paper: Section 5, Equation 15): Take the maximum across per-CFDFC IIs. | ||
| for (CFDFC *cfdfc : cfdfcs) { | ||
| double cfdfcII = latencyResult.cfdfcTargetIIs.lookup(cfdfc); | ||
| if (cfdfcII < 1.0) | ||
| continue; | ||
|
|
||
| for (Value channel : cfdfc->channels) { | ||
| if (requiredOccupancy.count(channel)) { | ||
| unsigned latency = latencyResult.channelExtraLatency.lookup(channel); | ||
| double specificOcc = static_cast<double>(latency) / cfdfcII; | ||
| if (specificOcc > requiredOccupancy[channel]) { | ||
| requiredOccupancy[channel] = specificOcc; | ||
| } | ||
| } | ||
| if (!requiredOccupancy.count(channel)) | ||
| continue; | ||
| unsigned latency = latencyResult.channelExtraLatency.lookup(channel); | ||
| double specificOcc = static_cast<double>(latency) / cfdfcII; | ||
| if (specificOcc > requiredOccupancy[channel]) | ||
| requiredOccupancy[channel] = specificOcc; | ||
| } | ||
| } | ||
|
|
||
| addMinOccupancyConstraints(requiredOccupancy, channelOccupancy); | ||
| addBackedgeConstraints(cfdfcs, channelOccupancy); | ||
| addChannelPropertyOccupancyConstraints(channelOccupancy); | ||
|
|
||
| /// (Paper: Section 5, Equations 10-11): Path occupancy equality. | ||
| addPathOccupancyEqualityConstraints(reconvergentPaths, cfdfcs, | ||
| channelOccupancy); | ||
|
|
||
| /// (Paper: Section 5, Equation 14): Minimize sum(B_c * N_c). | ||
| this->setOccupancyBalancingObjective(channelOccupancy); | ||
|
|
||
| markReadyToOptimize(); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1350,6 +1350,70 @@ void BufferPlacementMILP::addBackedgeConstraints( | |
| } | ||
| } | ||
|
|
||
| void BufferPlacementMILP::addPathOccupancyEqualityConstraints( | ||
| ArrayRef<fpga24::ReconvergentPathWithGraph> reconvergentPaths, | ||
| ArrayRef<CFDFC *> cfdfcs, llvm::MapVector<Value, CPVar> &channelOccupancy) { | ||
|
|
||
| // Collect all operations that belong to at least one CFDFC. | ||
| DenseSet<Operation *> cfdfcUnits; | ||
| for (CFDFC *cfdfc : cfdfcs) | ||
| for (Operation *op : cfdfc->units) | ||
| cfdfcUnits.insert(op); | ||
|
|
||
| for (auto [pathIdx, pathWithGraph] : llvm::enumerate(reconvergentPaths)) { | ||
|
ziadomalik marked this conversation as resolved.
|
||
| const ReconvergentPath &rp = pathWithGraph.path; | ||
| const CFGTransitionSequenceSubgraph *graph = pathWithGraph.graph; | ||
|
|
||
| // We skip paths whose fork is outside all CFDFCs. | ||
| const DataflowGraphNode &forkNode = graph->nodes[rp.forkNodeId]; | ||
| if (forkNode.type == DataflowGraphNode::REGULAR && | ||
| !cfdfcUnits.contains(forkNode.op)) | ||
| continue; | ||
|
|
||
| std::vector<SimplePath> simplePaths = | ||
| enumerateSimplePaths(*graph, rp.forkNodeId, rp.joinNodeId, rp.nodeIds); | ||
| if (simplePaths.size() < 2) | ||
| continue; | ||
|
|
||
| // Compute the occupancy expression for each path (Eq 10): | ||
| // Occupancy(p) = sum(L_u / II for pipelined units u) + sum(N_c) | ||
| std::vector<std::pair<LinExpr, double>> pathOccupancies; | ||
| for (const auto &path : simplePaths) { | ||
| LinExpr varPart; | ||
| double constPart = 0.0; | ||
|
|
||
| for (NodeIdType nodeId : path.nodes) { | ||
| if (nodeId == rp.forkNodeId || nodeId == rp.joinNodeId) | ||
| continue; | ||
| const DataflowGraphNode &node = graph->nodes[nodeId]; | ||
| if (node.type != DataflowGraphNode::REGULAR) | ||
| continue; | ||
| auto latOrFail = | ||
| timingDB.getLatency(node.op, SignalType::DATA, targetPeriod); | ||
| if (succeeded(latOrFail) && *latOrFail > 0.0) | ||
| constPart += *latOrFail; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is II in the original constraint? |
||
| } | ||
|
|
||
| for (EdgeIdType edgeId : path.edges) { | ||
| Value channel = graph->edges[edgeId].channel; | ||
| if (channelOccupancy.count(channel)) | ||
| varPart += channelOccupancy[channel]; | ||
| } | ||
|
|
||
| pathOccupancies.emplace_back(std::move(varPart), constPart); | ||
| } | ||
|
Comment on lines
+1359
to
+1401
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // (Eq 11): Occupancy(p0) = Occupancy(pi) | ||
| for (size_t i = 1; i < pathOccupancies.size(); ++i) { | ||
| auto &[vars0, const0] = pathOccupancies[0]; | ||
| auto &[varsI, constI] = pathOccupancies[i]; | ||
|
|
||
| std::string name = llvm::formatv("pathOccEq_{0}_{1}", pathIdx, i).str(); | ||
| model->addConstr(vars0 + const0 == varsI + constI, name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void BufferPlacementMILP::addReconvergentPathConstraints( | ||
| ArrayRef<fpga24::ReconvergentPathWithGraph> reconvergentPaths) { | ||
| for (size_t pathIdx = 0; pathIdx < reconvergentPaths.size(); ++pathIdx) { | ||
|
|
||

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a remark saying why we care about this? (i guess this is for setting the occupancy to be 1 for every channel that is not part of a cfdfc, but has latency > 0?