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

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/circt/Conversion/DatapathToComb.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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_DATAPATHTOCOMB_H
#define CIRCT_CONVERSION_DATAPATHTOCOMB_H

#include "circt/Support/LLVM.h"

namespace circt {

#define GEN_PASS_DECL_CONVERTDATAPATHTOCOMB
#include "circt/Conversion/Passes.h.inc"

} // namespace circt

#endif // CIRCT_CONVERSION_DATAPATHTOCOMB_H
1 change: 1 addition & 0 deletions include/circt/Conversion/Passes.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "circt/Conversion/CombToSMT.h"
#include "circt/Conversion/ConvertToArcs.h"
#include "circt/Conversion/DCToHW.h"
#include "circt/Conversion/DatapathToComb.h"
#include "circt/Conversion/DatapathToSMT.h"
#include "circt/Conversion/ExportChiselInterface.h"
#include "circt/Conversion/ExportVerilog.h"
Expand Down
18 changes: 18 additions & 0 deletions include/circt/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -871,4 +871,22 @@ def ConvertDatapathToSMT : Pass<"convert-datapath-to-smt"> {
];
}

//===----------------------------------------------------------------------===//
// ConvertDatapathToComb
//===----------------------------------------------------------------------===//

def ConvertDatapathToComb : Pass<"convert-datapath-to-comb"> {
let summary = "Convert Datapath ops to Comb ops";
let dependentDialects = [
"circt::comb::CombDialect", "circt::datapath::DatapathDialect",
"circt::hw::HWDialect"
];
let options = [
Option<"lowerCompressToAdd", "lower-compress-to-add", "bool", "false",
"Lower compress operators to variadic add.">,
Option<"forceBooth", "lower-partial-product-to-booth", "bool", "false",
"Force all partial products to be lowered to Booth arrays.">
];
}

#endif // CIRCT_CONVERSION_PASSES_TD
13 changes: 13 additions & 0 deletions include/circt/Dialect/Comb/CombOps.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "mlir/Interfaces/InferIntRangeInterface.h"
#include "mlir/Interfaces/InferTypeOpInterface.h"
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Transforms/DialectConversion.h"

namespace llvm {
struct KnownBits;
Expand Down Expand Up @@ -85,6 +86,18 @@ Value createDynamicInject(OpBuilder &builder, Location loc, Value value,
Value createInject(OpBuilder &builder, Location loc, Value value,
unsigned offset, Value replacement);

/// Construct a full adder for three 1-bit inputs.
std::pair<Value, Value> fullAdder(OpBuilder &builder, Location loc, Value a,
Value b, Value c);

/// Perform Wallace tree reduction on partial products.
/// See https://en.wikipedia.org/wiki/Wallace_tree
/// \param targetAddends The number of addends to reduce to (2 for carry-save).
/// \param inputAddends The rows of bits to be summed.
SmallVector<Value> wallaceReduction(OpBuilder &builder, Location loc,
size_t width, size_t targetAddends,
SmallVector<SmallVector<Value>> &addends);

} // namespace comb
} // namespace circt

Expand Down
48 changes: 48 additions & 0 deletions integration_test/circt-synth/datapath-lowering-lec.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// REQUIRES: libz3
// REQUIRES: circt-lec-jit

// RUN: circt-opt %s --convert-datapath-to-comb -o %t.mlir
// RUN: circt-lec %t.mlir %s -c1=partial_product_5 -c2=partial_product_5 --shared-libs=%libz3 | FileCheck %s --check-prefix=AND5
// AND5: c1 == c2
hw.module @partial_product_5(in %a : i5, in %b : i5, out sum : i5) {
%0:5 = datapath.partial_product %a, %b : (i5, i5) -> (i5, i5, i5, i5, i5)
%1 = comb.add bin %0#0, %0#1, %0#2, %0#3, %0#4 : i5
hw.output %1 : i5
}

