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"
30using namespace firrtl;
33struct RemoveUnusedPortsPass
34 :
public circt::firrtl::impl::RemoveUnusedPortsBase<RemoveUnusedPortsPass> {
37 void runOnOperation()
override;
38 void removeUnusedModulePorts(FModuleOp module,
45 bool ignoreDontTouch =
false;
49void RemoveUnusedPortsPass::runOnOperation() {
50 auto &instanceGraph = getAnalysis<InstanceGraph>();
54 for (
auto *node :
llvm::post_order(&instanceGraph))
55 if (auto module = dyn_cast<FModuleOp>(*node->getModule()))
57 if (!module.isPublic())
58 removeUnusedModulePorts(module, node);
61void RemoveUnusedPortsPass::removeUnusedModulePorts(
63 LLVM_DEBUG(llvm::dbgs() <<
"Prune ports of module: " << module.getName()
67 SmallVector<std::optional<APSInt>> outputPortConstants;
68 auto ports =
module.getPorts();
70 llvm::BitVector removalPortIndexes(ports.size());
72 for (
const auto &e :
llvm::enumerate(ports)) {
73 unsigned index = e.index();
74 auto port = e.value();
75 auto arg =
module.getArgument(index);
79 if ((
hasDontTouch(arg) || !port.annotations.empty()) && !ignoreDontTouch)
88 if (port.isInput() && !arg.use_empty())
92 auto port = a->getInstance()->getResult(arg.getArgNumber());
93 return port.getUses().empty();
97 if (port.isOutput()) {
98 if (arg.use_empty()) {
101 outputPortConstants.push_back(std::nullopt);
102 }
else if (llvm::all_of(instanceGraphNode->
uses(), portIsUnused)) {
104 auto builder = ImplicitLocOpBuilder::atBlockBegin(
105 arg.getLoc(), module.getBodyBlock());
106 auto wire = builder.create<WireOp>(arg.getType());
107 arg.replaceAllUsesWith(wire.getResult());
108 outputPortConstants.push_back(std::nullopt);
109 }
else if (arg.hasOneUse()) {
112 Operation *op = arg.use_begin().getUser();
113 auto connectLike = dyn_cast<FConnectLike>(op);
116 auto *srcOp = connectLike.getSrc().getDefiningOp();
117 if (!isa_and_nonnull<InvalidValueOp, ConstantOp>(srcOp))
120 if (
auto constant = dyn_cast<ConstantOp>(srcOp))
121 outputPortConstants.push_back(constant.getValue());
123 assert(isa<InvalidValueOp>(srcOp) &&
"only expect invalid");
124 outputPortConstants.push_back(std::nullopt);
130 if (srcOp->use_empty())
138 removalPortIndexes.set(index);
142 if (removalPortIndexes.none())
146 module.erasePorts(removalPortIndexes);
147 LLVM_DEBUG(llvm::for_each(removalPortIndexes.set_bits(), [&](
unsigned index) {
148 llvm::dbgs() <<
"Delete port: " << ports[index].name <<
"\n";
152 for (
auto *use : instanceGraphNode->uses()) {
153 auto instance = ::cast<InstanceOp>(*use->getInstance());
154 ImplicitLocOpBuilder builder(instance.getLoc(), instance);
155 unsigned outputPortIndex = 0;
156 for (
auto index : removalPortIndexes.set_bits()) {
157 auto result = instance.getResult(index);
158 assert(!ports[index].isInOut() &&
"don't expect inout ports");
162 if (ports[index].isInput()) {
163 WireOp wire = builder.create<WireOp>(result.getType());
167 bool onlyWritten = llvm::all_of(result.getUsers(), [&](Operation *op) {
168 if (auto connect = dyn_cast<FConnectLike>(op))
169 return connect.getDest() == result;
173 result.replaceUsesWithIf(wire.getResult(), [&](OpOperand &op) ->
bool {
175 if (onlyWritten && isa<FConnectLike>(op.getOwner())) {
176 op.getOwner()->erase();
183 if (wire.use_empty())
191 auto portConstant = outputPortConstants[outputPortIndex++];
194 value = builder.create<ConstantOp>(*portConstant);
196 value = builder.create<InvalidValueOp>(result.getType());
198 result.replaceAllUsesWith(value);
202 instance.erasePorts(builder, removalPortIndexes);
207 numRemovedPorts += removalPortIndexes.count();
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.
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.