Skip to content

Commit 4ab3a06

Browse files
committed
[FIRRTL][LowerToHW] Lower firrtl.bind ops under emit.file ops
1 parent d6ec312 commit 4ab3a06

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

lib/Conversion/FIRRTLToHW/LowerToHW.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ struct FIRRTLModuleLowering
576576
CircuitLoweringState &loweringState);
577577
LogicalResult lowerSimulationBody(verif::SimulationOp simulationOp,
578578
CircuitLoweringState &loweringState);
579+
LogicalResult lowerFileBody(emit::FileOp op);
579580
LogicalResult lowerBody(Operation *op, CircuitLoweringState &loweringState);
580581
};
581582

@@ -691,6 +692,11 @@ void FIRRTLModuleLowering::runOnOperation() {
691692
opsToProcess.push_back(newOp);
692693
return success();
693694
})
695+
.Case<emit::FileOp>([&](auto fileOp) {
696+
fileOp->moveBefore(topLevelModule, topLevelModule->end());
697+
opsToProcess.push_back(fileOp);
698+
return success();
699+
})
694700
.Default([&](Operation *op) {
695701
// We don't know what this op is. If it has no illegal FIRRTL
696702
// types, we can forward the operation. Otherwise, we emit an
@@ -1779,6 +1785,7 @@ struct FIRRTLLowering : public FIRRTLVisitor<FIRRTLLowering, LogicalResult> {
17791785
LogicalResult visitStmt(RefForceInitialOp op);
17801786
LogicalResult visitStmt(RefReleaseOp op);
17811787
LogicalResult visitStmt(RefReleaseInitialOp op);
1788+
LogicalResult visitStmt(BindOp op);
17821789

17831790
FailureOr<Value> lowerSubindex(SubindexOp op, Value input);
17841791
FailureOr<Value> lowerSubaccess(SubaccessOp op, Value input);
@@ -1891,6 +1898,18 @@ FIRRTLModuleLowering::lowerModuleBody(hw::HWModuleOp module,
18911898
return FIRRTLLowering(module, loweringState).run();
18921899
}
18931900

1901+
LogicalResult FIRRTLModuleLowering::lowerFileBody(emit::FileOp fileOp) {
1902+
OpBuilder b(&getContext());
1903+
fileOp->walk([&](Operation *op) {
1904+
if (auto bindOp = dyn_cast<BindOp>(op)) {
1905+
b.setInsertionPointAfter(bindOp);
1906+
b.create<sv::BindOp>(bindOp.getLoc(), bindOp.getInstanceAttr());
1907+
bindOp->erase();
1908+
}
1909+
});
1910+
return success();
1911+
}
1912+
18941913
LogicalResult
18951914
FIRRTLModuleLowering::lowerBody(Operation *op,
18961915
CircuitLoweringState &loweringState) {
@@ -1900,6 +1919,8 @@ FIRRTLModuleLowering::lowerBody(Operation *op,
19001919
return lowerFormalBody(formalOp, loweringState);
19011920
if (auto simulationOp = dyn_cast<verif::SimulationOp>(op))
19021921
return lowerSimulationBody(simulationOp, loweringState);
1922+
if (auto fileOp = dyn_cast<emit::FileOp>(op))
1923+
return lowerFileBody(fileOp);
19031924
return failure();
19041925
}
19051926

@@ -5050,6 +5071,11 @@ LogicalResult FIRRTLLowering::visitStmt(AttachOp op) {
50505071
return success();
50515072
}
50525073

5074+
LogicalResult FIRRTLLowering::visitStmt(BindOp op) {
5075+
builder.create<sv::BindOp>(op.getInstanceAttr());
5076+
return success();
5077+
}
5078+
50535079
LogicalResult FIRRTLLowering::fixupLTLOps() {
50545080
if (ltlOpFixupWorklist.empty())
50555081
return success();

test/Conversion/FIRRTLToHW/lower-to-hw.mlir

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,23 @@ firrtl.circuit "Simple" attributes {annotations = [{class =
689689
firrtl.instance foo {doNotPrint} @foo()
690690
}
691691

692+
// Check that explicit bind ops are lowered to sv.bind, even if they are
693+
// buried in an emit block.
694+
695+
firrtl.module @BoundModule() {}
696+
697+
// CHECK-LABEL: hw.module @ExplicitBindTest()
698+
firrtl.module @ExplicitBindTest() {
699+
// CHECK: hw.instance "boundInstance" sym @boundInstance @BoundModule() -> () {doNotPrint}
700+
firrtl.instance boundInstance sym @boundInstance {doNotPrint} @BoundModule()
701+
}
702+
703+
// CHECK: emit.file "some-file.sv"
704+
emit.file "some-file.sv" {
705+
// CHECK: sv.bind <@ExplicitBindTest::@boundInstance>
706+
firrtl.bind <@ExplicitBindTest::@boundInstance>
707+
}
708+
692709
// CHECK-LABEL: hw.module private @attributes_preservation
693710
// CHECK-SAME: firrtl.foo = "bar"
694711
// CHECK-SAME: output_file = #hw.output_file<"output_fileTest.sv", excludeFromFileList>

0 commit comments

Comments
 (0)