Skip to content

Commit

Permalink
Handle ma.SR adaptation in propagate.
Browse files Browse the repository at this point in the history
  • Loading branch information
sletz committed Feb 21, 2025
1 parent 3b4bd67 commit b2c5d0e
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 38 deletions.
4 changes: 2 additions & 2 deletions compiler/draw/drawschema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include "compatibility.hh"
#include "description.hh"
#include "devLib.h"
#include "downsamplingSchema.h"
#include "drawschema.hh"
#include "exception.hh"
#include "files.hh"
Expand All @@ -56,13 +57,12 @@
#include "occur.hh"
#include "occurrences.hh"
#include "ondemandSchema.h"
#include "upsamplingSchema.h"
#include "downsamplingSchema.h"
#include "ppbox.hh"
#include "prim2.hh"
#include "property.hh"
#include "routeSchema.h"
#include "schema.h"
#include "upsamplingSchema.h"
#include "xtended.hh"

#if 0
Expand Down
7 changes: 4 additions & 3 deletions compiler/draw/schema/downsamplingSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

#include <iostream>

#include "exception.hh"
#include "downsamplingSchema.h"
#include "exception.hh"

using namespace std;

Expand All @@ -31,8 +31,9 @@ const double downsamplingSchema::fTopMargin(
const double downsamplingSchema::fHorMargin(10); // left and right gap
const double downsamplingSchema::fBotMargin(
10); // gap between the bottom and the bottom of the inside schema
const double downsamplingSchema::fMinWidth(50); // Minimal width of an downsampling block
const string downsamplingSchema::fText("downsampling"); // Test to display, tipically "downsampling"
const double downsamplingSchema::fMinWidth(50); // Minimal width of an downsampling block
const string downsamplingSchema::fText(
"downsampling"); // Test to display, tipically "downsampling"

/**
* Returns an enlarged schema, but only if really needed
Expand Down
2 changes: 1 addition & 1 deletion compiler/draw/schema/upsamplingSchema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const double upsamplingSchema::fTopMargin(
const double upsamplingSchema::fHorMargin(10); // left and right gap
const double upsamplingSchema::fBotMargin(
10); // gap between the bottom and the bottom of the inside schema
const double upsamplingSchema::fMinWidth(50); // Minimal width of an upsampling block
const double upsamplingSchema::fMinWidth(50); // Minimal width of an upsampling block
const string upsamplingSchema::fText("upsampling"); // Test to display, tipically "upsampling"

/**
Expand Down
6 changes: 2 additions & 4 deletions compiler/draw/sigToGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,9 @@ static string sigLabel(Tree sig)

else if (isSigAssertBounds(sig, x, y, z)) {
fout << "assertbounds";
}
else if (isSigLowest(sig, x)) {
} else if (isSigLowest(sig, x)) {
fout << "lowest";
}
else if (isSigHighest(sig, x)) {
} else if (isSigHighest(sig, x)) {
fout << "highest";
}

Expand Down
7 changes: 6 additions & 1 deletion compiler/generator/code_container.hh
Original file line number Diff line number Diff line change
Expand Up @@ -494,9 +494,14 @@ class CodeContainer : public virtual Garbageable {
void openUSblock(ValueInst* us_factor) { fCurLoop->openUSblock(us_factor); }
void closeUSblock() { fCurLoop->closeUSblock(); }

void openDSblock(ValueInst* ds_factor, const std::string& ds_counter) { fCurLoop->openDSblock(ds_factor, ds_counter); }
void openDSblock(ValueInst* ds_factor, const std::string& ds_counter)
{
fCurLoop->openDSblock(ds_factor, ds_counter);
}
void closeDSblock() { fCurLoop->closeDSblock(); }

ValueInst* getSRFactor() { return fCurLoop->getSRFactor(); }

void generateExtGlobalDeclarations(InstVisitor* visitor)
{
if (fExtGlobalDeclarationInstructions->fCode.size() > 0) {
Expand Down
38 changes: 36 additions & 2 deletions compiler/generator/instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3100,12 +3100,46 @@ struct IB {

static ValueInst* genMul(ValueInst* a1, ValueInst* a2)
{
return isOne(a1) ? a2 : (isOne(a2) ? a1 : genBinopInst(kMul, a1, a2));
if (isOne(a1)) {
return a2;
} else if (isOne(a2)) {
return a1;
} else if (castInt32(a1) && castInt32(a2)) {
return genInt32NumInst(castInt32(a1)->fNum * castInt32(a2)->fNum);
} else if (castInt64(a1) && castInt64(a2)) {
return genInt64NumInst(castInt64(a1)->fNum * castInt64(a2)->fNum);
} else if (castFloat(a1) && castFloat(a2)) {
return genFloatNumInst(castFloat(a1)->fNum * castFloat(a2)->fNum);
} else if (castDouble(a1) && castDouble(a2)) {
return genDoubleNumInst(castDouble(a1)->fNum * castDouble(a2)->fNum);
} else if (castQuad(a1) && castQuad(a2)) {
return genQuadNumInst(castQuad(a1)->fNum * castQuad(a2)->fNum);
} else if (castFixed(a1) && castFixed(a2)) {
return genFixedPointNumInst(castFixed(a1)->fNum * castFixed(a2)->fNum);
} else {
return genBinopInst(kMul, a1, a2);
}
}

static ValueInst* genDiv(ValueInst* a1, ValueInst* a2)
{
return isOne(a2) ? a1 : genBinopInst(kDiv, a1, a2);
if (isOne(a2)) {
return a1;
} else if (castInt32(a1) && castInt32(a2)) {
return genInt32NumInst(castInt32(a1)->fNum / castInt32(a2)->fNum);
} else if (castInt64(a1) && castInt64(a2)) {
return genInt64NumInst(castInt64(a1)->fNum / castInt64(a2)->fNum);
} else if (castFloat(a1) && castFloat(a2)) {
return genFloatNumInst(castFloat(a1)->fNum / castFloat(a2)->fNum);
} else if (castDouble(a1) && castDouble(a2)) {
return genDoubleNumInst(castDouble(a1)->fNum / castDouble(a2)->fNum);
} else if (castQuad(a1) && castQuad(a2)) {
return genQuadNumInst(castQuad(a1)->fNum / castQuad(a2)->fNum);
} else if (castFixed(a1) && castFixed(a2)) {
return genFixedPointNumInst(castFixed(a1)->fNum / castFixed(a2)->fNum);
} else {
return genBinopInst(kDiv, a1, a2);
}
}

static BinopInst* genRem(ValueInst* a1, ValueInst* a2) { return genBinopInst(kRem, a1, a2); }
Expand Down
16 changes: 8 additions & 8 deletions compiler/generator/instructions_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ bool InstructionsCompiler::getCompiledExpression(Tree sig, ValueType& cexp)
}

/**
* Set the ValueType of a compiled expression is already compiled
* Set the ValueType of a compiled expression if already compiled
* @param sig the signal expression to compile.
* @param cexp the ValueType representing the compiled expression.
* @return the cexp (for commodity)
*/
ValueType InstructionsCompiler::setCompiledExpression(Tree sig, const ValueType& cexp)
ValueInst* InstructionsCompiler::setCompiledExpression(Tree sig, const ValueType& cexp)
{
ValueType old;
ValueInst* old;
if (fCompileProperty.get(sig, old) && (old != cexp)) {
// stringstream error;
// error << "ERROR already a compiled expression attached : " << old << " replaced by " <<
Expand Down Expand Up @@ -845,7 +845,7 @@ void InstructionsCompiler::compileSingleSignal(Tree sig)

ValueInst* InstructionsCompiler::generateCode(Tree sig)
{
#if 0
#if 1
fprintf(stderr, "CALL generateCode(");
printSignal(sig, stderr);
fprintf(stderr, ")\n");
Expand Down Expand Up @@ -1333,7 +1333,7 @@ ValueInst* InstructionsCompiler::generateVariableStore(Tree sig, ValueInst* exp)
getTypedNames(t, "Const", ctype, vname);

// TODO: deactivated for now since getOccurrence fails in some cases

// The variable is used in compute (kBlock or kSamp),
// so define is as a field in the DSP struct
if (o->getOccurrence(kBlock) || o->getOccurrence(kSamp)) {
Expand All @@ -1345,7 +1345,7 @@ ValueInst* InstructionsCompiler::generateVariableStore(Tree sig, ValueInst* exp)
pushInitMethod(IB::genDecStackVar(vname, ctype, exp));
return IB::genLoadStackVar(vname);
}

/*
// Always put variables in DSP struct for now
pushDeclare(IB::genDecStructVar(vname, ctype));
Expand Down Expand Up @@ -3653,11 +3653,11 @@ ValueInst* InstructionsCompiler::generateDS(Tree sig, const tvec& w)
fDSCounter = "fDSCounter";
pushDeclare(IB::genDecStructVar(fDSCounter, IB::genInt32Typed()));
pushClearMethod(IB::genStoreStructVar(fDSCounter, IB::genInt32NumInst(0)));

FIRIndex value = FIRIndex(IB::genLoadStructVar(fDSCounter)) + 1;
pushPostComputeDSPMethod(IB::genStoreStructVar(fDSCounter, value));
}

// 1/ We extract the clock, the inputs and the outputs signals
// form w = [clock, input1, input2, ..., nil, output1, output2, ...]
faustassert(w.size() > 2);
Expand Down
4 changes: 2 additions & 2 deletions compiler/normalize/normalform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ static Tree simplifyToNormalFormAux(Tree LS)

// Update the SR value using US/DS information.
startTiming("L4 signalSampleRate");
L1 = signalSampleRate(L1);
// L1 = signalSampleRate(L1);
endTiming("L4 signalSampleRate");

// Annotate L1 with type information
Expand Down Expand Up @@ -145,7 +145,7 @@ static Tree simplifyToNormalFormAux(Tree LS)
startTiming("L4 signalChecker");
SignalChecker checker(L4);
endTiming("L4 signalChecker");
//std::cout << ppsig(L4) << std::endl;
// std::cout << ppsig(L4) << std::endl;
return L4;
}

Expand Down
18 changes: 14 additions & 4 deletions compiler/parallelize/code_loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,22 +357,32 @@ void CodeLoop::closeUSblock()
ForLoopInst* loop = IB::genForLoopInst(loop_decl, loop_end, loop_inc);
loop->pushFrontInst(us_block);
pushComputeDSPMethod(loop);
// Adjust fSRFactor
// fSRFactor = IB::genDiv(fSRFactor, b->fUSfactor);
// pushComputeDSPMethod(IB::genStoreStructVar("fIntSampleRate",
// IB::genDiv(IB::genLoadStructVar("fIntSampleRate"), b->fUSfactor)));
}

void CodeLoop::closeDSblock()
{
CodeDSblock* b = dynamic_cast<CodeDSblock*>(fCodeStack.top());
faustassert(b);
fCodeStack.pop();

BlockInst* ds_block1 = new BlockInst();
ds_block1->pushBackInst(b->fPreInst);
ds_block1->pushBackInst(b->fComputeInst);
ds_block1->pushBackInst(b->fPostInst);

BlockInst* ds_block2 = new BlockInst();
ds_block2->pushBackInst(IB::genIfInst(
IB::genEqual(IB::genRem(IB::genLoadStructVar(b->fDSCounter), b->fDSfactor), IB::genInt32NumInst(0)), ds_block1));
ds_block2->pushBackInst(
IB::genIfInst(IB::genEqual(IB::genRem(IB::genLoadStructVar(b->fDSCounter), b->fDSfactor),
IB::genInt32NumInst(0)),
ds_block1));

pushComputeDSPMethod(ds_block2);
// Adjust fSRFactor
// fSRFactor = IB::genMul(fSRFactor, b->fDSfactor);
// pushComputeDSPMethod(IB::genStoreStructVar("fIntSampleRate",
// IB::genMul(IB::genLoadStructVar("fIntSampleRate"), b->fDSfactor)));
}
24 changes: 20 additions & 4 deletions compiler/parallelize/code_loop.hh
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,13 @@ struct CodeUSblock : public Codeblock {
};

struct CodeDSblock : public Codeblock {
ValueInst* fDSfactor; ///< downsampling factor of the US block
ValueInst* fDSfactor; ///< downsampling factor of the US block
std::string fDSCounter;

CodeDSblock(ValueInst* ds_factor, const std::string& ds_counter) : Codeblock(), fDSfactor(ds_factor), fDSCounter(ds_counter) {}
CodeDSblock(ValueInst* ds_factor, const std::string& ds_counter)
: Codeblock(), fDSfactor(ds_factor), fDSCounter(ds_counter)
{
}
};

class CodeLoop : public virtual Garbageable {
Expand All @@ -113,6 +116,8 @@ class CodeLoop : public virtual Garbageable {

std::stack<Codeblock*> fCodeStack; //< stack of IF/US/DS code blocks

ValueInst* fSRFactor;

int fUseCount; ///< how many loops depend on this one
std::list<CodeLoop*> fExtraLoops; ///< extra loops that where in sequences

Expand Down Expand Up @@ -150,6 +155,7 @@ class CodeLoop : public virtual Garbageable {
fComputeInst(new BlockInst()),
fPostInst(new BlockInst()),
fLoopIndex(index_name),
fSRFactor(IB::genInt32NumInst(1)),
fUseCount(0)
{
}
Expand All @@ -166,6 +172,7 @@ class CodeLoop : public virtual Garbageable {
fComputeInst(new BlockInst()),
fPostInst(new BlockInst()),
fLoopIndex(index_name),
fSRFactor(IB::genInt32NumInst(1)),
fUseCount(0)
{
}
Expand Down Expand Up @@ -208,14 +215,15 @@ class CodeLoop : public virtual Garbageable {
ValueInst* getLoopIndex()
{
if (fCodeStack.size() > 0) {
CodeUSblock* us_block = dynamic_cast<CodeUSblock*>(fCodeStack.top());
if (us_block) {
if (CodeUSblock* us_block = dynamic_cast<CodeUSblock*>(fCodeStack.top())) {
return IB::genLoadLoopVar(us_block->fLoopIndex);
}
}
return IB::genLoadLoopVar(fLoopIndex);
}

ValueInst* getSRFactor() { return fSRFactor; }

ForLoopInst* generateScalarLoop(const std::string& counter, bool loop_var_in_bytes = false);

// For SYFALA : loop with a fixed size (known at compile time)
Expand Down Expand Up @@ -276,6 +284,10 @@ class CodeLoop : public virtual Garbageable {
{
CodeUSblock* b = new CodeUSblock(us_factor);
fCodeStack.push(b);
// Adjust fSRFactor
// fSRFactor = IB::genMul(fSRFactor, us_factor);
// pushComputeDSPMethod(IB::genStoreStructVar("fIntSampleRate",
// IB::genMul(IB::genLoadStructVar("fIntSampleRate"), us_factor)));
}

/**
Expand All @@ -291,6 +303,10 @@ class CodeLoop : public virtual Garbageable {
{
CodeDSblock* b = new CodeDSblock(ds_factor, ds_counter);
fCodeStack.push(b);
// Adjust fSRFactor
// fSRFactor = IB::genDiv(fSRFactor, ds_factor);
// pushComputeDSPMethod(IB::genStoreStructVar("fIntSampleRate",
// IB::genDiv(IB::genLoadStructVar("fIntSampleRate"), ds_factor)));
}

/**
Expand Down
24 changes: 19 additions & 5 deletions compiler/propagate/propagate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,19 @@ static siglist realPropagate(Tree clockenv, Tree slotenv, Tree path, Tree box, c

else if (isBoxFConst(box, type, name, file)) {
faustassert(lsig.size() == 0);
return makeList(sigFConst(type, name, file));
// Specific case for sampling rate (ma.SR)
string vname = string(tree2str(name));
if ((vname == "fSamplingFreq") || (vname == "fSamplingRate")) {
Tree clock = hd(clockenv);
Tree us_ds = hd(tl(clockenv));
if (us_ds == tree("Upsampling")) {
return makeList(sigMul(sigFConst(type, name, file), clock));
} else if (us_ds == tree("Downsampling")) {
return makeList(sigDiv(sigFConst(type, name, file), clock));
}
} else {
return makeList(sigFConst(type, name, file));
}
}

else if (isBoxFVar(box, type, name, file)) {
Expand Down Expand Up @@ -699,8 +711,9 @@ static siglist realPropagate(Tree clockenv, Tree slotenv, Tree path, Tree box, c

// 3/ We compute the clock environment inside the upsampling by combining the clock, the
// address of the circuit, and the current clock environment
Tree addr = boxPrim0((prim0)box);
Tree clockenv2 = cons(H, cons(addr, clockenv));
Tree addr = boxPrim0((prim0)box);
// Tree clockenv2 = cons(H, cons(addr, clockenv));
Tree clockenv2 = cons(H, cons(tree("Upsampling"), clockenv));

// 4/ We compute X1 the inputs of the ondemand using temporary variables
siglist X1;
Expand Down Expand Up @@ -764,8 +777,9 @@ static siglist realPropagate(Tree clockenv, Tree slotenv, Tree path, Tree box, c

// 3/ We compute the clock environment inside the downsampling by combining the clock, the
// address of the circuit, and the current clock environment
Tree addr = boxPrim0((prim0)box);
Tree clockenv2 = cons(H, cons(addr, clockenv));
Tree addr = boxPrim0((prim0)box);
// Tree clockenv2 = cons(H, cons(addr, clockenv));
Tree clockenv2 = cons(H, cons(tree("Downsampling"), clockenv));

// 4/ We compute X1 the inputs of the downsampling using temporary variables
siglist X1;
Expand Down
2 changes: 1 addition & 1 deletion compiler/transform/sigIdentity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,6 @@ Tree SignalIdentityImp<CACHE>::transformation(Tree sig)
return nullptr;
}

// Explicit instanciation for CACHE = true and false
// Explicit instanciation for CACHE = true and false
template class SignalIdentityImp<true>;
template class SignalIdentityImp<false>;
2 changes: 1 addition & 1 deletion compiler/transform/treeTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
#include <cstdlib>

#include "Text.hh"
#include "treeTransform.hh"
#include "list.hh"
#include "treeTransform.hh"

using namespace std;

Expand Down

0 comments on commit b2c5d0e

Please sign in to comment.