mirror of https://github.com/llvm/circt.git
[ImportVerilog] Fix bugs with constant folding (#8213)
Co-authored-by: Yan Churkin <yan@ispras.ru>
This commit is contained in:
parent
6e93a0e840
commit
2d2bee8dc0
|
@ -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)>,
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue