14 #include "mlir/IR/ImplicitLocOpBuilder.h"
15 #include "mlir/Pass/Pass.h"
16 #include "llvm/ADT/BitVector.h"
17 #include "llvm/ADT/PostOrderIterator.h"
18 #include "llvm/Support/Debug.h"
20 #define DEBUG_TYPE "firrtl-remove-unused-ports"
24 #define GEN_PASS_DEF_REMOVEUNUSEDPORTS
25 #include "circt/Dialect/FIRRTL/Passes.h.inc"
29 using namespace circt;
30 using namespace firrtl;
33 struct RemoveUnusedPortsPass
34 :
public circt::firrtl::impl::RemoveUnusedPortsBase<RemoveUnusedPortsPass> {
35 void runOnOperation()
override;
36 void removeUnusedModulePorts(FModuleOp module,
43 bool ignoreDontTouch =
false;
47 void RemoveUnusedPortsPass::runOnOperation() {
48 auto &instanceGraph = getAnalysis<InstanceGraph>();
52 for (
auto *node : llvm::post_order(&instanceGraph))
53 if (
auto module = dyn_cast<FModuleOp>(*node->getModule()))
55 if (!module.isPublic())
56 removeUnusedModulePorts(module, node);
59 void RemoveUnusedPortsPass::removeUnusedModulePorts(
61 LLVM_DEBUG(llvm::dbgs() <<
"Prune ports of module: " << module.getName()
65 SmallVector<std::optional<APSInt>> outputPortConstants;
66 auto ports = module.getPorts();
68 llvm::BitVector removalPortIndexes(ports.size());
70 for (
const auto &e : llvm::enumerate(ports)) {
71 unsigned index = e.index();
72 auto port = e.value();
73 auto arg = module.getArgument(index);
77 if ((
hasDontTouch(arg) || !port.annotations.empty()) && !ignoreDontTouch)
86 if (port.isInput() && !arg.use_empty())
90 auto port = a->getInstance()->getResult(arg.getArgNumber());
91 return port.getUses().empty();
95 if (port.isOutput()) {
96 if (arg.use_empty()) {
99 outputPortConstants.push_back(std::nullopt);
100 }
else if (llvm::all_of(instanceGraphNode->
uses(), portIsUnused)) {
102 auto builder = ImplicitLocOpBuilder::atBlockBegin(
103 arg.getLoc(), module.getBodyBlock());
104 auto wire = builder.create<WireOp>(arg.getType());
105 arg.replaceAllUsesWith(wire.getResult());
106 outputPortConstants.push_back(std::nullopt);
107 }
else if (arg.hasOneUse()) {
110 Operation *op = arg.use_begin().getUser();
111 auto connectLike = dyn_cast<FConnectLike>(op);
114 auto *srcOp = connectLike.getSrc().getDefiningOp();
115 if (!isa_and_nonnull<InvalidValueOp, ConstantOp>(srcOp))
118 if (
auto constant = dyn_cast<ConstantOp>(srcOp))
119 outputPortConstants.push_back(constant.getValue());
121 assert(isa<InvalidValueOp>(srcOp) &&
"only expect invalid");
122 outputPortConstants.push_back(std::nullopt);
128 if (srcOp->use_empty())
136 removalPortIndexes.set(index);
140 if (removalPortIndexes.none())
144 module.erasePorts(removalPortIndexes);
145 LLVM_DEBUG(llvm::for_each(removalPortIndexes.set_bits(), [&](
unsigned index) {
146 llvm::dbgs() <<
"Delete port: " << ports[index].name <<
"\n";
150 for (
auto *use : instanceGraphNode->
uses()) {
151 auto instance = ::cast<InstanceOp>(*use->getInstance());
152 ImplicitLocOpBuilder builder(instance.getLoc(), instance);
153 unsigned outputPortIndex = 0;
154 for (
auto index : removalPortIndexes.set_bits()) {
155 auto result = instance.getResult(index);
156 assert(!ports[index].isInOut() &&
"don't expect inout ports");
160 if (ports[index].isInput()) {
161 WireOp wire = builder.create<WireOp>(result.getType());
165 bool onlyWritten = llvm::all_of(result.getUsers(), [&](Operation *op) {
166 if (auto connect = dyn_cast<FConnectLike>(op))
167 return connect.getDest() == result;
171 result.replaceUsesWithIf(wire.getResult(), [&](OpOperand &op) ->
bool {
173 if (onlyWritten && isa<FConnectLike>(op.getOwner())) {
174 op.getOwner()->erase();
181 if (wire.use_empty())
189 auto portConstant = outputPortConstants[outputPortIndex++];
192 value = builder.create<ConstantOp>(*portConstant);
194 value = builder.create<InvalidValueOp>(result.getType());
196 result.replaceAllUsesWith(value);
200 instance.erasePorts(builder, removalPortIndexes);
205 numRemovedPorts += removalPortIndexes.count();
208 std::unique_ptr<mlir::Pass>
210 auto pass = std::make_unique<RemoveUnusedPortsPass>();
211 pass->ignoreDontTouch = ignoreDontTouch;
assert(baseType &&"element must be base type")
This is a Node in the InstanceGraph.
llvm::iterator_range< UseIterator > uses()
This is an edge in the InstanceGraph.
std::unique_ptr< mlir::Pass > createRemoveUnusedPortsPass(bool ignoreDontTouch=false)
bool hasDontTouch(Value value)
Check whether a block argument ("port") or the operation defining a value has a DontTouch annotation,...
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
llvm::raw_ostream & debugPassHeader(const mlir::Pass *pass, int width=80)
Write a boilerplate header for a pass to the debug stream.