Skip to content

Conversation

@Scheremo
Copy link
Contributor

@Scheremo Scheremo commented Sep 18, 2025

This PR disambiguates the RealType into 32-Bit real types ("shortreal") and 64-Bit real types ("real").
It adds support for materializing moore.shortreal from shortreal-specific statements like

localparam shortreal sr = 4.532;

as well. This PR also adds support for the builtins $shortrealtobits, $bitstoshortreal, $realtobits and $bitstoreal.

When working through the builtins mentioned above, I noticed that the shortreal type was missing.
I decided to add it as !moore.shortreal since the distinction between $shortrealtobits and $realtobits would not really make sense in the previous type system.

To keep backwards compatibility (and also because most real values in Verilog are actually real, not shortreal), I chose to default the new width parameter of RealType to 64 and add a TypeBuilder that accepts "widthless" construction and defaults to 64 Bit.

While Section 20.5 "Conversion functions" is unspecific about the domain of the "bits" end of the conversion functions, I infer these two lines to only mean two-valued bit rather than logic vectors:

$realtobits converts values from a real type to a 64-bit vector representation of the real number.
$bitstoreal converts a bit pattern created by $realtobits to a value of the real type.

Since this is the first time I add a full-on type to Moore, please let me know if I'm missing anything important!

@Scheremo Scheremo force-pushed the pr-b2r branch 4 times, most recently from 336e666 to 81d0fdf Compare September 18, 2025 12:38
@Scheremo Scheremo marked this pull request as ready for review September 18, 2025 16:54
This PR disambiguates the RealType into 32-Bit real types ("shortreal") and 64-Bit real types ("real").
It adds support for materializing moore.shortreal from shortreal-specific statements like

localparam shortreal sr = 4.532;
as well.
Apart from this new type disctinction, this PR also adds support for the builtins $shortrealtobits, $bitstoshortreal, $realtobits and $bitstoreal.

When working through the builtins mentioned above, I noticed that the shortreal type was missing.
I decided to add it as !moore.shortreal since the distinction between $shortrealtobits and $realtobits would not really make sense in the previous type system.

To keep backwards compatibility (and also because most real values in Verilog are actually real, not shortreal), I chose to default the new width parameter of RealType to 64 and add a TypeBuilder that accepts "widthless" construction and defaults to 64 Bit.

While Section 20.5 "Conversion functions" is unspecific about the domain of the "bits" end of the conversion functions, I infer these two lines to mean two-valued bit rather than logic vectors only:

$realtobits converts values from a real type to a 64-bit vector representation of the real number.
$bitstoreal converts a bit pattern created by $realtobits to a value of the real type.
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.

Very cool! Just a few nits here and there 😁

Comment on lines 124 to 147
void RealType::print(mlir::AsmPrinter &p) const {
// Print bare `real` when default (64), otherwise `real<32>`
if (getWidth() == 64)
return;
p << "<" << getWidth() << ">";
}

Type RealType::parse(AsmParser &p) {
// Default to 64 if no angle bracket group is present.
unsigned width = 64;

// If next token is '<', parse the explicit width.
if (succeeded(p.parseOptionalLess())) {
if (p.parseInteger(width) || p.parseGreater())
return Type(); // signal failure, diagnostic already emitted
}

if (width == 32 || width == 64)
return RealType::get(p.getContext(), width);
p.emitError(p.getCurrentLocation())
<< "RealType parser error: expected 64 or 32 bit width, but got "
<< width;
return Type();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I would make this parameter non-optional. We probably have a few tests that just call out !moore.real at the moment, but these should be fairly easy to fix up to !moore.real<64> 🙂. It's usually a good idea to not have too much syntactic sugar in the IR.

Copy link
Contributor

Choose a reason for hiding this comment

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

That would also allow you to continue to use the assemblyFormat and not have custom C++ code for the parser/printer 🙂. Which then also makes the real short form work when the !moore. is clear from context 🙂.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or actually coming to think of it, you could just add a f32 and f64 detection to parseMooreType, to make this real type look similar to MLIR's builtin f32 and f64, which we might want to use in the future 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I got all of those changed, but feel free to let me know if I missed any!

@Scheremo
Copy link
Contributor Author

I think I addressed all comments, and CI passes again. Ready to merge unless you got other comments 😄

@fabianschuiki fabianschuiki merged commit 8d47bf4 into llvm:main Sep 18, 2025
7 checks passed
@Scheremo Scheremo deleted the pr-b2r branch October 21, 2025 07:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants