1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
/************************************************************************
************************************************************************
FAUST compiler / Retiming transformation
Copyright (C) 2024-2024 INRIA
---------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
************************************************************************
************************************************************************/
#include <stdlib.h>
#include <cstdlib>
#include <sstream>
#include <vector>
#include "floats.hh"
#include "global.hh"
#include "ppsig.hh"
#include "prim2.hh"
#include "sigPromotion.hh"
#include "signals.hh"
#include "sigtyperules.hh"
#include "xtended.hh"
/**
* @brief Helper class that implements the Retiming transformation. It is used by sigRetiming to
* retime a list of signals. The transformation(lsig) method does the actual work.
*
*/
class SignalRetimer final : public SignalIdentity {
std::unordered_map<Tree, int> fTiming;
std::unordered_map<Tree, Tree> fRecVar;
public:
SignalRetimer() {}
protected:
Tree transformation(Tree sig);
int tmaxList(Tree l);
};
// --------------------------------------------------------------------------------------------
// API
// --------------------------------------------------------------------------------------------
/**
* @brief add registers to a list of signal to balance timing
*
* @param lsig a list of signals without registers
* @param trace optional trace flag
* @return Tree a list of signals with registers
*/
Tree sigRetiming(Tree lsig, bool trace)
{
SignalRetimer SR;
if (trace) {
SR.trace(true, "Retiming");
}
return SR.mapself(lsig);
}
// --------------------------------------------------------------------------------------------
// Implementation
// --------------------------------------------------------------------------------------------
/**
* @brief add n registers to a signal, group them if possible
*
* @param sig
* @param n number of registers to add
* @return Tree
*/
static Tree addRegisters(Tree sig, int n)
{
int i;
if (n == 0) {
// no registers to add
return sig;
}
if (Tree x; isSigRegister(sig, &i, x)) {
// we combine with existing registers
return addRegisters(x, i + n);
}
// simple case, we add n registers
return sigRegister(n, sig);
}
/**
* @brief max timing of a list of signals
*
* @param l
* @return int
*/
int SignalRetimer::tmaxList(Tree l)
{
int tmax = 0;
while (!isNil(l)) {
tmax = std::max(tmax, fTiming[hd(l)]);
l = tl(l);
}
return tmax;
}
/**
* @brief Add the needed registers to a signals and its subsignals
*
* @param sig a signal without registers
* @return Tree the transformed signal with registers
*/
Tree SignalRetimer::transformation(Tree sig)
{
int i;
int64_t i64;
double r;
Tree c, sel, w, x, y, z, var, le, label, ff, largs, type, name, file, sf;
if (getUserData(sig)) {
std::vector<Tree> nb1, nb2;
int tmax = 0;
for (Tree b : sig->branches()) {
Tree b2 = self(b);
tmax = std::max(tmax, fTiming[b2]);
nb1.push_back(self(b));
}
for (Tree c : nb1) {
nb2.push_back(addRegisters(c, tmax - fTiming[c]));
}
Tree res = sigRegister(1, tree(sig->node(), nb2));
fTiming[res] = tmax + 1;
return res;
} else if (isSigInt(sig, &i)) {
fTiming[sig] = 0;
return sig;
} else if (isSigInt64(sig, &i64)) {
fTiming[sig] = 0;
return sig;
} else if (isSigReal(sig, &r)) {
fTiming[sig] = 0;
return sig;
} else if (isSigWaveform(sig)) {
fTiming[sig] = 0;
return sig;
} else if (isSigInput(sig, &i)) {
fTiming[sig] = 0;
return sig;
} else if (isSigOutput(sig, &i, x)) {
Tree x2 = self(x);
Tree res = sigOutput(i, x2);
fTiming[res] = fTiming[x2]; // no register here
return res;
} else if (isSigDelay1(sig, x)) {
Tree v = self(x);
Tree res = sigRegister(1, sigDelay1(v));
fTiming[res] = fTiming[v] + 1;
return res;
} else if (isSigDelay(sig, x, y)) {
Tree v = self(x);
Tree w = self(y);
int tm = std::max(fTiming[v], fTiming[w]);
Tree res = sigRegister(
1, sigDelay(addRegisters(v, tm - fTiming[v]), addRegisters(w, tm - fTiming[w])));
fTiming[res] = tm + 1;
return res;
} else if (isSigPrefix(sig, x, y)) {
Tree x2 = self(x);
Tree y2 = self(y);
int tm = std::max(fTiming[x2], fTiming[y2]);
Tree res = sigRegister(
1, sigPrefix(addRegisters(x2, tm - fTiming[x2]), addRegisters(y2, tm - fTiming[y2])));
fTiming[res] = tm + 1;
return res;
} else if (isSigBinOp(sig, &i, x, y)) {
Tree x2 = self(x);
Tree y2 = self(y);
int tm = std::max(fTiming[x2], fTiming[y2]);
Tree res = sigRegister(
1, sigBinOp(i, addRegisters(x2, tm - fTiming[x2]), addRegisters(y2, tm - fTiming[y2])));
fTiming[res] = tm + 1;
return res;
}
// Foreign functions
else if (isSigFFun(sig, ff, largs)) {
Tree largs2 = mapself(largs);
int tmax = tmaxList(largs2);
Tree res = sigRegister(1, sigFFun(ff, mapself(largs)));
fTiming[res] = tmax + 1;
return res;
} else if (isSigFConst(sig, type, name, file)) {
fTiming[sig] = 0;
return sig;
} else if (isSigFVar(sig, type, name, file)) {
fTiming[sig] = 0;
return sig;
}
// Tables
else if (isSigWRTbl(sig, w, x, y, z)) {
if (y == gGlobal->nil) {
// rdtable
Tree w2 = self(w);
Tree x2 = self(x);
int tmax = std::max(fTiming[w2], fTiming[x2]);
Tree res = sigRegister(1, sigWRTbl(addRegisters(w2, tmax - fTiming[w2]),
addRegisters(x2, tmax - fTiming[x2])));
fTiming[res] = tmax + 1;
return res;
} else {
// rwtable
Tree w2 = self(w);
Tree x2 = self(x);
Tree y2 = self(y);
Tree z2 = self(z);
int tmax =
std::max(fTiming[w2], std::max(fTiming[x2], std::max(fTiming[y2], fTiming[z2])));
Tree res = sigRegister(1, sigWRTbl(addRegisters(w2, tmax - fTiming[w2]),
addRegisters(x2, tmax - fTiming[x2]),
addRegisters(y2, tmax - fTiming[y2]),
addRegisters(z2, tmax - fTiming[z2])));
fTiming[res] = tmax + 1;
return res;
}
} else if (isSigRDTbl(sig, x, y)) {
Tree x2 = self(x);
Tree y2 = self(y);
int tmax = std::max(fTiming[x2], fTiming[y2]);
Tree res = sigRegister(1, sigRDTbl(addRegisters(x2, tmax - fTiming[x2]),
addRegisters(y2, tmax - fTiming[y2])));
fTiming[res] = tmax + 1;
return res;
}
#if 0
// Doc
else if (isSigDocConstantTbl(sig, x, y)) {
return sigDocConstantTbl(self(x), self(y));
} else if (isSigDocWriteTbl(sig, x, y, u, v)) {
return sigDocWriteTbl(self(x), self(y), self(u), self(v));
} else if (isSigDocAccessTbl(sig, x, y)) {
return sigDocAccessTbl(self(x), self(y));
}
#endif
// Select2 and Select3
else if (isSigSelect2(sig, sel, x, y)) {
Tree sel2 = self(sel);
Tree x2 = self(x);
Tree y2 = self(y);
int tmax = std::max(fTiming[sel2], std::max(fTiming[x2], fTiming[y2]));
Tree res = sigRegister(1, sigSelect2(addRegisters(sel2, tmax - fTiming[sel2]),
addRegisters(x2, tmax - fTiming[x2]),
addRegisters(y2, tmax - fTiming[y2])));
fTiming[res] = tmax + 1;
return res;
}
// Table sigGen
else if (isSigGen(sig, x)) {
if (fVisitGen) {
Tree res = sigGen(self(x));
fTiming[res] = 0;
return res;
} else {
fTiming[sig] = 0;
return sig;
}
}
// recursive signals
else if (isProj(sig, &i, x)) {
Tree res = sigProj(i, self(x));
fTiming[res] = 0;
return res;
} else if (isRec(sig, var, le)) {
if (fRecVar.count(sig)) {
// we have/are already visiting this recursive group
Tree res = fRecVar[sig];
return res;
} else {
// first visit
Tree var2 = tree(Node(unique("RT"))); // New name for the transformed rec group
fRecVar[sig] = rec(var2, gGlobal->nil); // temporary result to avoid infinite loops
Tree le2 = mapself(le);
Tree res = rec(var2, le2);
fRecVar[sig] = res; // not needed, but for clarity
return res;
}
}
// Int and Float Cast
else if (isSigIntCast(sig, x)) {
Tree x2 = self(x);
Tree res = sigRegister(1, sigIntCast(x2));
fTiming[res] = fTiming[x2] + 1;
return res;
} else if (isSigBitCast(sig, x)) {
Tree x2 = self(x);
Tree res = sigRegister(1, sigBitCast(x2));
fTiming[res] = fTiming[x2] + 1;
return res;
} else if (isSigFloatCast(sig, x)) {
Tree x2 = self(x);
Tree res = sigRegister(1, sigFloatCast(x2));
fTiming[res] = fTiming[x2] + 1;
return res;
}
// UI
else if (isSigButton(sig, label)) {
fTiming[sig] = 0;
return sig;
} else if (isSigCheckbox(sig, label)) {
fTiming[sig] = 0;
return sig;
} else if (isSigVSlider(sig, label, c, x, y, z)) {
fTiming[sig] = 0;
return sig;
} else if (isSigHSlider(sig, label, c, x, y, z)) {
fTiming[sig] = 0;
return sig;
} else if (isSigNumEntry(sig, label, c, x, y, z)) {
fTiming[sig] = 0;
return sig;
} else if (isSigVBargraph(sig, label, x, y, z)) {
Tree z2 = self(z);
Tree res = sigRegister(1, sigVBargraph(label, x, y, z2));
fTiming[res] = fTiming[z2] + 1;
return res;
} else if (isSigHBargraph(sig, label, x, y, z)) {
Tree z2 = self(z);
Tree res = sigRegister(1, sigHBargraph(label, x, y, z2));
fTiming[res] = fTiming[z2] + 1;
return res;
}
// Soundfile length, rate, buffer
else if (isSigSoundfile(sig, label)) {
return sig;
} else if (isSigSoundfileLength(sig, sf, x)) {
return sigSoundfileLength(self(sf), self(x));
} else if (isSigSoundfileRate(sig, sf, x)) {
return sigSoundfileRate(self(sf), self(x));
} else if (isSigSoundfileBuffer(sig, sf, x, y, z)) {
return sigSoundfileBuffer(self(sf), self(x), self(y), self(z));
}
// Attach, Enable, Control
else if (isSigAttach(sig, x, y)) {
Tree x2 = self(x);
Tree y2 = self(y);
Tree res = sigRegister(1, sigAttach(x2, y2));
fTiming[res] = std::max(fTiming[x2], fTiming[y2]) + 1;
return res;
} else if (isSigEnable(sig, x, y)) {
Tree x2 = self(x);
Tree y2 = self(y);
Tree res = sigRegister(1, sigEnable(x2, y2));
fTiming[res] = std::max(fTiming[x2], fTiming[y2]) + 1;
return res;
} else if (isSigControl(sig, x, y)) {
Tree x2 = self(x);
Tree y2 = self(y);
Tree res = sigRegister(1, sigControl(x2, y2));
fTiming[res] = std::max(fTiming[x2], fTiming[y2]) + 1;
return res;
}
// Signal interval annotation
/*else if (isSigAssertBounds(sig, x, y, z)) {
return sigAssertBounds(self(x), self(y), self(z));
}*/
else if (isSigLowest(sig, x)) {
fTiming[sig] = 0;
return sig;
} else if (isSigHighest(sig, x)) {
fTiming[sig] = 0;
return sig;
}
else if (isSigRegister(sig, &i, x)) {
std::cerr << "ASSERT : already retimed : " << *sig << std::endl;
faustassert(false);
}
else {
std::cerr << "ASSERT : unrecognized signal : " << *sig << std::endl;
faustassert(false);
}
return 0;
}
|