Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: [AIG] Add an AIG dialect and circt-synth #7717

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

uenoku
Copy link
Member

@uenoku uenoku commented Oct 20, 2024

This proposes AIG(And Inverter Graph) dialect which is meant to be a foundational dialect for logic synthesis.

Please see docs/Dialects/AIG/RationaleAIG.md and include/Diaclects/AIG/AIGOps.td for design details.

This PR implements E2E flow for FPGA synthesis with a naive implementation for LUT mapping (test/circt-synth/lut-size.mlir). Only comb.and/or/xor are supported as an input but it’s straightforward to implement a lowering for arithmetic ops. Right now comb.truth_table op is used as a target of LUT mapping.

We probably still want Yosys integration (#7663) but CIRCT-native synthesizer would be worth doing in the long term.

@uenoku uenoku force-pushed the dev/hidetou/aig branch 3 times, most recently from 0b0ca9f to 15db77f Compare October 20, 2024 19:23
@sequencer
Copy link
Contributor

Since the developing of logic synthesis is really a non-trivial work, I wonder rather than using CIRCT to do the synthesis job by itself, how about exporting the aiger file directly, and then we can consume it by abc or mockturtle for future use.

@uenoku
Copy link
Member Author

uenoku commented Oct 21, 2024

Since the developing of logic synthesis is really a non-trivial work, I wonder rather than using CIRCT to do the synthesis job by itself, how about exporting the aiger file directly, and then we can consume it by abc or mockturtle for future use.

Implementing AIGER file exporter/importer is trivial once we lowered the IR to (low) AIG dialect (= aig.and_inv is lowered to have a single bit and two operands). It's totally reasonable to have an integration with AIGER file to leverage other tools and I'm happy to work/help on that!

That said even though I agree it's non-trivial to implement logic synthesizer but I believe it would be feasible to get OK results once we implemented necessary passes (Lower to FRAIG, Bit-sensitive IMCP/IMDCE, inlining, re-balancing).

@TaoBi22
Copy link
Contributor

TaoBi22 commented Oct 21, 2024

Super exciting 😄

@fabianschuiki
Copy link
Contributor

This is super exciting 🥳! I still have to look through the code in more detail. One question I had after a cursory look: is it beneficial to allow each operand in aig.and_inv to be inverted, or does that make writing passes more complex? I think I remember seeing other implementations make the single output of the AND gate invertable, which would mean it's just a boolean on the op.

lib/Dialect/AIG/AIGOps.cpp Outdated Show resolved Hide resolved
@uenoku
Copy link
Member Author

uenoku commented Oct 21, 2024

One question I had after a cursory look: is it beneficial to allow each operand in aig.and_inv to be inverted, or does that make writing passes more complex?

Inverting on operands would be more closer to traditional AIG format including AIGER (thank you for checking @fabianschuiki!) so the current representation seems to be right way to go.

@@ -26,6 +26,7 @@ set(CIRCT_TEST_DEPENDS
circt-dis
circt-lec
circt-opt
circt-synth
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes me unreasonably happy 🥳 😄

Copy link
Contributor

@fabianschuiki fabianschuiki left a comment

Choose a reason for hiding this comment

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

This is seriously cool stuff! Can't wait to have this landed and be a starting point for optimizations, tech mapping, potential static timing analysis, etc. Really cool!

@uenoku uenoku force-pushed the dev/hidetou/aig branch 2 times, most recently from e8bb21f to 75852a7 Compare October 22, 2024 09:39
Copy link
Contributor

@mikeurbach mikeurbach left a comment

Choose a reason for hiding this comment

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

A couple minor questions. This is awesome in general. The motivation and dialect boilerplate make sense to me. I skimmed through the new tool and the initial passes as well, and they make sense as a starting point. Looking forward to seeing more advanced integration here!

lib/Dialect/AIG/Transforms/GreedyCutDecomp.cpp Outdated Show resolved Hide resolved
lib/Dialect/AIG/Transforms/LowerVariadic.cpp Outdated Show resolved Hide resolved
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.

Really cool to see this coming!

include/circt/Conversion/CombToAIG.h Outdated Show resolved Hide resolved
include/circt/Conversion/CombToAIG.h Outdated Show resolved Hide resolved
include/circt/Dialect/AIG/AIG.td Outdated Show resolved Hide resolved
include/circt/Dialect/AIG/AIG.td Outdated Show resolved Hide resolved
include/circt/Dialect/AIG/AIG.td Outdated Show resolved Hide resolved
lib/Dialect/AIG/AIGOps.cpp Outdated Show resolved Hide resolved
lib/Dialect/AIG/AIGOps.cpp Outdated Show resolved Hide resolved
lib/Dialect/AIG/AIGOps.cpp Outdated Show resolved Hide resolved
Comment on lines 106 to 108
oldCutOutput->erase();
// Erase arguments before inlining. Arguments are already replaced.
oldCutBlock->eraseArguments([](BlockArgument block) { return true; });
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 these things should go through the rewriter and not be done directly on the op/block.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for pointing out. oldCutOutput must be erased through rewriter but block arguments should be fine to erase directly. There is no API in rewriter to erase block arguments and upstream does the same (https://github.com/llvm/llvm-project/blob/a285ba7529feaa7c49890e314facb5e9f4d8dc11/mlir/lib/Conversion/TosaToSCF/TosaToSCF.cpp#L38-L39)

}
if (!changed)
return failure();
block->eraseArguments(eraseArgs);
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this call and the one further down go through the rewriter?

@uenoku
Copy link
Member Author

uenoku commented Oct 25, 2024

@maerhart Thank you for really detailed review! and really sorry for a bunch of dirty code remaining in my PR. I'll make sure to double check basic stuffs before removing draft..

@uenoku uenoku force-pushed the dev/hidetou/aig branch 3 times, most recently from 519947a to 6b467f4 Compare October 25, 2024 17:21
@uenoku
Copy link
Member Author

uenoku commented Oct 25, 2024

Thank you really for detailed review @mikeurbach @fabianschuiki @maerhart! I'm going to merge basic stuffs in refined PRs/commits. I'll separate non-trivial passes (GreedyCutDecomp, CutToLUT) into a different PR for the ease of review.

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.

6 participants