11 #include "mlir/Pass/Pass.h"
18 #include "mlir/IR/ImplicitLocOpBuilder.h"
19 #include "llvm/ADT/APSInt.h"
20 #include "llvm/ADT/BitVector.h"
21 #include "llvm/ADT/PostOrderIterator.h"
22 #include "llvm/Support/Debug.h"
24 #define DEBUG_TYPE "firrtl-remove-unused-ports"
28 #define GEN_PASS_DEF_REMOVEUNUSEDPORTS
29 #include "circt/Dialect/FIRRTL/Passes.h.inc"
33 using namespace circt;
34 using namespace firrtl;
37 struct RemoveUnusedPortsPass
38 :
public circt::firrtl::impl::RemoveUnusedPortsBase<RemoveUnusedPortsPass> {
39 void runOnOperation()
override;
40 void removeUnusedModulePorts(FModuleOp module,
47 bool ignoreDontTouch =
false;
51 void RemoveUnusedPortsPass::runOnOperation() {
52 auto &instanceGraph = getAnalysis<InstanceGraph>();
56 for (
auto *node : llvm::post_order(&instanceGraph))
57 if (
auto module = dyn_cast<FModuleOp>(*node->getModule()))
59 if (!module.isPublic())
60 removeUnusedModulePorts(module, node);
63 void RemoveUnusedPortsPass::removeUnusedModulePorts(
65 LLVM_DEBUG(llvm::dbgs() <<
"Prune ports of module: " << module.getName()
69 SmallVector<std::optional<APSInt>> outputPortConstants;
70 auto ports = module.getPorts();
72 llvm::BitVector removalPortIndexes(ports.size());
74 for (
const auto &e : llvm::enumerate(ports)) {
75 unsigned index = e.index();
76 auto port = e.value();
77 auto arg = module.getArgument(index);
81 if ((
hasDontTouch(arg) || !port.annotations.canBeDeleted()) &&
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 = builder.create<WireOp>(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);
158 unsigned outputPortIndex = 0;
159 for (
auto index : removalPortIndexes.set_bits()) {
160 auto result = instance.getResult(index);
161 assert(!ports[index].isInOut() &&
"don't expect inout ports");
165 if (ports[index].isInput()) {
166 WireOp wire = builder.create<WireOp>(result.getType());
170 bool onlyWritten = llvm::all_of(result.getUsers(), [&](Operation *op) {
171 if (auto connect = dyn_cast<FConnectLike>(op))
172 return connect.getDest() == result;
176 result.replaceUsesWithIf(wire.getResult(), [&](OpOperand &op) ->
bool {
178 if (onlyWritten && isa<FConnectLike>(op.getOwner())) {
179 op.getOwner()->erase();
186 if (wire.use_empty())
194 auto portConstant = outputPortConstants[outputPortIndex++];
197 value = builder.create<ConstantOp>(*portConstant);
199 value = builder.create<InvalidValueOp>(result.getType());
201 result.replaceAllUsesWith(value);
205 instance.erasePorts(builder, removalPortIndexes);
210 numRemovedPorts += removalPortIndexes.count();
213 std::unique_ptr<mlir::Pass>
215 auto pass = std::make_unique<RemoveUnusedPortsPass>();
216 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.