Skip to content

Commit

Permalink
[MooreToCore] Support StructExtractRefOp (#7497)
Browse files Browse the repository at this point in the history
  • Loading branch information
maerhart authored Aug 9, 2024
1 parent 09fc725 commit bfed535
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
33 changes: 30 additions & 3 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,29 @@ static hw::ModulePortInfo getModulePortInfo(const TypeConverter &typeConverter,
inputs.reserve(moduleTy.getNumInputs());
outputs.reserve(moduleTy.getNumOutputs());

for (auto port : moduleTy.getPorts())
for (auto port : moduleTy.getPorts()) {
Type portTy = typeConverter.convertType(port.type);
if (auto ioTy = dyn_cast_or_null<hw::InOutType>(portTy)) {
inputs.push_back(hw::PortInfo(
{{port.name, ioTy.getElementType(), hw::ModulePort::InOut},
inputNum++,
{}}));
continue;
}

if (port.dir == hw::ModulePort::Direction::Output) {
outputs.push_back(
hw::PortInfo({{port.name, port.type, port.dir}, resultNum++, {}}));
hw::PortInfo({{port.name, portTy, port.dir}, resultNum++, {}}));
} else {
// FIXME: Once we support net<...>, ref<...> type to represent type of
// special port like inout or ref port which is not a input or output
// port. It can change to generate corresponding types for direction of
// port or do specified operation to it. Now inout and ref port is treated
// as input port.
inputs.push_back(
hw::PortInfo({{port.name, port.type, port.dir}, inputNum++, {}}));
hw::PortInfo({{port.name, portTy, port.dir}, inputNum++, {}}));
}
}

return hw::ModulePortInfo(inputs, outputs);
}
Expand All @@ -113,6 +123,9 @@ struct SVModuleOpConversion : public OpConversionPattern<SVModuleOp> {
SymbolTable::setSymbolVisibility(hwModuleOp,
SymbolTable::getSymbolVisibility(op));
rewriter.eraseBlock(hwModuleOp.getBodyBlock());
if (failed(
rewriter.convertRegionTypes(&op.getBodyRegion(), *typeConverter)))
return failure();
rewriter.inlineRegionBefore(op.getBodyRegion(), hwModuleOp.getBodyRegion(),
hwModuleOp.getBodyRegion().end());

Expand Down Expand Up @@ -456,6 +469,19 @@ struct StructExtractOpConversion : public OpConversionPattern<StructExtractOp> {
}
};

struct StructExtractRefOpConversion
: public OpConversionPattern<StructExtractRefOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(StructExtractRefOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<llhd::SigStructExtractOp>(
op, adaptor.getInput(), adaptor.getFieldNameAttr());
return success();
}
};

struct ReduceAndOpConversion : public OpConversionPattern<ReduceAndOp> {
using OpConversionPattern::OpConversionPattern;
LogicalResult
Expand Down Expand Up @@ -908,6 +934,7 @@ static void populateOpConversion(RewritePatternSet &patterns,
ConstantOpConv, ConcatOpConversion, ReplicateOpConversion,
ExtractOpConversion, DynExtractOpConversion, ConversionOpConversion,
ReadOpConversion, NamedConstantOpConv, StructExtractOpConversion,
StructExtractRefOpConversion,

// Patterns of unary operations.
ReduceAndOpConversion, ReduceOrOpConversion, ReduceXorOpConversion,
Expand Down
6 changes: 5 additions & 1 deletion test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,13 @@ moore.module @Variable() {
}

// 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}>) {
moore.module @Struct(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>
%0 = moore.struct_extract %arg0, "exp_bits" : !moore.struct<{exp_bits: i32, man_bits: i32}> -> i32

// CHECK: llhd.sig.struct_extract %arg1["exp_bits"] : !hw.inout<struct<exp_bits: i32, man_bits: i32>>
%ref = moore.struct_extract_ref %arg1, "exp_bits" : <!moore.struct<{exp_bits: i32, man_bits: i32}>> -> <i32>
moore.assign %ref, %0 : !moore.i32

// CHECK: [[C0:%.+]] = hw.constant 0 : i64
// CHECK: [[INIT:%.+]] = hw.bitcast [[C0]] : (i64) -> !hw.struct<exp_bits: i32, man_bits: i32>
Expand Down

0 comments on commit bfed535

Please sign in to comment.