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

Skip to content

Conversation

cowardsa
Copy link
Contributor

A pass lowering datapath operators to a subset of the comb dialect, typically gate-level comb operators. The pass is for usage in the circt-synth pipeline, where we create datapath operators, perform datapath optimisations and then lower back to comb for further processing in the AIG generation pipeline.

The compress operator can be lowered to a Wallace tree and also to a variadic add. The lower-compress-to-add option is used for comparing compressor tree synthesis against other tools (e.g. Yosys). The partial product operator can be lowered to alternative multiplier array implementations depending on the bitwidth. Currently the pass supports lowering to a simple AND array and a Radix-4 Booth multiplier.

A subsequent PR will incorporate the datapath dialect into circt-synth tool flow.

@cowardsa cowardsa requested a review from uenoku July 18, 2025 11:16
@cowardsa
Copy link
Contributor Author

Initially created this in review because think we need to discuss where some of the shared infrastructure should live.

@uenoku - you'll notice that I've currently duplicated your wallace tree and full adder code for example - since this is common infrastructure - where do you think this should best reside?

@uenoku
Copy link
Member

uenoku commented Jul 18, 2025

Super cool!

since this is common infrastructure - where do you think this should best reside?

Could you add function declarations to https://github.com/llvm/circt/blob/main/include/circt/Dialect/Comb/CombOps.h and define in CombOps.cpp?

@cowardsa
Copy link
Contributor Author

@uenoku - have now moved these over to comb ops as suggested - think this is now ready for a full review

@cowardsa cowardsa marked this pull request as ready for review July 21, 2025 14:27
@cowardsa cowardsa requested a review from darthscsi as a code owner July 21, 2025 14:27
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! Could you add a test for option, and also translation validation LEC tests under integration_test for both lowerAndArray and lowerBoothArray lowering paths?

@@ -0,0 +1,47 @@
// RUN: circt-opt %s --convert-datapath-to-comb | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

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

Please test lower-compress-to-add as well.


/// Perform Wallace tree reduction on partial products.
/// See https://en.wikipedia.org/wiki/Wallace_tree
SmallVector<Value>
Copy link
Member

Choose a reason for hiding this comment

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

Could you add documents for targetAddends and partialProducts?

Comment on lines 96 to 98
wallaceReduction(Value falseValue, size_t width, size_t targetAddends,
ConversionPatternRewriter &rewriter, Location loc,
SmallVector<SmallVector<Value>> &partialProducts);
Copy link
Member

Choose a reason for hiding this comment

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

nit: Could you refactor the signature a bit since it's exposed as a library? Specifically could you remove falseValue and use OpBuilder instead of ConversionRewriter, reorder to look like more a builder function.

Suggested change
wallaceReduction(Value falseValue, size_t width, size_t targetAddends,
ConversionPatternRewriter &rewriter, Location loc,
SmallVector<SmallVector<Value>> &partialProducts);
wallaceReduction(OpBuilder &builder, Location loc, size_t width,
size_t targetAddends,
SmallVector<SmallVector<Value>>& partialProducts);

unsigned offset, Value replacement);

/// Construct a full adder for three 1-bit inputs.
std::pair<Value, Value> fullAdder(ConversionPatternRewriter &rewriter,
Copy link
Member

Choose a reason for hiding this comment

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

ConversionPatternRewriter is sub-class of OpBuilder so OpBuilder would be appropriate (I defined this function in this so sorry).

Suggested change
std::pair<Value, Value> fullAdder(ConversionPatternRewriter &rewriter,
std::pair<Value, Value> fullAdder(OpBuilder &builder,

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 for this pointer - wasn't sure on the appropriate signature for these functions!

for (unsigned i = 0; i < width; i += 2) {
// Get Booth bits: b[i+1], b[i], b[i-1] (b[-1] = 0)
Value bim1 = (i == 0) ? zeroFalse : bBits[i - 1];
Value bi = (i < width) ? bBits[i] : zeroFalse;
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
Value bi = (i < width) ? bBits[i] : zeroFalse;
Value bi = bBits[i];

ConversionPatternRewriter &rewriter) const override {

auto inputs = op.getOperands();
Value a = inputs[0];
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
Value a = inputs[0];
Value a = op.getLhs();
Value b = op.getRhs();


assert(addends.size() <= targetAddends);
SmallVector<Value> carrySave;
for (auto addend : addends) {
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
for (auto addend : addends) {
for (auto& addend : addends) {

// CHECK-NEXT: %[[PP2:.+]] = comb.and %[[B2R]], %a : i3
// CHECK-NEXT: %c2_i3 = hw.constant 2 : i3
// CHECK-NEXT: comb.shl %[[PP2]], %c2_i3 : i3
%0:3 = datapath.partial_product %a, %b : (i3, i3) -> (i3, i3, i3)
Copy link
Member

Choose a reason for hiding this comment

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

Could you add a test for booth as well (at least it compiles)?

@cowardsa
Copy link
Contributor Author

@uenoku - have moved everything over to CombOps to provide shared infrastructure - added tests to circt-synth integration tests - since this is largely about lowering? Although open to suggestions to move? - have added an option to forceBooth array lowering so that we can check equivalence in a resonable runtime. Let me know if you've got any other concerns or queries!

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.

Thank you for addressing comments, one question for pattern population helper but LGTM!i

  • added tests to circt-synth integration tests - since this is largely about lowering

I think that makes sense. Eventually we might want to clean up that directory as currently there are tons of translation validation are added altogether but we can do that later.

@cowardsa
Copy link
Contributor Author

Will merge tomorrow unless anyone has any final comments :)

@cowardsa cowardsa merged commit 30a4a38 into llvm:main Jul 25, 2025
7 checks passed
@cowardsa cowardsa deleted the coward/datapath_to_comb branch October 20, 2025 16:14
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.

2 participants