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

Skip to content

Conversation

@fabianschuiki
Copy link
Contributor

Add conversion patterns for the following ops:

  • moore.unreachable
  • moore.builtin.stop
  • moore.builtin.finish
  • moore.builtin.finish_message

Also create a ConversionPatternSet utility that simplifies creating small conversion patterns. This basically allows us to define patterns simply through a conversion function instead of having to define an entire pattern struct. The RewritePatternSet already supports this, but only for functions with the matchAndRewrite signature of rewrite patterns:

LogicalResult (Op, PatternRewriter&)

This new utility allows us to do the same for functions with the usual matchAndRewrite signature of conversion patterns, too:

LogicalResult (Op, Op::Adaptor, ConversionPatternRewriter&)

Copy link
Member

@uenoku uenoku left a comment

Choose a reason for hiding this comment

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

I think this is reasonable but ConversionPatternSet is not actually used in the PR yet, is that correct?

Copy link
Member

@maerhart maerhart left a comment

Choose a reason for hiding this comment

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

LGTM

Comment on lines 41 to 55
/// Add a `matchAndRewrite` function as a conversion pattern to the set.
template <class Op>
ConversionPatternSet &
add(LogicalResult (*implFn)(Op, typename Op::Adaptor,
ConversionPatternRewriter &)) {

struct FnPattern final : public OpConversionPattern<Op> {
using OpConversionPattern<Op>::OpConversionPattern;
LogicalResult (*implFn)(Op, typename Op::Adaptor,
ConversionPatternRewriter &);

LogicalResult
matchAndRewrite(Op op, typename Op::Adaptor adaptor,
ConversionPatternRewriter &rewriter) const override {
return implFn(op, adaptor, rewriter);
}
};

auto pattern =
std::make_unique<FnPattern>(typeConverter, patterns.getContext());
pattern->implFn = implFn;
patterns.add(std::move(pattern));
return *this;
}
Copy link
Member

Choose a reason for hiding this comment

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

This part doesn't seem to be used, thus also not tested. Maybe it'd be a good idea to add this upstream? Seems like it's also useful outside of CIRCT.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I forgot to migrate one of the patterns that actually needs the adaptor. Added that. Good idea about upstreaming this!

Copy link
Member

Choose a reason for hiding this comment

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

I think you might actually have to migrate over all the patterns, because the upstream implementation uses OpRewritePattern as base. I'm not sure if it's still the case with all the dialect conversion framework changes happening, but it used to be (and possibly still is) incompatible with conversion patterns.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems to work 🤔

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm yeah, the RewritePatternSet uses OpRewritePattern. But it's still used to collect the OpConversionPatterns in a dialect conversion 🤔. This new pattern set just factors out creating a subclass of OpConversionPattern into a helper function, but the rest is still the same as any other dialect conversion.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, that's part of what's meant with "API design suggests that Conversion/RewritePattern are compatible" in the slides. What I was suggesting is to change all convert functions in the pass to have the adaptor argument even if it is not used (or change the add function in ConversionPatternSet that currently delegates to RewritePatternSet to also create a OpConversionPattern instead)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah I see! Sorry, slow brain today. Let me change those over to use the adaptor 👍.

Add conversion patterns for the following ops:

- `moore.unreachable`
- `moore.builtin.stop`
- `moore.builtin.finish`
- `moore.builtin.finish_message`

Also create a `ConversionPatternSet` utility that simplifies creating
small conversion patterns. This basically allows us to define patterns
simply through a conversion function instead of having to define an
entire pattern struct. The `RewritePatternSet` already supports this,
but only for functions with the `matchAndRewrite` signature of rewrite
patterns:

    LogicalResult (Op, PatternRewriter&)

This new utility allows us to do the same for functions with the usual
`matchAndRewrite` signature of conversion patterns, too:

    LogicalResult (Op, Op::Adaptor, ConversionPatternRewriter&)
@fabianschuiki fabianschuiki force-pushed the fschuiki/verilog-stop-finish branch from 12efba4 to 1df40c6 Compare August 27, 2025 20:44
@fabianschuiki fabianschuiki merged commit a3dd884 into main Aug 27, 2025
7 checks passed
@fabianschuiki fabianschuiki deleted the fschuiki/verilog-stop-finish branch August 27, 2025 21:01
fabianschuiki added a commit that referenced this pull request Aug 28, 2025
Use the conversion function signature that includes the op's adaptor as
well as the `ConversionPatternRewriter`. See @maerhart's comments on
this in #8895.
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.

3 participants