[ImportVerilog][MooreToCore] Adds support for System Verilog real maths functions#10746
Open
jpkirs wants to merge 6 commits into
Open
[ImportVerilog][MooreToCore] Adds support for System Verilog real maths functions#10746jpkirs wants to merge 6 commits into
jpkirs wants to merge 6 commits into
Conversation
fabianschuiki
approved these changes
Jun 30, 2026
fabianschuiki
left a comment
Contributor
There was a problem hiding this comment.
This is really cool, thanks a lot for tackling these @jpkirs. Just a small nit about an accidental deletion in the code -- will probably break CI.
Comment on lines
-3251
to
-3278
| static LogicalResult | ||
| emitScanAssignments(Context &context, const Context::ScanStringResult &result, | ||
| Location loc) { | ||
| auto &builder = context.builder; | ||
| auto newBlockAfter = [&](Block *after) -> Block * { | ||
| auto block = std::make_unique<Block>(); | ||
| block->insertAfter(after); | ||
| return block.release(); | ||
| }; | ||
|
|
||
| for (auto [destExpr, value, matched] : result.assignments) { | ||
| auto lhs = context.convertLvalueExpression(*destExpr); | ||
| if (!lhs) | ||
| return failure(); | ||
| auto cond = moore::ToBuiltinIntOp::create(builder, loc, matched); | ||
|
|
||
| auto *assignBlock = newBlockAfter(builder.getInsertionBlock()); | ||
| auto *continuedBlock = newBlockAfter(assignBlock); | ||
| mlir::cf::CondBranchOp::create(builder, loc, cond, assignBlock, | ||
| continuedBlock); | ||
|
|
||
| builder.setInsertionPointToEnd(assignBlock); | ||
| moore::BlockingAssignOp::create(builder, loc, lhs, value); | ||
| mlir::cf::BranchOp::create(builder, loc, continuedBlock); | ||
|
|
||
| builder.setInsertionPointToEnd(continuedBlock); | ||
| } | ||
| return success(); |
Contributor
There was a problem hiding this comment.
Nit: looks like an accidental deletion or rebase mishap.
Comment on lines
+1799
to
+1821
| struct Clog2BIOpConversion : public OpConversionPattern<Clog2BIOp> { | ||
| using OpConversionPattern::OpConversionPattern; | ||
| LogicalResult | ||
| matchAndRewrite(Clog2BIOp op, OpAdaptor adaptor, | ||
| ConversionPatternRewriter &rewriter) const override { | ||
| Value ceil_log; | ||
| auto len_in = adaptor.getValue().getType().getIntOrFloatBitWidth(); | ||
| auto tofloat = arith::UIToFPOp::create( | ||
| rewriter, op.getLoc(), rewriter.getF32Type(), adaptor.getValue()); | ||
| if (tofloat == 0) { | ||
| ceil_log = 0; | ||
| } else { | ||
| ceil_log = math::CeilOp::create( | ||
| rewriter, op.getLoc(), | ||
| math::Log2Op::create(rewriter, op.getLoc(), tofloat)); | ||
| } | ||
| Value out = arith::FPToUIOp::create( | ||
| rewriter, op.getLoc(), rewriter.getIntegerType(len_in), ceil_log); | ||
| rewriter.replaceOp(op, out); | ||
| return success(); | ||
| } | ||
| }; | ||
|
|
Contributor
There was a problem hiding this comment.
Does the Arith dialect have some flavor of leading zero counting, such that we could implement this purely as an integer operation instead of casting to floats?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This commit adds support for the System Verilog real math functions (IEEE1800-2017 Chapter 20.8) by adding previously missing patterns for conversion from Verilog to the 'moore' dialect and adds patterns for conversion using the MLIR dialects 'arith' and 'math' to allow the use of the real math functions as non-synthesizable constructs.
In combination with adding explicit conversion between int and real from the pull requests #9088 and #9317 this addresses the issue using the math functions as raised in issue #8930.
This is done by adding: