Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions include/circt/Dialect/Moore/MooreOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,23 @@ def StringConstantOp : MooreOp<"string_constant", [Pure]> {
let assemblyFormat = "$value attr-dict `:` type($result)";
}

def ShortrealLiteralOp : MooreOp<"shortreal_constant", [Pure]> {
let summary = "Produce a constant shortreal value";
let description = [{
Produces a constant value of shortreal type.

Example:
```mlir
%0 = moore.shortreal_constant 1.23
```
The shortreal type has fixed-point(1.2) and exponent(2.0e10) formats and corresponds to IEEE 754 f32.
See IEEE 1800-2017 § 5.7.2 "Real literal constants".
}];
let arguments = (ins F32Attr:$value);
let results = (outs RealF32:$result);
let assemblyFormat = "$value attr-dict";
}

def RealLiteralOp : MooreOp<"real_constant", [Pure]> {
let summary = "Produce a constant real value";
let description = [{
Expand All @@ -580,12 +597,12 @@ def RealLiteralOp : MooreOp<"real_constant", [Pure]> {
```mlir
%0 = moore.real_constant 1.23
```
The real type has fixed-point(1.2) and exponent(2.0e10) formats.
The real type has fixed-point(1.2) and exponent(2.0e10) formats and corresponds to IEEE 754 f64.
See IEEE 1800-2017 § 5.7.2 "Real literal constants".
}];
let arguments = (ins F64Attr:$value);
let results = (outs RealType:$result);
let assemblyFormat = "$value attr-dict `:` type($result)";
let results = (outs RealF64:$result);
let assemblyFormat = "$value attr-dict";
}

//===----------------------------------------------------------------------===//
Expand Down
25 changes: 15 additions & 10 deletions lib/Conversion/ImportVerilog/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1502,26 +1502,31 @@ Value Context::convertToBool(Value value) {
Value Context::materializeSVReal(const slang::ConstantValue &svreal,
const slang::ast::Type &astType,
Location loc) {

mlir::FloatType fTy = mlir::Float64Type::get(getContext());
mlir::FloatType fTy;
Type resultType;
double val;

if (const auto *floatType = astType.as_if<slang::ast::FloatingType>()) {
if (floatType->floatKind == slang::ast::FloatingType::ShortReal) {
fTy = mlir::Float32Type::get(getContext());
resultType = moore::RealType::getShortReal(getContext());
val = svreal.shortReal().v;
} else if (floatType->floatKind == slang::ast::FloatingType::Real) {

mlir::FloatAttr attr = mlir::FloatAttr::get(fTy, val);
return moore::ShortrealLiteralOp::create(builder, loc, resultType, attr)
.getResult();
}
if (floatType->floatKind == slang::ast::FloatingType::Real) {
fTy = mlir::Float64Type::get(getContext());
resultType = moore::RealType::getReal(getContext());
val = svreal.real().v;

mlir::FloatAttr attr = mlir::FloatAttr::get(fTy, val);
return moore::RealLiteralOp::create(builder, loc, resultType, attr)
.getResult();
}
}

if (!resultType)
return {};

mlir::FloatAttr attr = mlir::FloatAttr::get(fTy, val);
return moore::RealLiteralOp::create(builder, loc, resultType, attr)
.getResult();
return {};
}

/// Materialize a Slang integer literal as a constant op.
Expand Down
42 changes: 42 additions & 0 deletions lib/Conversion/MooreToCore/MooreToCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,35 @@ struct ConstantOpConv : public OpConversionPattern<ConstantOp> {
}
};

struct RealConstantOpConv : public OpConversionPattern<RealLiteralOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(RealLiteralOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
llvm::APFloat apf = op.getValue();
Type outTy = rewriter.getF64Type();
auto attr = FloatAttr::get(outTy, apf);
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, attr);
return success();
}
};

struct ShortrealConstantOpConv
: public OpConversionPattern<ShortrealLiteralOp> {
using OpConversionPattern::OpConversionPattern;

LogicalResult
matchAndRewrite(ShortrealLiteralOp op, OpAdaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
llvm::APFloat apf = op.getValue();
Type outTy = rewriter.getF32Type();
auto attr = FloatAttr::get(outTy, apf);
rewriter.replaceOpWithNewOp<arith::ConstantOp>(op, attr);
return success();
}
};

struct ConstantTimeOpConv : public OpConversionPattern<ConstantTimeOp> {
using OpConversionPattern::OpConversionPattern;

Expand Down Expand Up @@ -1742,6 +1771,7 @@ static void populateLegality(ConversionTarget &target,
target.addLegalDialect<sim::SimDialect>();
target.addLegalDialect<mlir::LLVM::LLVMDialect>();
target.addLegalDialect<verif::VerifDialect>();
target.addLegalDialect<arith::ArithDialect>();

target.addLegalOp<debug::ScopeOp>();

Expand Down Expand Up @@ -1772,6 +1802,16 @@ static void populateTypeConversion(TypeConverter &typeConverter) {
return IntegerType::get(type.getContext(), type.getWidth());
});

typeConverter.addConversion([&](RealType type) -> mlir::Type {
MLIRContext *ctx = type.getContext();
switch (type.getWidth()) {
case moore::RealWidth::f32:
return mlir::Float32Type::get(ctx);
case moore::RealWidth::f64:
return mlir::Float64Type::get(ctx);
}
});

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

Expand Down Expand Up @@ -1916,6 +1956,8 @@ static void populateOpConversion(ConversionPatternSet &patterns,

// Patterns of miscellaneous operations.
ConstantOpConv,
RealConstantOpConv,
ShortrealConstantOpConv,
ConcatOpConversion,
ReplicateOpConversion,
ConstantTimeOpConv,
Expand Down
4 changes: 2 additions & 2 deletions test/Conversion/ImportVerilog/basic.sv
Original file line number Diff line number Diff line change
Expand Up @@ -3347,15 +3347,15 @@ endfunction

// CHECK: func.func private @testRealLiteral() -> !moore.f64 {
function automatic real testRealLiteral();
// CHECK: [[TMP:%.+]] = moore.real_constant 1.234500e+00 : f64
// CHECK: [[TMP:%.+]] = moore.real_constant 1.234500e+00
localparam test = 1.2345;
// CHECK-NEXT: return [[TMP]] : !moore.f64
return test;
endfunction

// CHECK: func.func private @testShortrealLiteral() -> !moore.f32 {
function automatic shortreal testShortrealLiteral();
// CHECK: [[TMP:%.+]] = moore.real_constant 1.2345000505447388 : f32
// CHECK: [[TMP:%.+]] = moore.shortreal_constant 1.234500e+00
localparam test = shortreal'(1.2345);
// CHECK-NEXT: return [[TMP]] : !moore.f32
return test;
Expand Down
9 changes: 9 additions & 0 deletions test/Conversion/MooreToCore/basic.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1330,3 +1330,12 @@ func.func @NonBlockingAssignment(%arg0: !moore.ref<i42>, %arg1: !moore.i42, %arg
moore.delayed_nonblocking_assign %arg0, %arg1, %arg2 : i42
return
}

// CHECK-LABEL: func.func @RealConstantOp
func.func @RealConstantOp() {
// CHECK: [[REAL:%.+]] = arith.constant 1.234500e+00 : f64
%real = moore.real_constant 1.234500e+00
// CHECK: [[SHORTREAL:%.+]] = arith.constant 1.234500e+00 : f32
%shortreal = moore.shortreal_constant 1.234500e+00
return
}