Skip to content

Commit

Permalink
Implementation of the os flag
Browse files Browse the repository at this point in the history
  • Loading branch information
crop2000 committed Nov 14, 2024
1 parent 3c65f91 commit 99764a8
Show file tree
Hide file tree
Showing 8 changed files with 352 additions and 27 deletions.
21 changes: 15 additions & 6 deletions compiler/generator/instructions_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1138,9 +1138,14 @@ ValueInst* InstructionsCompiler::generateVariableStore(Tree sig, ValueInst* exp)
return IB::genLoadStructVar(vname);
}
} else {
// "Slow" variables are declared as locals in 'frame' or 'compute' functions
pushComputeBlockMethod(IB::genDecStackVar(vname, ctype, exp));
return IB::genLoadStackVar(vname);
if (gGlobal->gOneSample) {
pushControlDeclare(IB::genDecStackVar(vname, ctype, exp));
return IB::genLoadStackVar(vname);
} else {
// "Slow" variables are declared as locals in 'frame' or 'compute' functions
pushComputeBlockMethod(IB::genDecStackVar(vname, ctype, exp));
return IB::genLoadStackVar(vname);
}
}
}

Expand Down Expand Up @@ -1178,9 +1183,13 @@ ValueInst* InstructionsCompiler::generateVariableStore(Tree sig, ValueInst* exp)
getConditionCode(sig), IB::genStoreStructVar(vname_perm, exp)));
return IB::genLoadStructVar(vname_perm);
} else {
// copy the object variable to the local one
pushComputeBlockMethod(
IB::genDecStackVar(vname, ctype, IB::genLoadStructVar(vname_perm)));
if (gGlobal->gOneSample) {
pushControlDeclare(IB::genDecStackVar(vname, ctype, IB::genLoadStructVar(vname_perm)));
} else {
// copy the object variable to the local one
pushComputeBlockMethod(
IB::genDecStackVar(vname, ctype, IB::genLoadStructVar(vname_perm)));
}
// execute the code
pushComputeDSPMethod(IB::genControlInst(getConditionCode(sig),
IB::genStoreStackVar(vname, exp)));
Expand Down
58 changes: 53 additions & 5 deletions compiler/generator/rust/rust_code_container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,12 @@ void RustCodeContainer::produceClass()
}

// Compute
generateCompute(n + 1);
generateComputeInterface(n + 1);
if (gGlobal->gOneSample) {
generateComputeFrame(n + 1);
} else {
generateCompute(n + 1);
generateComputeInterface(n + 1);
}

tab(n, *fOut);
*fOut << "}" << endl;
Expand Down Expand Up @@ -634,13 +638,56 @@ void RustCodeContainer::generateComputeHeader(int n, std::ostream* fOut, int fNu
void RustCodeContainer::generateComputeInterfaceHeader(int n, std::ostream* fOut, int fNumInputs,
int fNumOutputs)
{
// Compute "compute" declaration
*fOut << "pub fn compute("
<< "&mut self, " << fFullCount << ": usize, inputs: & [& [FaustFloat] ]"
<< ", outputs: & mut[& mut[FaustFloat] ]) {";
tab(n + 1, *fOut);
}

void RustCodeContainer::generateComputeFrame(int n)
{
tab(n, *fOut);
tab(n, *fOut);
*fOut << "pub fn compute_frame(&mut self, inputs: &[&FaustFloat], ";
*fOut << "outputs: &mut [&mut FaustFloat]) {";

for(int i = 0; i < fNumInputs; i++){
tab(n+1, *fOut);
*fOut << "let input"<< i <<" = inputs["<< i <<"];";
};

for(int i = 0; i < fNumOutputs; i++){
tab(n+1, *fOut);
*fOut << "let (nextoutput, outputs): (&mut [&mut f64], &mut [&mut f64]) = outputs.split_at_mut(1);";
tab(n+1, *fOut);
*fOut << "let output"<< i <<": &mut FaustFloat = nextoutput[0];";
};

fCodeProducer.Tab(n + 1);

if (!gGlobal->gExtControl) {
generateControlDeclarations(&fCodeProducer);
} else {
}

tab(n+1, *fOut);
*fOut << "//generateOneSample";
tab(n+1, *fOut);
// Generates one sample computation
BlockInst* block = fCurLoop->generateOneSample();
block->accept(&fCodeProducer);

/*
// TODO : atomic switch
// Currently for soundfile management
// also for temp vars
*/
generatePostComputeBlock(&fCodeProducer);
tab(n, *fOut);
*fOut << "}";
tab(n, *fOut);
}

void RustCodeContainer::generateComputeInterface(int n)
{
// Generates declaration
Expand Down Expand Up @@ -676,7 +723,7 @@ void RustScalarCodeContainer::generateCompute(int n)
tab(n + 1, *fOut);
fCodeProducer.Tab(n + 1);

// Generates local variables declaration and setup

generateComputeBlock(&fCodeProducer);

// Generates one single scalar loop
Expand All @@ -691,9 +738,10 @@ void RustScalarCodeContainer::generateCompute(int n)
loop->accept(&fCodeProducer);

// Currently for soundfile management
// and for temp vars
generatePostComputeBlock(&fCodeProducer);

back(1, *fOut);
tab(n, *fOut);
*fOut << "}" << endl;
}

Expand Down
1 change: 1 addition & 0 deletions compiler/generator/rust/rust_code_container.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class RustCodeContainer : public virtual CodeContainer {
void generateComputeHeader(int n, std::ostream* fOut, int fNumInputs, int fNumOutputs);
void generateComputeInterfaceHeader(int n, std::ostream* fOut, int fNumInputs, int fNumOutputs);
void generateComputeInterface(int tab);
void generateComputeFrame(int tab);
virtual void generateCompute(int tab) = 0;
void produceInternal();
void produceFaustDspBlob();
Expand Down
12 changes: 4 additions & 8 deletions compiler/generator/rust/rust_instructions.hh
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,9 @@ class RustInstVisitor : public TextInstVisitor {
virtual void visit(DeclareBufferIterators* inst)
{
/* Generates an expression like:
let (outputs0, outputs1) = if let [outputs0, outputs1, ..] = outputs {
let outputs0 = outputs0[..count as usize].iter_mut();
let outputs1 = outputs1[..count as usize].iter_mut();
(outputs0, outputs1)
} else {
panic!("wrong number of outputs");
};
let [outputs0, outputs1, ..] = outputs;
let outputs0 = outputs0[..count].iter_mut();
let outputs1 = outputs1[..count].iter_mut();
*/

// Don't generate if no channels
Expand All @@ -251,7 +247,7 @@ class RustInstVisitor : public TextInstVisitor {

for (int i = 0; i < inst->fChannels; ++i) {
tab(fTab, *fOut);
*fOut << "let " << name << i << " = " << name << i << "[..count as usize]";
*fOut << "let " << name << i << " = " << name << i << "[..count]";
if (inst->fMutable) {
if (inst->fChunk) {
*fOut << ".chunks_mut(vsize as usize);";
Expand Down
4 changes: 2 additions & 2 deletions compiler/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1678,9 +1678,9 @@ bool global::processCmdline(int argc, const char* argv[])
}
#endif
if (gOneSample && gOutputLang != "cpp" && gOutputLang != "c" && gOutputLang != "dlang" &&
!startWith(gOutputLang, "cmajor") && gOutputLang != "fir") {
!startWith(gOutputLang, "cmajor") && gOutputLang != "fir" && gOutputLang != "rust") {
throw faustexception(
"ERROR : '-os' option can only be used with 'cpp', 'c', 'fir' or 'cmajor' "
"ERROR : '-os' option can only be used with 'cpp', 'c', 'dlang', 'cmajor', 'fir' or 'rust'"
"backends\n");
}

Expand Down
20 changes: 14 additions & 6 deletions tests/impulse-tests/Make.rust
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,34 @@ filesCompare:
#########################################################################
# rules

ir/$(outdir)/sound.ir: dsp/sound.dsp
ir/$(outdir)/sound.ir: dsp/sound.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/sound.dsp -o archs/rust/src/bin/sound.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : 'soundfile' primitive not yet supported for Rust" $@

ir/rust/ec/bs.ir: dsp/bs.dsp
ir/rust/ec/bs.ir: dsp/bs.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/bs.dsp -o archs/rust/src/bin/bs.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : accessing foreign variable 'count' is not allowed in this compilation mode" $@

ir/rust/vec4/osc_enable.ir: dsp/osc_enable.dsp
ir/rust/osec/bs.ir: dsp/bs.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/bs.dsp -o archs/rust/src/bin/bs.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : accessing foreign variable 'count' is not allowed in this compilation mode" $@

ir/rust/os/bs.ir: dsp/bs.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/bs.dsp -o archs/rust/src/bin/bs.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : accessing foreign variable 'count' is not allowed in this compilation mode" $@

ir/rust/vec4/osc_enable.ir: dsp/osc_enable.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/osc_enable.dsp -o archs/rust/src/bin/osc_enable.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : 'control/enable' can only be used in scalar mode" $@

ir/rust/vec32/osc_enable.ir: dsp/osc_enable.dsp
ir/rust/vec32/osc_enable.ir: dsp/osc_enable.dsp
$(FAUST) -lang rust $(FAUSTOPTIONS) dsp/osc_enable.dsp -o archs/rust/src/bin/osc_enable.rs > $@ 2>&1 || (echo "expected failure")
grep "ERROR : 'control/enable' can only be used in scalar mode" $@

ir/rust/vec4/prefix.ir:
ir/rust/vec4/prefix.ir:
echo "todo fix bug #1071 to test dsp/prefix.dsp"

ir/rust/vec32/prefix.ir:
ir/rust/vec32/prefix.ir:
echo "todo fix bug #1071 to test dsp/prefix.dsp"

ir/$(outdir)/%.ir: dsp/%.dsp reference/%.ir
Expand Down
1 change: 1 addition & 0 deletions tests/impulse-tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ interp1:
#########################################################################
# Rust backend
rust:
$(MAKE) -f Make.rust outdir=rust/osec CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -os -ec -rnt -a archs/rust/architecture_os.rs"
$(MAKE) -f Make.rust outdir=rust/ec CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -cm -a archs/rust/architecture_cm.rs"
$(MAKE) -f Make.rust outdir=rust/ec CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -rnt -ec -a archs/rust/architecture.rs"
$(MAKE) -f Make.rust outdir=rust/no CARGOOPTIONS="" FAUSTOPTIONS="-I ../../libraries/ -double -a archs/rust/architecture_normal.rs"
Expand Down
Loading

0 comments on commit 99764a8

Please sign in to comment.