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
31 // Variables used for subpatterns
32 state <SigSpec> argQ argD
34 udata <SigSpec> dffD dffQ
35 udata <SigBit> dffclock
38 // (1) Starting from a DSP48* cell that (a) doesn't have a CREG already,
39 // and (b) uses the 'C' port
41 select dsp->type.in(\DSP48A, \DSP48A1, \DSP48E1)
42 select param(dsp, \CREG).as_int() == 0
43 select nusers(port(dsp, \C, SigSpec())) > 1
47 unextend = [](const SigSpec &sig) {
49 for (i = GetSize(sig)-1; i > 0; i--)
50 if (sig[i] != sig[i-1])
52 // Do not remove non-const sign bit
55 return sig.extract(0, i);
57 sigC = unextend(port(dsp, \C, SigSpec()));
59 SigSpec P = port(dsp, \P);
60 if (!dsp->type.in(\DSP48E1) ||
61 param(dsp, \USE_MULT).decode_string() == "MULTIPLY") {
62 // Only care about those bits that are used
64 for (i = GetSize(P)-1; i >= 0; i--)
68 log_assert(nusers(P.extract_end(i)) <= 1);
69 sigP = P.extract(0, i);
74 clock = port(dsp, \CLK, SigBit());
77 // (2) Match the driver of the 'C' input to a possible $dff cell (CREG)
78 // (attached to at most two $mux cells that implement clock-enable or
79 // reset functionality, using the in_dffe subpattern)
80 code argQ ffC sigC clock
95 // #######################
97 // Subpattern for matching against input registers, based on knowledge of the
106 for (const auto &c : argQ.chunks()) {
107 // Abandon matches when 'Q' is a constant
110 // Abandon matches when 'Q' has the keep attribute set
111 if (c.wire->get_bool_attribute(\keep))
113 // Abandon matches when 'Q' has a non-zero init attribute set
114 // (not supported by DSP48E1)
115 Const init = c.wire->attributes.at(\init, Const());
117 for (auto b : init.extract(c.offset, c.width))
118 if (b != State::Sx && b != State::S0)
124 select ff->type.in($dff, $dffe, $sdff, $sdffe)
125 // DSP48E1 does not support clock inversion
126 select param(ff, \CLK_POLARITY).as_bool()
128 // Check that reset value, if present, is fully 0.
129 filter ff->type.in($dff, $dffe) || param(ff, \SRST_VALUE).is_fully_zero()
131 slice offset GetSize(port(ff, \D))
132 index <SigBit> port(ff, \Q)[offset] === argQ[0]
134 // Check that the rest of argQ is present
135 filter GetSize(port(ff, \Q)) >= offset + GetSize(argQ)
136 filter port(ff, \Q).extract(offset, GetSize(argQ)) == argQ
138 filter clock == SigBit() || port(ff, \CLK) == clock
142 SigSpec Q = port(ff, \Q);
144 dffclock = port(ff, \CLK);
146 SigSpec D = port(ff, \D);
148 dffD.replace(argQ, D);