-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AIGToComb] [circt-synth] Add a AIG to Comb conversion pass
This adds a conversion pass from AIG dialect to Comb dialect. AndInverterOp can be easily converted into comb.and + comb.xor + hw.constant. This enables us to utilize core dialects tools for synthesis results without any addition. Primarly use case is running LEC on IR before and after synthesis.
- Loading branch information
Showing
12 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===- AIGToComb.h - AIG to Comb dialect conversion -------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef CIRCT_CONVERSION_AIGTOCOMB_H | ||
#define CIRCT_CONVERSION_AIGTOCOMB_H | ||
|
||
#include "circt/Support/LLVM.h" | ||
#include <memory> | ||
|
||
namespace circt { | ||
|
||
#define GEN_PASS_DECL_CONVERTAIGTOCOMB | ||
#include "circt/Conversion/Passes.h.inc" | ||
|
||
} // namespace circt | ||
|
||
#endif // CIRCT_CONVERSION_AIGTOCOMB_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
//===- AIGToComb.cpp - AIG to Comb Conversion Pass --------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This is the main AIG to Comb Conversion Pass Implementation. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "circt/Conversion/AIGToComb.h" | ||
#include "circt/Dialect/AIG/AIGOps.h" | ||
#include "circt/Dialect/Comb/CombOps.h" | ||
#include "circt/Dialect/HW/HWOps.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
|
||
namespace circt { | ||
#define GEN_PASS_DEF_CONVERTAIGTOCOMB | ||
#include "circt/Conversion/Passes.h.inc" | ||
} // namespace circt | ||
|
||
using namespace circt; | ||
using namespace comb; | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Conversion patterns | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
|
||
struct AIGAndInverterOpConversion : OpConversionPattern<aig::AndInverterOp> { | ||
using OpConversionPattern<aig::AndInverterOp>::OpConversionPattern; | ||
LogicalResult | ||
matchAndRewrite(aig::AndInverterOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const override { | ||
// Convert to comb.and + comb.xor + hw.constant | ||
auto width = op.getResult().getType().getIntOrFloatBitWidth(); | ||
auto allOnes = | ||
rewriter.create<hw::ConstantOp>(op.getLoc(), APInt::getAllOnes(width)); | ||
SmallVector<Value> operands; | ||
operands.reserve(op.getNumOperands()); | ||
for (auto [input, inverted] : llvm::zip(op.getOperands(), op.getInverted())) | ||
operands.push_back(inverted ? rewriter.createOrFold<comb::XorOp>( | ||
op.getLoc(), input, allOnes, true) | ||
: input); | ||
// NOTE: Use createOrFold to avoid creating a new operation if possible. | ||
rewriter.replaceOp( | ||
op, rewriter.createOrFold<comb::AndOp>(op.getLoc(), operands, true)); | ||
return success(); | ||
} | ||
}; | ||
|
||
} // namespace | ||
|
||
//===----------------------------------------------------------------------===// | ||
// Convert AIG to Comb pass | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
struct ConvertAIGToCombPass | ||
: public impl::ConvertAIGToCombBase<ConvertAIGToCombPass> { | ||
|
||
void runOnOperation() override; | ||
using ConvertAIGToCombBase<ConvertAIGToCombPass>::ConvertAIGToCombBase; | ||
}; | ||
} // namespace | ||
|
||
static void populateAIGToCombConversionPatterns(RewritePatternSet &patterns) { | ||
patterns.add<AIGAndInverterOpConversion>(patterns.getContext()); | ||
} | ||
|
||
void ConvertAIGToCombPass::runOnOperation() { | ||
ConversionTarget target(getContext()); | ||
target.addLegalDialect<comb::CombDialect, hw::HWDialect>(); | ||
target.addIllegalDialect<aig::AIGDialect>(); | ||
|
||
RewritePatternSet patterns(&getContext()); | ||
populateAIGToCombConversionPatterns(patterns); | ||
|
||
if (failed(mlir::applyPartialConversion(getOperation(), target, | ||
std::move(patterns)))) | ||
return signalPassFailure(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
add_circt_conversion_library(CIRCTAIGToComb | ||
AIGToComb.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${CIRCT_MAIN_INCLUDE_DIR}/circt/Conversion/AIGToComb | ||
|
||
DEPENDS | ||
CIRCTConversionPassIncGen | ||
|
||
LINK_LIBS PUBLIC | ||
CIRCTAIG | ||
CIRCTHW | ||
CIRCTComb | ||
MLIRIR | ||
MLIRPass | ||
MLIRSupport | ||
MLIRTransforms | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// RUN: circt-opt %s --convert-aig-to-comb | FileCheck %s | ||
|
||
// CHECK-LABEL: @test | ||
hw.module @test(in %a: i32, in %b: i32, in %c: i32, in %d: i32, out out0: i32) { | ||
// CHECK: %c-1_i32 = hw.constant -1 : i32 | ||
// CHECK: %[[NOT_A:.+]] = comb.xor bin %a, %c-1_i32 : i32 | ||
// CHECK: %[[NOT_C:.+]] = comb.xor bin %c, %c-1_i32 : i32 | ||
// CHECK: %[[RESULT:.+]] = comb.and bin %[[NOT_A]], %b, %[[NOT_C]], %d : i32 | ||
// CHECK: hw.output %[[RESULT]] : i32 | ||
%0 = aig.and_inv not %a, %b, not %c, %d : i32 | ||
hw.output %0 : i32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters