CIRCT 23.0.0git
Loading...
Searching...
No Matches
LowerSeqCompRegCE.cpp
Go to the documentation of this file.
1//===- LowerSeqCompRegCE.cpp - seq.compreg.ce 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
13#include "mlir/Pass/Pass.h"
14#include "mlir/Transforms/DialectConversion.h"
15#include "llvm/ADT/TypeSwitch.h"
16
17namespace circt {
18namespace seq {
19#define GEN_PASS_DEF_LOWERSEQCOMPREGCE
20#include "circt/Dialect/Seq/SeqPasses.h.inc"
21} // namespace seq
22} // namespace circt
23
24using namespace circt;
25using namespace seq;
26
27namespace {
28
29/// Lowers seq.compreg.ce to a seq.compreg with the clock enable signal
30/// built into the next logic, i.e. `next := mux(clock_enable, next, current)`
31struct CompRegCELowering
32 : public OpConversionPattern<seq::CompRegClockEnabledOp> {
33public:
34 using OpConversionPattern::OpConversionPattern;
35
36 LogicalResult
37 matchAndRewrite(seq::CompRegClockEnabledOp op, OpAdaptor adaptor,
38 ConversionPatternRewriter &rewriter) const final {
39 // compreg.ce and compreg have the same inputs of the same types
40 // other than the additional clockEnable, so not much conversion
41 // is needed, we just incorporate the clock enable into the input
42 auto mux =
43 comb::MuxOp::create(rewriter, op.getLoc(), adaptor.getClockEnable(),
44 adaptor.getInput(), op.getResult());
45
46 // Create the new compreg to replace the compreg.ce
47 auto compreg = seq::CompRegOp::create(
48 rewriter, op.getLoc(), mux, adaptor.getClk(), op.getNameAttr(),
49 adaptor.getReset(), adaptor.getResetValue(), adaptor.getInitialValue(),
50 op.getInnerSymAttr());
51
52 rewriter.replaceOp(op, compreg);
53 return success();
54 }
55};
56
57struct LowerSeqCompRegCEPass
58 : public circt::seq::impl::LowerSeqCompRegCEBase<LowerSeqCompRegCEPass> {
59 void runOnOperation() override;
60};
61
62} // namespace
63
64void LowerSeqCompRegCEPass::runOnOperation() {
65 MLIRContext &ctxt = getContext();
66 ConversionTarget target(ctxt);
67
68 // In theory all compreg.ce ops should be gone after this
69 target.addIllegalOp<seq::CompRegClockEnabledOp>();
70 target.addLegalDialect<seq::SeqDialect, hw::HWDialect, comb::CombDialect>();
71 RewritePatternSet patterns(&ctxt);
72 patterns.add<CompRegCELowering>(&ctxt);
73
74 if (failed(
75 applyPartialConversion(getOperation(), target, std::move(patterns))))
76 signalPassFailure();
77}
78
80 return std::make_unique<LowerSeqCompRegCEPass>();
81}
create(cls, result_type, reset=None, reset_value=None, name=None, sym_name=None, **kwargs)
Definition seq.py:157
std::unique_ptr< mlir::Pass > createLowerSeqCompRegCEPass()
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Definition seq.py:1