CIRCT 23.0.0git
Loading...
Searching...
No Matches
LowerSeqShiftReg.cpp
Go to the documentation of this file.
1//===- LowerSeqShiftReg.cpp - seq.shiftreg lowering -----------------------===//
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
14#include "mlir/Pass/Pass.h"
15#include "mlir/Transforms/DialectConversion.h"
16#include "llvm/ADT/TypeSwitch.h"
17
18namespace circt {
19namespace seq {
20#define GEN_PASS_DEF_LOWERSEQSHIFTREG
21#include "circt/Dialect/Seq/SeqPasses.h.inc"
22} // namespace seq
23} // namespace circt
24
25using namespace circt;
26using namespace seq;
27
28namespace {
29
30struct ShiftRegLowering : public OpConversionPattern<seq::ShiftRegOp> {
31public:
32 using OpConversionPattern::OpConversionPattern;
33
34 LogicalResult
35 matchAndRewrite(seq::ShiftRegOp op, OpAdaptor adaptor,
36 ConversionPatternRewriter &rewriter) const final {
37 Value in = adaptor.getInput();
38 auto baseName = op.getName();
39 Value init = {};
40 if (auto powerOn = adaptor.getPowerOnValue()) {
41 if (auto op = powerOn.getDefiningOp()) {
42 if (op->hasTrait<mlir::OpTrait::ConstantLike>())
43 init = createConstantInitialValue(rewriter, op);
44 }
45
46 if (!init)
47 return op->emitError() << "non-constant initial value is not supported";
48 }
49
50 for (size_t i = 0; i < op.getNumElements(); ++i) {
51 // Needs to be initialized or will sefault on shiftregisters with
52 // generated ssa names
53 StringAttr name =
54 rewriter.getStringAttr(baseName.value_or("") + "_sh" + Twine(i + 1));
55
57 rewriter, op.getLoc(), in, adaptor.getClk(), adaptor.getClockEnable(),
58 adaptor.getReset(), adaptor.getResetValue(), name, init);
59 }
60
61 rewriter.replaceOp(op, in);
62 return success();
63 }
64};
65
66struct LowerSeqShiftRegPass
67 : public circt::seq::impl::LowerSeqShiftRegBase<LowerSeqShiftRegPass> {
68 void runOnOperation() override;
69};
70
71} // namespace
72
73void LowerSeqShiftRegPass::runOnOperation() {
74 MLIRContext &ctxt = getContext();
75 ConversionTarget target(ctxt);
76
77 target.addIllegalOp<seq::ShiftRegOp>();
78 target.addLegalDialect<seq::SeqDialect, hw::HWDialect>();
79 RewritePatternSet patterns(&ctxt);
80 patterns.add<ShiftRegLowering>(&ctxt);
81
82 if (failed(
83 applyPartialConversion(getOperation(), target, std::move(patterns))))
84 signalPassFailure();
85}
86
88 return std::make_unique<LowerSeqShiftRegPass>();
89}
create(cls, result_type, reset=None, reset_value=None, name=None, sym_name=None, **kwargs)
Definition seq.py:187
mlir::TypedValue< seq::ImmutableType > createConstantInitialValue(OpBuilder builder, Location loc, mlir::IntegerAttr attr)
Definition SeqOps.cpp:1188
std::unique_ptr< mlir::Pass > createLowerSeqShiftRegPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition seq.py:1