From 84d73b94aac932f2b1e545446a4c0b8720c04545 Mon Sep 17 00:00:00 2001 From: Martin Erhart Date: Fri, 9 Aug 2024 08:46:31 +0100 Subject: [PATCH] [MooreToCore] Fix variable op lowering of aggregate types (#7481) --- lib/Conversion/MooreToCore/MooreToCore.cpp | 22 ++++++++++++++-------- test/Conversion/MooreToCore/basic.mlir | 17 ++++++++++++++--- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/lib/Conversion/MooreToCore/MooreToCore.cpp b/lib/Conversion/MooreToCore/MooreToCore.cpp index ac741f9e9d..f6b53d77d7 100644 --- a/lib/Conversion/MooreToCore/MooreToCore.cpp +++ b/lib/Conversion/MooreToCore/MooreToCore.cpp @@ -158,6 +158,7 @@ struct VariableOpConversion : public OpConversionPattern { LogicalResult matchAndRewrite(VariableOp op, OpAdaptor adaptor, ConversionPatternRewriter &rewriter) const override { + Location loc = op.getLoc(); Type resultType = typeConverter->convertType(op.getResult().getType()); Value init = adaptor.getInitial(); // TODO: Unsupport x/z, so the initial value is 0. @@ -165,10 +166,18 @@ struct VariableOpConversion : public OpConversionPattern { Domain::FourValued) return failure(); - if (!init) - init = rewriter.create(op->getLoc(), resultType, 0); - rewriter.replaceOpWithNewOp(op, hw::InOutType::get(resultType), - op.getNameAttr(), init); + if (!init) { + Type elementType = cast(resultType).getElementType(); + int64_t width = hw::getBitWidth(elementType); + if (width == -1) + return failure(); + + Value constZero = rewriter.create(loc, APInt(width, 0)); + init = rewriter.createOrFold(loc, elementType, constZero); + } + + rewriter.replaceOpWithNewOp(op, resultType, op.getNameAttr(), + init); return success(); } }; @@ -715,10 +724,7 @@ static void populateTypeConversion(TypeConverter &typeConverter) { }); typeConverter.addConversion([&](RefType type) -> std::optional { - if (isa(type.getNestedType())) - return mlir::IntegerType::get(type.getContext(), - type.getBitSize().value()); - return std::nullopt; + return hw::InOutType::get(typeConverter.convertType(type.getNestedType())); }); // Valid target types. diff --git a/test/Conversion/MooreToCore/basic.mlir b/test/Conversion/MooreToCore/basic.mlir index 0722144b29..1df63b4ea9 100644 --- a/test/Conversion/MooreToCore/basic.mlir +++ b/test/Conversion/MooreToCore/basic.mlir @@ -312,9 +312,20 @@ moore.module @Variable() { moore.output } -// CHECK-LABEL: func @Struct -func.func @Struct(%arg0: !moore.struct<{exp_bits: i32, man_bits: i32}>) -> !moore.i32 { +// CHECK-LABEL: hw.module @Struct +moore.module @Struct(in %arg0 : !moore.struct<{exp_bits: i32, man_bits: i32}>, 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 %0 = moore.struct_extract %arg0, "exp_bits" : !moore.struct<{exp_bits: i32, man_bits: i32}> -> i32 - return %0 : !moore.i32 + + // CHECK: [[C0:%.+]] = hw.constant 0 : i64 + // CHECK: [[INIT:%.+]] = hw.bitcast [[C0]] : (i64) -> !hw.struct + // CHECK: llhd.sig "" [[INIT]] : !hw.struct + // CHECK: llhd.sig "" %arg0 : !hw.struct + %1 = moore.variable : > + %2 = moore.variable %arg0 : > + + %3 = moore.read %1 : > + %4 = moore.read %2 : > + + moore.output %0, %3, %4 : !moore.i32, !moore.struct<{exp_bits: i32, man_bits: i32}>, !moore.struct<{exp_bits: i32, man_bits: i32}> }