CIRCT  19.0.0git
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 
10 #include "circt/Dialect/SV/SVOps.h"
14 #include "mlir/Pass/Pass.h"
15 #include "mlir/Transforms/DialectConversion.h"
16 #include "llvm/ADT/TypeSwitch.h"
17 
18 namespace circt {
19 namespace seq {
20 #define GEN_PASS_DEF_LOWERSEQSHIFTREG
21 #include "circt/Dialect/Seq/SeqPasses.h.inc"
22 } // namespace seq
23 } // namespace circt
24 
25 using namespace circt;
26 using namespace seq;
27 
28 namespace {
29 
30 struct ShiftRegLowering : public OpConversionPattern<seq::ShiftRegOp> {
31 public:
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  for (size_t i = 0; i < op.getNumElements(); ++i) {
40  StringAttr name;
41  if (baseName.has_value())
42  name = rewriter.getStringAttr(baseName.value() + "_sh" + Twine(i + 1));
43  in = rewriter.create<seq::CompRegClockEnabledOp>(
44  op.getLoc(), in, adaptor.getClk(), adaptor.getClockEnable(),
45  adaptor.getReset(), adaptor.getResetValue(), name,
46  op.getPowerOnValue());
47  }
48 
49  op.replaceAllUsesWith(in);
50  rewriter.eraseOp(op);
51  return success();
52  }
53 };
54 
55 struct LowerSeqShiftRegPass
56  : public circt::seq::impl::LowerSeqShiftRegBase<LowerSeqShiftRegPass> {
57  void runOnOperation() override;
58 };
59 
60 } // namespace
61 
62 void LowerSeqShiftRegPass::runOnOperation() {
63  MLIRContext &ctxt = getContext();
64  ConversionTarget target(ctxt);
65 
66  target.addIllegalOp<seq::ShiftRegOp>();
67  target.addLegalDialect<seq::SeqDialect>();
68  RewritePatternSet patterns(&ctxt);
69  patterns.add<ShiftRegLowering>(&ctxt);
70 
71  if (failed(
72  applyPartialConversion(getOperation(), target, std::move(patterns))))
73  signalPassFailure();
74 }
75 
76 std::unique_ptr<Pass> circt::seq::createLowerSeqShiftRegPass() {
77  return std::make_unique<LowerSeqShiftRegPass>();
78 }
std::unique_ptr< mlir::Pass > createLowerSeqShiftRegPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition: DebugAnalysis.h:21
Definition: seq.py:1