Skip to content

Commit

Permalink
[ImportVerilog] Fix bugs with constant folding (#8213)
Browse files Browse the repository at this point in the history
Co-authored-by: Yan Churkin <[email protected]>
  • Loading branch information
likeamahoney and Yan Churkin authored Feb 10, 2025
1 parent 6e93a0e commit 2d2bee8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion include/circt/Dialect/Moore/MooreOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ def ConstantOp : MooreOp<"constant", [Pure, ConstantLike]> {
let builders = [
OpBuilder<(ins "IntType":$type, "const FVInt &":$value)>,
OpBuilder<(ins "IntType":$type, "const APInt &":$value)>,
OpBuilder<(ins "IntType":$type, "int64_t":$value)>,
OpBuilder<(ins "IntType":$type, "int64_t":$value, CArg<"bool", "true">:$isSigned)>,
];
}

Expand Down
15 changes: 8 additions & 7 deletions lib/Conversion/ImportVerilog/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,8 @@ struct RvalueExprVisitor {

Value getSelectIndex(Value index, const slang::ConstantRange &range) const {
auto indexType = cast<moore::UnpackedType>(index.getType());
auto bw = std::max(llvm::Log2_32_Ceil(std::abs(range.upper())),
auto bw = std::max(llvm::Log2_32_Ceil(std::max(std::abs(range.lower()),
std::abs(range.upper()))),
indexType.getBitSize().value());
auto intType =
moore::IntType::get(index.getContext(), bw, indexType.getDomain());
Expand All @@ -443,8 +444,8 @@ struct RvalueExprVisitor {

Value newIndex =
builder.createOrFold<moore::ConversionOp>(loc, intType, index);
Value offset =
builder.create<moore::ConstantOp>(loc, intType, range.lower());
Value offset = builder.create<moore::ConstantOp>(
loc, intType, range.lower(), /*isSigned = */ range.lower() < 0);
return builder.createOrFold<moore::SubOp>(loc, newIndex, offset);
}

Expand All @@ -453,8 +454,8 @@ struct RvalueExprVisitor {

Value newIndex =
builder.createOrFold<moore::ConversionOp>(loc, intType, index);
Value offset =
builder.create<moore::ConstantOp>(loc, intType, range.upper());
Value offset = builder.create<moore::ConstantOp>(
loc, intType, range.upper(), /* isSigned = */ range.upper() < 0);
return builder.createOrFold<moore::SubOp>(loc, offset, newIndex);
}

Expand Down Expand Up @@ -529,8 +530,8 @@ struct RvalueExprVisitor {
subtrahendType.getDomain());
auto sliceWidth =
expr.right().constant->integer().as<uint32_t>().value() - 1;
auto minuend =
builder.create<moore::ConstantOp>(loc, intType, sliceWidth);
auto minuend = builder.create<moore::ConstantOp>(
loc, intType, sliceWidth, expr.left().type->isSigned());
dynLowBit = builder.create<moore::SubOp>(loc, subtrahend, minuend);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions lib/Dialect/Moore/MooreOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -623,9 +623,9 @@ void ConstantOp::build(OpBuilder &builder, OperationState &result, IntType type,
/// folding because it only works with values that can be expressed in an
/// `int64_t`.
void ConstantOp::build(OpBuilder &builder, OperationState &result, IntType type,
int64_t value) {
int64_t value, bool isSigned) {
build(builder, result, type,
APInt(type.getWidth(), (uint64_t)value, /*isSigned=*/true));
APInt(type.getWidth(), (uint64_t)value, isSigned));
}

OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) {
Expand Down
25 changes: 25 additions & 0 deletions test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -2344,3 +2344,28 @@ function int AssignFuncArgs2(int x, int y);
// CHECK: [[ADD:%.+]] = moore.add [[READ_X]], [[READ_Y]] : i32
return x+y;
endfunction

// CHECK-LABEL: moore.module @RangeElementSelection(
module RangeElementSelection(
input reg [3:0] a [0:2],
output reg [3:0] b,
input reg [1:0] c);
// CHECK: [[A:%.+]] = moore.variable name "a" : <uarray<3 x l4>
// CHECK: [[C:%.+]] = moore.variable name "c" : <l2>

always_comb begin
// CHECK: [[READ_A:%.+]] = moore.read [[A]] : <uarray<3 x l4>>
// CHECK: [[READ_C:%.+]] = moore.read [[C]] : <l2>
// CHECK: [[M2:%.+]] = moore.constant -2 : l2
// CHECK: [[SUB_1:%.+]] = moore.sub [[M2]], [[READ_C]] : l2
// CHECK: [[DYN_EXT_1:%.+]] = moore.dyn_extract [[READ_A]] from [[SUB_1]] : uarray<3 x l4>, l2 -> l4
// CHECK: [[READ_B:%.+]] = moore.read %b : <l4>
// CHECK: [[READ_C_1:%.+]] = moore.read [[C]] : <l2>
// CHECK: [[EXTRACT:%.+]] = moore.extract [[READ_C_1]] from 0 : l2 -> l1
// CHECK: [[ONE:%.+]] = moore.constant 1 : l1
// CHECK: [[SUB_2:%.+]] = moore.sub [[EXTRACT]], [[ONE]] : l1
// CHECK: [[DYN_EXT_2:%.+]] = moore.dyn_extract [[READ_B]] from [[SUB_2]] : l4, l1 -> l2
b = a[c];
b[3:0] = b[c[0]-:2];
end
endmodule

0 comments on commit 2d2bee8

Please sign in to comment.