Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Scheremo
Copy link
Contributor

@Scheremo Scheremo commented Oct 14, 2025

This patch teaches the ImportVerilog conversion to correctly lower SystemVerilog
real and shortreal constant values into moore.real_constant operations.

  • MooreOps.td

    • Relax RealLiteralOp result type from fixed RealF64 to generic
      RealType to support both 32-bit and 64-bit real kinds.
  • ImportVerilog/Expressions.cpp

    • Add Context::materializeSVReal() helper to construct RealLiteralOps from
      slang::ConstantValue reals.
    • Replace direct creation of an F64 literal with a call to the new helper.
    • Hook materializeConstant() to handle ConstantValue::isReal() and
      isShortReal().
  • ImportVerilogInternals.h

    • Declare materializeSVReal() alongside materializeSVInt().
  • Tests

    • Add testRealLiteral and testShortrealLiteral functions to verify that
      real and shortreal literals produce the expected moore.real_constant
      operations with f64 and f32 types.

Overall, this enables correct handling of floating-point literals in the
ImportVerilog pipeline.

@Scheremo Scheremo marked this pull request as ready for review October 14, 2025 14:47
Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this cast to the expected result type, like the int materialization? Otherwise LGTM!

Comment on lines 1502 to 1526
/// Materialize a Slang real literal as a constant op.
Value Context::materializeSVReal(const slang::real_t &svreal, Location loc) {
auto f64Ty = mlir::Float64Type::get(getContext());
mlir::FloatAttr attr = mlir::FloatAttr::get(f64Ty, llvm::APFloat(svreal.v));
return moore::RealLiteralOp::create(builder, loc, attr);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this accept a slang::ast::Type and call materializeConversion like materializeSVInt does? How are shortreal constants materialized?

Copy link
Contributor Author

@Scheremo Scheremo Oct 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good questions; the "usual" path to materialization is already implemented when visiting slang::ast::RealLiteral in the ExprVisitor.
This materialization is only called when the LHS does not have a type, as in

parameter test = 1.2345;

, since the type cannot be inferred in this case - I don't expect Slang to ever emit a shortreal when the LHS type cannot be determined, because the spec, in Section 5.7.2 says it shouldn't; materializing a real should be correct. Constructs like

parameter real test = 1.2345;
parameter shortreal test = 1.2345;

both already work correctly without this patch 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, sounds great!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I just tried with an explicit cast, e.g.

parameter test = shortreal'(1.2345);

which actually emits a shortreal, so we do need a case distinction 😃

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe the existing support for parameter shortreal test = 1.2345; could use the same materializeSVReal code path with the shortreal provided as a type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that's a good idea! Refactored to do exactly that.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Scheremo Scheremo force-pushed the pr-real-constants branch 2 times, most recently from 94d7637 to 9ff1964 Compare October 14, 2025 16:16
@Scheremo Scheremo changed the title [ImportVerilog] Fix real constant import edge case [ImportVerilog] Generalize materialization of constant real values Oct 14, 2025
This patch teaches the ImportVerilog conversion to correctly lower SystemVerilog
real and shortreal constant values into `moore.real_constant` operations.

* **MooreOps.td**
  - Relax `RealLiteralOp` result type from fixed `RealF64` to generic
    `RealType` to support both 32-bit and 64-bit real kinds.

* **ImportVerilog/Expressions.cpp**
  - Add `Context::materializeSVReal()` helper to construct `RealLiteralOp`s from
    `slang::ConstantValue` reals.
  - Replace direct creation of an F64 literal with a call to the new helper.
  - Hook `materializeConstant()` to handle `ConstantValue::isReal()` and
    `isShortReal()`.

* **ImportVerilogInternals.h**
  - Declare `materializeSVReal()` alongside `materializeSVInt()`.

* **Tests**
  - Add `testRealLiteral` and `testShortrealLiteral` functions to verify that
    real and shortreal literals produce the expected `moore.real_constant`
    operations with `f64` and `f32` types.

Overall, this enables correct handling of floating-point literals in the
ImportVerilog pipeline.
@Scheremo Scheremo merged commit ca46d45 into llvm:main Oct 14, 2025
7 checks passed
@Scheremo Scheremo deleted the pr-real-constants branch October 21, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants