CIRCT 24.0.0git
Loading...
Searching...
No Matches
CaptureUtils.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"
11#include "mlir/IR/Dominance.h"
12#include "llvm/ADT/SmallPtrSet.h"
13#include "llvm/Support/Debug.h"
14#include "llvm/Support/GenericIteratedDominanceFrontier.h"
15
16#define DEBUG_TYPE "llhd-capture-utils"
17
18using namespace mlir;
19using namespace circt;
20using namespace llhd;
21
22/// Add a probe as block argument to a list of wait ops and update uses of the
23/// probe to use the added block arguments as appropriate. This may insert
24/// additional block arguments in case the probe and added block arguments both
25/// reach the same block.
26void llhd::captureAcrossWait(Value value, ArrayRef<WaitOp> waitOps,
27 Liveness &liveness, DominanceInfo &dominance) {
28 // Constant-like values are guaranteed not to change across a wait and can
29 // be rematerialized freely, so they never need to be captured. Everything
30 // else defined inside the process, including time and ref values derived
31 // from mutable state, must be captured so that uses after the wait observe
32 // the value from before the wait. Values defined outside the process are
33 // filtered by the caller.
34 if (auto *defOp = value.getDefiningOp())
35 if (defOp->hasTrait<OpTrait::ConstantLike>())
36 return;
37
38 SmallVector<WaitOp> capturedWaitOps;
39 for (auto waitOp : waitOps) {
40 auto *waitBlock = waitOp->getBlock();
41 auto *blockLiveness = liveness.getLiveness(waitBlock);
42 if (!blockLiveness || !blockLiveness->isLiveOut(value))
43 continue;
44 if (!dominance.dominates(value, waitOp.getOperation()))
45 continue;
46 capturedWaitOps.push_back(waitOp);
47 }
48 if (capturedWaitOps.empty())
49 return;
50
51 LLVM_DEBUG({
52 llvm::dbgs() << "Capture " << value << "\n";
53 for (auto waitOp : capturedWaitOps)
54 llvm::dbgs() << "- Across " << waitOp << "\n";
55 });
56
57 // Calculate the merge points for this probe once it gets promoted to block
58 // arguments across the wait ops.
59 Region &region = *value.getParentRegion();
60 auto &domTree = dominance.getDomTree(&region);
61 llvm::IDFCalculatorBase<Block, false> idfCalculator(domTree);
62
63 // Calculate the set of blocks which will define this probe as a distinct
64 // value.
65 SmallPtrSet<Block *, 4> definingBlocks;
66 definingBlocks.insert(value.getParentBlock());
67 for (auto waitOp : capturedWaitOps)
68 definingBlocks.insert(waitOp.getDest());
69 idfCalculator.setDefiningBlocks(definingBlocks);
70
71 // Calculate where the probe is live.
72 SmallPtrSet<Block *, 16> liveInBlocks;
73 for (auto &block : region)
74 if (liveness.getLiveness(&block)->isLiveIn(value))
75 liveInBlocks.insert(&block);
76 idfCalculator.setLiveInBlocks(liveInBlocks);
77
78 // Calculate the merge points where we will have to insert block arguments for
79 // this probe.
80 SmallVector<Block *> mergePointsVec;
81 idfCalculator.calculate(mergePointsVec);
82 SmallPtrSet<Block *, 16> mergePoints(mergePointsVec.begin(),
83 mergePointsVec.end());
84 for (auto waitOp : capturedWaitOps)
85 mergePoints.insert(waitOp.getDest());
86 LLVM_DEBUG(llvm::dbgs() << "- " << mergePoints.size() << " merge points\n");
87
88 // Perform a depth-first search starting at the block containing the probe,
89 // which dominates all its uses. When we encounter a block that is a merge
90 // point, insert a block argument.
91 struct WorklistItem {
92 DominanceInfoNode *domNode;
93 Value reachingDef;
94 };
95 SmallVector<WorklistItem> worklist;
96 worklist.push_back({domTree.getNode(value.getParentBlock()), value});
97
98 while (!worklist.empty()) {
99 auto item = worklist.pop_back_val();
100 auto *block = item.domNode->getBlock();
101
102 // If this block is a merge point, insert a block argument for the probe.
103 if (mergePoints.contains(block))
104 item.reachingDef = block->addArgument(value.getType(), value.getLoc());
105
106 // Replace any uses of the probe in this block with the current reaching
107 // definition.
108 for (auto &op : *block)
109 op.replaceUsesOfWith(value, item.reachingDef);
110
111 // If the terminator of this block branches to a merge point, add the
112 // current reaching definition as a destination operand.
113 if (auto branchOp = dyn_cast<BranchOpInterface>(block->getTerminator())) {
114 for (auto &blockOperand : branchOp->getBlockOperands())
115 if (mergePoints.contains(blockOperand.get()))
116 branchOp.getSuccessorOperands(blockOperand.getOperandNumber())
117 .append(item.reachingDef);
118 } else if (auto waitOp = dyn_cast<WaitOp>(block->getTerminator())) {
119 if (mergePoints.contains(waitOp.getDest()))
120 waitOp.getDestOperandsMutable().append(item.reachingDef);
121 }
122
123 for (auto *child : item.domNode->children())
124 worklist.push_back({child, item.reachingDef});
125 }
126}
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.