Skip to content

Commit

Permalink
[SeqToSV] Do not use always_ff for compreg with initializer (#7838)
Browse files Browse the repository at this point in the history
  • Loading branch information
fzi-hielscher authored Nov 20, 2024
1 parent 65e230c commit 2d416af
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,7 @@ def LowerSeqToSV: Pass<"lower-seq-to-sv", "mlir::ModuleOp"> {
Option<"emitSeparateAlwaysBlocks", "emit-separate-always-blocks", "bool", "false",
"Emit assigments to registers in separate always blocks">,
Option<"lowerToAlwaysFF", "lower-to-always-ff", "bool", "true",
"Place assignments to registers into `always_ff` blocks">
"Place assignments to registers into `always_ff` blocks if possible">
];
let statistics = [
Statistic<"numSubaccessRestored", "num-subaccess-restored",
Expand Down
2 changes: 1 addition & 1 deletion integration_test/Bindings/Python/dialects/seq.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def top(module):

pm = PassManager.parse("builtin.module(lower-seq-to-sv,canonicalize)")
pm.run(m.operation)
# CHECK: always_ff @(posedge clk)
# CHECK: always @(posedge clk)
# CHECK: my_reg <= {{.+}}
# CHECK: (* no_merge *)
# CHECK: reg [31:0] reg1;
Expand Down
9 changes: 7 additions & 2 deletions lib/Conversion/SeqToSV/SeqToSV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,13 @@ class CompRegLower : public OpConversionPattern<OpTy> {
rewriter.create<sv::PAssignOp>(loc, svReg, adaptor.getResetValue());
};

// Registers written in an `always_ff` process may not have any assignments
// outside of that process.
// For some tools this also prohibits inititalization.
bool mayLowerToAlwaysFF = lowerToAlwaysFF && !reg.getInitialValue();

if (adaptor.getReset() && adaptor.getResetValue()) {
if (lowerToAlwaysFF) {
if (mayLowerToAlwaysFF) {
rewriter.create<sv::AlwaysFFOp>(
loc, sv::EventControl::AtPosEdge, adaptor.getClk(),
sv::ResetType::SyncReset, sv::EventControl::AtPosEdge,
Expand All @@ -191,7 +196,7 @@ class CompRegLower : public OpConversionPattern<OpTy> {
});
}
} else {
if (lowerToAlwaysFF) {
if (mayLowerToAlwaysFF) {
rewriter.create<sv::AlwaysFFOp>(loc, sv::EventControl::AtPosEdge,
adaptor.getClk(), assignValue);
} else {
Expand Down
35 changes: 35 additions & 0 deletions test/Conversion/SeqToSV/compreg.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: circt-opt %s --lower-seq-to-sv=lower-to-always-ff | FileCheck %s

// CHECK-LABEL: hw.module @basic(in %clk : i1, in %d : i8, out q : i8) {
// CHECK: %[[REG:.*]] = sv.reg : !hw.inout<i8>
// CHECK: %[[RD:.*]] = sv.read_inout %[[REG]] : !hw.inout<i8>
// CHECK: sv.alwaysff(posedge %clk) {
// CHECK-NEXT: sv.passign %[[REG]], %d : i8
// CHECK-NEXT: }
// CHECK-NEXT: hw.output %[[RD]] : i8
// CHECK-NEXT: }
hw.module @basic(in %clk: !seq.clock, in %d: i8, out q: i8) {
%q = seq.compreg %d, %clk : i8
hw.output %q : i8
}

// CHECK-LABEL: hw.module @basicWithInit(in %clk : i1, in %d : i8, out q : i8) {
// CHECK: sv.initial {
// CHECK: }
// CHECK: %[[CST:.*]] = hw.constant 19 : i8
// CHECK: %[[REG:.*]] = sv.reg init %[[CST]] : !hw.inout<i8>
// CHECK: %[[RD:.*]] = sv.read_inout %[[REG]] : !hw.inout<i8>
// CHECK: sv.always posedge %clk {
// CHECK-NEXT: sv.passign %[[REG]], %d : i8
// CHECK-NEXT: }
// CHECK-NEXT: hw.output %[[RD]] : i8
// CHECK-NEXT: }
hw.module @basicWithInit(in %clk: !seq.clock, in %d: i8, out q: i8) {
%init = seq.initial () {
%cst = hw.constant 19 : i8
seq.yield %cst : i8
} : () -> !seq.immutable<i8>

%q = seq.compreg %d, %clk initial %init : i8
hw.output %q : i8
}

1 comment on commit 2d416af

@fzi-hielscher
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CI failure seems unrelated. See #7859.

Please sign in to comment.