diff --git a/lib/Conversion/MooreToCore/MooreToCore.cpp b/lib/Conversion/MooreToCore/MooreToCore.cpp index f6fa3472f2e7..200dc183ab9c 100644 --- a/lib/Conversion/MooreToCore/MooreToCore.cpp +++ b/lib/Conversion/MooreToCore/MooreToCore.cpp @@ -496,6 +496,44 @@ struct VariableOpConversion : public OpConversionPattern { } }; +struct NetOpConversion : public OpConversionPattern { + using OpConversionPattern::OpConversionPattern; + + LogicalResult + matchAndRewrite(NetOp op, OpAdaptor adaptor, + ConversionPatternRewriter &rewriter) const override { + auto loc = op.getLoc(); + if (op.getKind() != NetKind::Wire) + return rewriter.notifyMatchFailure(loc, "only wire nets supported"); + + auto resultType = typeConverter->convertType(op.getResult().getType()); + if (!resultType) + return rewriter.notifyMatchFailure(loc, "invalid net type"); + + // TODO: Once the core dialects support four-valued integers, this code + // will additionally need to generate an all-X value for four-valued nets. + auto elementType = cast(resultType).getElementType(); + int64_t width = hw::getBitWidth(elementType); + if (width == -1) + return failure(); + auto constZero = rewriter.create(loc, APInt(width, 0)); + auto init = + rewriter.createOrFold(loc, elementType, constZero); + + auto signal = rewriter.replaceOpWithNewOp( + op, resultType, op.getNameAttr(), init); + + if (auto assignedValue = adaptor.getAssignment()) { + auto timeAttr = llhd::TimeAttr::get(resultType.getContext(), 0U, + llvm::StringRef("ns"), 0, 1); + auto time = rewriter.create(loc, timeAttr); + rewriter.create(loc, signal, assignedValue, time, Value{}); + } + + return success(); + } +}; + //===----------------------------------------------------------------------===// // Expression Conversion //===----------------------------------------------------------------------===// @@ -1342,6 +1380,7 @@ static void populateOpConversion(RewritePatternSet &patterns, patterns.add< // Patterns of declaration operations. VariableOpConversion, + NetOpConversion, // Patterns of miscellaneous operations. ConstantOpConv, ConcatOpConversion, ReplicateOpConversion, diff --git a/test/Conversion/MooreToCore/basic.mlir b/test/Conversion/MooreToCore/basic.mlir index d5f303bb4dd2..b342270cd04a 100644 --- a/test/Conversion/MooreToCore/basic.mlir +++ b/test/Conversion/MooreToCore/basic.mlir @@ -362,6 +362,7 @@ moore.module private @SubModule_0(in %a : !moore.l1, in %b : !moore.l1, out c : moore.output %0 : !moore.l1 } +// CHECK-LABEL: hw.module @Variable moore.module @Variable() { // CHECK: [[TMP0:%.+]] = hw.constant 0 : i32 // CHECK: %a = llhd.sig [[TMP0]] : i32 @@ -395,6 +396,28 @@ moore.module @Variable() { moore.output } +// CHECK-LABEL: hw.module @Net +moore.module @Net() { + // CHECK: [[TMP:%.+]] = hw.constant 0 : i32 + // CHECK: %a = llhd.sig [[TMP]] : i32 + %a = moore.net wire : + + // CHECK: [[PRB:%.+]] = llhd.prb %a : !hw.inout + %0 = moore.read %a : + + // CHECK: [[TMP:%.+]] = hw.constant 0 : i32 + // CHECK: %b = llhd.sig [[TMP]] : i32 + // CHECK: [[TIME:%.+]] = llhd.constant_time <0ns, 0d, 1e> + // CHECK: llhd.drv %b, [[PRB]] after [[TIME]] : !hw.inout + %b = moore.net wire %0 : + + // CHECK: [[TMP:%.+]] = hw.constant 10 : i32 + %3 = moore.constant 10 : i32 + // CHECK: [[TIME:%.+]] = llhd.constant_time <0ns, 0d, 1e> + // CHECK: llhd.drv %a, [[TMP]] after [[TIME]] : !hw.inout + moore.assign %a, %3 : i32 +} + // CHECK-LABEL: hw.module @Struct moore.module @Struct(in %a : !moore.i32, in %b : !moore.i32, in %arg0 : !moore.struct<{exp_bits: i32, man_bits: i32}>, in %arg1 : !moore.ref>, out a : !moore.i32, out b : !moore.struct<{exp_bits: i32, man_bits: i32}>, out c : !moore.struct<{exp_bits: i32, man_bits: i32}>) { // CHECK: hw.struct_extract %arg0["exp_bits"] : !hw.struct