// RUN: circt-lec %t.mlir %s -c1=partial_product_4 -c2=partial_product_4 --shared-libs=%libz3 | FileCheck %s --check-prefix=AND4
// AND4: c1 == c2
hw.module @partial_product_4(in %a : i4, in %b : i4, out sum : i4) {
%0:4 = datapath.partial_product %a, %b : (i4, i4) -> (i4, i4, i4, i4)
%1 = comb.add bin %0#0, %0#1, %0#2, %0#3 : i4
hw.output %1 : i4
}

// RUN: circt-lec %t.mlir %s -c1=compress_3 -c2=compress_3 --shared-libs=%libz3 | FileCheck %s --check-prefix=COMP3
// COMP3: c1 == c2
hw.module @compress_3(in %a : i4, in %b : i4, in %c : i4, out sum : i4) {
%0:2 = datapath.compress %a, %b, %c : i4 [3 -> 2]
%1 = comb.add bin %0#0, %0#1 : i4
hw.output %1 : i4
}

// RUN: circt-lec %t.mlir %s -c1=compress_6 -c2=compress_6 --shared-libs=%libz3 | FileCheck %s --check-prefix=COMP6
// COMP6: c1 == c2
hw.module @compress_6(in %a : i4, in %b : i4, in %c : i4, in %d : i4, in %e : i4, in %f : i4, out sum : i4) {
%0:3 = datapath.compress %a, %b, %c, %d, %e, %f : i4 [6 -> 3]
%1 = comb.add bin %0#0, %0#1, %0#2 : i4
hw.output %1 : i4
}

// RUN: circt-opt %s --pass-pipeline="builtin.module(hw.module(convert-datapath-to-comb{lower-partial-product-to-booth=true lower-compress-to-add=true}))" -o %t.mlir
// RUN: circt-lec %t.mlir %s -c1=partial_product_5 -c2=partial_product_5 --shared-libs=%libz3 | FileCheck %s --check-prefix=BOOTH5
// BOOTH5: c1 == c2

// RUN: circt-lec %t.mlir %s -c1=partial_product_4 -c2=partial_product_4 --shared-libs=%libz3 | FileCheck %s --check-prefix=BOOTH4
// BOOTH4: c1 == c2

// RUN: circt-lec %t.mlir %s -c1=compress_3 -c2=compress_3 --shared-libs=%libz3 | FileCheck %s --check-prefix=COMPADD3
// COMPADD3: c1 == c2

// RUN: circt-lec %t.mlir %s -c1=compress_6 -c2=compress_6 --shared-libs=%libz3 | FileCheck %s --check-prefix=COMPADD6
// COMPADD6: c1 == c2
1 change: 1 addition & 0 deletions lib/CAPI/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ add_circt_public_c_api_library(CIRCTCAPIConversion
CIRCTCombToLLVM
CIRCTCombToSMT
CIRCTConvertToArcs
CIRCTDatapathToComb
CIRCTDatapathToSMT
CIRCTDCToHW
CIRCTExportChiselInterface
Expand Down
1 change: 1 addition & 0 deletions lib/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ add_subdirectory(CombToDatapath)
add_subdirectory(CombToLLVM)
add_subdirectory(CombToSMT)
add_subdirectory(ConvertToArcs)
add_subdirectory(DatapathToComb)
add_subdirectory(DatapathToSMT)
add_subdirectory(DCToHW)
add_subdirectory(ExportAIGER)
Expand Down
87 changes: 6 additions & 81 deletions lib/Conversion/CombToAIG/CombToAIG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,21 +668,6 @@ struct CombSubOpConversion : OpConversionPattern<SubOp> {
}
};

// Construct a full adder for three 1-bit inputs.
std::pair<Value, Value> fullAdder(ConversionPatternRewriter &rewriter,
Location loc, Value a, Value b, Value c) {
auto aXorB = rewriter.createOrFold<comb::XorOp>(loc, a, b, true);
Value sum = rewriter.createOrFold<comb::XorOp>(loc, aXorB, c, true);

auto carry = rewriter.createOrFold<comb::OrOp>(
loc,
ArrayRef<Value>{rewriter.createOrFold<comb::AndOp>(loc, a, b, true),
rewriter.createOrFold<comb::AndOp>(loc, aXorB, c, true)},
true);

return {sum, carry};
}

