Skip to content

Commit e152380

Browse files
committed
[FIRRTL] Improve eager inliner
Make the eager inliner reduction work even if there are multiple instances of a module.
1 parent fc5ffe3 commit e152380

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

lib/Dialect/FIRRTL/FIRRTLReductions.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -972,45 +972,49 @@ struct EagerInliner : public OpReduction<firrtl::InstanceOp> {
972972
auto *tableOp = SymbolTable::getNearestSymbolTable(instOp);
973973
auto *moduleOp =
974974
instOp.getReferencedOperation(symbols.getSymbolTable(tableOp));
975-
if (!isa<firrtl::FModuleOp>(moduleOp))
976-
return 0;
977-
return symbols.getSymbolUserMap(tableOp).getUsers(moduleOp).size() == 1;
975+
976+
return isa<firrtl::FModuleOp>(moduleOp);
978977
}
979978

980979
LogicalResult rewrite(firrtl::InstanceOp instOp) override {
981-
LLVM_DEBUG(llvm::dbgs()
982-
<< "Inlining instance `" << instOp.getName() << "`\n");
983-
SmallVector<Value> argReplacements;
984-
ImplicitLocOpBuilder builder(instOp.getLoc(), instOp);
980+
auto *tableOp = SymbolTable::getNearestSymbolTable(instOp);
981+
auto moduleOp = cast<firrtl::FModuleOp>(
982+
instOp.getReferencedOperation(symbols.getSymbolTable(tableOp)));
983+
bool isLastUse =
984+
(symbols.getSymbolUserMap(tableOp).getUsers(moduleOp).size() == 1);
985+
auto clonedModuleOp = isLastUse ? moduleOp : moduleOp.clone();
986+
987+
// Create wires to replace the instance results.
988+
IRRewriter rewriter(instOp);
989+
SmallVector<Value> argWires;
985990
for (unsigned i = 0, e = instOp.getNumResults(); i != e; ++i) {
986991
auto result = instOp.getResult(i);
987-
auto name = builder.getStringAttr(Twine(instOp.getName()) + "_" +
988-
instOp.getPortNameStr(i));
992+
auto name = rewriter.getStringAttr(Twine(instOp.getName()) + "_" +
993+
instOp.getPortNameStr(i));
989994
auto wire =
990-
firrtl::WireOp::create(builder, result.getType(), name,
991-
firrtl::NameKindEnum::DroppableName,
995+
firrtl::WireOp::create(rewriter, instOp.getLoc(), result.getType(),
996+
name, firrtl::NameKindEnum::DroppableName,
992997
instOp.getPortAnnotation(i), StringAttr{})
993998
.getResult();
994999
result.replaceAllUsesWith(wire);
995-
argReplacements.push_back(wire);
996-
}
997-
auto *tableOp = SymbolTable::getNearestSymbolTable(instOp);
998-
auto moduleOp = cast<firrtl::FModuleOp>(
999-
instOp.getReferencedOperation(symbols.getSymbolTable(tableOp)));
1000-
for (auto &op : llvm::make_early_inc_range(*moduleOp.getBodyBlock())) {
1001-
op.remove();
1002-
builder.insert(&op);
1003-
for (auto &operand : op.getOpOperands())
1004-
if (auto blockArg = dyn_cast<BlockArgument>(operand.get()))
1005-
operand.set(argReplacements[blockArg.getArgNumber()]);
1000+
argWires.push_back(wire);
10061001
}
1002+
1003+
// Splice in the cloned module body.
1004+
rewriter.inlineBlockBefore(clonedModuleOp.getBodyBlock(), instOp, argWires);
1005+
1006+
// Make sure we remove any NLAs that go through this instance, and the
1007+
// module if we're about the delete the module.
10071008
nlaRemover.markNLAsInOperation(instOp);
1008-
instOp->erase();
1009-
moduleOp->erase();
1009+
if (isLastUse)
1010+
nlaRemover.markNLAsInOperation(moduleOp);
1011+
1012+
instOp.erase();
1013+
clonedModuleOp.erase();
10101014
return success();
10111015
}
10121016

1013-
std::string getName() const override { return "eager-inliner"; }
1017+
std::string getName() const override { return "firrtl-eager-inliner"; }
10141018
bool acceptSizeIncrease() const override { return true; }
10151019

10161020
::detail::SymbolCache symbols;

test/Dialect/FIRRTL/Reduction/pattern-registration.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// CHECK-DAG: connect-source-operand-2-forwarder
1717
// CHECK-DAG: cse
1818
// CHECK-DAG: detach-subaccesses
19-
// CHECK-DAG: eager-inliner
19+
// CHECK-DAG: firrtl-eager-inliner
2020
// CHECK-DAG: extmodule-instance-remover
2121
// CHECK-DAG: firrtl-constantifier
2222
// CHECK-DAG: firrtl-expand-whens

tools/circt-reduce/circt-reduce.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,10 +481,16 @@ static LogicalResult execute(MLIRContext &context) {
481481

482482
// Write the reduced test case to the output.
483483
clearSummary();
484-
VERBOSE(llvm::errs() << "All reduction strategies exhausted\n");
485-
VERBOSE(llvm::errs() << "Final size: " << bestSize << " ("
486-
<< (100 - bestSize * 100 / initialTest.getSize())
487-
<< "% reduction)\n");
484+
VERBOSE({
485+
llvm::errs() << "All reduction strategies exhausted\n";
486+
llvm::errs() << "Final size: " << bestSize << " (";
487+
if (bestSize > initialTest.getSize())
488+
llvm::errs() << (bestSize * 100 / initialTest.getSize() - 100)
489+
<< "% increase)\n";
490+
else
491+
llvm::errs() << (100 - bestSize * 100 / initialTest.getSize())
492+
<< "% reduction)\n";
493+
});
488494
return writeOutput(module.get());
489495
}
490496

0 commit comments

Comments
 (0)