CIRCT 24.0.0git
Loading...
Searching...
No Matches
HoistNets.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 "CaptureUtils.h"
13#include "mlir/Analysis/Liveness.h"
14#include "mlir/Dialect/SCF/IR/SCF.h"
15#include "mlir/IR/Dominance.h"
16#include "llvm/Support/Debug.h"
17
18#define DEBUG_TYPE "llhd-hoist-nets"
19
20namespace circt {
21namespace llhd {
22#define GEN_PASS_DEF_HOISTNETSPASS
23#include "circt/Dialect/LLHD/LLHDPasses.h.inc"
24} // namespace llhd
25} // namespace circt
26
27using namespace mlir;
28using namespace circt;
29using namespace llhd;
30
31namespace {
32struct HoistNetsPass : public llhd::impl::HoistNetsPassBase<HoistNetsPass> {
33protected:
34 void runOnOperation() override;
35
36private:
37 void hoistProbes(Region *region);
38};
39} // namespace
40
41void HoistNetsPass::hoistProbes(Region *region) {
42 Operation *parentOp = region->getParentOp();
43 DominanceInfo dominance(parentOp);
44
45 // Collect any probes of nets defined outside the region
46 SmallVector<ProbeNetOp> probes;
47 region->walk([&](ProbeNetOp probe) {
48 if (dominance.properlyDominates(probe.getNet(), parentOp))
49 probes.push_back(probe);
50 });
51 if (probes.empty())
52 return;
53
54 // ProcessOps can contain waits - if a probe is used across a wait we can't
55 // just replace it with the hoisted version from the wrong timestep. Instead
56 // we have to capture the value across the waits as a block argument
57 if (auto processOp = dyn_cast<ProcessOp>(parentOp)) {
58 SmallVector<WaitOp> waitOps;
59 for (auto &block : *region)
60 if (auto waitOp = dyn_cast<WaitOp>(block.getTerminator()))
61 waitOps.push_back(waitOp);
62 if (!waitOps.empty()) {
63 Liveness liveness(processOp);
64 for (auto probe : probes)
65 captureAcrossWait(probe, waitOps, liveness, dominance);
66 }
67 }
68
69 OpBuilder builder(parentOp);
70 for (auto probe : probes) {
71 auto *clone = builder.clone(*probe);
72 probe.replaceAllUsesWith(clone);
73 probe.erase();
74 }
75}
76
77void HoistNetsPass::runOnOperation() {
78 SmallVector<Region *> regions;
79 getOperation()->walk([&](Operation *op) {
80 if (isa<ProcessOp, FinalOp, CombinationalOp, scf::IfOp>(op))
81 for (auto &region : op->getRegions())
82 if (!region.empty())
83 regions.push_back(&region);
84 });
85 for (auto *region : regions) {
86 hoistProbes(region);
87 }
88}
void captureAcrossWait(Value value, ArrayRef< WaitOp > waitOps, mlir::Liveness &liveness, mlir::DominanceInfo &dominance)
Add a value as block argument to a list of wait ops and update uses of the value to use the added blo...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.