17 #include "mlir/IR/Dominance.h"
18 #include "mlir/Pass/Pass.h"
22 #define GEN_PASS_DEF_EARLYCODEMOTION
23 #include "circt/Dialect/LLHD/Transforms/Passes.h.inc"
27 using namespace circt;
29 struct EarlyCodeMotionPass
30 :
public circt::llhd::impl::EarlyCodeMotionBase<EarlyCodeMotionPass> {
31 void runOnOperation()
override;
32 void runOnProcess(llhd::ProcessOp proc);
37 static SmallVector<Block *, 8>
intersection(SmallVectorImpl<Block *> &v1,
38 SmallVectorImpl<Block *> &v2) {
39 SmallVector<Block *, 8> res;
40 std::sort(v1.begin(), v1.end());
41 std::sort(v2.begin(), v2.end());
43 std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),
44 std::back_inserter(res));
48 void EarlyCodeMotionPass::runOnOperation() {
49 for (
auto proc : getOperation().getOps<llhd::ProcessOp>())
53 void EarlyCodeMotionPass::runOnProcess(llhd::ProcessOp proc) {
55 mlir::DominanceInfo dom(proc);
57 DenseMap<Block *, unsigned> entryDistance;
58 SmallPtrSet<Block *, 32> workDone;
59 SmallPtrSet<Block *, 32> workPending;
61 Block &entryBlock = proc.getBody().front();
62 workPending.insert(&entryBlock);
63 entryDistance.insert(std::make_pair(&entryBlock, 0));
65 while (!workPending.empty()) {
66 Block *block = *workPending.begin();
67 workPending.erase(block);
68 workDone.insert(block);
70 for (
auto iter = block->getOperations().begin();
71 iter != block->getOperations().end(); ++iter) {
72 Operation &op = *iter;
73 if (!isa<llhd::PrbOp>(op) && !isa<llhd::SignalOp>(op) &&
74 (!mlir::isMemoryEffectFree(&op) ||
75 op.hasTrait<OpTrait::IsTerminator>()))
78 SmallVector<Block *, 8> validPlacements;
80 for (Block &b : proc.getBody().getBlocks())
81 validPlacements.push_back(&b);
85 for (Value operand : op.getOperands()) {
86 SmallVector<Block *, 8> dominationSet;
87 Block *instBlock =
nullptr;
88 if (BlockArgument arg = dyn_cast<BlockArgument>(operand)) {
89 instBlock = arg.getParentBlock();
91 instBlock = operand.getDefiningOp()->getBlock();
94 for (Block &b : proc.getBody().getBlocks()) {
95 if (dom.dominates(instBlock, &b))
96 dominationSet.push_back(&b);
98 validPlacements =
intersection(dominationSet, validPlacements);
102 if (isa<llhd::PrbOp>(op)) {
103 SmallVector<Block *, 8> blocksInTR =
105 validPlacements =
intersection(validPlacements, blocksInTR);
108 if (validPlacements.empty())
113 unsigned minBBdist = -1;
114 Block *minBB =
nullptr;
115 for (Block *b : validPlacements) {
116 if (!entryDistance.count(b))
118 if (entryDistance[b] < minBBdist) {
119 minBBdist = entryDistance[b];
124 if (!minBB || minBB == op.getBlock())
127 auto prev = std::prev(iter);
128 op.moveBefore(minBB->getTerminator());
133 for (Block *succ : block->getSuccessors()) {
134 if (!workDone.count(succ)) {
135 workPending.insert(succ);
136 assert(entryDistance.count(block) &&
137 "ECM: block has to be present in entryDistance map");
138 unsigned nextDist = entryDistance[block] + 1;
139 entryDistance.insert(std::make_pair(succ, nextDist));
145 std::unique_ptr<OperationPass<hw::HWModuleOp>>
147 return std::make_unique<EarlyCodeMotionPass>();
assert(baseType &&"element must be base type")
static SmallVector< Block *, 8 > intersection(SmallVectorImpl< Block * > &v1, SmallVectorImpl< Block * > &v2)
Calculate intersection of two vectors, returns a new vector.
std::unique_ptr< OperationPass< hw::HWModuleOp > > createEarlyCodeMotionPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
int getBlockTR(Block *) const
SmallVector< Block *, 8 > getBlocksInTR(int) const