15#include "mlir/IR/ImplicitLocOpBuilder.h"
16#include "mlir/Pass/Pass.h"
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/PostOrderIterator.h"
19#include "llvm/Support/Debug.h"
21#define DEBUG_TYPE "firrtl-remove-unused-ports"
25#define GEN_PASS_DEF_REMOVEUNUSEDPORTS
26#include "circt/Dialect/FIRRTL/Passes.h.inc"
31using namespace firrtl;
35struct RemoveUnusedPortsPass
36 :
public circt::firrtl::impl::RemoveUnusedPortsBase<RemoveUnusedPortsPass> {
39 void runOnOperation()
override;
40 void removeUnusedModulePorts(FModuleOp module,
47 bool ignoreDontTouch =
false;
51void RemoveUnusedPortsPass::runOnOperation() {
54 auto &instanceGraph = getAnalysis<InstanceGraph>();
57 for (
auto *node :
llvm::post_order(&instanceGraph))
58 if (auto module = dyn_cast<FModuleOp>(*node->getModule()))
60 if (!module.isPublic())
61 removeUnusedModulePorts(module, node);
64void RemoveUnusedPortsPass::removeUnusedModulePorts(
66 LLVM_DEBUG(llvm::dbgs() <<
"Prune ports of module: " << module.getName()
70 SmallVector<std::optional<APSInt>> outputPortConstants;
71 auto ports =
module.getPorts();
73 llvm::BitVector removalPortIndexes(ports.size());
75 for (
const auto &e :
llvm::enumerate(ports)) {
76 unsigned index = e.index();
77 auto port = e.value();
78 auto arg =
module.getArgument(index);
82 if ((
hasDontTouch(arg) || !port.annotations.empty()) && !ignoreDontTouch)
91 if (port.isInput() && !arg.use_empty())
95 auto port = a->getInstance()->getResult(arg.getArgNumber());
96 return port.getUses().empty();
100 if (port.isOutput()) {
101 if (arg.use_empty()) {
104 outputPortConstants.push_back(std::nullopt);
105 }
else if (llvm::all_of(instanceGraphNode->
uses(), portIsUnused)) {
107 auto builder = ImplicitLocOpBuilder::atBlockBegin(
108 arg.getLoc(), module.getBodyBlock());
109 auto wire = WireOp::create(builder, arg.getType());
110 arg.replaceAllUsesWith(wire.getResult());
111 outputPortConstants.push_back(std::nullopt);
112 }
else if (arg.hasOneUse()) {
115 Operation *op = arg.use_begin().getUser();
116 auto connectLike = dyn_cast<FConnectLike>(op);
119 auto *srcOp = connectLike.getSrc().getDefiningOp();
120 if (!isa_and_nonnull<InvalidValueOp, ConstantOp>(srcOp))
123 if (
auto constant = dyn_cast<ConstantOp>(srcOp))
124 outputPortConstants.push_back(constant.getValue());
126 assert(isa<InvalidValueOp>(srcOp) &&
"only expect invalid");
127 outputPortConstants.push_back(std::nullopt);
133 if (srcOp->use_empty())
141 removalPortIndexes.set(index);
145 if (removalPortIndexes.none())
149 module.erasePorts(removalPortIndexes);
150 LLVM_DEBUG(llvm::for_each(removalPortIndexes.set_bits(), [&](
unsigned index) {
151 llvm::dbgs() <<
"Delete port: " << ports[index].name <<
"\n";
155 for (
auto *use : instanceGraphNode->uses()) {
156 auto instance = ::cast<InstanceOp>(*use->getInstance());
157 ImplicitLocOpBuilder builder(instance.getLoc(), instance);
159 unsigned outputPortIndex = 0;
160 for (
auto index : removalPortIndexes.set_bits()) {
161 auto result = instance.getResult(index);
162 assert(!ports[index].isInOut() &&
"don't expect inout ports");
166 if (ports[index].isInput()) {
167 WireOp wire = WireOp::create(builder, result.getType());
171 bool onlyWritten = llvm::all_of(result.getUsers(), [&](Operation *op) {
172 if (auto connect = dyn_cast<FConnectLike>(op))
173 return connect.getDest() == result;
177 result.replaceUsesWithIf(wire.getResult(), [&](OpOperand &op) ->
bool {
179 if (onlyWritten && isa<FConnectLike>(op.getOwner())) {
180 op.getOwner()->erase();
187 if (wire.use_empty())
195 auto portConstant = outputPortConstants[outputPortIndex++];
198 value = ConstantOp::create(builder, *portConstant);
202 dyn_cast<firrtl::FIRRTLBaseType>(result.getType())) {
203 value = tieOffCache.getInvalid(baseType);
211 result.replaceAllUsesWith(value);
215 instance.cloneWithErasedPortsAndReplaceUses(removalPortIndexes);
220 numRemovedPorts += removalPortIndexes.count();
assert(baseType &&"element must be base type")
#define CIRCT_DEBUG_SCOPED_PASS_LOGGER(PASS)
Helper class to cache tie-off values for different FIRRTL types.
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.