struct CombMulOpConversion : OpConversionPattern<MulOp> {
using OpConversionPattern<MulOp>::OpConversionPattern;
using OpAdaptor = typename OpConversionPattern<MulOp>::OpAdaptor;
Expand Down Expand Up @@ -729,74 +714,14 @@ struct CombMulOpConversion : OpConversionPattern<MulOp> {
return success();
}

// Wallace tree reduction
replaceOpAndCopyNamehint(
rewriter, op,
wallaceReduction(falseValue, width, rewriter, loc, partialProducts));
// Wallace tree reduction - reduce to two addends.
auto addends =
comb::wallaceReduction(rewriter, loc, width, 2, partialProducts);
// Sum the two addends using a carry-propagate adder
auto newAdd = rewriter.create<comb::AddOp>(loc, addends, true);
replaceOpAndCopyNamehint(rewriter, op, newAdd);
return success();
}

private:
// Perform Wallace tree reduction on partial products.
// See https://en.wikipedia.org/wiki/Wallace_tree
static Value
wallaceReduction(Value falseValue, size_t width,
ConversionPatternRewriter &rewriter, Location loc,
SmallVector<SmallVector<Value>> &partialProducts) {
SmallVector<SmallVector<Value>> newPartialProducts;
newPartialProducts.reserve(partialProducts.size());
// Continue reduction until we have only two rows. The length of
// `partialProducts` is reduced by 1/3 in each iteration.
while (partialProducts.size() > 2) {
newPartialProducts.clear();
// Take three rows at a time and reduce to two rows(sum and carry).
for (unsigned i = 0; i < partialProducts.size(); i += 3) {
if (i + 2 < partialProducts.size()) {
// We have three rows to reduce
auto &row1 = partialProducts[i];
auto &row2 = partialProducts[i + 1];
auto &row3 = partialProducts[i + 2];

assert(row1.size() == width && row2.size() == width &&
row3.size() == width);

SmallVector<Value> sumRow, carryRow;
sumRow.reserve(width);
carryRow.reserve(width);
carryRow.push_back(falseValue);

// Process each bit position
for (unsigned j = 0; j < width; ++j) {
// Full adder logic
auto [sum, carry] =
fullAdder(rewriter, loc, row1[j], row2[j], row3[j]);
sumRow.push_back(sum);
if (j + 1 < width)
carryRow.push_back(carry);
}

newPartialProducts.push_back(std::move(sumRow));
newPartialProducts.push_back(std::move(carryRow));
} else {
// Add remaining rows as is
newPartialProducts.append(partialProducts.begin() + i,
partialProducts.end());
}
}

std::swap(newPartialProducts, partialProducts);
}

assert(partialProducts.size() == 2);
// Reverse the order of the bits
std::reverse(partialProducts[0].begin(), partialProducts[0].end());
std::reverse(partialProducts[1].begin(), partialProducts[1].end());
auto lhs = rewriter.create<comb::ConcatOp>(loc, partialProducts[0]);
auto rhs = rewriter.create<comb::ConcatOp>(loc, partialProducts[1]);

// Use comb.add for the final addition.
return rewriter.create<comb::AddOp>(loc, ArrayRef<Value>{lhs, rhs}, true);
}
};

template <typename OpTy>
Expand Down
15 changes: 15 additions & 0 deletions lib/Conversion/DatapathToComb/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_circt_conversion_library(CIRCTDatapathToComb
DatapathToComb.cpp

DEPENDS
CIRCTConversionPassIncGen

LINK_LIBS PUBLIC
CIRCTComb
CIRCTDatapath
CIRCTHW
MLIRIR
MLIRPass
MLIRSupport
MLIRTransforms
)
Loading