[ImportVerilog][MooreToCore]Lower moore.namedConstant to hw.param.value (#7122)

* [mooretocore] Support namedconstant

Signed-off-by: mingzheTerapines <mingzhe.zhang@terapines.com>

* Limit type to integertype.

* add mooretocore support

* remove include

* remove verify of hwops

* remove include

* Test added.

* Add test for MooreToCore

* Support any attribute

* Modify test.

* Add symbol for wireOp

* Remove useless include

* Add more test and error cases

* slang-tidy

* some Imporvement

* Improved version

* little change

* Imporve

---------

Signed-off-by: mingzheTerapines <mingzhe.zhang@terapines.com>
This commit is contained in:
mingzheTerapines 2024-06-14 09:22:46 +08:00 committed by GitHub
parent f26f534d60
commit b136101756
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 82 additions and 37 deletions

View File

@ -413,18 +413,19 @@ def NamedConstantOp : MooreOp<"named_constant", [
]>{
let summary = "An elaboration time constant expression";
let description = [{
Constants are named data objects that never change. SystemVerilog provides
Constants are named data objects that never change. SystemVerilog provides
three elaboration-time constants: parameter, localparam, and specparam.
See IEEE 1800-2017 § 6.20 "Constants".
}];
let arguments = (ins StrAttr:$name,
NamedConstAttr:$kind,
UnpackedType:$value);
let results = (outs UnpackedType:$result);
let arguments = (ins StrAttr:$name,
NamedConstAttr:$kind,
IntType:$value);
let results = (outs IntType:$result);
let assemblyFormat = [{
`` custom<ImplicitSSAName>($name) $kind $value attr-dict
`:` type($result)
$kind
``custom<ImplicitSSAName>($name)
$value `:` type($result) attr-dict
}];
}

View File

@ -287,17 +287,12 @@ struct MemberVisitor {
// Handle parameters.
LogicalResult visit(const slang::ast::ParameterSymbol &paramNode) {
auto type = context.convertType(*paramNode.getDeclaredType());
auto type = cast<moore::IntType>(context.convertType(paramNode.getType()));
if (!type)
return failure();
const auto *init = paramNode.getInitializer();
// skip parameters without value.
if (!init)
return success();
Value initial = context.convertRvalueExpression(*init);
if (!initial)
return failure();
auto valueInt = paramNode.getValue().integer().as<uint64_t>().value();
Value value = builder.create<moore::ConstantOp>(loc, type, valueInt);
auto namedConstantOp = builder.create<moore::NamedConstantOp>(
loc, type, builder.getStringAttr(paramNode.name),
@ -306,30 +301,25 @@ struct MemberVisitor {
moore::NamedConst::LocalParameter)
: moore::NamedConstAttr::get(context.getContext(),
moore::NamedConst::Parameter),
initial);
value);
context.valueSymbols.insert(&paramNode, namedConstantOp);
return success();
}
// Handle specparam.
LogicalResult visit(const slang::ast::SpecparamSymbol &spNode) {
auto type = context.convertType(*spNode.getDeclaredType());
auto type = cast<moore::IntType>(context.convertType(spNode.getType()));
if (!type)
return failure();
const auto *init = spNode.getInitializer();
// skip specparam without value.
if (!init)
return success();
Value initial = context.convertRvalueExpression(*init);
if (!initial)
return failure();
auto valueInt = spNode.getValue().integer().as<uint64_t>().value();
Value value = builder.create<moore::ConstantOp>(loc, type, valueInt);
auto namedConstantOp = builder.create<moore::NamedConstantOp>(
loc, type, builder.getStringAttr(spNode.name),
moore::NamedConstAttr::get(context.getContext(),
moore::NamedConst::SpecParameter),
initial);
value);
context.valueSymbols.insert(&spNode, namedConstantOp);
return success();
}

View File

@ -155,6 +155,35 @@ struct ConstantOpConv : public OpConversionPattern<ConstantOp> {
}
};
struct NamedConstantOpConv : public OpConversionPattern<NamedConstantOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
matchAndRewrite(NamedConstantOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
Type resultType = typeConverter->convertType(op.getResult().getType());
SmallString<32> symStr;
switch (op.getKind()) {
case NamedConst::Parameter:
symStr = "parameter";
break;
case NamedConst::LocalParameter:
symStr = "localparameter";
break;
case NamedConst::SpecParameter:
symStr = "specparameter";
break;
}
auto symAttr =
rewriter.getStringAttr(symStr + Twine(":") + adaptor.getName());
rewriter.replaceOpWithNewOp<hw::WireOp>(op, resultType, adaptor.getValue(),
op.getNameAttr(),
hw::InnerSymAttr::get(symAttr));
return success();
}
};
struct ConcatOpConversion : public OpConversionPattern<ConcatOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
@ -569,6 +598,7 @@ static void populateOpConversion(RewritePatternSet &patterns,
// Patterns of miscellaneous operations.
ConstantOpConv, ConcatOpConversion, ReplicateOpConversion,
ExtractOpConversion, ConversionOpConversion,
NamedConstantOpConv,
// Patterns of unary operations.
ReduceAndOpConversion, ReduceOrOpConversion, ReduceXorOpConversion,

View File

@ -90,23 +90,28 @@ module Basic;
bit [0:0] b1;
bit b2 = b1;
// CHECK: [[TMP1:%.+]] = moore.constant 1 : i32
// CHECK: [[TMP2:%.+]] = moore.conversion [[TMP1]] : !moore.i32 -> !moore.l32
// CHECK: %p1 = moore.named_constant parameter [[TMP2]] : l32
// CHECK: [[TMP:%.+]] = moore.constant 1 : l32
// CHECK: %p1 = moore.named_constant parameter [[TMP]] : l32
parameter p1 = 1;
// CHECK: %p2 = moore.named_constant parameter %p1 : l32
// CHECK: [[TMP:%.+]] = moore.constant 1 : l32
// CHECK: %p2 = moore.named_constant parameter [[TMP]] : l32
parameter p2 = p1;
// CHECK: [[TMP1:%.+]] = moore.constant 2 : i32
// CHECK: [[TMP2:%.+]] = moore.conversion [[TMP1]] : !moore.i32 -> !moore.l32
// CHECK: %lp1 = moore.named_constant localparam [[TMP2]] : l32
// CHECK: [[TMP:%.+]] = moore.constant 2 : l32
// CHECK: %lp1 = moore.named_constant localparam [[TMP]] : l32
localparam lp1 = 2;
// CHECK: %lp2 = moore.named_constant localparam %lp1 : l32
// CHECK: [[TMP:%.+]] = moore.constant 2 : l32
// CHECK: %lp2 = moore.named_constant localparam [[TMP]] : l32
localparam lp2 = lp1;
// CHECK: [[TMP1:%.+]] = moore.constant 3 : i32
// CHECK: [[TMP2:%.+]] = moore.conversion [[TMP1]] : !moore.i32 -> !moore.l32
// CHECK: %sp1 = moore.named_constant specparam [[TMP2]] : l32
// CHECK: [[TMP:%.+]] = moore.constant 3 : l32
// CHECK: %sp1 = moore.named_constant specparam [[TMP]] : l32
specparam sp1 = 3;
// CHECK: %sp2 = moore.named_constant specparam %sp1 : l32
// CHECK: [[TMP:%.+]] = moore.constant 3 : l32
// CHECK: %sp2 = moore.named_constant specparam [[TMP]] : l32
specparam sp2 = sp1;
// CHECK: moore.procedure initial {

View File

@ -235,3 +235,22 @@ moore.module @SubModule_0(in %a : !moore.l1, in %b : !moore.l1, out c : !moore.l
// CHECK-NEXT: hw.output %[[V2]] : i1
moore.output %0 : !moore.l1
}
// CHECK-LABEL: hw.module @ParamTest() {
moore.module @ParamTest(){
// CHECK-NEXT: [[Pa:%.+]] = hw.constant 1 : i32
// CHECK-NEXT: %p1 = hw.wire [[Pa]] sym @parameter:p1 : i32
%0 = moore.constant 1 : l32
%p1 = moore.named_constant parameter %0 : l32
// CHECK-NEXT: [[LPa:%.+]] = hw.constant 2 : i32
// CHECK-NEXT: %lp1 = hw.wire [[LPa]] sym @localparameter:lp1 : i32
%1 = moore.constant 2 : l32
%lp1 = moore.named_constant localparam %1 : l32
// CHECK-NEXT: [[SPa:%.+]] = hw.constant 3 : i32
// CHECK-NEXT: %sp1 = hw.wire [[SPa]] sym @specparameter:sp1 : i32
%2 = moore.constant 3 : l32
%sp1 = moore.named_constant specparam %2 : l32
}