-
Notifications
You must be signed in to change notification settings - Fork 383
[ImportVerilog] Generalize materialization of constant real values #9092
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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!
| /// 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); | ||
| } | ||
|
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, sounds great!
There was a problem hiding this comment.
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 😃
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
94d7637 to
9ff1964
Compare
9ff1964 to
e9c0ec9
Compare
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.
e9c0ec9 to
31f7611
Compare
This patch teaches the ImportVerilog conversion to correctly lower SystemVerilog
real and shortreal constant values into
moore.real_constantoperations.MooreOps.td
RealLiteralOpresult type from fixedRealF64to genericRealTypeto support both 32-bit and 64-bit real kinds.ImportVerilog/Expressions.cpp
Context::materializeSVReal()helper to constructRealLiteralOps fromslang::ConstantValuereals.materializeConstant()to handleConstantValue::isReal()andisShortReal().ImportVerilogInternals.h
materializeSVReal()alongsidematerializeSVInt().Tests
testRealLiteralandtestShortrealLiteralfunctions to verify thatreal and shortreal literals produce the expected
moore.real_constantoperations with
f64andf32types.Overall, this enables correct handling of floating-point literals in the
ImportVerilog pipeline.