Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HWToBTOR2] Fix crashes on initial value corner cases #7844

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions lib/Conversion/HWToBTOR2/HWToBTOR2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,8 @@ struct ConvertHWToBTOR2Pass
void visit(hw::PortInfo &port) {
// Separate the inputs from outputs and generate the first btor2 lines for
// input declaration We only consider ports with an explicit bit-width (so
// ignore clocks)
if (port.isInput() && !isa<seq::ClockType>(port.type)) {
// ignore clocks and immutables)
if (port.isInput() && !isa<seq::ClockType, seq::ImmutableType>(port.type)) {
// Generate the associated btor declaration for the inputs
StringRef iName = port.getName();

Expand Down Expand Up @@ -880,6 +880,11 @@ struct ConvertHWToBTOR2Pass
genState(reg, w, regName);

if (init) {
if (!init.getDefiningOp<seq::InitialOp>()) {
reg->emitError(
"Initial value must be emitted directly by a seq.initial op");
return;
}
// Check that the initial value is a non-null constant
auto initialConstant = circt::seq::unwrapImmutableValue(init)
.getDefiningOp<hw::ConstantOp>();
Expand Down
25 changes: 25 additions & 0 deletions test/Conversion/HWToBTOR2/errors.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// RUN: circt-opt %s --convert-hw-to-btor2 --verify-diagnostics --split-input-file -o tmp.mlir

hw.module @init_emitter(out out: !seq.immutable<i32>) {
%init = seq.initial () {
%c0_i32 = hw.constant 0 : i32
seq.yield %c0_i32 : i32
} : () -> !seq.immutable<i32>
hw.output %init : !seq.immutable<i32>
}

hw.module @reg_with_instance_initial(in %clk: !seq.clock, in %in: i32, out out: i32) {
// expected-error @below {{'hw.instance' op not supported in BTOR2 conversion}}
%init = hw.instance "foo" @init_emitter () -> (out: !seq.immutable<i32>)
// expected-error @below {{Initial value must be emitted directly by a seq.initial op}}
%1 = seq.compreg %in, %clk initial %init : i32
hw.output %1 : i32
}

// -----

hw.module @reg_with_argument_initial(in %clk: !seq.clock, in %in: i32, in %init: !seq.immutable<i32>, out out: i32) {
// expected-error @below {{Initial value must be emitted directly by a seq.initial op}}
%1 = seq.compreg %in, %clk initial %init : i32
hw.output %1 : i32
}
Loading