Skip to content

Conversation

uenoku
Copy link
Member

@uenoku uenoku commented Sep 15, 2025

This commit implements a domain-specific structural hashing (CSE) pass for Synth dialect operations (AIG/MIG), providing more aggressive optimization than MLIR's general CSE pass, e.g:

  %0 = synth.aig.and_inv %a, not %b : i2
  %1 = synth.aig.and_inv not %b, %a : i2
=>
  %0 = synth.aig.and_inv %a, not %b: i2 

The pass performs structural hashing for synth.aig.and_inv and synth.mig.maj_inv operations, with operand sorting based on IR positions for better canonicalization and inversion-aware hashing that properly handles AIG/MIG inversion flags.

Basically this corresponds to ABC's strash command.

This commit implements a domain-specific structural hashing (CSE) pass
for Synth dialect operations (AIG/MIG), providing more aggressive
optimization than MLIR's general CSE pass.

The pass performs structural hashing for synth.aig.and_inv and
synth.mig.maj_inv operations, with operand sorting based on IR postions
for better canonicalization and inversion-aware hashing that
properly handles AIG inversion flags.
Copy link
Contributor

@cowardsa cowardsa left a comment

Choose a reason for hiding this comment

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

Only had 30 mins to review - will continue in the morning

Comment on lines 125 to 126
/// Maps inverted values to their non-inverted equivalents for propagation.
DenseMap<Value, Value> inversion;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: struggled to understand what is meant by "for propagation" - would it be possible to add a small example in the comment?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, totally!

Copy link
Contributor

@cowardsa cowardsa left a comment

Choose a reason for hiding this comment

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

LGTM - cannot say I've fully understood how the algorithm works so will have to read the papers from the field. Only comment would be to see if there's a way to provide examples to guide understanding - although the tests are very useful to see the capabilities.

Non-Blocking: As the available passes grow - would be good to expand documentation to understand the capabilities of different passes?

Comment on lines 245 to 247
// Topologically sort target ops within the block.
return !isa<circt::synth::aig::AndInverterOp,
circt::synth::mig::MajorityInverterOp>(op);
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Comment doesn't seem to match function?

Comment on lines +86 to 87
pm.addPass(createStructuralHash());
pm.addPass(createSimpleCanonicalizerPass());
Copy link
Contributor

Choose a reason for hiding this comment

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

Q: is there an argument for running strash after canonicalization - does canonicalization expose more opportunities? e.g. constant folding 1 & x -> x?

Copy link
Member Author

@uenoku uenoku Sep 16, 2025

Choose a reason for hiding this comment

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

Yes, strash would help canonicalization (vice-versa). I added another run of strash after canonicalization.

@uenoku
Copy link
Member Author

uenoku commented Sep 16, 2025

Thank you for the review! Section 2.3 in https://people.eecs.berkeley.edu/~alanmi/publications/2005/tech05_fraigs.pdf briefly mentions Structural hashing. The current implementation probably corresponds to the most basic one-level structural hashing. This is still stronger than MLIR's CSE pass, e.g. MLIR one doesn't use commutativity (a+b and b+a are not CSE'd).

https://circt.llvm.org/docs/Passes/ has a list of passes in CIRCT FYI (Synth was missing but should appear tomorrow f6b70b9). Also I'll describe Synth rational doc.

@uenoku uenoku merged commit 83effb2 into main Sep 16, 2025
7 checks passed
@uenoku uenoku deleted the dev/uenoku/synth-strash branch September 16, 2025 10:03
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