Skip to content

Commit

Permalink
[MooreToCore] Lower moore.net to llhd.sig (#7652)
Browse files Browse the repository at this point in the history
Lower `moore.net` operations to the corresponding `llhd.sig`, similar to
how `moore.variable` is lowered. This currently only makes sense for
"wire" nets, since it's not yet clear how all the other net types map to
LLHD.
  • Loading branch information
fabianschuiki authored Oct 2, 2024
1 parent e26c974 commit 8c577d5
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,44 @@ struct VariableOpConversion : public OpConversionPattern<VariableOp> {
}
};

struct NetOpConversion : public OpConversionPattern<NetOp> {
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<hw::InOutType>(resultType).getElementType();
int64_t width = hw::getBitWidth(elementType);
if (width == -1)
return failure();
auto constZero = rewriter.create<hw::ConstantOp>(loc, APInt(width, 0));
auto init =
rewriter.createOrFold<hw::BitcastOp>(loc, elementType, constZero);

auto signal = rewriter.replaceOpWithNewOp<llhd::SignalOp>(
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<llhd::ConstantTimeOp>(loc, timeAttr);
rewriter.create<llhd::DrvOp>(loc, signal, assignedValue, time, Value{});
}

return success();
}
};

//===----------------------------------------------------------------------===//
// Expression Conversion
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -1342,6 +1380,7 @@ static void populateOpConversion(RewritePatternSet &patterns,
patterns.add<
// Patterns of declaration operations.
VariableOpConversion,
NetOpConversion,

// Patterns of miscellaneous operations.
ConstantOpConv, ConcatOpConversion, ReplicateOpConversion,
Expand Down
23 changes: 23 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 : <i32>

// CHECK: [[PRB:%.+]] = llhd.prb %a : !hw.inout<i32>
%0 = moore.read %a : <i32>

// 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<i32>
%b = moore.net wire %0 : <i32>

// 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<i32>
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<!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<exp_bits: i32, man_bits: i32>
Expand Down

0 comments on commit 8c577d5

Please sign in to comment.