Skip to content

Commit 2312c9d

Browse files
committed
[LTL] Enhance delay operations to support clock and edge specifications
1 parent 6eb95c9 commit 2312c9d

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

include/circt/Dialect/LTL/LTLFolds.td

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ def valueTail : NativeCodeCall<"$0.drop_front()">;
2121
def concatValues : NativeCodeCall<
2222
"concatValues(ValueRange{$0}, ValueRange{$1})">;
2323

24+
// Ensure that outer and inner delays use the same clock and edge before
25+
// merging. Accepts (outerClock, innerClock, outerEdge, innerEdge).
26+
def SameClockAndEdge : Constraint<CPred<"$0 == $1 && $2 == $3">>;
27+
2428
//===----------------------------------------------------------------------===//
2529
// DelayOp
2630
//===----------------------------------------------------------------------===//
@@ -36,14 +40,15 @@ def mergedLengths : NativeCodeCall<[{
3640
: IntegerAttr{}
3741
}]>;
3842
def NestedDelays : Pat<
39-
(DelayOp (DelayOp $input, $delay1, $length1), $delay2, $length2),
43+
(DelayOp (DelayOp $input, $delay1, $length1, $clock1, $edge1), $delay2, $length2, $clock0, $edge0),
4044
(DelayOp $input, (mergedDelays $delay1, $delay2),
41-
(mergedLengths $length1, $length2))>;
45+
(mergedLengths $length1, $length2), $clock0, $edge0),
46+
[(SameClockAndEdge $clock0, $clock1, $edge0, $edge1)]>;
4247

4348
// delay(concat(s, ...), N, M) -> concat(delay(s, N, M), ...)
4449
def MoveDelayIntoConcat : Pat<
45-
(DelayOp (ConcatOp $inputs), $delay, $length),
46-
(ConcatOp (concatValues (DelayOp (valueHead $inputs), $delay, $length),
50+
(DelayOp (ConcatOp $inputs), $delay, $length, $clock, $edge),
51+
(ConcatOp (concatValues (DelayOp (valueHead $inputs), $delay, $length, $clock, $edge),
4752
(valueTail $inputs)))>;
4853

4954
//===----------------------------------------------------------------------===//

lib/Conversion/FIRRTLToHW/LowerToHW.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4717,7 +4717,8 @@ LogicalResult FIRRTLLowering::visitExpr(LTLIntersectIntrinsicOp op) {
47174717

47184718
LogicalResult FIRRTLLowering::visitExpr(LTLDelayIntrinsicOp op) {
47194719
return setLoweringToLTL<ltl::DelayOp>(op, getLoweredValue(op.getInput()),
4720-
op.getDelayAttr(), op.getLengthAttr());
4720+
op.getDelayAttr(), op.getLengthAttr(),
4721+
Value{}, ltl::ClockEdgeAttr{});
47214722
}
47224723

47234724
LogicalResult FIRRTLLowering::visitExpr(LTLConcatIntrinsicOp op) {

lib/Dialect/LTL/LTLFolds.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,35 @@ namespace patterns {
4646
#include "circt/Dialect/LTL/LTLFolds.cpp.inc"
4747
} // namespace patterns
4848

49+
namespace {
50+
struct MergeNestedDelays : OpRewritePattern<DelayOp> {
51+
using OpRewritePattern<DelayOp>::OpRewritePattern;
52+
53+
LogicalResult matchAndRewrite(DelayOp op,
54+
PatternRewriter &rewriter) const override {
55+
auto inner = op.getInput().getDefiningOp<DelayOp>();
56+
if (!inner)
57+
return failure();
58+
59+
if (op.getClock() != inner.getClock() || op.getEdge() != inner.getEdge())
60+
return failure();
61+
62+
IntegerAttr mergedDelay =
63+
rewriter.getI64IntegerAttr(inner.getDelay() + op.getDelay());
64+
IntegerAttr mergedLength;
65+
if (inner.getLength() && op.getLength())
66+
mergedLength =
67+
rewriter.getI64IntegerAttr(*inner.getLength() + *op.getLength());
68+
69+
auto newDelay =
70+
DelayOp::create(rewriter, op.getLoc(), inner.getInput(), mergedDelay,
71+
mergedLength, op.getClock(), op.getEdgeAttr());
72+
rewriter.replaceOp(op, newDelay.getResult());
73+
return success();
74+
}
75+
};
76+
} // namespace
77+
4978
//===----------------------------------------------------------------------===//
5079
// AndOp / OrOp / IntersectOp
5180
//===----------------------------------------------------------------------===//
@@ -90,7 +119,7 @@ OpFoldResult DelayOp::fold(FoldAdaptor adaptor) {
90119

91120
void DelayOp::getCanonicalizationPatterns(RewritePatternSet &results,
92121
MLIRContext *context) {
93-
results.add<patterns::NestedDelays>(results.getContext());
122+
results.add<MergeNestedDelays>(results.getContext());
94123
results.add<patterns::MoveDelayIntoConcat>(results.getContext());
95124
}
96125

0 commit comments

Comments
 (0)