-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a shift register operation to the seq dialect. The operation has an interface similar to a `seq.compreg.ce` operation, with an additional `size` to specify the number of stages. Included is a default lowering to `seq.compreg.ce` operations. The main intention of introducing this operation is to be able to emit shift registers using target-specific resources. I don't see a reason to add a non-clock-enabled shift reg operation in addition to this. If users need a non-clock-enabled shift register, it should suffice to emit a shift register with constant 1 clock-enable, whereafter (if lowered to e.g. `seq.compreg.ce`), the lowered primitive should have a canonicalization that considers the constant clock enable signal.
- Loading branch information
Showing
8 changed files
with
189 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===- LowerSeqShiftReg.cpp - seq.shiftreg lowering -----------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "PassDetails.h" | ||
#include "circt/Dialect/Comb/CombOps.h" | ||
#include "circt/Dialect/SV/SVOps.h" | ||
#include "circt/Dialect/Seq/SeqOps.h" | ||
#include "circt/Dialect/Seq/SeqPasses.h" | ||
#include "circt/Support/BackedgeBuilder.h" | ||
#include "mlir/Transforms/DialectConversion.h" | ||
#include "llvm/ADT/TypeSwitch.h" | ||
|
||
using namespace circt; | ||
using namespace seq; | ||
|
||
namespace { | ||
|
||
struct ShiftRegLowering : public OpConversionPattern<seq::ShiftRegOp> { | ||
public: | ||
using OpConversionPattern::OpConversionPattern; | ||
|
||
LogicalResult | ||
matchAndRewrite(seq::ShiftRegOp op, OpAdaptor adaptor, | ||
ConversionPatternRewriter &rewriter) const final { | ||
Value in = adaptor.getInput(); | ||
auto baseName = op.getName(); | ||
for (size_t i = 0; i < op.getNumElements(); ++i) { | ||
StringAttr name; | ||
if (baseName.has_value()) | ||
name = rewriter.getStringAttr(baseName.value() + "_sh" + Twine(i + 1)); | ||
in = rewriter.create<seq::CompRegClockEnabledOp>( | ||
op.getLoc(), in, adaptor.getClk(), adaptor.getClockEnable(), | ||
adaptor.getReset(), adaptor.getResetValue(), name, | ||
op.getPowerOnValue()); | ||
} | ||
|
||
op.replaceAllUsesWith(in); | ||
rewriter.eraseOp(op); | ||
return success(); | ||
} | ||
}; | ||
|
||
#define GEN_PASS_DEF_LOWERSEQSHIFTREG | ||
#include "circt/Dialect/Seq/SeqPasses.h.inc" | ||
|
||
struct LowerSeqShiftRegPass | ||
: public impl::LowerSeqShiftRegBase<LowerSeqShiftRegPass> { | ||
void runOnOperation() override; | ||
}; | ||
|
||
} // namespace | ||
|
||
void LowerSeqShiftRegPass::runOnOperation() { | ||
MLIRContext &ctxt = getContext(); | ||
ConversionTarget target(ctxt); | ||
|
||
target.addIllegalOp<seq::ShiftRegOp>(); | ||
target.addLegalDialect<seq::SeqDialect>(); | ||
RewritePatternSet patterns(&ctxt); | ||
patterns.add<ShiftRegLowering>(&ctxt); | ||
|
||
if (failed( | ||
applyPartialConversion(getOperation(), target, std::move(patterns)))) | ||
signalPassFailure(); | ||
} | ||
|
||
std::unique_ptr<Pass> circt::seq::createLowerSeqShiftRegPass() { | ||
return std::make_unique<LowerSeqShiftRegPass>(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// RUN: circt-opt %s | circt-opt | FileCheck %s | ||
// RUN: circt-opt --lower-seq-shiftreg %s | FileCheck %s --check-prefix=LO | ||
|
||
// CHECK: %r0 = seq.shiftreg[3] %i, %clk, %ce : i32 | ||
// CHECK: %myShiftReg = seq.shiftreg[3] sym @myShiftReg %i, %clk, %ce reset %rst, %c0_i32 powerOn %c0_i32 : i32 | ||
|
||
// LO: %r0_sh1 = seq.compreg.ce sym @r0_sh1 %i, %clk, %ce : i32 | ||
// LO: %r0_sh2 = seq.compreg.ce sym @r0_sh2 %r0_sh1, %clk, %ce : i32 | ||
// LO: %r0_sh3 = seq.compreg.ce sym @r0_sh3 %r0_sh2, %clk, %ce : i32 | ||
// LO: %myShiftReg_sh1 = seq.compreg.ce sym @myShiftReg_sh1 %i, %clk, %ce reset %rst, %c0_i32 powerOn %c0_i32 : i32 | ||
// LO: %myShiftReg_sh2 = seq.compreg.ce sym @myShiftReg_sh2 %myShiftReg_sh1, %clk, %ce reset %rst, %c0_i32 powerOn %c0_i32 : i32 | ||
// LO: %myShiftReg_sh3 = seq.compreg.ce sym @myShiftReg_sh3 %myShiftReg_sh2, %clk, %ce reset %rst, %c0_i32 powerOn %c0_i32 : i32 | ||
// LO: hw.output %r0_sh3, %myShiftReg_sh3 : i32, i32 | ||
|
||
hw.module @top(in %clk: !seq.clock, in %rst: i1, in %ce: i1, in %i: i32, out out1 : i32, out out2 : i32) { | ||
%rv = hw.constant 0 : i32 | ||
%r0 = seq.shiftreg [3] %i, %clk, %ce : i32 | ||
%myShiftReg = seq.shiftreg [3] sym @myShiftReg %i, %clk, %ce reset %rst, %rv powerOn %rv : i32 | ||
hw.output %r0, %myShiftReg : i32, i32 | ||
} |