Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Seq] Add clock gate operation #5082

Merged
merged 1 commit into from
Apr 26, 2023
Merged

[Seq] Add clock gate operation #5082

merged 1 commit into from
Apr 26, 2023

Conversation

fabianschuiki
Copy link
Contributor

Add a seq.clock_gate operation that represents a safe, glitchless gating of a clock. It isn't totally clear to me which dialect this op best fits into, but given that it interacts with clocks and contains an internal storage element to avoid glitching, Seq seems like a good fit.

This commit merely adds the op, canonicalizations, and basic tests. It isn't used or lowered yet. I imagine that the LowerSeqToSV pass would be able to lower this clock gate either to an extmodule or to an inline simulation model later.

@fabianschuiki fabianschuiki added the Seq Involving the `seq` dialect label Apr 25, 2023
Copy link
Contributor

@teqdruid teqdruid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@@ -24,6 +24,29 @@ using namespace mlir;
using namespace circt;
using namespace seq;

/// Determine the integer value of a constant operand.
static std::optional<APInt> getConstantInt(Attribute operand) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like these are useful in general (and likely copied from combfolds). Would it make sense to put them in a shared utility header?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any such op in the comb folds yet. But we should definitely add such a header to reduce code duplication.

Add a `seq.clock_gate` operation that represents a safe, glitchless
gating of a clock. It isn't totally clear to me which dialect this op
best fits into, but given that it interacts with clocks and contains an
internal storage element to avoid glitching, Seq seems like a good fit.

This commit merely adds the op, canonicalizations, and basic tests. It
isn't used or lowered yet. I imagine that the `LowerSeqToSV` pass would
be able to lower this clock gate either to an extmodule or to an inline
simulation model later.
OpFoldResult ClockGateOp::fold(FoldAdaptor adaptor) {
// Forward the clock if one of the enables is always true.
if (isConstantOne(adaptor.getEnable()) ||
isConstantOne(adaptor.getTestEnable()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to check for existence of the enable first?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's done in isConstantOne. The adaptor will return null if the enable isn't there, and the function handles that case.

// CHECK-NEXT: %alwaysOff1 = hw.wire %false sym @alwaysOff1
// CHECK-NEXT: %alwaysOff2 = hw.wire %false sym @alwaysOff2
%1 = seq.clock_gate %clock, %false
%2 = seq.clock_gate %clock, %false, %false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using true for the enable would provide more coverage.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are separate always-on tests for the constant true case. The problem here is if I pass one of the enables as constant true, it will just make the clock gate fold away (the enables are ORed).

@mwachs5
Copy link
Contributor

mwachs5 commented Apr 26, 2023

Actual clock gate cells have DTF signals eg a test enable override. Does this op include space for that...? (Did not try to read the code but curious your thoughts on this)

@fabianschuiki
Copy link
Contributor Author

Actual clock gate cells have DTF signals eg a test enable override. Does this op include space for that...?

Yes! The op has an optional test_enable operand (the third one) which you can pass in if you need the override. During lowering we can either map that to a comb.or if your clock gate cell doesn't have a separate test enable, or we hook it up straight to the cell's pin if it does.

@fabianschuiki fabianschuiki merged commit 1dffc23 into main Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Seq Involving the `seq` dialect
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants