18 #include "mlir/Transforms/DialectConversion.h"
22 #define GEN_PASS_DEF_LOWERCONCATREF
23 #include "circt/Dialect/Moore/MoorePasses.h.inc"
27 using namespace circt;
28 using namespace moore;
34 static void collectOperands(Value operand, SmallVectorImpl<Value> &operands) {
35 if (
auto concatRefOp = operand.getDefiningOp<ConcatRefOp>())
36 for (
auto nestedOperand : concatRefOp.getValues())
37 collectOperands(nestedOperand, operands);
39 operands.push_back(operand);
42 template <
typename OpTy>
45 using OpAdaptor =
typename OpTy::Adaptor;
48 matchAndRewrite(OpTy op, OpAdaptor adaptor,
49 ConversionPatternRewriter &rewriter)
const override {
51 SmallVector<Value, 4> operands;
52 collectOperands(op.getDst(), operands);
54 cast<UnpackedType>(op.getSrc().getType()).getBitSize().value();
58 for (
auto operand : operands) {
59 auto type = cast<RefType>(operand.getType()).getNestedType();
60 auto width = type.getBitSize().value();
62 rewriter.setInsertionPoint(op);
67 auto extract = rewriter.create<ExtractOp>(op.getLoc(), type, op.getSrc(),
72 srcWidth = srcWidth -
width;
74 rewriter.create<OpTy>(op.getLoc(), operand, extract);
81 struct LowerConcatRefPass
82 :
public circt::moore::impl::LowerConcatRefBase<LowerConcatRefPass> {
83 void runOnOperation()
override;
89 return std::make_unique<LowerConcatRefPass>();
92 void LowerConcatRefPass::runOnOperation() {
93 MLIRContext &context = getContext();
94 ConversionTarget target(context);
96 target.addDynamicallyLegalOp<ContinuousAssignOp, BlockingAssignOp,
97 NonBlockingAssignOp>([](
auto op) {
98 return !op->getOperand(0).template getDefiningOp<ConcatRefOp>();
101 target.addLegalDialect<MooreDialect>();
102 RewritePatternSet
patterns(&context);
103 patterns.add<ConcatRefLowering<ContinuousAssignOp>,
104 ConcatRefLowering<BlockingAssignOp>,
105 ConcatRefLowering<NonBlockingAssignOp>>(&context);
108 applyPartialConversion(getOperation(), target, std::move(
patterns))))
std::unique_ptr< mlir::Pass > createLowerConcatRefPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.