Merge pull request #1637 from YosysHQ/mwk/fix-1634
[yosys.git] / passes / pmgen / xilinx_dsp_CREG.pmg
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)
9 // Notes:
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
15 // yet
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.
23
24 pattern xilinx_dsp_packC
25
26 udata <std::function<SigSpec(const SigSpec&)>> unextend
27 state <SigBit> clock
28 state <SigSpec> sigC sigP
29 state <bool> ffCcepol ffCrstpol
30 state <Cell*> ffC ffCcemux ffCrstmux
31
32 // Variables used for subpatterns
33 state <SigSpec> argQ argD
34 state <bool> ffcepol ffrstpol
35 state <int> ffoffset
36 udata <SigSpec> dffD dffQ
37 udata <SigBit> dffclock
38 udata <Cell*> dff dffcemux dffrstmux
39 udata <bool> dffcepol dffrstpol
40
41 // (1) Starting from a DSP48* cell that (a) doesn't have a CREG already,
42 // and (b) uses the 'C' port
43 match dsp
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
47 endmatch
48
49 code sigC sigP clock
50 unextend = [](const SigSpec &sig) {
51 int i;
52 for (i = GetSize(sig)-1; i > 0; i--)
53 if (sig[i] != sig[i-1])
54 break;
55 // Do not remove non-const sign bit
56 if (sig[i].wire)
57 ++i;
58 return sig.extract(0, i);
59 };
60 sigC = unextend(port(dsp, \C, SigSpec()));
61
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
66 int i;
67 for (i = GetSize(P)-1; i >= 0; i--)
68 if (nusers(P[i]) > 1)
69 break;
70 i++;
71 log_assert(nusers(P.extract_end(i)) <= 1);
72 sigP = P.extract(0, i);
73 }
74 else
75 sigP = P;
76
77 clock = port(dsp, \CLK, SigBit());
78 endcode
79
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
84 argQ = sigC;
85 subpattern(in_dffe);
86 if (dff) {
87 ffC = dff;
88 clock = dffclock;
89 if (dffrstmux) {
90 ffCrstmux = dffrstmux;
91 ffCrstpol = dffrstpol;
92 }
93 if (dffcemux) {
94 ffCcemux = dffcemux;
95 ffCcepol = dffcepol;
96 }
97 sigC = dffD;
98 }
99 endcode
100
101 code
102 if (ffC)
103 accept;
104 endcode
105
106 // #######################
107
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.
113 // At a high level:
114 // (1) Starting from a $dff cell that (partially or fully) drives the given
115 // 'Q' argument
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
123 subpattern in_dffe
124 arg argD argQ clock
125
126 code
127 dff = nullptr;
128 for (const auto &c : argQ.chunks()) {
129 // Abandon matches when 'Q' is a constant
130 if (!c.wire)
131 reject;
132 // Abandon matches when 'Q' has the keep attribute set
133 if (c.wire->get_bool_attribute(\keep))
134 reject;
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)
140 reject;
141 }
142 endcode
143
144 // (1) Starting from a $dff cell that (partially or fully) drives the given
145 // 'Q' argument
146 match ff
147 select ff->type.in($dff)
148 // DSP48E1 does not support clock inversion
149 select param(ff, \CLK_POLARITY).as_bool()
150
151 slice offset GetSize(port(ff, \D))
152 index <SigBit> port(ff, \Q)[offset] === argQ[0]
153
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
157
158 filter clock == SigBit() || port(ff, \CLK) == clock
159
160 set ffoffset offset
161 endmatch
162
163 code argQ argD
164 SigSpec Q = port(ff, \Q);
165 dff = ff;
166 dffclock = port(ff, \CLK);
167 dffD = argQ;
168 argD = port(ff, \D);
169 argQ = Q;
170 dffD.replace(argQ, argD);
171 // Only search for ffrstmux if dffD only
172 // has two (ff, ffrstmux) users
173 if (nusers(dffD) > 2)
174 argD = SigSpec();
175 endcode
176
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
180 match ffrstmux
181 if !argD.empty()
182 select ffrstmux->type.in($mux)
183 index <SigSpec> port(ffrstmux, \Y) === argD
184
185 choice <IdString> BA {\B, \A}
186 // DSP48E1 only supports reset to zero
187 select port(ffrstmux, BA).is_fully_zero()
188
189 define <bool> pol (BA == \B)
190 set ffrstpol pol
191 semioptional
192 endmatch
193
194 code argD
195 if (ffrstmux) {
196 dffrstmux = ffrstmux;
197 dffrstpol = ffrstpol;
198 argD = port(ffrstmux, ffrstpol ? \A : \B);
199 dffD.replace(port(ffrstmux, \Y), argD);
200
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))
205 argD = SigSpec();
206 }
207 else
208 dffrstmux = nullptr;
209 endcode
210
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
215 match ffcemux
216 if !argD.empty()
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)
222 set ffcepol pol
223 semioptional
224 endmatch
225
226 code argD
227 if (ffcemux) {
228 dffcemux = ffcemux;
229 dffcepol = ffcepol;
230 argD = port(ffcemux, ffcepol ? \B : \A);
231 dffD.replace(port(ffcemux, \Y), argD);
232 }
233 else
234 dffcemux = nullptr;
235 endcode