12#include "mlir/Pass/Pass.h"
13#include "mlir/Transforms/DialectConversion.h"
14#include "llvm/ADT/APInt.h"
18#define GEN_PASS_DEF_HWAGGREGATETOCOMB
19#include "circt/Dialect/HW/Passes.h.inc"
29template <
typename OpTy>
34 matchAndRewrite(OpTy op, OpAdaptor adaptor,
35 ConversionPatternRewriter &rewriter)
const override {
36 rewriter.replaceOpWithNewOp<
comb::ConcatOp>(op, adaptor.getInputs());
41struct HWAggregateConstantOpConversion
45 static LogicalResult peelAttribute(Location loc, Attribute attr,
46 ConversionPatternRewriter &rewriter,
48 SmallVector<Attribute> worklist;
49 worklist.push_back(attr);
50 unsigned nextInsertion = intVal.getBitWidth();
52 while (!worklist.empty()) {
53 auto current = worklist.pop_back_val();
54 if (
auto innerArray = dyn_cast<ArrayAttr>(current)) {
55 for (
auto elem :
llvm::reverse(innerArray))
56 worklist.push_back(elem);
60 if (
auto intAttr = dyn_cast<IntegerAttr>(current)) {
61 auto chunk = intAttr.getValue();
62 nextInsertion -= chunk.getBitWidth();
63 intVal.insertBits(chunk, nextInsertion);
74 matchAndRewrite(hw::AggregateConstantOp op, OpAdaptor adaptor,
75 ConversionPatternRewriter &rewriter)
const override {
77 SmallVector<Value> results;
78 auto bitWidth = hw::getBitWidth(op.getType());
79 assert(bitWidth >= 0 &&
"bit width must be known for constant");
80 APInt intVal(bitWidth, 0);
81 if (failed(peelAttribute(op.getLoc(), adaptor.getFieldsAttr(), rewriter,
94 ConversionPatternRewriter &rewriter)
const override {
95 SmallVector<Value> results;
96 auto arrayType = cast<hw::ArrayType>(op.getInput().getType());
97 auto elemType = arrayType.getElementType();
99 auto elemWidth = hw::getBitWidth(elemType);
101 return rewriter.notifyMatchFailure(op.getLoc(),
"unknown element width");
103 auto lowered = adaptor.getInput();
106 op.getLoc(), lowered, i * elemWidth, elemWidth));
108 SmallVector<Value> bits;
109 comb::extractBits(rewriter, op.getIndex(), bits);
110 auto result = comb::constructMuxTree(rewriter, op.getLoc(), bits, results,
113 rewriter.replaceOp(op, result);
120class AggregateTypeConverter :
public TypeConverter {
122 AggregateTypeConverter() {
123 addConversion([](Type type) -> Type {
return type; });
124 addConversion([](hw::ArrayType t) -> Type {
125 return IntegerType::get(t.getContext(), hw::getBitWidth(t));
127 addTargetMaterialization([](mlir::OpBuilder &builder, mlir::Type resultType,
128 mlir::ValueRange inputs,
129 mlir::Location loc) -> mlir::Value {
130 if (inputs.size() != 1)
133 return builder.create<
hw::BitcastOp>(loc, resultType, inputs[0])
137 addSourceMaterialization([](mlir::OpBuilder &builder, mlir::Type resultType,
138 mlir::ValueRange inputs,
139 mlir::Location loc) -> mlir::Value {
140 if (inputs.size() != 1)
143 return builder.create<
hw::BitcastOp>(loc, resultType, inputs[0])
151 RewritePatternSet &
patterns, AggregateTypeConverter &typeConverter) {
152 patterns.add<HWArrayGetOpConversion,
153 HWArrayCreateLikeOpConversion<hw::ArrayCreateOp>,
154 HWArrayCreateLikeOpConversion<hw::ArrayConcatOp>,
155 HWAggregateConstantOpConversion>(typeConverter,
160struct HWAggregateToCombPass
161 :
public hw::impl::HWAggregateToCombBase<HWAggregateToCombPass> {
162 void runOnOperation()
override;
163 using HWAggregateToCombBase<HWAggregateToCombPass>::HWAggregateToCombBase;
167void HWAggregateToCombPass::runOnOperation() {
168 ConversionTarget target(getContext());
172 hw::AggregateConstantOp>();
174 target.addLegalDialect<hw::HWDialect, comb::CombDialect>();
176 RewritePatternSet
patterns(&getContext());
177 AggregateTypeConverter typeConverter;
180 if (failed(mlir::applyPartialConversion(getOperation(), target,
182 return signalPassFailure();
186 return std::make_unique<HWAggregateToCombPass>();
assert(baseType &&"element must be base type")
MlirType uint64_t numElements
static void populateHWAggregateToCombOpConversionPatterns(RewritePatternSet &patterns, AggregateTypeConverter &typeConverter)
std::unique_ptr< mlir::Pass > createHWAggregateToCombPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.