Skip to content

Commit

Permalink
[MooreToCore] Add support for format strings and display task (#7694)
Browse files Browse the repository at this point in the history
Lower the format string ops and the `moore.builtin.display` task to the
corresponding ops in the Sim dialect. This is not perfect yet, since Sim
does not support some of the formatting options. They will be easy to
add in the future though.
  • Loading branch information
fabianschuiki authored Oct 11, 2024
1 parent acd335d commit dc387c5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 8 deletions.
14 changes: 10 additions & 4 deletions include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,18 @@ def HandshakeToHW : Pass<"lower-handshake-to-hw", "mlir::ModuleOp"> {
def ConvertMooreToCore : Pass<"convert-moore-to-core", "mlir::ModuleOp"> {
let summary = "Convert Moore to Core";
let description = [{
This pass translates Moore to the core dialects (Comb/HW/LLHD).
This pass translates Moore to the core dialects.
}];
let constructor = "circt::createConvertMooreToCorePass()";
let dependentDialects = ["comb::CombDialect", "hw::HWDialect",
"llhd::LLHDDialect", "mlir::cf::ControlFlowDialect",
"mlir::scf::SCFDialect", "verif::VerifDialect"];
let dependentDialects = [
"comb::CombDialect",
"hw::HWDialect",
"llhd::LLHDDialect",
"mlir::cf::ControlFlowDialect",
"mlir::scf::SCFDialect",
"sim::SimDialect",
"verif::VerifDialect",
];
}

//===----------------------------------------------------------------------===//
Expand Down
1 change: 1 addition & 0 deletions lib/Conversion/MooreToCore/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ add_circt_conversion_library(CIRCTMooreToCore
CIRCTHW
CIRCTLLHD
CIRCTMoore
CIRCTSim
CIRCTVerif
MLIRControlFlowDialect
MLIRFuncDialect
Expand Down
83 changes: 79 additions & 4 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "circt/Dialect/HW/HWOps.h"
#include "circt/Dialect/LLHD/IR/LLHDOps.h"
#include "circt/Dialect/Moore/MooreOps.h"
#include "circt/Dialect/Sim/SimOps.h"
#include "circt/Dialect/Verif/VerifOps.h"
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
Expand Down Expand Up @@ -1256,6 +1257,69 @@ struct AssertLikeOpConversion : public OpConversionPattern<MooreOpTy> {
}
};

//===----------------------------------------------------------------------===//
// Format String Conversion
//===----------------------------------------------------------------------===//

struct FormatLiteralOpConversion : public OpConversionPattern<FormatLiteralOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(FormatLiteralOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<sim::FormatLitOp>(op, adaptor.getLiteral());
return success();
}
};

struct FormatConcatOpConversion : public OpConversionPattern<FormatConcatOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(FormatConcatOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<sim::FormatStringConcatOp>(op,
adaptor.getInputs());
return success();
}
};

struct FormatIntOpConversion : public OpConversionPattern<FormatIntOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(FormatIntOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
// TODO: These should honor the width, alignment, and padding.
switch (op.getFormat()) {
case IntFormat::Decimal:
rewriter.replaceOpWithNewOp<sim::FormatDecOp>(op, adaptor.getValue());
return success();
case IntFormat::Binary:
rewriter.replaceOpWithNewOp<sim::FormatBinOp>(op, adaptor.getValue());
return success();
case IntFormat::HexLower:
case IntFormat::HexUpper:
rewriter.replaceOpWithNewOp<sim::FormatHexOp>(op, adaptor.getValue());
return success();
default:
return rewriter.notifyMatchFailure(op, "unsupported int format");
}
}
};

struct DisplayBIOpConversion : public OpConversionPattern<DisplayBIOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(DisplayBIOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
rewriter.replaceOpWithNewOp<sim::PrintFormattedProcOp>(
op, adaptor.getMessage());
return success();
}
};

} // namespace

//===----------------------------------------------------------------------===//
Expand All @@ -1265,10 +1329,11 @@ struct AssertLikeOpConversion : public OpConversionPattern<MooreOpTy> {
static void populateLegality(ConversionTarget &target,
const TypeConverter &converter) {
target.addIllegalDialect<MooreDialect>();
target.addLegalDialect<mlir::BuiltinDialect>();
target.addLegalDialect<comb::CombDialect>();
target.addLegalDialect<hw::HWDialect>();
target.addLegalDialect<llhd::LLHDDialect>();
target.addLegalDialect<comb::CombDialect>();
target.addLegalDialect<mlir::BuiltinDialect>();
target.addLegalDialect<sim::SimDialect>();
target.addLegalDialect<verif::VerifDialect>();

target.addLegalOp<debug::ScopeOp>();
Expand All @@ -1295,6 +1360,10 @@ static void populateTypeConversion(TypeConverter &typeConverter) {
return IntegerType::get(type.getContext(), type.getWidth());
});

typeConverter.addConversion([&](FormatStringType type) {
return sim::FormatStringType::get(type.getContext());
});

typeConverter.addConversion([&](ArrayType type) -> std::optional<Type> {
if (auto elementType = typeConverter.convertType(type.getElementType()))
return hw::ArrayType::get(elementType, type.getSize());
Expand Down Expand Up @@ -1469,12 +1538,18 @@ static void populateOpConversion(RewritePatternSet &patterns,
// Patterns of assert-like operations
AssertLikeOpConversion<AssertOp, verif::AssertOp>,
AssertLikeOpConversion<AssumeOp, verif::AssumeOp>,
AssertLikeOpConversion<CoverOp, verif::CoverOp>
AssertLikeOpConversion<CoverOp, verif::CoverOp>,

// Format strings.
FormatLiteralOpConversion,
FormatConcatOpConversion,
FormatIntOpConversion,
DisplayBIOpConversion
>(typeConverter, context);
// clang-format on

mlir::populateAnyFunctionOpInterfaceTypeConversionPattern(patterns,
typeConverter);

hw::populateHWModuleLikeTypeConversionPattern(
hw::HWModuleOp::getOperationName(), patterns, typeConverter);
}
Expand Down
19 changes: 19 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,25 @@ func.func @Statements(%arg0: !moore.i42) {
return
}

// CHECK-LABEL: func @FormatStrings
func.func @FormatStrings(%arg0: !moore.i42) {
// CHECK: [[TMP:%.+]] = sim.fmt.lit "hello"
%0 = moore.fmt.literal "hello"
// CHECK: sim.fmt.concat ([[TMP]], [[TMP]])
%1 = moore.fmt.concat (%0, %0)
// CHECK: sim.fmt.dec %arg0 : i42
moore.fmt.int decimal %arg0, width 42, align right, pad space : i42
// CHECK: sim.fmt.bin %arg0 : i42
moore.fmt.int binary %arg0, width 42, align right, pad space : i42
// CHECK: sim.fmt.hex %arg0 : i42
moore.fmt.int hex_lower %arg0, width 42, align right, pad space : i42
// CHECK: sim.fmt.hex %arg0 : i42
moore.fmt.int hex_upper %arg0, width 42, align right, pad space : i42
// CHECK: sim.proc.print [[TMP]]
moore.builtin.display %0
return
}

// CHECK-LABEL: hw.module @InstanceNull() {
moore.module @InstanceNull() {

Expand Down

0 comments on commit dc387c5

Please sign in to comment.