Skip to content

Conversation

cowardsa
Copy link
Contributor

@cowardsa cowardsa commented Jul 8, 2025

Add support for lowering variadic adders and two-input multipliers to datapath operations. Now automiatng

%0 = comb.mul %a, %b : i4
%1 = comb.add %0, %c : i4

Resulting from comb-to-datapath and canonicalize:

%0:4 = datapath.partial_product %a, %b : (i4, i4) -> (i4, i4, i4, i4)
%1:2 = datapath.compress %0#0, %0#1, %0#2, %0#3, %c : i4 [5 -> 2]
%2 = comb.add %1#0, %1#1 : i4

This will be integrated into circt-synth during a subsequent PR.

cowardsa added 30 commits July 1, 2025 11:52
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.

Seems like you're missing a link target in https://github.com/llvm/circt/blob/main/lib/CAPI/Conversion/CMakeLists.txt

Otherwise LGTM!

target.addLegalDialect<datapath::DatapathDialect, comb::CombDialect,
hw::HWDialect>();

target.addIllegalOp<comb::AddOp, comb::MulOp>();
Copy link
Member

Choose a reason for hiding this comment

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

Isn't this redundant due to the below dynamic legality constraints?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks Martin - didn't realise that addDynamicallyLegalOp would mark the remainder as illegal - useful to know!

ConversionPatternRewriter &rewriter) const override {
// Can only introduce compressor for three-inputs or more
if (op.getNumOperands() <= 2)
return failure();
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I think the preferred way to fail is to use rewriter.notifyMatchFailure which will also notify the listener (also in the pattern below).

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 operations with numOperands() <= 2 are legal so I believe patterns won't apply in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed this check as suggested

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.

LGTM


// Permit 2-input adders (carry-propagate adders)
target.addDynamicallyLegalOp<comb::AddOp>(
[](comb::AddOp op) { return op.getNumOperands() == 2; });
Copy link
Member

Choose a reason for hiding this comment

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

nit: Can we make unary add legal as well? They will be canonicalized anyway

Suggested change
[](comb::AddOp op) { return op.getNumOperands() == 2; });
[](comb::AddOp op) { return op.getNumOperands() <= 2; });

ConversionPatternRewriter &rewriter) const override {
// Can only introduce compressor for three-inputs or more
if (op.getNumOperands() <= 2)
return failure();
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 operations with numOperands() <= 2 are legal so I believe patterns won't apply in the first place?

@cowardsa
Copy link
Contributor Author

cowardsa commented Jul 9, 2025

Thanks reviewers - suggestions implemented ready to merge once checks are completed - still awaiting commit access approval...

@uenoku uenoku merged commit 713a91d into llvm:main Jul 9, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants