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