Repository navigation
Simplify KnownDomain - #3163
Simplify KnownDomain#3163kleinreact wants to merge 27 commits into
KnownDomain#3163Conversation
|
Looking at the failing tests, it seems the existing encoding is ingrained in the Clash compiler to figure out things about domain configurations during HDL generation:
|
|
I would like to do this. In the past I tried something like this as well but I never got it working without breaking type inference on existing code. If you want @kleinreact I can take up this PR and see if I can get it to pass all existing tests. |
|
Thanks for the offer @rowanG077. I am currently evaluating some more improvements that should be applicable here, where I'd prefer to ship them all in a single PR in the end, as this is a quite sensible part of the prelude. I hope to be able to finish my evaluation soon. |
425a142 to
ed077ab
Compare
You can find the results of my evaluation for further discussion in #3178. |
ed077ab to
64ca22f
Compare
64ca22f to
ed077ab
Compare
ed077ab to
d3fa440
Compare
|
The PR now includes a full implementation of the changes proposed in #3178, except for the clock periods, which I solved sightly differently than originally proposed. A few tests are still failing, but I could neither find any good solution for fixing them yet nor do I understand the origin of their failure. Nevertheless, I'd like to focus and discuss the particular design decisions made, basically sharing this as a proposal with the goal to eventually reach some consensus of what we want. My proposal is to continue any technical discussions here in the PR and keep general design considerations for #3178. |
f1303da to
11e94e6
Compare
|
Thanks for all the fixes @marijn-qbaylogic and @rowanG077. I tested this with the following example, for which this PR unfortunately still errors. I haven't reported the error before, because I was hoping that your fixes may fix this one as well. Reproducerimport Clash.Prelude
import Data.Typeable (Typeable)
class Typeable sdom => SecurityDomain sdom where
data (@!) :: Domain -> sdom -> Domain
type (lblA :: sdom) <! (lblB :: sdom) :: Bool
type SecurityLabel (lbl :: sdom) =
(Typeable lbl, SecurityDomain sdom)
type family SwitchFromSecDom dom0 lblA dom1 where
SwitchFromSecDom dom lblA (dom @! lblB) = lblA <! lblB
SwitchFromSecDom _ _ _ = False
type instance (dom0 @! lbl) `SupportsSwitchingTo` dom1
= SwitchFromSecDom dom0 lbl dom1
instance (SecurityLabel lbl, KnownDomain dom) => KnownDomain (dom @! lbl) where
type DomainPeriod (dom @! lbl) = DomainPeriod dom
type DomainPeriodFraction (dom @! lbl) = DomainPeriodFraction dom
type DomainActiveEdge (dom @! lbl) = DomainActiveEdge dom
type DomainResetKind (dom @! lbl) = DomainResetKind dom
type DomainInitBehavior (dom @! lbl) = DomainInitBehavior dom
type DomainResetPolarity (dom @! lbl) = DomainResetPolarity dom
data SecDom = Public | Confidential
instance SecurityDomain SecDom where
data _ @! _
type Public <! _ = True
type Confidential <! Confidential = True
type Confidential <! Public = False
topEntity ::
HiddenClockResetEnable (System @! Public) =>
Signal (System @! Confidential) Bool
topEntity = pure Trueerrors with |
|
Just to be clear: these type families reduce in the REPL, e.g., A similar reduction should be enforced through introduced as part of these changes, but for some reason it does not reduce to the requested |
11e94e6 to
89199f7
Compare
9f87a15 to
1d2d771
Compare
|
Love the work overall! I had a question about the new DSignal implementation that I raised in #3558 but figured I'd re-raise it here because it's the more relevant thread. It seems with this code, the new DSignal implementation (a Signal with a delayed dom) will typecheck when passed through Clash's If the above is true, it seems like a massive footgun for users. If the above is not true, I'd love to be educated. |
Great to hear that. Thanks.
@enathang This is the development PR with a focus on code changes. Hence, please use the corresponding GitHub discussion #3178 for questions or suggestions of the particular interface changes instead. |
|
|
||
| # Major Domain overhaul | ||
| Clash's `Domain`-system has had a major overhaul. The most important changes are: | ||
| - Synthesis domains are empty data types. Their type parameters can distinguish logical domains sharing a clock. |
There was a problem hiding this comment.
The empty is misleading here, they are just types.
| string refers to a single /synthesis domain/. A synthesis domain describes the | ||
| behavior of certain aspects of memory elements in it. More specifically, a | ||
| domain looks like: | ||
| For synthesis, /dom/ is a data type with no constructors. It may have type |
There was a problem hiding this comment.
| For synthesis, /dom/ is a data type with no constructors. It may have type | |
| For synthesis, /dom/ can be any concrete type without any type |
The scope of supported types is more than just empty data types.
| type ClockResetPolarity cdom :: ResetPolarity | ||
|
|
||
| deriving instance Show (SDomainConfiguration dom conf) | ||
| -- | For synthesis, declare a domain as a data type with no constructors. |
There was a problem hiding this comment.
| -- | For synthesis, declare a domain as a data type with no constructors. | |
| -- | For synthesis, declare a domain as a data type. |
|
|
||
| deriving instance Show (SDomainConfiguration dom conf) | ||
| -- | For synthesis, declare a domain as a data type with no constructors. | ||
| -- It may have type parameters, as in the @Secure@ example below. |
There was a problem hiding this comment.
I find this formulation misleading, as a type with parameters won't have kind Type, but we require domains to be a Type, e.g., Maybe True :: Type is a valid domain, while Maybe :: Type -> Type is not.
| string refers to a single /synthesis domain/. A synthesis domain describes the | ||
| behavior of certain aspects of memory elements in it. More specifically, a | ||
| domain looks like: | ||
| For synthesis, /dom/ is a data type with no constructors. It may have type |
There was a problem hiding this comment.
| For synthesis, /dom/ is a data type with no constructors. It may have type | |
| For synthesis, /dom/ can be any concrete type without any type |
I disagree with that on the basis that we introduce these new domain things and we have an existing thing that fits this like a glove. Why would we not dogfood this immediately? |
|
christiaanb
left a comment
There was a problem hiding this comment.
This is a review up to and including d67d4065ec5d37035a65fc4c984ad1a1c522bb3b "Carry the clock environment through profiling snapshots"
| -- and if so, the applied type, and the type in the substitution should match. | ||
| Just ty' | ty' `aeqType` ty -> Just s | ||
| Just ty' | ty' `aeqType` ty | ||
| || familyType ty' `aeqType` familyType ty -> Just s |
There was a problem hiding this comment.
This requires a comment. When can we have ty' == ty but not `familyType ty' == familyType ty', and why shouldn't it be a match?
| -- Repeated variables need equality rather than another pattern match: | ||
| -- neither occurrence may introduce new substitutions. Look through family | ||
| -- representations recursively, including nested family arguments. | ||
| familyType ty | ||
| | Just ty' <- dataFamilyView tcm ty = familyType ty' | ||
| familyType (AppTy f a) = AppTy (familyType f) (familyType a) | ||
| familyType (ForAllTy v ty) = ForAllTy v (familyType ty) | ||
| familyType (AnnType attrs ty) = AnnType attrs (familyType ty) | ||
| familyType ty = ty | ||
|
|
There was a problem hiding this comment.
I don't understand how the comment and the code go together.
| -- Keep a newtype-family representation until a representation view | ||
| -- is requested. Erasing it to its payload would lose the family | ||
| -- identity needed for later matching; ordinary data-family | ||
| -- representations can follow the normal normalization path. |
There was a problem hiding this comment.
What is a "representation view", do you mean tyView? Also what later matching where? inside of this function? Explain.
| -- Keep the relationship to the source family even when GHC has | ||
| -- lowered applications to this representation constructor. Use the | ||
| -- structural converter at the family head so this mapping does not | ||
| -- immediately reduce back to the representation (#3548). |
There was a problem hiding this comment.
What reduces it back to the representation? Something in the GHC Core to Clash Core conversion? Also what structural converter? you mean all the functions in this module that take GHC Core to Clash Core?
| -- Also support higher-kinded uses of the representation constructor: | ||
| -- R:TaggedBool is already equivalent to Tagged @Bool, before a label | ||
| -- is supplied. Structured or repeated indices must retain their binders. |
There was a problem hiding this comment.
What does this have to do with the eta-reduction implementation? I understand it might have something to do with why you want to do eta-reduction, but then this comment belongs elsewhere.
| ) | ||
| , KnownConfiguration slow | ||
| ( 'DomainConfiguration slow | ||
| (2*fPeriod) ((2*fNumer) :% fDenom) |
There was a problem hiding this comment.
I guess we dropped the earlier decision that the fraction should be smaller than 1?
| , testCase "read rejects zero clock period denominators" $ | ||
| mapM_ (\s -> readMaybe @E.Period s @?= Nothing) | ||
| ["1 % 0", "1/0 ps", "2 + 1/0 ps"] | ||
| , testCase "clock fractions need not be in lowest terms" $ do |
There was a problem hiding this comment.
What do you mean by "lowest terms"?
| ClockPeriodFraction (ClockDomainOf dom) | ||
|
|
||
| -- Express the complete period as a ratio without requiring the fraction in | ||
| -- a user-defined clock configuration to be in lowest terms. |
There was a problem hiding this comment.
What is meant by "lowest terms"?
| type family ScaledPeriod (whole :: Nat) (fraction :: Ratio Nat) :: Nat where | ||
| ScaledPeriod whole (n :% d) = whole * d + n | ||
|
|
||
| -- | The period of @slow@ is exactly @n@ times the period of @fast@. |
There was a problem hiding this comment.
I would like a comment that shows the steps of the math/equations.
| go (i, dom) = | ||
| -- VDomainConfiguration stores period in ps, SDC expects it in ns. | ||
| let p = MkFixed (toInteger $ vPeriod dom) :: Fixed E3 | ||
| let p = MkFixed (floor (1000 * vPeriod dom)) :: Fixed E6 |
There was a problem hiding this comment.
Meh, this seems rather arbitrary. We should just emit a comment what the actual period is, and that the SDC period could be truncated.
Removing the domain tag from Clock also removes the field that kept the testbench enable and forwarded DDR waveform lazy in simulation. Keep both arguments available to their HDL blackboxes without forcing them when reading clock periods. A synchronized enable can depend on the very clock the simulator is stepping. Cover this feedback for static, dynamic, and DDR-forwarded clocks.
Co-authored-by: Christiaan Baaij <christiaan@qbaylogic.com>
4815566 to
3c451b5
Compare
Represent the clock period in whole picoseconds. Use that value directly in clock simulation, configuration lookup, manifest JSON, and blackbox period substitution. Remove the fractional-period proof and its supporting API.
Seed GHC-to-Core translation with the configuration families even when the design has no direct reference to them. Pass the same environment to the GHC 9.14 interpreter. Update the rewrite test translator and its binder-printing fixtures for this environment. Co-authored-by: Marijn <marijn@qbaylogic.com>
Instantiate the dynamic clock testbench with its domain types. Use the same notation in the signal, reset, and clock-generator examples. Co-authored-by: Marijn <marijn@qbaylogic.com>
Leave normalized let bindings in place when inlining work-free expressions. Their local bindings can still be shared by the caller. Co-authored-by: Marijn <marijn@qbaylogic.com>
Rename construction, singleton access, and configuration records together with their callers. This reserves domain terminology for logical signal identities.
Distinguish data families from reducible type families and retain newtype identities and function multiplicities across specialization. Provide nominal family matching for the evaluator and clock lookup.
Keep permission checks in the public functions. Give signal, clock, reset, and enable switches separate primitives so each passes its input through in synthesis.
Introduce ClockDomainOf and KnownClockDomain in the prelude and compiler together. Resolve clock properties at the physical marker, require that marker to project to itself, and check bare ports. Exercise permitted switches between logical partitions and reject invalid physical projections. Co-authored-by: Marijn <marijn@qbaylogic.com>
Reduce superclass selectors before specializing and discard evidence once the clock properties are known. Keep observable TypeRep fingerprints out of the generated hardware. Check the selected edge and period on a register in a logical domain.
Reduce equal markers to a proof and distinct concrete markers to Nothing. Leave unresolved indices for specialization and report an error if equality still cannot be decided at netlisting. Cover aliases, newtypes, data families, and function indices. Co-authored-by: Marijn <marijn@qbaylogic.com>
Give ClockN a nominal role and keep its wire representation during normalization, port splitting, and zero-width specialization. Test the generated negative clock input and reject coercions across domains.
Carry the backend’s domain map through HDL generation and serialize it in the manifest. Include configurations found only on signal and bidirectional ports, while allowing unconfigured data signals to pass through. Exercise both the generated manifest and its JSON round trip. Co-authored-by: Marijn <marijn@qbaylogic.com>
Render qualified clock names and nominal type and kind arguments consistently in the prelude and compiler. Compare runtime names to manifest entries for indexed clocks and logical domains that share a clock.
Force the compiler environment before timing and pass the domain-aware translator to the actual benchmark workloads.
Serialize the configuration family identifiers with the prepared environment, then restore them when replaying normalization or netlist generation.
Explain how to declare a physical clock, define logical partitions, and migrate string domains. Document the clock identifiers exposed by manifests and blackboxes. Co-authored-by: Marijn <marijn@qbaylogic.com>
3c451b5 to
b615c6a
Compare
The PR implements the suggestions of #3178, except for the clock periods, where it instead keeps the exiting integer period for backward compatibility, while just adding the period fraction that may result from conversion imprecision. This fraction then can be used to recover the precise frequency, if needed.
Still TODO:
Approved for public release. This work has been supported by funding from the Agentur für Innovation in der Cybersicherheit GmbH (Cyberagentur) as part of the Clash Formal project.