CIRCT  20.0.0git
AIGToComb.cpp
Go to the documentation of this file.
1 //===- AIGToComb.cpp - AIG to Comb Conversion Pass --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is the main AIG to Comb Conversion Pass Implementation.
10 //
11 //===----------------------------------------------------------------------===//
12 
16 #include "circt/Dialect/HW/HWOps.h"
17 #include "mlir/Pass/Pass.h"
18 #include "mlir/Transforms/DialectConversion.h"
19 
20 namespace circt {
21 #define GEN_PASS_DEF_CONVERTAIGTOCOMB
22 #include "circt/Conversion/Passes.h.inc"
23 } // namespace circt
24 
25 using namespace circt;
26 using namespace comb;
27 
28 //===----------------------------------------------------------------------===//
29 // Conversion patterns
30 //===----------------------------------------------------------------------===//
31 
32 namespace {
33 
34 struct AIGAndInverterOpConversion : OpConversionPattern<aig::AndInverterOp> {
36  LogicalResult
37  matchAndRewrite(aig::AndInverterOp op, OpAdaptor adaptor,
38  ConversionPatternRewriter &rewriter) const override {
39  // Convert to comb.and + comb.xor + hw.constant
40  auto width = op.getResult().getType().getIntOrFloatBitWidth();
41  auto allOnes =
42  rewriter.create<hw::ConstantOp>(op.getLoc(), APInt::getAllOnes(width));
43  SmallVector<Value> operands;
44  operands.reserve(op.getNumOperands());
45  for (auto [input, inverted] : llvm::zip(op.getOperands(), op.getInverted()))
46  operands.push_back(inverted ? rewriter.createOrFold<comb::XorOp>(
47  op.getLoc(), input, allOnes, true)
48  : input);
49  // NOTE: Use createOrFold to avoid creating a new operation if possible.
50  rewriter.replaceOp(
51  op, rewriter.createOrFold<comb::AndOp>(op.getLoc(), operands, true));
52  return success();
53  }
54 };
55 
56 } // namespace
57 
58 //===----------------------------------------------------------------------===//
59 // Convert AIG to Comb pass
60 //===----------------------------------------------------------------------===//
61 
62 namespace {
63 struct ConvertAIGToCombPass
64  : public impl::ConvertAIGToCombBase<ConvertAIGToCombPass> {
65 
66  void runOnOperation() override;
67  using ConvertAIGToCombBase<ConvertAIGToCombPass>::ConvertAIGToCombBase;
68 };
69 } // namespace
70 
71 static void populateAIGToCombConversionPatterns(RewritePatternSet &patterns) {
72  patterns.add<AIGAndInverterOpConversion>(patterns.getContext());
73 }
74 
75 void ConvertAIGToCombPass::runOnOperation() {
76  ConversionTarget target(getContext());
77  target.addLegalDialect<comb::CombDialect, hw::HWDialect>();
78  target.addIllegalDialect<aig::AIGDialect>();
79 
80  RewritePatternSet patterns(&getContext());
82 
83  if (failed(mlir::applyPartialConversion(getOperation(), target,
84  std::move(patterns))))
85  return signalPassFailure();
86 }
static void populateAIGToCombConversionPatterns(RewritePatternSet &patterns)
Definition: AIGToComb.cpp:71
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: comb.py:1