1 // This file describes the second of three pattern matcher setups that
2 // forms the `xilinx_dsp` pass described in xilinx_dsp.cc
3 // At a high level, it works as follows:
4 // (1) Starting from a DSP48* cell that (a) doesn't have a CREG already,
5 // and (b) uses the 'C' port
6 // (2) Match the driver of the 'C' input to a possible $dff cell (CREG)
7 // (attached to at most two $mux cells that implement clock-enable or
8 // reset functionality, using a subpattern discussed below)
10 // - Running CREG packing after xilinx_dsp_pack is necessary since there is no
11 // guarantee that the cell ordering corresponds to the "expected" case (i.e.
12 // the order in which they appear in the source) thus the possiblity existed
13 // that a register got packed as a CREG into a downstream DSP that should
14 // have otherwise been a PREG of an upstream DSP that had not been visited
16 // - The reason this is separated out from the xilinx_dsp.pmg file is
17 // for efficiency --- each *.pmg file creates a class of the same basename,
18 // which when constructed, creates a custom database tailored to the
19 // pattern(s) contained within. Since the pattern in this file must be
20 // executed after the pattern contained in xilinx_dsp.pmg, it is necessary
21 // to reconstruct this database. Separating the two patterns into
22 // independent files causes two smaller, more specific, databases.
24 pattern xilinx_dsp_packC
26 udata <std::function<SigSpec(const SigSpec&)>> unextend
28 state <SigSpec> sigC sigP
29 state <bool> ffCcepol ffCrstpol
30 state <Cell*> ffC ffCcemux ffCrstmux
32 // Variables used for subpatterns
33 state <SigSpec> argQ argD
34 state <bool> ffcepol ffrstpol
36 udata <SigSpec> dffD dffQ
37 udata <SigBit> dffclock
38 udata <Cell*> dff dffcemux dffrstmux
39 udata <bool> dffcepol dffrstpol
41 // (1) Starting from a DSP48* cell that (a) doesn't have a CREG already,
42 // and (b) uses the 'C' port
44 select dsp->type.in(\DSP48A, \DSP48A1, \DSP48E1)
45 select param(dsp, \CREG, 1).as_int() == 0
46 select nusers(port(dsp, \C, SigSpec())) > 1
50 unextend = [](const SigSpec &sig) {
52 for (i = GetSize(sig)-1; i > 0; i--)
53 if (sig[i] != sig[i-1])
55 // Do not remove non-const sign bit
58 return sig.extract(0, i);
60 sigC = unextend(port(dsp, \C, SigSpec()));
62 SigSpec P = port(dsp, \P);
63 if (!dsp->type.in(\DSP48E1) ||
64 param(dsp, \USE_MULT, Const("MULTIPLY")).decode_string() == "MULTIPLY") {
65 // Only care about those bits that are used
67 for (i = GetSize(P)-1; i >= 0; i--)
71 log_assert(nusers(P.extract_end(i)) <= 1);
72 sigP = P.extract(0, i);
77 clock = port(dsp, \CLK, SigBit());
80 // (2) Match the driver of the 'C' input to a possible $dff cell (CREG)
81 // (attached to at most two $mux cells that implement clock-enable or
82 // reset functionality, using the in_dffe subpattern)
83 code argQ ffC ffCcemux ffCrstmux ffCcepol ffCrstpol sigC clock
90 ffCrstmux = dffrstmux;
91 ffCrstpol = dffrstpol;
106 // #######################
108 // Subpattern for matching against input registers, based on knowledge of the
109 // 'Q' input. Typically, identifying registers with clock-enable and reset
110 // capability would be a task would be handled by other Yosys passes such as
111 // dff2dffe, but since DSP inference happens much before this, these patterns
112 // have to be manually identified.
114 // (1) Starting from a $dff cell that (partially or fully) drives the given
116 // (2) Match for a $mux cell implementing synchronous reset semantics ---
117 // one that exclusively drives the 'D' input of the $dff, with one of its
118 // $mux inputs being fully zero
119 // (3) Match for a $mux cell implement clock enable semantics --- one that
120 // exclusively drives the 'D' input of the $dff (or the other input of
121 // the reset $mux) and where one of this $mux's inputs is connected to
122 // the 'Q' output of the $dff
128 for (const auto &c : argQ.chunks()) {
129 // Abandon matches when 'Q' is a constant
132 // Abandon matches when 'Q' has the keep attribute set
133 if (c.wire->get_bool_attribute(\keep))
135 // Abandon matches when 'Q' has a non-zero init attribute set
136 // (not supported by DSP48E1)
137 Const init = c.wire->attributes.at(\init, Const());
138 for (auto b : init.extract(c.offset, c.width))
139 if (b != State::Sx && b != State::S0)
144 // (1) Starting from a $dff cell that (partially or fully) drives the given
147 select ff->type.in($dff)
148 // DSP48E1 does not support clock inversion
149 select param(ff, \CLK_POLARITY).as_bool()
151 slice offset GetSize(port(ff, \D))
152 index <SigBit> port(ff, \Q)[offset] === argQ[0]
154 // Check that the rest of argQ is present
155 filter GetSize(port(ff, \Q)) >= offset + GetSize(argQ)
156 filter port(ff, \Q).extract(offset, GetSize(argQ)) == argQ
158 filter clock == SigBit() || port(ff, \CLK) == clock
164 SigSpec Q = port(ff, \Q);
166 dffclock = port(ff, \CLK);
170 dffD.replace(argQ, argD);
171 // Only search for ffrstmux if dffD only
172 // has two (ff, ffrstmux) users
173 if (nusers(dffD) > 2)
177 // (2) Match for a $mux cell implementing synchronous reset semantics ---
178 // exclusively drives the 'D' input of the $dff, with one of the $mux
179 // inputs being fully zero
182 select ffrstmux->type.in($mux)
183 index <SigSpec> port(ffrstmux, \Y) === argD
185 choice <IdString> BA {\B, \A}
186 // DSP48E1 only supports reset to zero
187 select port(ffrstmux, BA).is_fully_zero()
189 define <bool> pol (BA == \B)
196 dffrstmux = ffrstmux;
197 dffrstpol = ffrstpol;
198 argD = port(ffrstmux, ffrstpol ? \A : \B);
199 dffD.replace(port(ffrstmux, \Y), argD);
201 // Only search for ffcemux if argQ has at
202 // least 3 users (ff, <upstream>, ffrstmux) and
203 // dffD only has two (ff, ffrstmux)
204 if (!(nusers(argQ) >= 3 && nusers(dffD) == 2))
211 // (3) Match for a $mux cell implement clock enable semantics --- one that
212 // exclusively drives the 'D' input of the $dff (or the other input of
213 // the reset $mux) and where one of this $mux's inputs is connected to
214 // the 'Q' output of the $dff
217 select ffcemux->type.in($mux)
218 index <SigSpec> port(ffcemux, \Y) === argD
219 choice <IdString> AB {\A, \B}
220 index <SigSpec> port(ffcemux, AB) === argQ
221 define <bool> pol (AB == \A)
230 argD = port(ffcemux, ffcepol ? \B : \A);
231 dffD.replace(port(ffcemux, \Y), argD);